blob: 3578c9997cd75da4b5e6b1a774e3973d28d3ebdd [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100475static void f_and(typval_T *argvars, typval_T *rettv);
476static void f_append(typval_T *argvars, typval_T *rettv);
477static void f_argc(typval_T *argvars, typval_T *rettv);
478static void f_argidx(typval_T *argvars, typval_T *rettv);
479static void f_arglistid(typval_T *argvars, typval_T *rettv);
480static void f_argv(typval_T *argvars, typval_T *rettv);
481static void f_assert_equal(typval_T *argvars, typval_T *rettv);
482static void f_assert_exception(typval_T *argvars, typval_T *rettv);
483static void f_assert_fails(typval_T *argvars, typval_T *rettv);
484static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200485static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200486static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
487static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100490static void f_asin(typval_T *argvars, typval_T *rettv);
491static void f_atan(typval_T *argvars, typval_T *rettv);
492static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000493#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100494static void f_browse(typval_T *argvars, typval_T *rettv);
495static void f_browsedir(typval_T *argvars, typval_T *rettv);
496static void f_bufexists(typval_T *argvars, typval_T *rettv);
497static void f_buflisted(typval_T *argvars, typval_T *rettv);
498static void f_bufloaded(typval_T *argvars, typval_T *rettv);
499static void f_bufname(typval_T *argvars, typval_T *rettv);
500static void f_bufnr(typval_T *argvars, typval_T *rettv);
501static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
502static void f_byte2line(typval_T *argvars, typval_T *rettv);
503static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
504static void f_byteidx(typval_T *argvars, typval_T *rettv);
505static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
506static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000509#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100512static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100514static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100515static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100516static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100517static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100518static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
519static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100520static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100521static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100522static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
523static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100524static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100525static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_changenr(typval_T *argvars, typval_T *rettv);
528static void f_char2nr(typval_T *argvars, typval_T *rettv);
529static void f_cindent(typval_T *argvars, typval_T *rettv);
530static void f_clearmatches(typval_T *argvars, typval_T *rettv);
531static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000532#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_complete(typval_T *argvars, typval_T *rettv);
534static void f_complete_add(typval_T *argvars, typval_T *rettv);
535static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_confirm(typval_T *argvars, typval_T *rettv);
538static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_cos(typval_T *argvars, typval_T *rettv);
541static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_count(typval_T *argvars, typval_T *rettv);
544static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
545static void f_cursor(typval_T *argsvars, typval_T *rettv);
546static void f_deepcopy(typval_T *argvars, typval_T *rettv);
547static void f_delete(typval_T *argvars, typval_T *rettv);
548static void f_did_filetype(typval_T *argvars, typval_T *rettv);
549static void f_diff_filler(typval_T *argvars, typval_T *rettv);
550static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
551static void f_empty(typval_T *argvars, typval_T *rettv);
552static void f_escape(typval_T *argvars, typval_T *rettv);
553static void f_eval(typval_T *argvars, typval_T *rettv);
554static void f_eventhandler(typval_T *argvars, typval_T *rettv);
555static void f_executable(typval_T *argvars, typval_T *rettv);
556static void f_exepath(typval_T *argvars, typval_T *rettv);
557static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_expand(typval_T *argvars, typval_T *rettv);
562static void f_extend(typval_T *argvars, typval_T *rettv);
563static void f_feedkeys(typval_T *argvars, typval_T *rettv);
564static void f_filereadable(typval_T *argvars, typval_T *rettv);
565static void f_filewritable(typval_T *argvars, typval_T *rettv);
566static void f_filter(typval_T *argvars, typval_T *rettv);
567static void f_finddir(typval_T *argvars, typval_T *rettv);
568static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000569#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100570static void f_float2nr(typval_T *argvars, typval_T *rettv);
571static void f_floor(typval_T *argvars, typval_T *rettv);
572static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_fnameescape(typval_T *argvars, typval_T *rettv);
575static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
576static void f_foldclosed(typval_T *argvars, typval_T *rettv);
577static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
578static void f_foldlevel(typval_T *argvars, typval_T *rettv);
579static void f_foldtext(typval_T *argvars, typval_T *rettv);
580static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
581static void f_foreground(typval_T *argvars, typval_T *rettv);
582static void f_function(typval_T *argvars, typval_T *rettv);
583static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
584static void f_get(typval_T *argvars, typval_T *rettv);
585static void f_getbufline(typval_T *argvars, typval_T *rettv);
586static void f_getbufvar(typval_T *argvars, typval_T *rettv);
587static void f_getchar(typval_T *argvars, typval_T *rettv);
588static void f_getcharmod(typval_T *argvars, typval_T *rettv);
589static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
590static void f_getcmdline(typval_T *argvars, typval_T *rettv);
591static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
592static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
593static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
594static void f_getcwd(typval_T *argvars, typval_T *rettv);
595static void f_getfontname(typval_T *argvars, typval_T *rettv);
596static void f_getfperm(typval_T *argvars, typval_T *rettv);
597static void f_getfsize(typval_T *argvars, typval_T *rettv);
598static void f_getftime(typval_T *argvars, typval_T *rettv);
599static void f_getftype(typval_T *argvars, typval_T *rettv);
600static void f_getline(typval_T *argvars, typval_T *rettv);
601static void f_getmatches(typval_T *argvars, typval_T *rettv);
602static void f_getpid(typval_T *argvars, typval_T *rettv);
603static void f_getcurpos(typval_T *argvars, typval_T *rettv);
604static void f_getpos(typval_T *argvars, typval_T *rettv);
605static void f_getqflist(typval_T *argvars, typval_T *rettv);
606static void f_getreg(typval_T *argvars, typval_T *rettv);
607static void f_getregtype(typval_T *argvars, typval_T *rettv);
608static void f_gettabvar(typval_T *argvars, typval_T *rettv);
609static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
610static void f_getwinposx(typval_T *argvars, typval_T *rettv);
611static void f_getwinposy(typval_T *argvars, typval_T *rettv);
612static void f_getwinvar(typval_T *argvars, typval_T *rettv);
613static void f_glob(typval_T *argvars, typval_T *rettv);
614static void f_globpath(typval_T *argvars, typval_T *rettv);
615static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
616static void f_has(typval_T *argvars, typval_T *rettv);
617static void f_has_key(typval_T *argvars, typval_T *rettv);
618static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
619static void f_hasmapto(typval_T *argvars, typval_T *rettv);
620static void f_histadd(typval_T *argvars, typval_T *rettv);
621static void f_histdel(typval_T *argvars, typval_T *rettv);
622static void f_histget(typval_T *argvars, typval_T *rettv);
623static void f_histnr(typval_T *argvars, typval_T *rettv);
624static void f_hlID(typval_T *argvars, typval_T *rettv);
625static void f_hlexists(typval_T *argvars, typval_T *rettv);
626static void f_hostname(typval_T *argvars, typval_T *rettv);
627static void f_iconv(typval_T *argvars, typval_T *rettv);
628static void f_indent(typval_T *argvars, typval_T *rettv);
629static void f_index(typval_T *argvars, typval_T *rettv);
630static void f_input(typval_T *argvars, typval_T *rettv);
631static void f_inputdialog(typval_T *argvars, typval_T *rettv);
632static void f_inputlist(typval_T *argvars, typval_T *rettv);
633static void f_inputrestore(typval_T *argvars, typval_T *rettv);
634static void f_inputsave(typval_T *argvars, typval_T *rettv);
635static void f_inputsecret(typval_T *argvars, typval_T *rettv);
636static void f_insert(typval_T *argvars, typval_T *rettv);
637static void f_invert(typval_T *argvars, typval_T *rettv);
638static void f_isdirectory(typval_T *argvars, typval_T *rettv);
639static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100640#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
641static void f_isnan(typval_T *argvars, typval_T *rettv);
642#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100645static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100646static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100647static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100648static void f_job_start(typval_T *argvars, typval_T *rettv);
649static void f_job_stop(typval_T *argvars, typval_T *rettv);
650static void f_job_status(typval_T *argvars, typval_T *rettv);
651#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100653static void f_js_decode(typval_T *argvars, typval_T *rettv);
654static void f_js_encode(typval_T *argvars, typval_T *rettv);
655static void f_json_decode(typval_T *argvars, typval_T *rettv);
656static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_keys(typval_T *argvars, typval_T *rettv);
658static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
659static void f_len(typval_T *argvars, typval_T *rettv);
660static void f_libcall(typval_T *argvars, typval_T *rettv);
661static void f_libcallnr(typval_T *argvars, typval_T *rettv);
662static void f_line(typval_T *argvars, typval_T *rettv);
663static void f_line2byte(typval_T *argvars, typval_T *rettv);
664static void f_lispindent(typval_T *argvars, typval_T *rettv);
665static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_log(typval_T *argvars, typval_T *rettv);
668static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200670#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_map(typval_T *argvars, typval_T *rettv);
674static void f_maparg(typval_T *argvars, typval_T *rettv);
675static void f_mapcheck(typval_T *argvars, typval_T *rettv);
676static void f_match(typval_T *argvars, typval_T *rettv);
677static void f_matchadd(typval_T *argvars, typval_T *rettv);
678static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
679static void f_matcharg(typval_T *argvars, typval_T *rettv);
680static void f_matchdelete(typval_T *argvars, typval_T *rettv);
681static void f_matchend(typval_T *argvars, typval_T *rettv);
682static void f_matchlist(typval_T *argvars, typval_T *rettv);
683static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200684static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_max(typval_T *argvars, typval_T *rettv);
686static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000687#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100691#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
695static void f_nr2char(typval_T *argvars, typval_T *rettv);
696static void f_or(typval_T *argvars, typval_T *rettv);
697static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100698#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100700#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000701#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
705static void f_printf(typval_T *argvars, typval_T *rettv);
706static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200707#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#endif
710#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_range(typval_T *argvars, typval_T *rettv);
714static void f_readfile(typval_T *argvars, typval_T *rettv);
715static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100716#ifdef FEAT_FLOAT
717static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
718#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_reltimestr(typval_T *argvars, typval_T *rettv);
720static void f_remote_expr(typval_T *argvars, typval_T *rettv);
721static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
722static void f_remote_peek(typval_T *argvars, typval_T *rettv);
723static void f_remote_read(typval_T *argvars, typval_T *rettv);
724static void f_remote_send(typval_T *argvars, typval_T *rettv);
725static void f_remove(typval_T *argvars, typval_T *rettv);
726static void f_rename(typval_T *argvars, typval_T *rettv);
727static void f_repeat(typval_T *argvars, typval_T *rettv);
728static void f_resolve(typval_T *argvars, typval_T *rettv);
729static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_screenattr(typval_T *argvars, typval_T *rettv);
734static void f_screenchar(typval_T *argvars, typval_T *rettv);
735static void f_screencol(typval_T *argvars, typval_T *rettv);
736static void f_screenrow(typval_T *argvars, typval_T *rettv);
737static void f_search(typval_T *argvars, typval_T *rettv);
738static void f_searchdecl(typval_T *argvars, typval_T *rettv);
739static void f_searchpair(typval_T *argvars, typval_T *rettv);
740static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
741static void f_searchpos(typval_T *argvars, typval_T *rettv);
742static void f_server2client(typval_T *argvars, typval_T *rettv);
743static void f_serverlist(typval_T *argvars, typval_T *rettv);
744static void f_setbufvar(typval_T *argvars, typval_T *rettv);
745static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
746static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100747static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_setline(typval_T *argvars, typval_T *rettv);
749static void f_setloclist(typval_T *argvars, typval_T *rettv);
750static void f_setmatches(typval_T *argvars, typval_T *rettv);
751static void f_setpos(typval_T *argvars, typval_T *rettv);
752static void f_setqflist(typval_T *argvars, typval_T *rettv);
753static void f_setreg(typval_T *argvars, typval_T *rettv);
754static void f_settabvar(typval_T *argvars, typval_T *rettv);
755static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
756static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100757#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100759#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_shellescape(typval_T *argvars, typval_T *rettv);
761static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
762static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sin(typval_T *argvars, typval_T *rettv);
765static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sort(typval_T *argvars, typval_T *rettv);
768static void f_soundfold(typval_T *argvars, typval_T *rettv);
769static void f_spellbadword(typval_T *argvars, typval_T *rettv);
770static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
771static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_sqrt(typval_T *argvars, typval_T *rettv);
774static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_str2nr(typval_T *argvars, typval_T *rettv);
777static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200781static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_stridx(typval_T *argvars, typval_T *rettv);
783static void f_string(typval_T *argvars, typval_T *rettv);
784static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200785static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_strpart(typval_T *argvars, typval_T *rettv);
787static void f_strridx(typval_T *argvars, typval_T *rettv);
788static void f_strtrans(typval_T *argvars, typval_T *rettv);
789static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
790static void f_strwidth(typval_T *argvars, typval_T *rettv);
791static void f_submatch(typval_T *argvars, typval_T *rettv);
792static void f_substitute(typval_T *argvars, typval_T *rettv);
793static void f_synID(typval_T *argvars, typval_T *rettv);
794static void f_synIDattr(typval_T *argvars, typval_T *rettv);
795static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
796static void f_synstack(typval_T *argvars, typval_T *rettv);
797static void f_synconcealed(typval_T *argvars, typval_T *rettv);
798static void f_system(typval_T *argvars, typval_T *rettv);
799static void f_systemlist(typval_T *argvars, typval_T *rettv);
800static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
802static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
803static void f_taglist(typval_T *argvars, typval_T *rettv);
804static void f_tagfiles(typval_T *argvars, typval_T *rettv);
805static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200806static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
807static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200808static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
809#ifdef FEAT_JOB_CHANNEL
810static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
811#endif
812static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_job(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_list(typval_T *argvars, typval_T *rettv);
817static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
818static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200819#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_tan(typval_T *argvars, typval_T *rettv);
821static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200822#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100823#ifdef FEAT_TIMERS
824static void f_timer_start(typval_T *argvars, typval_T *rettv);
825static void f_timer_stop(typval_T *argvars, typval_T *rettv);
826#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static void f_tolower(typval_T *argvars, typval_T *rettv);
828static void f_toupper(typval_T *argvars, typval_T *rettv);
829static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000830#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_type(typval_T *argvars, typval_T *rettv);
834static void f_undofile(typval_T *argvars, typval_T *rettv);
835static void f_undotree(typval_T *argvars, typval_T *rettv);
836static void f_uniq(typval_T *argvars, typval_T *rettv);
837static void f_values(typval_T *argvars, typval_T *rettv);
838static void f_virtcol(typval_T *argvars, typval_T *rettv);
839static void f_visualmode(typval_T *argvars, typval_T *rettv);
840static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100841static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100842static void f_win_getid(typval_T *argvars, typval_T *rettv);
843static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
844static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
845static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static void f_winbufnr(typval_T *argvars, typval_T *rettv);
847static void f_wincol(typval_T *argvars, typval_T *rettv);
848static void f_winheight(typval_T *argvars, typval_T *rettv);
849static void f_winline(typval_T *argvars, typval_T *rettv);
850static void f_winnr(typval_T *argvars, typval_T *rettv);
851static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
852static void f_winrestview(typval_T *argvars, typval_T *rettv);
853static void f_winsaveview(typval_T *argvars, typval_T *rettv);
854static void f_winwidth(typval_T *argvars, typval_T *rettv);
855static void f_writefile(typval_T *argvars, typval_T *rettv);
856static void f_wordcount(typval_T *argvars, typval_T *rettv);
857static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
860static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
861static int get_env_len(char_u **arg);
862static int get_id_len(char_u **arg);
863static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
864static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000865#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
866#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
867 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
869static int eval_isnamec(int c);
870static int eval_isnamec1(int c);
871static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
872static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static typval_T *alloc_string_tv(char_u *string);
874static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100875#ifdef FEAT_FLOAT
876static float_T get_tv_float(typval_T *varp);
877#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static linenr_T get_tv_lnum(typval_T *argvars);
879static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
881static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
882static hashtab_T *find_var_ht(char_u *name, char_u **varname);
883static funccall_T *get_funccal(void);
884static void vars_clear_ext(hashtab_T *ht, int free_val);
885static void delete_var(hashtab_T *ht, hashitem_T *hi);
886static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
887static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
888static void set_var(char_u *name, typval_T *varp, int copy);
889static int var_check_ro(int flags, char_u *name, int use_gettext);
890static int var_check_fixed(int flags, char_u *name, int use_gettext);
891static int var_check_func_name(char_u *name, int new_var);
892static int valid_varname(char_u *varname);
893static int tv_check_lock(int lock, char_u *name, int use_gettext);
894static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
895static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100896static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100897static int eval_fname_script(char_u *p);
898static int eval_fname_sid(char_u *p);
899static void list_func_head(ufunc_T *fp, int indent);
900static ufunc_T *find_func(char_u *name);
901static int function_exists(char_u *name);
902static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static void func_do_profile(ufunc_T *fp);
905static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
906static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000907static int
908# ifdef __BORLANDC__
909 _RTLENTRYF
910# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100918static int script_autoload(char_u *name, int reload);
919static char_u *autoload_name(char_u *name);
920static void cat_func_name(char_u *buf, ufunc_T *fp);
921static void func_free(ufunc_T *fp);
922static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
923static int can_free_funccal(funccall_T *fc, int copyID) ;
924static void free_funccal(funccall_T *fc, int free_val);
925static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
926static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
927static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
928static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
929static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
930static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
931static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
932static int write_list(FILE *fd, list_T *list, int binary);
933static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200935
936#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100937static int compare_func_name(const void *s1, const void *s2);
938static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939#endif
940
Bram Moolenaar33570922005-01-25 22:26:29 +0000941/*
942 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000943 */
944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 int i;
948 struct vimvar *p;
949
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
951 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200952 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000953 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955
956 for (i = 0; i < VV_LEN; ++i)
957 {
958 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200959 if (STRLEN(p->vv_name) > 16)
960 {
961 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
962 getout(1);
963 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 STRCPY(p->vv_di.di_key, p->vv_name);
965 if (p->vv_flags & VV_RO)
966 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
967 else if (p->vv_flags & VV_RO_SBX)
968 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
969 else
970 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000971
972 /* add to v: scope dict, unless the value is not always available */
973 if (p->vv_type != VAR_UNKNOWN)
974 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000976 /* add to compat scope dict */
977 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100979 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
980
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000981 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100982 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200983 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100984 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100985
986 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
987 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
988 set_vim_var_nr(VV_NONE, VVAL_NONE);
989 set_vim_var_nr(VV_NULL, VVAL_NULL);
990
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200991 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200992
993#ifdef EBCDIC
994 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100995 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200996 */
997 sortFunctions();
998#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000999}
1000
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001#if defined(EXITFREE) || defined(PROTO)
1002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001003eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004{
1005 int i;
1006 struct vimvar *p;
1007
1008 for (i = 0; i < VV_LEN; ++i)
1009 {
1010 p = &vimvars[i];
1011 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001012 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001013 vim_free(p->vv_str);
1014 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001015 }
1016 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1017 {
1018 list_unref(p->vv_list);
1019 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001020 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021 }
1022 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001023 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024 hash_clear(&compat_hashtab);
1025
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001027# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001028 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030
1031 /* global variables */
1032 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001033
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001034 /* autoloaded script names */
1035 ga_clear_strings(&ga_loaded);
1036
Bram Moolenaarcca74132013-09-25 21:00:28 +02001037 /* Script-local variables. First clear all the variables and in a second
1038 * loop free the scriptvar_T, because a variable in one script might hold
1039 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001040 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 ga_clear(&ga_scripts);
1045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001046 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001047 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001048
1049 /* functions */
1050 free_all_functions();
1051 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001052}
1053#endif
1054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 * Return the name of the executed function.
1057 */
1058 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the next breakpoint line for a funccall cookie.
1066 */
1067 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the address holding the debug tick for a funccall cookie.
1075 */
1076 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Return the nesting level for a funccall cookie.
1084 */
1085 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001092funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001094/* pointer to list of previously used funccal, still around because some
1095 * item in it is still being used. */
1096funccall_T *previous_funccal = NULL;
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
1099 * Return TRUE when a function was ended by a ":return" command.
1100 */
1101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 return current_funccal->returned;
1105}
1106
1107
1108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * Set an internal variable to a string value. Creates the variable if it does
1110 * not already exist.
1111 */
1112 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001113set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 val = vim_strsave(value);
1119 if (val != NULL)
1120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001122 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 }
1128}
1129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132static char_u *redir_endp = NULL;
1133static char_u *redir_varname = NULL;
1134
1135/*
1136 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001137 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 * Returns OK if successfully completed the setup. FAIL otherwise.
1139 */
1140 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001141var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
1143 int save_emsg;
1144 int err;
1145 typval_T tv;
1146
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001148 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 {
1150 EMSG(_(e_invarg));
1151 return FAIL;
1152 }
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 redir_varname = vim_strsave(name);
1156 if (redir_varname == NULL)
1157 return FAIL;
1158
1159 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1160 if (redir_lval == NULL)
1161 {
1162 var_redir_stop();
1163 return FAIL;
1164 }
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 /* The output is stored in growarray "redir_ga" until redirection ends. */
1167 ga_init2(&redir_ga, (int)sizeof(char), 500);
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001170 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001171 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1173 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001174 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 if (redir_endp != NULL && *redir_endp != NUL)
1176 /* Trailing characters are present after the variable name */
1177 EMSG(_(e_trailing));
1178 else
1179 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
1184
1185 /* check if we can write to the variable: set it to or append an empty
1186 * string */
1187 save_emsg = did_emsg;
1188 did_emsg = FALSE;
1189 tv.v_type = VAR_STRING;
1190 tv.vval.v_string = (char_u *)"";
1191 if (append)
1192 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1193 else
1194 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001195 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001197 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 if (err)
1199 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001200 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
1202 return FAIL;
1203 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 * Append "value[value_len]" to the variable set by var_redir_start().
1210 * The actual appending is postponed until redirection ends, because the value
1211 * appended may in fact be the string we write to, changing it may cause freed
1212 * memory to be used:
1213 * :redir => foo
1214 * :let foo
1215 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 */
1217 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001218var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001220 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221
1222 if (redir_lval == NULL)
1223 return;
1224
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001226 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001228 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231 {
1232 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 }
1235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237}
1238
1239/*
1240 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 */
1243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001244var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001246 typval_T tv;
1247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248 if (redir_lval != NULL)
1249 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 /* If there was no error: assign the text to the variable. */
1251 if (redir_endp != NULL)
1252 {
1253 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1254 tv.v_type = VAR_STRING;
1255 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001256 /* Call get_lval() again, if it's inside a Dict or List it may
1257 * have changed. */
1258 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001259 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001260 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1261 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1262 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001264
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001265 /* free the collected output */
1266 vim_free(redir_ga.ga_data);
1267 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001269 vim_free(redir_lval);
1270 redir_lval = NULL;
1271 }
1272 vim_free(redir_varname);
1273 redir_varname = NULL;
1274}
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# if defined(FEAT_MBYTE) || defined(PROTO)
1277 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001278eval_charconvert(
1279 char_u *enc_from,
1280 char_u *enc_to,
1281 char_u *fname_from,
1282 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int err = FALSE;
1285
1286 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1287 set_vim_var_string(VV_CC_TO, enc_to, -1);
1288 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1289 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1290 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1291 err = TRUE;
1292 set_vim_var_string(VV_CC_FROM, NULL, -1);
1293 set_vim_var_string(VV_CC_TO, NULL, -1);
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296
1297 if (err)
1298 return FAIL;
1299 return OK;
1300}
1301# endif
1302
1303# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err = FALSE;
1308
1309 set_vim_var_string(VV_FNAME_IN, fname, -1);
1310 set_vim_var_string(VV_CMDARG, args, -1);
1311 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1312 err = TRUE;
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_CMDARG, NULL, -1);
1315
1316 if (err)
1317 {
1318 mch_remove(fname);
1319 return FAIL;
1320 }
1321 return OK;
1322}
1323# endif
1324
1325# if defined(FEAT_DIFF) || defined(PROTO)
1326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_diff(
1328 char_u *origfile,
1329 char_u *newfile,
1330 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int err = FALSE;
1333
1334 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1335 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1336 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1337 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1338 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1339 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1340 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1341}
1342
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_patch(
1345 char_u *origfile,
1346 char_u *difffile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359# endif
1360
1361/*
1362 * Top level evaluation function, returning a boolean.
1363 * Sets "error" to TRUE if there was an error.
1364 * Return TRUE or FALSE.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_bool(
1368 char_u *arg,
1369 int *error,
1370 char_u **nextcmd,
1371 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 int retval = FALSE;
1375
1376 if (skip)
1377 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
1381 {
1382 *error = FALSE;
1383 if (!skip)
1384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001385 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
1396 * Top level evaluation function, returning a string. If "skip" is TRUE,
1397 * only parsing to "nextcmd" is done, without reporting errors. Return
1398 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1399 */
1400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401eval_to_string_skip(
1402 char_u *arg,
1403 char_u **nextcmd,
1404 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 char_u *retval;
1408
1409 if (skip)
1410 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = NULL;
1413 else
1414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 retval = vim_strsave(get_tv_string(&tv));
1416 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 if (skip)
1419 --emsg_skip;
1420
1421 return retval;
1422}
1423
1424/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001425 * Skip over an expression at "*pp".
1426 * Return FAIL for an error, OK otherwise.
1427 */
1428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001429skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430{
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001432
1433 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435}
1436
1437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001439 * When "convert" is TRUE convert a List into a sequence of lines and convert
1440 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * Return pointer to allocated memory, or NULL for failure.
1442 */
1443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444eval_to_string(
1445 char_u *arg,
1446 char_u **nextcmd,
1447 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001451 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001452#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = NULL;
1458 else
1459 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001460 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001461 {
1462 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001463 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001464 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001466 if (tv.vval.v_list->lv_len > 0)
1467 ga_append(&ga, NL);
1468 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 ga_append(&ga, NUL);
1470 retval = (char_u *)ga.ga_data;
1471 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001472#ifdef FEAT_FLOAT
1473 else if (convert && tv.v_type == VAR_FLOAT)
1474 {
1475 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1476 retval = vim_strsave(numbuf);
1477 }
1478#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001479 else
1480 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483
1484 return retval;
1485}
1486
1487/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 * Call eval_to_string() without using current local variables and using
1489 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
1491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492eval_to_string_safe(
1493 char_u *arg,
1494 char_u **nextcmd,
1495 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 char_u *retval;
1498 void *save_funccalp;
1499
1500 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 if (use_sandbox)
1502 ++sandbox;
1503 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001504 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 if (use_sandbox)
1506 --sandbox;
1507 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 restore_funccal(save_funccalp);
1509 return retval;
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
1513 * Top level evaluation function, returning a number.
1514 * Evaluates "expr" silently.
1515 * Returns -1 for an error.
1516 */
1517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001522 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 ++emsg_off;
1525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 retval = -1;
1528 else
1529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001530 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 --emsg_off;
1534
1535 return retval;
1536}
1537
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538/*
1539 * Prepare v: variable "idx" to be used.
1540 * Save the current typeval in "save_tv".
1541 * When not used yet add the variable to the v: hashtable.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545{
1546 *save_tv = vimvars[idx].vv_tv;
1547 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1548 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1549}
1550
1551/*
1552 * Restore v: variable "idx" to typeval "save_tv".
1553 * When no longer defined, remove the variable from the v: hashtable.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001557{
1558 hashitem_T *hi;
1559
Bram Moolenaara40058a2005-07-11 22:42:07 +00001560 vimvars[idx].vv_tv = *save_tv;
1561 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1562 {
1563 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1564 if (HASHITEM_EMPTY(hi))
1565 EMSG2(_(e_intern2), "restore_vimvar()");
1566 else
1567 hash_remove(&vimvarht, hi);
1568 }
1569}
1570
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001571#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001572/*
1573 * Evaluate an expression to a list with suggestions.
1574 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001575 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576 */
1577 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 typval_T save_val;
1581 typval_T rettv;
1582 list_T *list = NULL;
1583 char_u *p = skipwhite(expr);
1584
1585 /* Set "v:val" to the bad word. */
1586 prepare_vimvar(VV_VAL, &save_val);
1587 vimvars[VV_VAL].vv_type = VAR_STRING;
1588 vimvars[VV_VAL].vv_str = badword;
1589 if (p_verbose == 0)
1590 ++emsg_off;
1591
1592 if (eval1(&p, &rettv, TRUE) == OK)
1593 {
1594 if (rettv.v_type != VAR_LIST)
1595 clear_tv(&rettv);
1596 else
1597 list = rettv.vval.v_list;
1598 }
1599
1600 if (p_verbose == 0)
1601 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001602 restore_vimvar(VV_VAL, &save_val);
1603
1604 return list;
1605}
1606
1607/*
1608 * "list" is supposed to contain two items: a word and a number. Return the
1609 * word in "pp" and the number as the return value.
1610 * Return -1 if anything isn't right.
1611 * Used to get the good word and score from the eval_spell_expr() result.
1612 */
1613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615{
1616 listitem_T *li;
1617
1618 li = list->lv_first;
1619 if (li == NULL)
1620 return -1;
1621 *pp = get_tv_string(&li->li_tv);
1622
1623 li = li->li_next;
1624 if (li == NULL)
1625 return -1;
1626 return get_tv_number(&li->li_tv);
1627}
1628#endif
1629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001631 * Top level evaluation function.
1632 * Returns an allocated typval_T with the result.
1633 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634 */
1635 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001637{
1638 typval_T *tv;
1639
1640 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001641 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642 {
1643 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 }
1646
1647 return tv;
1648}
1649
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653 * Uses argv[argc] for the function arguments. Only Number and String
1654 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658call_vim_function(
1659 char_u *func,
1660 int argc,
1661 char_u **argv,
1662 int safe, /* use the sandbox */
1663 int str_arg_only, /* all arguments are strings */
1664 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 long n;
1668 int len;
1669 int i;
1670 int doesrange;
1671 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001674 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 for (i = 0; i < argc; i++)
1679 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001680 /* Pass a NULL or empty argument as an empty string */
1681 if (argv[i] == NULL || *argv[i] == NUL)
1682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001683 argvars[i].v_type = VAR_STRING;
1684 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 continue;
1686 }
1687
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001688 if (str_arg_only)
1689 len = 0;
1690 else
1691 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001692 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (len != 0 && len == (int)STRLEN(argv[i]))
1694 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001695 argvars[i].v_type = VAR_NUMBER;
1696 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 else
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 }
1704
1705 if (safe)
1706 {
1707 save_funccalp = save_funccal();
1708 ++sandbox;
1709 }
1710
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1712 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001714 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (safe)
1716 {
1717 --sandbox;
1718 restore_funccal(save_funccalp);
1719 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 vim_free(argvars);
1721
1722 if (ret == FAIL)
1723 clear_tv(rettv);
1724
1725 return ret;
1726}
1727
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001728/*
1729 * Call vimL function "func" and return the result as a number.
1730 * Returns -1 when calling the function fails.
1731 * Uses argv[argc] for the function arguments.
1732 */
1733 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734call_func_retnr(
1735 char_u *func,
1736 int argc,
1737 char_u **argv,
1738 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739{
1740 typval_T rettv;
1741 long retval;
1742
1743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1745 return -1;
1746
1747 retval = get_tv_number_chk(&rettv, NULL);
1748 clear_tv(&rettv);
1749 return retval;
1750}
1751
1752#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1753 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1754
Bram Moolenaar4f688582007-07-24 12:34:30 +00001755# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001757 * Call vimL function "func" and return the result as a string.
1758 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 * Uses argv[argc] for the function arguments.
1760 */
1761 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762call_func_retstr(
1763 char_u *func,
1764 int argc,
1765 char_u **argv,
1766 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767{
1768 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001769 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001771 /* All arguments are passed as strings, no conversion to number. */
1772 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773 return NULL;
1774
1775 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 return retval;
1778}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001779# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001781/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001782 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001784 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 */
1786 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787call_func_retlist(
1788 char_u *func,
1789 int argc,
1790 char_u **argv,
1791 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792{
1793 typval_T rettv;
1794
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001795 /* All arguments are passed as strings, no conversion to number. */
1796 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797 return NULL;
1798
1799 if (rettv.v_type != VAR_LIST)
1800 {
1801 clear_tv(&rettv);
1802 return NULL;
1803 }
1804
1805 return rettv.vval.v_list;
1806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
1809/*
1810 * Save the current function call pointer, and set it to NULL.
1811 * Used when executing autocommands and for ":source".
1812 */
1813 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 current_funccal = NULL;
1819 return (void *)fc;
1820}
1821
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825 funccall_T *fc = (funccall_T *)vfc;
1826
1827 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830#if defined(FEAT_PROFILE) || defined(PROTO)
1831/*
1832 * Prepare profiling for entering a child or something else that is not
1833 * counted for the script/function itself.
1834 * Should always be called in pair with prof_child_exit().
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837prof_child_enter(
1838 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001839{
1840 funccall_T *fc = current_funccal;
1841
1842 if (fc != NULL && fc->func->uf_profiling)
1843 profile_start(&fc->prof_child);
1844 script_prof_save(tm);
1845}
1846
1847/*
1848 * Take care of time spent in a child.
1849 * Should always be called after prof_child_enter().
1850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852prof_child_exit(
1853 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001854{
1855 funccall_T *fc = current_funccal;
1856
1857 if (fc != NULL && fc->func->uf_profiling)
1858 {
1859 profile_end(&fc->prof_child);
1860 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1861 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1862 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1863 }
1864 script_prof_restore(tm);
1865}
1866#endif
1867
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_FOLDING
1870/*
1871 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1872 * it in "*cp". Doesn't give error messages.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int retval;
1879 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001880 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1881 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001884 if (use_sandbox)
1885 ++sandbox;
1886 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 retval = 0;
1890 else
1891 {
1892 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (tv.v_type == VAR_NUMBER)
1894 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001895 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 retval = 0;
1897 else
1898 {
1899 /* If the result is a string, check if there is a non-digit before
1900 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (!VIM_ISDIGIT(*s) && *s != '-')
1903 *cp = *s++;
1904 retval = atol((char *)s);
1905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001909 if (use_sandbox)
1910 --sandbox;
1911 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 return retval;
1914}
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 * ":let" list all variable values
1919 * ":let var1 var2" list variable values
1920 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * ":let var += expr" assignment command.
1922 * ":let var -= expr" assignment command.
1923 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 int var_count = 0;
1934 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001936 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 argend = skip_var_list(arg, &var_count, &semicolon);
1940 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001942 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1943 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 expr = skipwhite(argend);
1945 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1946 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001948 /*
1949 * ":let" without "=": list variables
1950 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 if (*arg == '[')
1952 EMSG(_(e_invarg));
1953 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001959 list_glob_vars(&first);
1960 list_buf_vars(&first);
1961 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001962#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_script_vars(&first);
1966 list_func_vars(&first);
1967 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 eap->nextcmd = check_nextcmd(arg);
1970 }
1971 else
1972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 op[0] = '=';
1974 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001975 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001977 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1978 op[0] = *expr; /* +=, -= or .= */
1979 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001981 else
1982 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (eap->skip)
1985 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (eap->skip)
1988 {
1989 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --emsg_skip;
1992 }
1993 else if (i != FAIL)
1994 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
2000}
2001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002/*
2003 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2004 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 * When "nextchars" is not NULL it points to a string with characters that
2006 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2007 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 * Returns OK or FAIL;
2009 */
2010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011ex_let_vars(
2012 char_u *arg_start,
2013 typval_T *tv,
2014 int copy, /* copy values from "tv", don't move */
2015 int semicolon, /* from skip_var_list() */
2016 int var_count, /* from skip_var_list() */
2017 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018{
2019 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 listitem_T *item;
2023 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024
2025 if (*arg != '[')
2026 {
2027 /*
2028 * ":let var = expr" or ":for var in list"
2029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return FAIL;
2032 return OK;
2033 }
2034
2035 /*
2036 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2037 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002038 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 {
2040 EMSG(_(e_listreq));
2041 return FAIL;
2042 }
2043
2044 i = list_len(l);
2045 if (semicolon == 0 && var_count < i)
2046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002047 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 }
2050 if (var_count - semicolon > i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055
2056 item = l->lv_first;
2057 while (*arg != ']')
2058 {
2059 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 item = item->li_next;
2062 if (arg == NULL)
2063 return FAIL;
2064
2065 arg = skipwhite(arg);
2066 if (*arg == ';')
2067 {
2068 /* Put the rest of the list (may be empty) in the var after ';'.
2069 * Create a new list for this. */
2070 l = list_alloc();
2071 if (l == NULL)
2072 return FAIL;
2073 while (item != NULL)
2074 {
2075 list_append_tv(l, &item->li_tv);
2076 item = item->li_next;
2077 }
2078
2079 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002080 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 ltv.vval.v_list = l;
2082 l->lv_refcount = 1;
2083
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2085 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 clear_tv(&ltv);
2087 if (arg == NULL)
2088 return FAIL;
2089 break;
2090 }
2091 else if (*arg != ',' && *arg != ']')
2092 {
2093 EMSG2(_(e_intern2), "ex_let_vars()");
2094 return FAIL;
2095 }
2096 }
2097
2098 return OK;
2099}
2100
2101/*
2102 * Skip over assignable variable "var" or list of variables "[var, var]".
2103 * Used for ":let varvar = expr" and ":for varvar in expr".
2104 * For "[var, var]" increment "*var_count" for each variable.
2105 * for "[var, var; var]" set "semicolon".
2106 * Return NULL for an error.
2107 */
2108 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109skip_var_list(
2110 char_u *arg,
2111 int *var_count,
2112 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113{
2114 char_u *p, *s;
2115
2116 if (*arg == '[')
2117 {
2118 /* "[var, var]": find the matching ']'. */
2119 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002120 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 {
2122 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2123 s = skip_var_one(p);
2124 if (s == p)
2125 {
2126 EMSG2(_(e_invarg2), p);
2127 return NULL;
2128 }
2129 ++*var_count;
2130
2131 p = skipwhite(s);
2132 if (*p == ']')
2133 break;
2134 else if (*p == ';')
2135 {
2136 if (*semicolon == 1)
2137 {
2138 EMSG(_("Double ; in list of variables"));
2139 return NULL;
2140 }
2141 *semicolon = 1;
2142 }
2143 else if (*p != ',')
2144 {
2145 EMSG2(_(e_invarg2), p);
2146 return NULL;
2147 }
2148 }
2149 return p + 1;
2150 }
2151 else
2152 return skip_var_one(arg);
2153}
2154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002156 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002157 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 if (*arg == '@' && arg[1] != NUL)
2163 return arg + 2;
2164 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2165 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166}
2167
Bram Moolenaara7043832005-01-21 11:56:39 +00002168/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 * List variables for hashtab "ht" with prefix "prefix".
2170 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_hashtable_vars(
2174 hashtab_T *ht,
2175 char_u *prefix,
2176 int empty,
2177 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 hashitem_T *hi;
2180 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 int todo;
2182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002183 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2185 {
2186 if (!HASHITEM_EMPTY(hi))
2187 {
2188 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 di = HI2DI(hi);
2190 if (empty || di->di_tv.v_type != VAR_STRING
2191 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193 }
2194 }
2195}
2196
2197/*
2198 * List global variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002204}
2205
2206/*
2207 * List buffer variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002211{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u numbuf[NUMBUFLEN];
2213
Bram Moolenaar429fa852013-04-15 12:27:36 +02002214 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216
2217 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2219 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002220}
2221
2222/*
2223 * List window variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002227{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002228 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002230}
2231
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232#ifdef FEAT_WINDOWS
2233/*
2234 * List tab page variables.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002239 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241}
2242#endif
2243
Bram Moolenaara7043832005-01-21 11:56:39 +00002244/*
2245 * List Vim variables.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251}
2252
2253/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254 * List script-local variables, if there is a script.
2255 */
2256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258{
2259 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2261 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262}
2263
2264/*
2265 * List function variables, if there is a function.
2266 */
2267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002269{
2270 if (current_funccal != NULL)
2271 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002273}
2274
2275/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 * List variables in "arg".
2277 */
2278 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002279list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280{
2281 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 char_u *name_start;
2285 char_u *arg_subsc;
2286 char_u *tofree;
2287 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288
2289 while (!ends_excmd(*arg) && !got_int)
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2295 {
2296 emsg_severe = TRUE;
2297 EMSG(_(e_trailing));
2298 break;
2299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 /* get_name_len() takes care of expanding curly braces */
2304 name_start = name = arg;
2305 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2306 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* This is mainly to keep test 49 working: when expanding
2309 * curly braces fails overrule the exception error message. */
2310 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 emsg_severe = TRUE;
2313 EMSG2(_(e_invarg2), arg);
2314 break;
2315 }
2316 error = TRUE;
2317 }
2318 else
2319 {
2320 if (tofree != NULL)
2321 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002322 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 else
2325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 /* handle d.key, l[idx], f(expr) */
2327 arg_subsc = arg;
2328 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002336 case 'g': list_glob_vars(first); break;
2337 case 'b': list_buf_vars(first); break;
2338 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002339#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002340 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002341#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 case 'v': list_vim_vars(first); break;
2343 case 's': list_script_vars(first); break;
2344 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 default:
2346 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002347 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
2349 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 {
2351 char_u numbuf[NUMBUFLEN];
2352 char_u *tf;
2353 int c;
2354 char_u *s;
2355
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002356 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002357 c = *arg;
2358 *arg = NUL;
2359 list_one_var_a((char_u *)"",
2360 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002361 tv.v_type,
2362 s == NULL ? (char_u *)"" : s,
2363 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002364 *arg = c;
2365 vim_free(tf);
2366 }
2367 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002371
2372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374
2375 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
2377
2378 return arg;
2379}
2380
2381/*
2382 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2383 * Returns a pointer to the char just after the var name.
2384 * Returns NULL if there is an error.
2385 */
2386 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002387ex_let_one(
2388 char_u *arg, /* points to variable name */
2389 typval_T *tv, /* value to assign to variable */
2390 int copy, /* copy value from "tv" */
2391 char_u *endchars, /* valid chars after variable name or NULL */
2392 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393{
2394 int c1;
2395 char_u *name;
2396 char_u *p;
2397 char_u *arg_end = NULL;
2398 int len;
2399 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
2402 /*
2403 * ":let $VAR = expr": Set environment variable.
2404 */
2405 if (*arg == '$')
2406 {
2407 /* Find the end of the name. */
2408 ++arg;
2409 name = arg;
2410 len = get_env_len(&arg);
2411 if (len == 0)
2412 EMSG2(_(e_invarg2), name - 1);
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002420 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 {
2422 c1 = name[len];
2423 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 p = get_tv_string_chk(tv);
2425 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
2427 int mustfree = FALSE;
2428 char_u *s = vim_getenv(name, &mustfree);
2429
2430 if (s != NULL)
2431 {
2432 p = tofree = concat_str(s, p);
2433 if (mustfree)
2434 vim_free(s);
2435 }
2436 }
2437 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 if (STRICMP(name, "HOME") == 0)
2441 init_homedir();
2442 else if (didset_vim && STRICMP(name, "VIM") == 0)
2443 didset_vim = FALSE;
2444 else if (didset_vimruntime
2445 && STRICMP(name, "VIMRUNTIME") == 0)
2446 didset_vimruntime = FALSE;
2447 arg_end = arg;
2448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 }
2452 }
2453 }
2454
2455 /*
2456 * ":let &option = expr": Set option value.
2457 * ":let &l:option = expr": Set local option value.
2458 * ":let &g:option = expr": Set global option value.
2459 */
2460 else if (*arg == '&')
2461 {
2462 /* Find the end of the name. */
2463 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 if (p == NULL || (endchars != NULL
2465 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 EMSG(_(e_letunexp));
2467 else
2468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 long n;
2470 int opt_type;
2471 long numval;
2472 char_u *stringval = NULL;
2473 char_u *s;
2474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 c1 = *p;
2476 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477
2478 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 s = get_tv_string_chk(tv); /* != NULL if number or string */
2480 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 {
2482 opt_type = get_option_value(arg, &numval,
2483 &stringval, opt_flags);
2484 if ((opt_type == 1 && *op == '.')
2485 || (opt_type == 0 && *op != '.'))
2486 EMSG2(_(e_letwrong), op);
2487 else
2488 {
2489 if (opt_type == 1) /* number */
2490 {
2491 if (*op == '+')
2492 n = numval + n;
2493 else
2494 n = numval - n;
2495 }
2496 else if (opt_type == 0 && stringval != NULL) /* string */
2497 {
2498 s = concat_str(stringval, s);
2499 vim_free(stringval);
2500 stringval = s;
2501 }
2502 }
2503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (s != NULL)
2505 {
2506 set_option_value(arg, n, s, opt_flags);
2507 arg_end = p;
2508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let @r = expr": Set register contents.
2516 */
2517 else if (*arg == '@')
2518 {
2519 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 if (op != NULL && (*op == '+' || *op == '-'))
2521 EMSG2(_(e_letwrong), op);
2522 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 char_u *s;
2529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 p = get_tv_string_chk(tv);
2531 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002533 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 if (s != NULL)
2535 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 vim_free(s);
2538 }
2539 }
2540 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 arg_end = arg + 1;
2544 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
2547 }
2548
2549 /*
2550 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2561 EMSG(_(e_letunexp));
2562 else
2563 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 arg_end = p;
2566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 }
2570
2571 else
2572 EMSG2(_(e_invarg2), arg);
2573
2574 return arg_end;
2575}
2576
2577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2579 */
2580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582{
2583 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2584 {
2585 EMSG2(_(e_readonlyvar), arg);
2586 return TRUE;
2587 }
2588 return FALSE;
2589}
2590
2591/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Get an lval: variable, Dict item or List item that can be assigned a value
2593 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2594 * "name.key", "name.key[expr]" etc.
2595 * Indexing only works if "name" is an existing List or Dictionary.
2596 * "name" points to the start of the name.
2597 * If "rettv" is not NULL it points to the value to be assigned.
2598 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2599 * wrong; must end in space or cmd separator.
2600 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 * flags:
2602 * GLV_QUIET: do not give error messages
2603 * GLV_NO_AUTOLOAD: do not use script autoloading
2604 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002606 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 */
2609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610get_lval(
2611 char_u *name,
2612 typval_T *rettv,
2613 lval_T *lp,
2614 int unlet,
2615 int skip,
2616 int flags, /* GLV_ values */
2617 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 char_u *expr_start, *expr_end;
2621 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 dictitem_T *v;
2623 typval_T var1;
2624 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634
2635 if (skip)
2636 {
2637 /* When skipping just find the end of the name. */
2638 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002639 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 }
2641
2642 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002643 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (expr_start != NULL)
2645 {
2646 /* Don't expand the name when we already know there is an error. */
2647 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2648 && *p != '[' && *p != '.')
2649 {
2650 EMSG(_(e_trailing));
2651 return NULL;
2652 }
2653
2654 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2655 if (lp->ll_exp_name == NULL)
2656 {
2657 /* Report an invalid expression in braces, unless the
2658 * expression evaluation has been cancelled due to an
2659 * aborting error, an interrupt, or an exception. */
2660 if (!aborting() && !quiet)
2661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 EMSG2(_(e_invarg2), name);
2664 return NULL;
2665 }
2666 }
2667 lp->ll_name = lp->ll_exp_name;
2668 }
2669 else
2670 lp->ll_name = name;
2671
2672 /* Without [idx] or .key we are done. */
2673 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2674 return p;
2675
2676 cc = *p;
2677 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002678 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (v == NULL && !quiet)
2680 EMSG2(_(e_undefvar), lp->ll_name);
2681 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 if (v == NULL)
2683 return NULL;
2684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 /*
2686 * Loop until no more [idx] or .key is following.
2687 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002688 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2692 && !(lp->ll_tv->v_type == VAR_DICT
2693 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E689: Can only index a List or Dictionary"));
2697 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E708: [:] must come last"));
2703 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 len = -1;
2707 if (*p == '.')
2708 {
2709 key = p + 1;
2710 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2711 ;
2712 if (len == 0)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
2716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 p = key + len;
2719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 else
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p == ':')
2725 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 else
2727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 empty1 = FALSE;
2729 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var1) == NULL)
2732 {
2733 /* not a number or string */
2734 clear_tv(&var1);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
2738
2739 /* Optionally get the second index [ :expr]. */
2740 if (*p == ':')
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (rettv != NULL && (rettv->v_type != VAR_LIST
2751 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759 p = skipwhite(p + 1);
2760 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (!empty1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 if (get_tv_string_chk(&var2) == NULL)
2772 {
2773 /* not a number or string */
2774 if (!empty1)
2775 clear_tv(&var1);
2776 clear_tv(&var2);
2777 return NULL;
2778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
2788 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (!empty1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795
2796 /* Skip to past ']'. */
2797 ++p;
2798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
2802 if (len == -1)
2803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002805 key = get_tv_string_chk(&var1); /* is number or string */
2806 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 lp->ll_list = NULL;
2813 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 /* When assigning to a scope dictionary check that a function and
2817 * variable name is valid (only variable name unless it is l: or
2818 * g: dictionary). Disallow overwriting a builtin function. */
2819 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 int prevval;
2822 int wrong;
2823
2824 if (len != -1)
2825 {
2826 prevval = key[len];
2827 key[len] = NUL;
2828 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002829 else
2830 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002831 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2832 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 || !valid_varname(key);
2835 if (len != -1)
2836 key[len] = prevval;
2837 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* Can't add "v:" variable. */
2844 if (lp->ll_dict == &vimvardict)
2845 {
2846 EMSG2(_(e_illvar), name);
2847 return NULL;
2848 }
2849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002850 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 if (len == -1)
2856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 }
2859 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (len == -1)
2864 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 p = NULL;
2867 break;
2868 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002869 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002870 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002871 return NULL;
2872
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 if (len == -1)
2874 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877 else
2878 {
2879 /*
2880 * Get the number and item for the only or first index of the List.
2881 */
2882 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 else
2885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 clear_tv(&var1);
2888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_list = lp->ll_tv->vval.v_list;
2891 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2892 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 if (lp->ll_n1 < 0)
2895 {
2896 lp->ll_n1 = 0;
2897 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2898 }
2899 }
2900 if (lp->ll_li == NULL)
2901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 }
2908
2909 /*
2910 * May need to find the item or absolute index for the second
2911 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 * When no index given: "lp->ll_empty2" is TRUE.
2913 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002917 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002923 {
2924 if (!quiet)
2925 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002927 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2932 if (lp->ll_n1 < 0)
2933 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2934 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002935 {
2936 if (!quiet)
2937 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
2941
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 return p;
2947}
2948
2949/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 */
2952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954{
2955 vim_free(lp->ll_exp_name);
2956 vim_free(lp->ll_newkey);
2957}
2958
2959/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 */
2964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965set_var_lval(
2966 lval_T *lp,
2967 char_u *endp,
2968 typval_T *rettv,
2969 int copy,
2970 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971{
2972 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002973 listitem_T *ri;
2974 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975
2976 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 cc = *endp;
2981 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002986 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002987 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002988 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002989 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 if ((di == NULL
2992 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2993 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2994 FALSE)))
2995 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 set_var(lp->ll_name, &tv, FALSE);
2997 clear_tv(&tv);
2998 }
2999 }
3000 else
3001 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 else if (tv_check_lock(lp->ll_newkey == NULL
3006 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003007 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003008 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 else if (lp->ll_range)
3010 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 listitem_T *ll_li = lp->ll_li;
3012 int ll_n1 = lp->ll_n1;
3013
3014 /*
3015 * Check whether any of the list items is locked
3016 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003017 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003019 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020 return;
3021 ri = ri->li_next;
3022 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3023 break;
3024 ll_li = ll_li->li_next;
3025 ++ll_n1;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /*
3029 * Assign the List values to the list items.
3030 */
3031 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3035 else
3036 {
3037 clear_tv(&lp->ll_li->li_tv);
3038 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3042 break;
3043 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003046 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 ri = NULL;
3049 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_li = lp->ll_li->li_next;
3053 ++lp->ll_n1;
3054 }
3055 if (ri != NULL)
3056 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (lp->ll_empty2
3058 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 : lp->ll_n1 != lp->ll_n2)
3060 EMSG(_("E711: List value has not enough items"));
3061 }
3062 else
3063 {
3064 /*
3065 * Assign to a List or Dictionary item.
3066 */
3067 if (lp->ll_newkey != NULL)
3068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 if (op != NULL && *op != '=')
3070 {
3071 EMSG2(_(e_letwrong), op);
3072 return;
3073 }
3074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003076 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 if (di == NULL)
3078 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003079 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3080 {
3081 vim_free(di);
3082 return;
3083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 else if (op != NULL && *op != '=')
3087 {
3088 tv_op(lp->ll_tv, rettv, op);
3089 return;
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /*
3095 * Assign the value to the variable or list item.
3096 */
3097 if (copy)
3098 copy_tv(rettv, lp->ll_tv);
3099 else
3100 {
3101 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003102 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 }
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106}
3107
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3110 * Returns OK or FAIL.
3111 */
3112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114{
3115 long n;
3116 char_u numbuf[NUMBUFLEN];
3117 char_u *s;
3118
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003119 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3120 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3121 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 {
3123 switch (tv1->v_type)
3124 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003125 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 case VAR_DICT:
3127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003128 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003129 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003131 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 break;
3133
3134 case VAR_LIST:
3135 if (*op != '+' || tv2->v_type != VAR_LIST)
3136 break;
3137 /* List += List */
3138 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3139 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3140 return OK;
3141
3142 case VAR_NUMBER:
3143 case VAR_STRING:
3144 if (tv2->v_type == VAR_LIST)
3145 break;
3146 if (*op == '+' || *op == '-')
3147 {
3148 /* nr += nr or nr -= nr*/
3149 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150#ifdef FEAT_FLOAT
3151 if (tv2->v_type == VAR_FLOAT)
3152 {
3153 float_T f = n;
3154
3155 if (*op == '+')
3156 f += tv2->vval.v_float;
3157 else
3158 f -= tv2->vval.v_float;
3159 clear_tv(tv1);
3160 tv1->v_type = VAR_FLOAT;
3161 tv1->vval.v_float = f;
3162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164#endif
3165 {
3166 if (*op == '+')
3167 n += get_tv_number(tv2);
3168 else
3169 n -= get_tv_number(tv2);
3170 clear_tv(tv1);
3171 tv1->v_type = VAR_NUMBER;
3172 tv1->vval.v_number = n;
3173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 else
3176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 if (tv2->v_type == VAR_FLOAT)
3178 break;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* str .= str */
3181 s = get_tv_string(tv1);
3182 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3183 clear_tv(tv1);
3184 tv1->v_type = VAR_STRING;
3185 tv1->vval.v_string = s;
3186 }
3187 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003190#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 {
3192 float_T f;
3193
3194 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3195 && tv2->v_type != VAR_NUMBER
3196 && tv2->v_type != VAR_STRING))
3197 break;
3198 if (tv2->v_type == VAR_FLOAT)
3199 f = tv2->vval.v_float;
3200 else
3201 f = get_tv_number(tv2);
3202 if (*op == '+')
3203 tv1->vval.v_float += f;
3204 else
3205 tv1->vval.v_float -= f;
3206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003208 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003209 }
3210 }
3211
3212 EMSG2(_(e_letwrong), op);
3213 return FAIL;
3214}
3215
3216/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 * Add a watcher to a list.
3218 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
3222 lw->lw_next = l->lv_watch;
3223 l->lv_watch = lw;
3224}
3225
3226/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003227 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 * No warning when it isn't found...
3229 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 lwp = &l->lv_watch;
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 {
3238 if (lw == lwrem)
3239 {
3240 *lwp = lw->lw_next;
3241 break;
3242 }
3243 lwp = &lw->lw_next;
3244 }
3245}
3246
3247/*
3248 * Just before removing an item from a list: advance watchers to the next
3249 * item.
3250 */
3251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3257 if (lw->lw_item == item)
3258 lw->lw_item = item->li_next;
3259}
3260
3261/*
3262 * Evaluate the expression used in a ":for var in expr" command.
3263 * "arg" points to "var".
3264 * Set "*errp" to TRUE for an error, FALSE otherwise;
3265 * Return a pointer that holds the info. Null when there is an error.
3266 */
3267 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval_for_line(
3269 char_u *arg,
3270 int *errp,
3271 char_u **nextcmdp,
3272 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T tv;
3277 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 *errp = TRUE; /* default: there is an error */
3280
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 if (fi == NULL)
3283 return NULL;
3284
3285 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3286 if (expr == NULL)
3287 return fi;
3288
3289 expr = skipwhite(expr);
3290 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3291 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003292 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 return fi;
3294 }
3295
3296 if (skip)
3297 ++emsg_skip;
3298 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3299 {
3300 *errp = FALSE;
3301 if (!skip)
3302 {
3303 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003304 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 clear_tv(&tv);
3308 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003309 else if (l == NULL)
3310 {
3311 /* a null list is like an empty list: do nothing */
3312 clear_tv(&tv);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 else
3315 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003316 /* No need to increment the refcount, it's already set for the
3317 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 fi->fi_list = l;
3319 list_add_watch(l, &fi->fi_lw);
3320 fi->fi_lw.lw_item = l->lv_first;
3321 }
3322 }
3323 }
3324 if (skip)
3325 --emsg_skip;
3326
3327 return fi;
3328}
3329
3330/*
3331 * Use the first item in a ":for" list. Advance to the next.
3332 * Assign the values to the variable (list). "arg" points to the first one.
3333 * Return TRUE when a valid item was found, FALSE when at end of list or
3334 * something wrong.
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342
3343 item = fi->fi_lw.lw_item;
3344 if (item == NULL)
3345 result = FALSE;
3346 else
3347 {
3348 fi->fi_lw.lw_item = item->li_next;
3349 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3350 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3351 }
3352 return result;
3353}
3354
3355/*
3356 * Free the structure used to store info used by ":for".
3357 */
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360{
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003363 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003364 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003366 list_unref(fi->fi_list);
3367 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 vim_free(fi);
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3372
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374set_context_for_expression(
3375 expand_T *xp,
3376 char_u *arg,
3377 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int got_eq = FALSE;
3380 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 if (cmdidx == CMD_let)
3384 {
3385 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003386 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 {
3388 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003389 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 {
3391 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003393 if (vim_iswhite(*p))
3394 break;
3395 }
3396 return;
3397 }
3398 }
3399 else
3400 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3401 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while ((xp->xp_pattern = vim_strpbrk(arg,
3403 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3404 {
3405 c = *xp->xp_pattern;
3406 if (c == '&')
3407 {
3408 c = xp->xp_pattern[1];
3409 if (c == '&')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = cmdidx != CMD_let || got_eq
3413 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3414 }
3415 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003418 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3419 xp->xp_pattern += 2;
3420
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else if (c == '$')
3424 {
3425 /* environment variable */
3426 xp->xp_context = EXPAND_ENV_VARS;
3427 }
3428 else if (c == '=')
3429 {
3430 got_eq = TRUE;
3431 xp->xp_context = EXPAND_EXPRESSION;
3432 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003433 else if (c == '#'
3434 && xp->xp_context == EXPAND_EXPRESSION)
3435 {
3436 /* Autoload function/variable contains '#'. */
3437 break;
3438 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003439 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && xp->xp_context == EXPAND_FUNCTIONS
3441 && vim_strchr(xp->xp_pattern, '(') == NULL)
3442 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003443 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445 }
3446 else if (cmdidx != CMD_let || got_eq)
3447 {
3448 if (c == '"') /* string */
3449 {
3450 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3451 if (c == '\\' && xp->xp_pattern[1] != NUL)
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_NOTHING;
3454 }
3455 else if (c == '\'') /* literal string */
3456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003457 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3459 /* skip */ ;
3460 xp->xp_context = EXPAND_NOTHING;
3461 }
3462 else if (c == '|')
3463 {
3464 if (xp->xp_pattern[1] == '|')
3465 {
3466 ++xp->xp_pattern;
3467 xp->xp_context = EXPAND_EXPRESSION;
3468 }
3469 else
3470 xp->xp_context = EXPAND_COMMANDS;
3471 }
3472 else
3473 xp->xp_context = EXPAND_EXPRESSION;
3474 }
3475 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003476 /* Doesn't look like something valid, expand as an expression
3477 * anyway. */
3478 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 arg = xp->xp_pattern;
3480 if (*arg != NUL)
3481 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3482 /* skip */ ;
3483 }
3484 xp->xp_pattern = arg;
3485}
3486
3487#endif /* FEAT_CMDL_COMPL */
3488
3489/*
3490 * ":1,25call func(arg1, arg2)" function call.
3491 */
3492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 char_u *arg = eap->arg;
3496 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 linenr_T lnum;
3502 int doesrange;
3503 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003505 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 if (eap->skip)
3508 {
3509 /* trans_function_name() doesn't work well when skipping, use eval0()
3510 * instead to skip to any following command, e.g. for:
3511 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003512 ++emsg_skip;
3513 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3514 clear_tv(&rettv);
3515 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003516 return;
3517 }
3518
Bram Moolenaar65639032016-03-16 21:40:30 +01003519 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003520 if (fudi.fd_newkey != NULL)
3521 {
3522 /* Still need to give an error message for missing key. */
3523 EMSG2(_(e_dictkey), fudi.fd_newkey);
3524 vim_free(fudi.fd_newkey);
3525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003526 if (tofree == NULL)
3527 return;
3528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 /* Increase refcount on dictionary, it could get deleted when evaluating
3530 * the arguments. */
3531 if (fudi.fd_dict != NULL)
3532 ++fudi.fd_dict->dv_refcount;
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3535 * contents. For VAR_PARTIAL get its partial, unless we already have one
3536 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 name = deref_func_name(tofree, &len,
3539 partial != NULL ? NULL : &partial, FALSE);
3540
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 /* Skip white space to allow ":call func ()". Not good, but required for
3542 * backward compatibility. */
3543 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 if (*startarg != '(')
3547 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003548 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 goto end;
3550 }
3551
3552 /*
3553 * When skipping, evaluate the function once, to find the end of the
3554 * arguments.
3555 * When the function takes a range, this is discovered after the first
3556 * call, and the loop is broken.
3557 */
3558 if (eap->skip)
3559 {
3560 ++emsg_skip;
3561 lnum = eap->line2; /* do it once, also with an invalid range */
3562 }
3563 else
3564 lnum = eap->line1;
3565 for ( ; lnum <= eap->line2; ++lnum)
3566 {
3567 if (!eap->skip && eap->addr_count > 0)
3568 {
3569 curwin->w_cursor.lnum = lnum;
3570 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003571#ifdef FEAT_VIRTUALEDIT
3572 curwin->w_cursor.coladd = 0;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003578 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 failed = TRUE;
3581 break;
3582 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003583
3584 /* Handle a function returning a Funcref, Dictionary or List. */
3585 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3586 {
3587 failed = TRUE;
3588 break;
3589 }
3590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (doesrange || eap->skip)
3593 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (aborting())
3600 break;
3601 }
3602 if (eap->skip)
3603 --emsg_skip;
3604
3605 if (!failed)
3606 {
3607 /* Check for trailing illegal characters and a following command. */
3608 if (!ends_excmd(*arg))
3609 {
3610 emsg_severe = TRUE;
3611 EMSG(_(e_trailing));
3612 }
3613 else
3614 eap->nextcmd = check_nextcmd(arg);
3615 }
3616
3617end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003618 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * ":unlet[!] var1 ... " command.
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 ex_unletlock(eap, eap->arg, 0);
3629}
3630
3631/*
3632 * ":lockvar" and ":unlockvar" commands
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 int deep = 2;
3639
3640 if (eap->forceit)
3641 deep = -1;
3642 else if (vim_isdigit(*arg))
3643 {
3644 deep = getdigits(&arg);
3645 arg = skipwhite(arg);
3646 }
3647
3648 ex_unletlock(eap, arg, deep);
3649}
3650
3651/*
3652 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3653 */
3654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ex_unletlock(
3656 exarg_T *eap,
3657 char_u *argstart,
3658 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659{
3660 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 do
3666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003668 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003669 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 if (lv.ll_name == NULL)
3671 error = TRUE; /* error but continue parsing */
3672 if (name_end == NULL || (!vim_iswhite(*name_end)
3673 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (name_end != NULL)
3676 {
3677 emsg_severe = TRUE;
3678 EMSG(_(e_trailing));
3679 }
3680 if (!(eap->skip || error))
3681 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 break;
3683 }
3684
3685 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 {
3687 if (eap->cmdidx == CMD_unlet)
3688 {
3689 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3690 error = TRUE;
3691 }
3692 else
3693 {
3694 if (do_lock_var(&lv, name_end, deep,
3695 eap->cmdidx == CMD_lockvar) == FAIL)
3696 error = TRUE;
3697 }
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 if (!eap->skip)
3701 clear_lval(&lv);
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 arg = skipwhite(name_end);
3704 } while (!ends_excmd(*arg));
3705
3706 eap->nextcmd = check_nextcmd(arg);
3707}
3708
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710do_unlet_var(
3711 lval_T *lp,
3712 char_u *name_end,
3713 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 int ret = OK;
3716 int cc;
3717
3718 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003731 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 else if (lp->ll_range)
3736 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003738 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003739 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003740
3741 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3742 {
3743 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003744 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745 return FAIL;
3746 ll_li = li;
3747 ++ll_n1;
3748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749
3750 /* Delete a range of List items. */
3751 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3752 {
3753 li = lp->ll_li->li_next;
3754 listitem_remove(lp->ll_list, lp->ll_li);
3755 lp->ll_li = li;
3756 ++lp->ll_n1;
3757 }
3758 }
3759 else
3760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003761 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 /* unlet a List item. */
3763 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003766 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 }
3768
3769 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770}
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hashtab_T *ht;
3780 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003782 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 ht = find_var_ht(name, &varname);
3786 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003788 if (ht == &globvarht)
3789 d = &globvardict;
3790 else if (current_funccal != NULL
3791 && ht == &current_funccal->l_vars.dv_hashtab)
3792 d = &current_funccal->l_vars;
3793 else if (ht == &compat_hashtab)
3794 d = &vimvardict;
3795 else
3796 {
3797 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3798 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3799 }
3800 if (d == NULL)
3801 {
3802 EMSG2(_(e_intern2), "do_unlet()");
3803 return FAIL;
3804 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hi = hash_find(ht, varname);
3806 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003808 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003809 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003810 || var_check_ro(di->di_flags, name, FALSE)
3811 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003812 return FAIL;
3813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003814 delete_var(ht, hi);
3815 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (forceit)
3819 return OK;
3820 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822}
3823
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003824/*
3825 * Lock or unlock variable indicated by "lp".
3826 * "deep" is the levels to go (-1 for unlimited);
3827 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3828 */
3829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830do_lock_var(
3831 lval_T *lp,
3832 char_u *name_end,
3833 int deep,
3834 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835{
3836 int ret = OK;
3837 int cc;
3838 dictitem_T *di;
3839
3840 if (deep == 0) /* nothing to do */
3841 return OK;
3842
3843 if (lp->ll_tv == NULL)
3844 {
3845 cc = *name_end;
3846 *name_end = NUL;
3847
3848 /* Normal name or expanded name. */
3849 if (check_changedtick(lp->ll_name))
3850 ret = FAIL;
3851 else
3852 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003853 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854 if (di == NULL)
3855 ret = FAIL;
3856 else
3857 {
3858 if (lock)
3859 di->di_flags |= DI_FLAGS_LOCK;
3860 else
3861 di->di_flags &= ~DI_FLAGS_LOCK;
3862 item_lock(&di->di_tv, deep, lock);
3863 }
3864 }
3865 *name_end = cc;
3866 }
3867 else if (lp->ll_range)
3868 {
3869 listitem_T *li = lp->ll_li;
3870
3871 /* (un)lock a range of List items. */
3872 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3873 {
3874 item_lock(&li->li_tv, deep, lock);
3875 li = li->li_next;
3876 ++lp->ll_n1;
3877 }
3878 }
3879 else if (lp->ll_list != NULL)
3880 /* (un)lock a List item. */
3881 item_lock(&lp->ll_li->li_tv, deep, lock);
3882 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003883 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 item_lock(&lp->ll_di->di_tv, deep, lock);
3885
3886 return ret;
3887}
3888
3889/*
3890 * Lock or unlock an item. "deep" is nr of levels to go.
3891 */
3892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003893item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003894{
3895 static int recurse = 0;
3896 list_T *l;
3897 listitem_T *li;
3898 dict_T *d;
3899 hashitem_T *hi;
3900 int todo;
3901
3902 if (recurse >= DICT_MAXNEST)
3903 {
3904 EMSG(_("E743: variable nested too deep for (un)lock"));
3905 return;
3906 }
3907 if (deep == 0)
3908 return;
3909 ++recurse;
3910
3911 /* lock/unlock the item itself */
3912 if (lock)
3913 tv->v_lock |= VAR_LOCKED;
3914 else
3915 tv->v_lock &= ~VAR_LOCKED;
3916
3917 switch (tv->v_type)
3918 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 case VAR_UNKNOWN:
3920 case VAR_NUMBER:
3921 case VAR_STRING:
3922 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003923 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003924 case VAR_FLOAT:
3925 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003927 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 break;
3929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003930 case VAR_LIST:
3931 if ((l = tv->vval.v_list) != NULL)
3932 {
3933 if (lock)
3934 l->lv_lock |= VAR_LOCKED;
3935 else
3936 l->lv_lock &= ~VAR_LOCKED;
3937 if (deep < 0 || deep > 1)
3938 /* recursive: lock/unlock the items the List contains */
3939 for (li = l->lv_first; li != NULL; li = li->li_next)
3940 item_lock(&li->li_tv, deep - 1, lock);
3941 }
3942 break;
3943 case VAR_DICT:
3944 if ((d = tv->vval.v_dict) != NULL)
3945 {
3946 if (lock)
3947 d->dv_lock |= VAR_LOCKED;
3948 else
3949 d->dv_lock &= ~VAR_LOCKED;
3950 if (deep < 0 || deep > 1)
3951 {
3952 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003954 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3955 {
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 --todo;
3959 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3960 }
3961 }
3962 }
3963 }
3964 }
3965 --recurse;
3966}
3967
Bram Moolenaara40058a2005-07-11 22:42:07 +00003968/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003969 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3970 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003971 */
3972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003974{
3975 return (tv->v_lock & VAR_LOCKED)
3976 || (tv->v_type == VAR_LIST
3977 && tv->vval.v_list != NULL
3978 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3979 || (tv->v_type == VAR_DICT
3980 && tv->vval.v_dict != NULL
3981 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3982}
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3985/*
3986 * Delete all "menutrans_" variables.
3987 */
3988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003989del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003995 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 {
3998 if (!HASHITEM_EMPTY(hi))
3999 {
4000 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4002 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 }
4004 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006}
4007#endif
4008
4009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4010
4011/*
4012 * Local string buffer for the next two functions to store a variable name
4013 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4014 * get_user_var_name().
4015 */
4016
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004017static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019static char_u *varnamebuf = NULL;
4020static int varnamebuflen = 0;
4021
4022/*
4023 * Function to concatenate a prefix and a variable name.
4024 */
4025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 int len;
4029
4030 len = (int)STRLEN(name) + 3;
4031 if (len > varnamebuflen)
4032 {
4033 vim_free(varnamebuf);
4034 len += 10; /* some additional space */
4035 varnamebuf = alloc(len);
4036 if (varnamebuf == NULL)
4037 {
4038 varnamebuflen = 0;
4039 return NULL;
4040 }
4041 varnamebuflen = len;
4042 }
4043 *varnamebuf = prefix;
4044 varnamebuf[1] = ':';
4045 STRCPY(varnamebuf + 2, name);
4046 return varnamebuf;
4047}
4048
4049/*
4050 * Function given to ExpandGeneric() to obtain the list of user defined
4051 * (global/buffer/window/built-in) variable names.
4052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004054get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 static long_u gdone;
4057 static long_u bdone;
4058 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 static long_u tdone;
4061#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 static int vidx;
4063 static hashitem_T *hi;
4064 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 tdone = 0;
4071#endif
4072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* Global variables */
4075 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004079 else
4080 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004081 while (HASHITEM_EMPTY(hi))
4082 ++hi;
4083 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4084 return cat_prefix_varname('g', hi->hi_key);
4085 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
4088 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004089 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004102 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return (char_u *)"b:changedtick";
4104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105
4106 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004110 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004112 else
4113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004118
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119#ifdef FEAT_WINDOWS
4120 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004121 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 if (tdone < ht->ht_used)
4123 {
4124 if (tdone++ == 0)
4125 hi = ht->ht_array;
4126 else
4127 ++hi;
4128 while (HASHITEM_EMPTY(hi))
4129 ++hi;
4130 return cat_prefix_varname('t', hi->hi_key);
4131 }
4132#endif
4133
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 /* v: variables */
4135 if (vidx < VV_LEN)
4136 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 vim_free(varnamebuf);
4139 varnamebuf = NULL;
4140 varnamebuflen = 0;
4141 return NULL;
4142}
4143
4144#endif /* FEAT_CMDL_COMPL */
4145
4146/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004147 * Return TRUE if "pat" matches "text".
4148 * Does not use 'cpo' and always uses 'magic'.
4149 */
4150 static int
4151pattern_match(char_u *pat, char_u *text, int ic)
4152{
4153 int matches = FALSE;
4154 char_u *save_cpo;
4155 regmatch_T regmatch;
4156
4157 /* avoid 'l' flag in 'cpoptions' */
4158 save_cpo = p_cpo;
4159 p_cpo = (char_u *)"";
4160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4161 if (regmatch.regprog != NULL)
4162 {
4163 regmatch.rm_ic = ic;
4164 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4165 vim_regfree(regmatch.regprog);
4166 }
4167 p_cpo = save_cpo;
4168 return matches;
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * types for expressions.
4173 */
4174typedef enum
4175{
4176 TYPE_UNKNOWN = 0
4177 , TYPE_EQUAL /* == */
4178 , TYPE_NEQUAL /* != */
4179 , TYPE_GREATER /* > */
4180 , TYPE_GEQUAL /* >= */
4181 , TYPE_SMALLER /* < */
4182 , TYPE_SEQUAL /* <= */
4183 , TYPE_MATCH /* =~ */
4184 , TYPE_NOMATCH /* !~ */
4185} exptype_T;
4186
4187/*
4188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4191 */
4192
4193/*
4194 * Handle zero level expression.
4195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval0(
4202 char_u *arg,
4203 typval_T *rettv,
4204 char_u **nextcmd,
4205 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 int ret;
4208 char_u *p;
4209
4210 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (ret == FAIL || !ends_excmd(*p))
4213 {
4214 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /*
4217 * Report the invalid expression unless the expression evaluation has
4218 * been cancelled due to an aborting error, an interrupt, or an
4219 * exception.
4220 */
4221 if (!aborting())
4222 EMSG2(_(e_invexpr2), arg);
4223 ret = FAIL;
4224 }
4225 if (nextcmd != NULL)
4226 *nextcmd = check_nextcmd(p);
4227
4228 return ret;
4229}
4230
4231/*
4232 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004233 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004238 * Note: "rettv.v_lock" is not set.
4239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Return OK or FAIL.
4241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 if ((*arg)[0] == '?')
4255 {
4256 result = FALSE;
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
4260
4261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return FAIL;
4274
4275 /*
4276 * Check for the ":".
4277 */
4278 if ((*arg)[0] != ':')
4279 {
4280 EMSG(_("E109: Missing ':' after '?'"));
4281 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284 }
4285
4286 /*
4287 * Get the third variable.
4288 */
4289 *arg = skipwhite(*arg + 1);
4290 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4291 {
4292 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return FAIL;
4295 }
4296 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle first level expression:
4305 * expr2 || expr2 || expr2 logical OR
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 long result;
4317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Get the first variable.
4322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return FAIL;
4325
4326 /*
4327 * Repeat until there is no following "||".
4328 */
4329 first = TRUE;
4330 result = FALSE;
4331 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4332 {
4333 if (evaluate && first)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 first = FALSE;
4341 }
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(*arg + 2);
4347 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4348 return FAIL;
4349
4350 /*
4351 * Compute the result.
4352 */
4353 if (evaluate && !result)
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle second level expression:
4373 * expr3 && expr3 && expr3 logical AND
4374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 long result;
4385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 /*
4389 * Get the first variable.
4390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393
4394 /*
4395 * Repeat until there is no following "&&".
4396 */
4397 first = TRUE;
4398 result = TRUE;
4399 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4400 {
4401 if (evaluate && first)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
4407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 first = FALSE;
4409 }
4410
4411 /*
4412 * Get the second variable.
4413 */
4414 *arg = skipwhite(*arg + 2);
4415 if (eval4(arg, &var2, evaluate && result) == FAIL)
4416 return FAIL;
4417
4418 /*
4419 * Compute the result.
4420 */
4421 if (evaluate && result)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 if (error)
4427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 if (evaluate)
4430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 rettv->v_type = VAR_NUMBER;
4432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 }
4435
4436 return OK;
4437}
4438
4439/*
4440 * Handle third level expression:
4441 * var1 == var2
4442 * var1 =~ var2
4443 * var1 != var2
4444 * var1 !~ var2
4445 * var1 > var2
4446 * var1 >= var2
4447 * var1 < var2
4448 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 * var1 is var2
4450 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 *
4452 * "arg" must point to the first non-white of the expression.
4453 * "arg" is advanced to the next non-white after the recognized expression.
4454 *
4455 * Return OK or FAIL.
4456 */
4457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *p;
4462 int i;
4463 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int len = 2;
4466 long n1, n2;
4467 char_u *s1, *s2;
4468 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
4472 * Get the first variable.
4473 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476
4477 p = *arg;
4478 switch (p[0])
4479 {
4480 case '=': if (p[1] == '=')
4481 type = TYPE_EQUAL;
4482 else if (p[1] == '~')
4483 type = TYPE_MATCH;
4484 break;
4485 case '!': if (p[1] == '=')
4486 type = TYPE_NEQUAL;
4487 else if (p[1] == '~')
4488 type = TYPE_NOMATCH;
4489 break;
4490 case '>': if (p[1] != '=')
4491 {
4492 type = TYPE_GREATER;
4493 len = 1;
4494 }
4495 else
4496 type = TYPE_GEQUAL;
4497 break;
4498 case '<': if (p[1] != '=')
4499 {
4500 type = TYPE_SMALLER;
4501 len = 1;
4502 }
4503 else
4504 type = TYPE_SEQUAL;
4505 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 case 'i': if (p[1] == 's')
4507 {
4508 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4509 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004510 i = p[len];
4511 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 {
4513 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4514 type_is = TRUE;
4515 }
4516 }
4517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
4520 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004521 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 if (type != TYPE_UNKNOWN)
4524 {
4525 /* extra question mark appended: ignore case */
4526 if (p[len] == '?')
4527 {
4528 ic = TRUE;
4529 ++len;
4530 }
4531 /* extra '#' appended: match case */
4532 else if (p[len] == '#')
4533 {
4534 ic = FALSE;
4535 ++len;
4536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 ic = p_ic;
4540
4541 /*
4542 * Get the second variable.
4543 */
4544 *arg = skipwhite(p + len);
4545 if (eval5(arg, &var2, evaluate) == FAIL)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return FAIL;
4549 }
4550
4551 if (evaluate)
4552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 if (type_is && rettv->v_type != var2.v_type)
4554 {
4555 /* For "is" a different type always means FALSE, for "notis"
4556 * it means TRUE. */
4557 n1 = (type == TYPE_NEQUAL);
4558 }
4559 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4560 {
4561 if (type_is)
4562 {
4563 n1 = (rettv->v_type == var2.v_type
4564 && rettv->vval.v_list == var2.vval.v_list);
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 else if (rettv->v_type != var2.v_type
4569 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4570 {
4571 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004572 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004574 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
4579 else
4580 {
4581 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004582 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4583 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 }
4588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4590 {
4591 if (type_is)
4592 {
4593 n1 = (rettv->v_type == var2.v_type
4594 && rettv->vval.v_dict == var2.vval.v_dict);
4595 if (type == TYPE_NEQUAL)
4596 n1 = !n1;
4597 }
4598 else if (rettv->v_type != var2.v_type
4599 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4600 {
4601 if (rettv->v_type != var2.v_type)
4602 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4603 else
4604 EMSG(_("E736: Invalid operation for Dictionary"));
4605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 else
4610 {
4611 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004612 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4613 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 }
4618
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004619 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4620 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004621 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004622 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004624 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 clear_tv(rettv);
4626 clear_tv(&var2);
4627 return FAIL;
4628 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004630 if (type == TYPE_NEQUAL)
4631 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 }
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634#ifdef FEAT_FLOAT
4635 /*
4636 * If one of the two variables is a float, compare as a float.
4637 * When using "=~" or "!~", always compare as string.
4638 */
4639 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4640 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4641 {
4642 float_T f1, f2;
4643
4644 if (rettv->v_type == VAR_FLOAT)
4645 f1 = rettv->vval.v_float;
4646 else
4647 f1 = get_tv_number(rettv);
4648 if (var2.v_type == VAR_FLOAT)
4649 f2 = var2.vval.v_float;
4650 else
4651 f2 = get_tv_number(&var2);
4652 n1 = FALSE;
4653 switch (type)
4654 {
4655 case TYPE_EQUAL: n1 = (f1 == f2); break;
4656 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4657 case TYPE_GREATER: n1 = (f1 > f2); break;
4658 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4659 case TYPE_SMALLER: n1 = (f1 < f2); break;
4660 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4661 case TYPE_UNKNOWN:
4662 case TYPE_MATCH:
4663 case TYPE_NOMATCH: break; /* avoid gcc warning */
4664 }
4665 }
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * If one of the two variables is a number, compare as a number.
4670 * When using "=~" or "!~", always compare as string.
4671 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004672 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 n1 = get_tv_number(rettv);
4676 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 switch (type)
4678 {
4679 case TYPE_EQUAL: n1 = (n1 == n2); break;
4680 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4681 case TYPE_GREATER: n1 = (n1 > n2); break;
4682 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4683 case TYPE_SMALLER: n1 = (n1 < n2); break;
4684 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4685 case TYPE_UNKNOWN:
4686 case TYPE_MATCH:
4687 case TYPE_NOMATCH: break; /* avoid gcc warning */
4688 }
4689 }
4690 else
4691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 s1 = get_tv_string_buf(rettv, buf1);
4693 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4695 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4696 else
4697 i = 0;
4698 n1 = FALSE;
4699 switch (type)
4700 {
4701 case TYPE_EQUAL: n1 = (i == 0); break;
4702 case TYPE_NEQUAL: n1 = (i != 0); break;
4703 case TYPE_GREATER: n1 = (i > 0); break;
4704 case TYPE_GEQUAL: n1 = (i >= 0); break;
4705 case TYPE_SMALLER: n1 = (i < 0); break;
4706 case TYPE_SEQUAL: n1 = (i <= 0); break;
4707
4708 case TYPE_MATCH:
4709 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004710 n1 = pattern_match(s2, s1, ic);
4711 if (type == TYPE_NOMATCH)
4712 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714
4715 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4716 }
4717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 rettv->v_type = VAR_NUMBER;
4721 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723 }
4724
4725 return OK;
4726}
4727
4728/*
4729 * Handle fourth level expression:
4730 * + number addition
4731 * - number subtraction
4732 * . string concatenation
4733 *
4734 * "arg" must point to the first non-white of the expression.
4735 * "arg" is advanced to the next non-white after the recognized expression.
4736 *
4737 * Return OK or FAIL.
4738 */
4739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004740eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar33570922005-01-25 22:26:29 +00004742 typval_T var2;
4743 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 int op;
4745 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 float_T f1 = 0, f2 = 0;
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *s1, *s2;
4750 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4751 char_u *p;
4752
4753 /*
4754 * Get the first variable.
4755 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 /*
4760 * Repeat computing, until no '+', '-' or '.' is following.
4761 */
4762 for (;;)
4763 {
4764 op = **arg;
4765 if (op != '+' && op != '-' && op != '.')
4766 break;
4767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 if ((op != '+' || rettv->v_type != VAR_LIST)
4769#ifdef FEAT_FLOAT
4770 && (op == '.' || rettv->v_type != VAR_FLOAT)
4771#endif
4772 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
4774 /* For "list + ...", an illegal use of the first operand as
4775 * a number cannot be determined before evaluating the 2nd
4776 * operand: if this is also a list, all is ok.
4777 * For "something . ...", "something - ..." or "non-list + ...",
4778 * we know that the first operand needs to be a string or number
4779 * without evaluating the 2nd operand. So check before to avoid
4780 * side effects after an error. */
4781 if (evaluate && get_tv_string_chk(rettv) == NULL)
4782 {
4783 clear_tv(rettv);
4784 return FAIL;
4785 }
4786 }
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 /*
4789 * Get the second variable.
4790 */
4791 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
4797
4798 if (evaluate)
4799 {
4800 /*
4801 * Compute the result.
4802 */
4803 if (op == '.')
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4806 s2 = get_tv_string_buf_chk(&var2, buf2);
4807 if (s2 == NULL) /* type error ? */
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
4815 rettv->v_type = VAR_STRING;
4816 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004818 else if (op == '+' && rettv->v_type == VAR_LIST
4819 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004820 {
4821 /* concatenate Lists */
4822 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4823 &var3) == FAIL)
4824 {
4825 clear_tv(rettv);
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 clear_tv(rettv);
4830 *rettv = var3;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else
4833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 int error = FALSE;
4835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 f1 = rettv->vval.v_float;
4840 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 else
4843#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 n1 = get_tv_number_chk(rettv, &error);
4846 if (error)
4847 {
4848 /* This can only happen for "list + non-list". For
4849 * "non-list + ..." or "something - ...", we returned
4850 * before evaluating the 2nd operand. */
4851 clear_tv(rettv);
4852 return FAIL;
4853 }
4854#ifdef FEAT_FLOAT
4855 if (var2.v_type == VAR_FLOAT)
4856 f1 = n1;
4857#endif
4858 }
4859#ifdef FEAT_FLOAT
4860 if (var2.v_type == VAR_FLOAT)
4861 {
4862 f2 = var2.vval.v_float;
4863 n2 = 0;
4864 }
4865 else
4866#endif
4867 {
4868 n2 = get_tv_number_chk(&var2, &error);
4869 if (error)
4870 {
4871 clear_tv(rettv);
4872 clear_tv(&var2);
4873 return FAIL;
4874 }
4875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 f2 = n2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881
4882#ifdef FEAT_FLOAT
4883 /* If there is a float on either side the result is a float. */
4884 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4885 {
4886 if (op == '+')
4887 f1 = f1 + f2;
4888 else
4889 f1 = f1 - f2;
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
4895 {
4896 if (op == '+')
4897 n1 = n1 + n2;
4898 else
4899 n1 = n1 - n2;
4900 rettv->v_type = VAR_NUMBER;
4901 rettv->vval.v_number = n1;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907 return OK;
4908}
4909
4910/*
4911 * Handle fifth level expression:
4912 * * number multiplication
4913 * / number division
4914 * % number modulo
4915 *
4916 * "arg" must point to the first non-white of the expression.
4917 * "arg" is advanced to the next non-white after the recognized expression.
4918 *
4919 * Return OK or FAIL.
4920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922eval6(
4923 char_u **arg,
4924 typval_T *rettv,
4925 int evaluate,
4926 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int op;
4930 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 int use_float = FALSE;
4933 float_T f1 = 0, f2;
4934#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 /*
4938 * Get the first variable.
4939 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004940 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942
4943 /*
4944 * Repeat computing, until no '*', '/' or '%' is following.
4945 */
4946 for (;;)
4947 {
4948 op = **arg;
4949 if (op != '*' && op != '/' && op != '%')
4950 break;
4951
4952 if (evaluate)
4953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954#ifdef FEAT_FLOAT
4955 if (rettv->v_type == VAR_FLOAT)
4956 {
4957 f1 = rettv->vval.v_float;
4958 use_float = TRUE;
4959 n1 = 0;
4960 }
4961 else
4962#endif
4963 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 if (error)
4966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
4969 n1 = 0;
4970
4971 /*
4972 * Get the second variable.
4973 */
4974 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004975 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return FAIL;
4977
4978 if (evaluate)
4979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (var2.v_type == VAR_FLOAT)
4982 {
4983 if (!use_float)
4984 {
4985 f1 = n1;
4986 use_float = TRUE;
4987 }
4988 f2 = var2.vval.v_float;
4989 n2 = 0;
4990 }
4991 else
4992#endif
4993 {
4994 n2 = get_tv_number_chk(&var2, &error);
4995 clear_tv(&var2);
4996 if (error)
4997 return FAIL;
4998#ifdef FEAT_FLOAT
4999 if (use_float)
5000 f2 = n2;
5001#endif
5002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008#ifdef FEAT_FLOAT
5009 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 f1 = f1 * f2;
5013 else if (op == '/')
5014 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005015# ifdef VMS
5016 /* VMS crashes on divide by zero, work around it */
5017 if (f2 == 0.0)
5018 {
5019 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005020 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005021 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005022 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005024 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005025 }
5026 else
5027 f1 = f1 / f2;
5028# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 /* We rely on the floating point library to handle divide
5030 * by zero to result in "inf" and not a crash. */
5031 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005032# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005036 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 return FAIL;
5038 }
5039 rettv->v_type = VAR_FLOAT;
5040 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 n1 = n1 * n2;
5047 else if (op == '/')
5048 {
5049 if (n2 == 0) /* give an error message? */
5050 {
5051 if (n1 == 0)
5052 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5053 else if (n1 < 0)
5054 n1 = -0x7fffffffL;
5055 else
5056 n1 = 0x7fffffffL;
5057 }
5058 else
5059 n1 = n1 / n2;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 {
5063 if (n2 == 0) /* give an error message? */
5064 n1 = 0;
5065 else
5066 n1 = n1 % n2;
5067 }
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 return OK;
5075}
5076
5077/*
5078 * Handle sixth level expression:
5079 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005080 * "string" string constant
5081 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * &option-name option value
5083 * @r register contents
5084 * identifier variable value
5085 * function() function call
5086 * $VAR environment variable
5087 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005088 * [expr, expr] List
5089 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *
5091 * Also handle:
5092 * ! in front logical NOT
5093 * - in front unary minus
5094 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 * trailing [] subscript in String or List
5096 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *
5098 * "arg" must point to the first non-white of the expression.
5099 * "arg" is advanced to the next non-white after the recognized expression.
5100 *
5101 * Return OK or FAIL.
5102 */
5103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104eval7(
5105 char_u **arg,
5106 typval_T *rettv,
5107 int evaluate,
5108 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 long n;
5111 int len;
5112 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 char_u *start_leader, *end_leader;
5114 int ret = OK;
5115 char_u *alias;
5116
5117 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 /*
5124 * Skip '!' and '-' characters. They are handled later.
5125 */
5126 start_leader = *arg;
5127 while (**arg == '!' || **arg == '-' || **arg == '+')
5128 *arg = skipwhite(*arg + 1);
5129 end_leader = *arg;
5130
5131 switch (**arg)
5132 {
5133 /*
5134 * Number constant.
5135 */
5136 case '0':
5137 case '1':
5138 case '2':
5139 case '3':
5140 case '4':
5141 case '5':
5142 case '6':
5143 case '7':
5144 case '8':
5145 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 {
5147#ifdef FEAT_FLOAT
5148 char_u *p = skipdigits(*arg + 1);
5149 int get_float = FALSE;
5150
5151 /* We accept a float when the format matches
5152 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005153 * strict to avoid backwards compatibility problems.
5154 * Don't look for a float after the "." operator, so that
5155 * ":let vers = 1.2.3" doesn't fail. */
5156 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 get_float = TRUE;
5159 p = skipdigits(p + 2);
5160 if (*p == 'e' || *p == 'E')
5161 {
5162 ++p;
5163 if (*p == '-' || *p == '+')
5164 ++p;
5165 if (!vim_isdigit(*p))
5166 get_float = FALSE;
5167 else
5168 p = skipdigits(p + 1);
5169 }
5170 if (ASCII_ISALPHA(*p) || *p == '.')
5171 get_float = FALSE;
5172 }
5173 if (get_float)
5174 {
5175 float_T f;
5176
5177 *arg += string2float(*arg, &f);
5178 if (evaluate)
5179 {
5180 rettv->v_type = VAR_FLOAT;
5181 rettv->vval.v_float = f;
5182 }
5183 }
5184 else
5185#endif
5186 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005187 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 *arg += len;
5189 if (evaluate)
5190 {
5191 rettv->v_type = VAR_NUMBER;
5192 rettv->vval.v_number = n;
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 /*
5199 * String constant: "string".
5200 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 break;
5209
5210 /*
5211 * List: [expr, expr]
5212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215
5216 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 * Dictionary: {key: val, key: val}
5218 */
5219 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5220 break;
5221
5222 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005223 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227
5228 /*
5229 * Environment variable: $VAR.
5230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233
5234 /*
5235 * Register contents: @r.
5236 */
5237 case '@': ++*arg;
5238 if (evaluate)
5239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005241 rettv->vval.v_string = get_reg_contents(**arg,
5242 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 if (**arg != NUL)
5245 ++*arg;
5246 break;
5247
5248 /*
5249 * nested expression: (expression).
5250 */
5251 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (**arg == ')')
5254 ++*arg;
5255 else if (ret == OK)
5256 {
5257 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ret = FAIL;
5260 }
5261 break;
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
5265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266
5267 if (ret == NOTDONE)
5268 {
5269 /*
5270 * Must be a variable or function name.
5271 * Can also be a curly-braces kind of name: {expr}.
5272 */
5273 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 if (alias != NULL)
5276 s = alias;
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 ret = FAIL;
5280 else
5281 {
5282 if (**arg == '(') /* recursive! */
5283 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 partial_T *partial;
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /* If "s" is the name of a variable of type VAR_FUNC
5287 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005288 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289
5290 /* Invoke the function. */
5291 ret = get_func_tv(s, len, rettv, arg,
5292 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005293 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005294
5295 /* If evaluate is FALSE rettv->v_type was not set in
5296 * get_func_tv, but it's needed in handle_subscript() to parse
5297 * what follows. So set it here. */
5298 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5299 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005300 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005301 rettv->v_type = VAR_FUNC;
5302 }
5303
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /* Stop the expression evaluation when immediately
5305 * aborting on error, or when an interrupt occurred or
5306 * an exception was thrown but not caught. */
5307 if (aborting())
5308 {
5309 if (ret == OK)
5310 clear_tv(rettv);
5311 ret = FAIL;
5312 }
5313 }
5314 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005315 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 else
5317 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005319 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 }
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 *arg = skipwhite(*arg);
5323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5325 * expr(expr). */
5326 if (ret == OK)
5327 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5331 */
5332 if (ret == OK && evaluate && end_leader > start_leader)
5333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005335 int val = 0;
5336#ifdef FEAT_FLOAT
5337 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 if (rettv->v_type == VAR_FLOAT)
5340 f = rettv->vval.v_float;
5341 else
5342#endif
5343 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 clear_tv(rettv);
5347 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else
5350 {
5351 while (end_leader > start_leader)
5352 {
5353 --end_leader;
5354 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 {
5356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 f = !f;
5359 else
5360#endif
5361 val = !val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364 {
5365#ifdef FEAT_FLOAT
5366 if (rettv->v_type == VAR_FLOAT)
5367 f = -f;
5368 else
5369#endif
5370 val = -val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005373#ifdef FEAT_FLOAT
5374 if (rettv->v_type == VAR_FLOAT)
5375 {
5376 clear_tv(rettv);
5377 rettv->vval.v_float = f;
5378 }
5379 else
5380#endif
5381 {
5382 clear_tv(rettv);
5383 rettv->v_type = VAR_NUMBER;
5384 rettv->vval.v_number = val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
5389 return ret;
5390}
5391
5392/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005393 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5394 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5396 */
5397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398eval_index(
5399 char_u **arg,
5400 typval_T *rettv,
5401 int evaluate,
5402 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403{
5404 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 long len = -1;
5408 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005415 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 if (verbose)
5417 EMSG(_("E695: Cannot index a Funcref"));
5418 return FAIL;
5419 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005420#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 if (verbose)
5422 EMSG(_(e_float_as_string));
5423 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005424#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005425 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005427 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 if (verbose)
5429 EMSG(_("E909: Cannot index a special variable"));
5430 return FAIL;
5431 case VAR_UNKNOWN:
5432 if (evaluate)
5433 return FAIL;
5434 /* FALLTHROUGH */
5435
5436 case VAR_STRING:
5437 case VAR_NUMBER:
5438 case VAR_LIST:
5439 case VAR_DICT:
5440 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005443 init_tv(&var1);
5444 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 /*
5448 * dict.name
5449 */
5450 key = *arg + 1;
5451 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5452 ;
5453 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 /*
5460 * something[idx]
5461 *
5462 * Get the (first) variable from inside the [].
5463 */
5464 *arg = skipwhite(*arg + 1);
5465 if (**arg == ':')
5466 empty1 = TRUE;
5467 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5468 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005469 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5470 {
5471 /* not a number or string */
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475
5476 /*
5477 * Get the second variable from inside the [:].
5478 */
5479 if (**arg == ':')
5480 {
5481 range = TRUE;
5482 *arg = skipwhite(*arg + 1);
5483 if (**arg == ']')
5484 empty2 = TRUE;
5485 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005487 if (!empty1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5492 {
5493 /* not a number or string */
5494 if (!empty1)
5495 clear_tv(&var1);
5496 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 return FAIL;
5498 }
5499 }
5500
5501 /* Check for the ']'. */
5502 if (**arg != ']')
5503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 if (verbose)
5505 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 clear_tv(&var1);
5507 if (range)
5508 clear_tv(&var2);
5509 return FAIL;
5510 }
5511 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513
5514 if (evaluate)
5515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 n1 = 0;
5517 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n1 = get_tv_number(&var1);
5520 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 if (range)
5523 {
5524 if (empty2)
5525 n2 = -1;
5526 else
5527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 n2 = get_tv_number(&var2);
5529 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005537 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 case VAR_SPECIAL:
5540 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005541 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005542 break; /* not evaluating, skipping over subscript */
5543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 case VAR_NUMBER:
5545 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 len = (long)STRLEN(s);
5548 if (range)
5549 {
5550 /* The resulting variable is a substring. If the indexes
5551 * are out of range the result is empty. */
5552 if (n1 < 0)
5553 {
5554 n1 = len + n1;
5555 if (n1 < 0)
5556 n1 = 0;
5557 }
5558 if (n2 < 0)
5559 n2 = len + n2;
5560 else if (n2 >= len)
5561 n2 = len;
5562 if (n1 >= len || n2 < 0 || n1 > n2)
5563 s = NULL;
5564 else
5565 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5566 }
5567 else
5568 {
5569 /* The resulting variable is a string of a single
5570 * character. If the index is too big or negative the
5571 * result is empty. */
5572 if (n1 >= len || n1 < 0)
5573 s = NULL;
5574 else
5575 s = vim_strnsave(s + n1, 1);
5576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 break;
5581
5582 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 if (n1 < 0)
5585 n1 = len + n1;
5586 if (!empty1 && (n1 < 0 || n1 >= len))
5587 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 /* For a range we allow invalid values and return an empty
5589 * list. A list index out of range is an error. */
5590 if (!range)
5591 {
5592 if (verbose)
5593 EMSGN(_(e_listidx), n1);
5594 return FAIL;
5595 }
5596 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 }
5598 if (range)
5599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602
5603 if (n2 < 0)
5604 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005605 else if (n2 >= len)
5606 n2 = len - 1;
5607 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005608 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 l = list_alloc();
5610 if (l == NULL)
5611 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 n1 <= n2; ++n1)
5614 {
5615 if (list_append_tv(l, &item->li_tv) == FAIL)
5616 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 return FAIL;
5619 }
5620 item = item->li_next;
5621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 clear_tv(rettv);
5623 rettv->v_type = VAR_LIST;
5624 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005625 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 else
5628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005629 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 clear_tv(rettv);
5631 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634
5635 case VAR_DICT:
5636 if (range)
5637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005638 if (verbose)
5639 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (len == -1)
5641 clear_tv(&var1);
5642 return FAIL;
5643 }
5644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646
5647 if (len == -1)
5648 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005649 key = get_tv_string_chk(&var1);
5650 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 clear_tv(&var1);
5653 return FAIL;
5654 }
5655 }
5656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005660 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (len == -1)
5662 clear_tv(&var1);
5663 if (item == NULL)
5664 return FAIL;
5665
5666 copy_tv(&item->di_tv, &var1);
5667 clear_tv(rettv);
5668 *rettv = var1;
5669 }
5670 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 }
5672 }
5673
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Get an option value.
5679 * "arg" points to the '&' or '+' before the option name.
5680 * "arg" is advanced to character after the option name.
5681 * Return OK or FAIL.
5682 */
5683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684get_option_tv(
5685 char_u **arg,
5686 typval_T *rettv, /* when NULL, only check if option exists */
5687 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 char_u *option_end;
5690 long numval;
5691 char_u *stringval;
5692 int opt_type;
5693 int c;
5694 int working = (**arg == '+'); /* has("+option") */
5695 int ret = OK;
5696 int opt_flags;
5697
5698 /*
5699 * Isolate the option name and find its value.
5700 */
5701 option_end = find_option_end(arg, &opt_flags);
5702 if (option_end == NULL)
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 EMSG2(_("E112: Option name missing: %s"), *arg);
5706 return FAIL;
5707 }
5708
5709 if (!evaluate)
5710 {
5711 *arg = option_end;
5712 return OK;
5713 }
5714
5715 c = *option_end;
5716 *option_end = NUL;
5717 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (opt_type == -3) /* invalid name */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 EMSG2(_("E113: Unknown option: %s"), *arg);
5724 ret = FAIL;
5725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (opt_type == -2) /* hidden string option */
5729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730 rettv->v_type = VAR_STRING;
5731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else if (opt_type == -1) /* hidden number option */
5734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 rettv->v_type = VAR_NUMBER;
5736 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else if (opt_type == 1) /* number option */
5739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 rettv->v_type = VAR_NUMBER;
5741 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 else /* string option */
5744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 }
5749 else if (working && (opt_type == -2 || opt_type == -1))
5750 ret = FAIL;
5751
5752 *option_end = c; /* put back for error messages */
5753 *arg = option_end;
5754
5755 return ret;
5756}
5757
5758/*
5759 * Allocate a variable for a string constant.
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 char_u *p;
5766 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int extra = 0;
5768
5769 /*
5770 * Find the end of the string, skipping backslashed characters.
5771 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (*p == '\\' && p[1] != NUL)
5775 {
5776 ++p;
5777 /* A "\<x>" form occupies at least 4 characters, and produces up
5778 * to 6 characters: reserve space for 2 extra */
5779 if (*p == '<')
5780 extra += 2;
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783
5784 if (*p != '"')
5785 {
5786 EMSG2(_("E114: Missing quote: %s"), *arg);
5787 return FAIL;
5788 }
5789
5790 /* If only parsing, set *arg and return here */
5791 if (!evaluate)
5792 {
5793 *arg = p + 1;
5794 return OK;
5795 }
5796
5797 /*
5798 * Copy the string into allocated memory, handling backslashed
5799 * characters.
5800 */
5801 name = alloc((unsigned)(p - *arg + extra));
5802 if (name == NULL)
5803 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 rettv->v_type = VAR_STRING;
5805 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (*p == '\\')
5810 {
5811 switch (*++p)
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case 'b': *name++ = BS; ++p; break;
5814 case 'e': *name++ = ESC; ++p; break;
5815 case 'f': *name++ = FF; ++p; break;
5816 case 'n': *name++ = NL; ++p; break;
5817 case 'r': *name++ = CAR; ++p; break;
5818 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 case 'X': /* hex: "\x1", "\x12" */
5821 case 'x':
5822 case 'u': /* Unicode: "\u0023" */
5823 case 'U':
5824 if (vim_isxdigit(p[1]))
5825 {
5826 int n, nr;
5827 int c = toupper(*p);
5828
5829 if (c == 'X')
5830 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005831 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005833 else
5834 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 nr = 0;
5836 while (--n >= 0 && vim_isxdigit(p[1]))
5837 {
5838 ++p;
5839 nr = (nr << 4) + hex2nr(*p);
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef FEAT_MBYTE
5843 /* For "\u" store the number according to
5844 * 'encoding'. */
5845 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852
5853 /* octal: "\1", "\12", "\123" */
5854 case '0':
5855 case '1':
5856 case '2':
5857 case '3':
5858 case '4':
5859 case '5':
5860 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 case '7': *name = *p++ - '0';
5862 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *name = (*name << 3) + *p++ - '0';
5865 if (*p >= '0' && *p <= '7')
5866 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 break;
5870
5871 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (extra != 0)
5874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 }
5878 /* FALLTHROUGH */
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 }
5883 }
5884 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 *arg = p + 1;
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
5894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Return OK or FAIL.
5897 */
5898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005899get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 int reduce = 0;
5904
5905 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 */
5908 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 break;
5914 ++reduce;
5915 ++p;
5916 }
5917 }
5918
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 return FAIL;
5923 }
5924
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 if (!evaluate)
5927 {
5928 *arg = p + 1;
5929 return OK;
5930 }
5931
5932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 */
5935 str = alloc((unsigned)((p - *arg) - reduce));
5936 if (str == NULL)
5937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 rettv->v_type = VAR_STRING;
5939 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 break;
5947 ++p;
5948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 *arg = p + 1;
5953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 return OK;
5955}
5956
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005959{
5960 int i;
5961
5962 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005964 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966 func_unref(pt->pt_name);
5967 vim_free(pt->pt_name);
5968 vim_free(pt);
5969}
5970
5971/*
5972 * Unreference a closure: decrement the reference count and free it when it
5973 * becomes zero.
5974 */
5975 void
5976partial_unref(partial_T *pt)
5977{
5978 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980}
5981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 * Allocate a variable for a List and fill it from "*arg".
5984 * Return OK or FAIL.
5985 */
5986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l = NULL;
5990 typval_T tv;
5991 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992
5993 if (evaluate)
5994 {
5995 l = list_alloc();
5996 if (l == NULL)
5997 return FAIL;
5998 }
5999
6000 *arg = skipwhite(*arg + 1);
6001 while (**arg != ']' && **arg != NUL)
6002 {
6003 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6004 goto failret;
6005 if (evaluate)
6006 {
6007 item = listitem_alloc();
6008 if (item != NULL)
6009 {
6010 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006011 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 list_append(l, item);
6013 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 else
6015 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
6017
6018 if (**arg == ']')
6019 break;
6020 if (**arg != ',')
6021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 goto failret;
6024 }
6025 *arg = skipwhite(*arg + 1);
6026 }
6027
6028 if (**arg != ']')
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031failret:
6032 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006033 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 return FAIL;
6035 }
6036
6037 *arg = skipwhite(*arg + 1);
6038 if (evaluate)
6039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 rettv->v_type = VAR_LIST;
6041 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 ++l->lv_refcount;
6043 }
6044
6045 return OK;
6046}
6047
6048/*
6049 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006050 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 list_T *l;
6056
6057 l = (list_T *)alloc_clear(sizeof(list_T));
6058 if (l != NULL)
6059 {
6060 /* Prepend the list to the list of lists for garbage collection. */
6061 if (first_list != NULL)
6062 first_list->lv_used_prev = l;
6063 l->lv_used_prev = NULL;
6064 l->lv_used_next = first_list;
6065 first_list = l;
6066 }
6067 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068}
6069
6070/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006071 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006072 * Returns OK or FAIL.
6073 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076{
6077 list_T *l = list_alloc();
6078
6079 if (l == NULL)
6080 return FAIL;
6081
6082 rettv->vval.v_list = l;
6083 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006084 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006085 ++l->lv_refcount;
6086 return OK;
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Unreference a list: decrement the reference count and free it when it
6091 * becomes zero.
6092 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006096 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006101 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 * Ignores the reference count.
6103 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 static void
6105list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006109 for (item = l->lv_first; item != NULL; item = l->lv_first)
6110 {
6111 /* Remove the item before deleting it. */
6112 l->lv_first = item->li_next;
6113 clear_tv(&item->li_tv);
6114 vim_free(item);
6115 }
6116}
6117
6118 static void
6119list_free_list(list_T *l)
6120{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Remove the list from the list of lists for garbage collection. */
6122 if (l->lv_used_prev == NULL)
6123 first_list = l->lv_used_next;
6124 else
6125 l->lv_used_prev->lv_used_next = l->lv_used_next;
6126 if (l->lv_used_next != NULL)
6127 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 vim_free(l);
6130}
6131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 void
6133list_free(list_T *l)
6134{
6135 if (!in_free_unref_items)
6136 {
6137 list_free_contents(l);
6138 list_free_list(l);
6139 }
6140}
6141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142/*
6143 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006144 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006146 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006147listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150}
6151
6152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006153 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 vim_free(item);
6160}
6161
6162/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006163 * Remove a list item from a List and free it. Also clears the value.
6164 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006167{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006168 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006169 listitem_free(item);
6170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Get the number of items in a list.
6174 */
6175 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 if (l == NULL)
6179 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181}
6182
6183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * Return TRUE when two lists have exactly the same values.
6185 */
6186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187list_equal(
6188 list_T *l1,
6189 list_T *l2,
6190 int ic, /* ignore case for strings */
6191 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006195 if (l1 == NULL || l2 == NULL)
6196 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 if (l1 == l2)
6198 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006199 if (list_len(l1) != list_len(l2))
6200 return FALSE;
6201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 for (item1 = l1->lv_first, item2 = l2->lv_first;
6203 item1 != NULL && item2 != NULL;
6204 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 return FALSE;
6207 return item1 == NULL && item2 == NULL;
6208}
6209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210/*
6211 * Return the dictitem that an entry in a hashtable points to.
6212 */
6213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215{
6216 return HI2DI(hi);
6217}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 * Return TRUE when two dictionaries have exactly the same key/values.
6221 */
6222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223dict_equal(
6224 dict_T *d1,
6225 dict_T *d2,
6226 int ic, /* ignore case for strings */
6227 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228{
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hashitem_T *hi;
6230 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006232
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006233 if (d1 == NULL && d2 == NULL)
6234 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006235 if (d1 == NULL || d2 == NULL)
6236 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006237 if (d1 == d2)
6238 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006239 if (dict_len(d1) != dict_len(d2))
6240 return FALSE;
6241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006242 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006243 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006244 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006245 if (!HASHITEM_EMPTY(hi))
6246 {
6247 item2 = dict_find(d2, hi->hi_key, -1);
6248 if (item2 == NULL)
6249 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006250 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 return FALSE;
6252 --todo;
6253 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006254 }
6255 return TRUE;
6256}
6257
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006258static int tv_equal_recurse_limit;
6259
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006260/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006262 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006263 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006264 */
6265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266tv_equal(
6267 typval_T *tv1,
6268 typval_T *tv2,
6269 int ic, /* ignore case */
6270 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006271{
6272 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006273 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006274 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006275 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006276
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006277 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6278 if ((tv1->v_type == VAR_FUNC
6279 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6280 && (tv2->v_type == VAR_FUNC
6281 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6282 {
6283 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6284 : tv1->vval.v_partial->pt_name;
6285 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6286 : tv2->vval.v_partial->pt_name;
6287 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6288 }
6289
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006290 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006291 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006293 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 * recursiveness to a limit. We guess they are equal then.
6295 * A fixed limit has the problem of still taking an awful long time.
6296 * Reduce the limit every time running into it. That should work fine for
6297 * deeply linked structures that are not recursively linked and catch
6298 * recursiveness quickly. */
6299 if (!recursive)
6300 tv_equal_recurse_limit = 1000;
6301 if (recursive_cnt >= tv_equal_recurse_limit)
6302 {
6303 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006304 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006306
6307 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006308 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006309 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006310 ++recursive_cnt;
6311 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6312 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006313 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006314
6315 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006316 ++recursive_cnt;
6317 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6318 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006319 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006320
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006321 case VAR_NUMBER:
6322 return tv1->vval.v_number == tv2->vval.v_number;
6323
6324 case VAR_STRING:
6325 s1 = get_tv_string_buf(tv1, buf1);
6326 s2 = get_tv_string_buf(tv2, buf2);
6327 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006328
6329 case VAR_SPECIAL:
6330 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006331
6332 case VAR_FLOAT:
6333#ifdef FEAT_FLOAT
6334 return tv1->vval.v_float == tv2->vval.v_float;
6335#endif
6336 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006337#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006338 return tv1->vval.v_job == tv2->vval.v_job;
6339#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006340 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006342 return tv1->vval.v_channel == tv2->vval.v_channel;
6343#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006344 case VAR_FUNC:
6345 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006346 case VAR_UNKNOWN:
6347 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006349
Bram Moolenaara03f2332016-02-06 18:09:59 +01006350 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6351 * does not equal anything, not even itself. */
6352 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353}
6354
6355/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006356 * Locate item with index "n" in list "l" and return it.
6357 * A negative index is counted from the end; -1 is the last item.
6358 * Returns NULL when "n" is out of range.
6359 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006360 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362{
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 long idx;
6365
6366 if (l == NULL)
6367 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006368
6369 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006370 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006371 n = l->lv_len + n;
6372
6373 /* Check for index out of range. */
6374 if (n < 0 || n >= l->lv_len)
6375 return NULL;
6376
6377 /* When there is a cached index may start search from there. */
6378 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006380 if (n < l->lv_idx / 2)
6381 {
6382 /* closest to the start of the list */
6383 item = l->lv_first;
6384 idx = 0;
6385 }
6386 else if (n > (l->lv_idx + l->lv_len) / 2)
6387 {
6388 /* closest to the end of the list */
6389 item = l->lv_last;
6390 idx = l->lv_len - 1;
6391 }
6392 else
6393 {
6394 /* closest to the cached index */
6395 item = l->lv_idx_item;
6396 idx = l->lv_idx;
6397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398 }
6399 else
6400 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006401 if (n < l->lv_len / 2)
6402 {
6403 /* closest to the start of the list */
6404 item = l->lv_first;
6405 idx = 0;
6406 }
6407 else
6408 {
6409 /* closest to the end of the list */
6410 item = l->lv_last;
6411 idx = l->lv_len - 1;
6412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006414
6415 while (n > idx)
6416 {
6417 /* search forward */
6418 item = item->li_next;
6419 ++idx;
6420 }
6421 while (n < idx)
6422 {
6423 /* search backward */
6424 item = item->li_prev;
6425 --idx;
6426 }
6427
6428 /* cache the used index */
6429 l->lv_idx = idx;
6430 l->lv_idx_item = item;
6431
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 return item;
6433}
6434
6435/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006436 * Get list item "l[idx]" as a number.
6437 */
6438 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006439list_find_nr(
6440 list_T *l,
6441 long idx,
6442 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006443{
6444 listitem_T *li;
6445
6446 li = list_find(l, idx);
6447 if (li == NULL)
6448 {
6449 if (errorp != NULL)
6450 *errorp = TRUE;
6451 return -1L;
6452 }
6453 return get_tv_number_chk(&li->li_tv, errorp);
6454}
6455
6456/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006457 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6458 */
6459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006461{
6462 listitem_T *li;
6463
6464 li = list_find(l, idx - 1);
6465 if (li == NULL)
6466 {
6467 EMSGN(_(e_listidx), idx);
6468 return NULL;
6469 }
6470 return get_tv_string(&li->li_tv);
6471}
6472
6473/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006474 * Locate "item" list "l" and return its index.
6475 * Returns -1 when "item" is not in the list.
6476 */
6477 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006479{
6480 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006481 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006482
6483 if (l == NULL)
6484 return -1;
6485 idx = 0;
6486 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6487 ++idx;
6488 if (li == NULL)
6489 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006490 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006491}
6492
6493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006494 * Append item "item" to the end of list "l".
6495 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006497list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498{
6499 if (l->lv_last == NULL)
6500 {
6501 /* empty list */
6502 l->lv_first = item;
6503 l->lv_last = item;
6504 item->li_prev = NULL;
6505 }
6506 else
6507 {
6508 l->lv_last->li_next = item;
6509 item->li_prev = l->lv_last;
6510 l->lv_last = item;
6511 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006512 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 item->li_next = NULL;
6514}
6515
6516/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
Bram Moolenaar05159a02005-02-26 23:04:13 +00006525 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006527 copy_tv(tv, &li->li_tv);
6528 list_append(l, li);
6529 return OK;
6530}
6531
6532/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006533 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006534 * Return FAIL when out of memory.
6535 */
6536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006537list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006538{
6539 listitem_T *li = listitem_alloc();
6540
6541 if (li == NULL)
6542 return FAIL;
6543 li->li_tv.v_type = VAR_DICT;
6544 li->li_tv.v_lock = 0;
6545 li->li_tv.vval.v_dict = dict;
6546 list_append(list, li);
6547 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006548 return OK;
6549}
6550
6551/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006552 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006553 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006554 * Returns FAIL when out of memory.
6555 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006558{
6559 listitem_T *li = listitem_alloc();
6560
6561 if (li == NULL)
6562 return FAIL;
6563 list_append(l, li);
6564 li->li_tv.v_type = VAR_STRING;
6565 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006566 if (str == NULL)
6567 li->li_tv.vval.v_string = NULL;
6568 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006569 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006570 return FAIL;
6571 return OK;
6572}
6573
6574/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006575 * Append "n" to list "l".
6576 * Returns FAIL when out of memory.
6577 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006580{
6581 listitem_T *li;
6582
6583 li = listitem_alloc();
6584 if (li == NULL)
6585 return FAIL;
6586 li->li_tv.v_type = VAR_NUMBER;
6587 li->li_tv.v_lock = 0;
6588 li->li_tv.vval.v_number = n;
6589 list_append(l, li);
6590 return OK;
6591}
6592
6593/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595 * If "item" is NULL append at the end.
6596 * Return FAIL when out of memory.
6597 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006599list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600{
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602
6603 if (ni == NULL)
6604 return FAIL;
6605 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006606 list_insert(l, ni, item);
6607 return OK;
6608}
6609
6610 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006611list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006612{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 if (item == NULL)
6614 /* Append new item at end of list. */
6615 list_append(l, ni);
6616 else
6617 {
6618 /* Insert new item before existing item. */
6619 ni->li_prev = item->li_prev;
6620 ni->li_next = item;
6621 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006622 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006624 ++l->lv_idx;
6625 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006627 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006629 l->lv_idx_item = NULL;
6630 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006632 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634}
6635
6636/*
6637 * Extend "l1" with "l2".
6638 * If "bef" is NULL append at the end, otherwise insert before this item.
6639 * Returns FAIL when out of memory.
6640 */
6641 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006642list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643{
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006645 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006647 /* We also quit the loop when we have inserted the original item count of
6648 * the list, avoid a hang when we extend a list with itself. */
6649 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6651 return FAIL;
6652 return OK;
6653}
6654
6655/*
6656 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6657 * Return FAIL when out of memory.
6658 */
6659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006660list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661{
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006664 if (l1 == NULL || l2 == NULL)
6665 return FAIL;
6666
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006668 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 if (l == NULL)
6670 return FAIL;
6671 tv->v_type = VAR_LIST;
6672 tv->vval.v_list = l;
6673
6674 /* append all items from the second list */
6675 return list_extend(l, l2, NULL);
6676}
6677
6678/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006679 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 * Returns NULL when out of memory.
6683 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006684 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006686{
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *copy;
6688 listitem_T *item;
6689 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690
6691 if (orig == NULL)
6692 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693
6694 copy = list_alloc();
6695 if (copy != NULL)
6696 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006697 if (copyID != 0)
6698 {
6699 /* Do this before adding the items, because one of the items may
6700 * refer back to this list. */
6701 orig->lv_copyID = copyID;
6702 orig->lv_copylist = copy;
6703 }
6704 for (item = orig->lv_first; item != NULL && !got_int;
6705 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 {
6707 ni = listitem_alloc();
6708 if (ni == NULL)
6709 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006710 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 {
6712 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6713 {
6714 vim_free(ni);
6715 break;
6716 }
6717 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006719 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006720 list_append(copy, ni);
6721 }
6722 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006723 if (item != NULL)
6724 {
6725 list_unref(copy);
6726 copy = NULL;
6727 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 }
6729
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 return copy;
6731}
6732
6733/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006734 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006735 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006736 * This used to be called list_remove, but that conflicts with a Sun header
6737 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006738 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006740vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741{
Bram Moolenaar33570922005-01-25 22:26:29 +00006742 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006743
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006744 /* notify watchers */
6745 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006747 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748 list_fix_watch(l, ip);
6749 if (ip == item2)
6750 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006752
6753 if (item2->li_next == NULL)
6754 l->lv_last = item->li_prev;
6755 else
6756 item2->li_next->li_prev = item->li_prev;
6757 if (item->li_prev == NULL)
6758 l->lv_first = item2->li_next;
6759 else
6760 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006761 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762}
6763
6764/*
6765 * Return an allocated string with the string representation of a list.
6766 * May return NULL.
6767 */
6768 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006769list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770{
6771 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006772
6773 if (tv->vval.v_list == NULL)
6774 return NULL;
6775 ga_init2(&ga, (int)sizeof(char), 80);
6776 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006777 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006778 {
6779 vim_free(ga.ga_data);
6780 return NULL;
6781 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006782 ga_append(&ga, ']');
6783 ga_append(&ga, NUL);
6784 return (char_u *)ga.ga_data;
6785}
6786
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006787typedef struct join_S {
6788 char_u *s;
6789 char_u *tofree;
6790} join_T;
6791
6792 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006793list_join_inner(
6794 garray_T *gap, /* to store the result in */
6795 list_T *l,
6796 char_u *sep,
6797 int echo_style,
6798 int copyID,
6799 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006800{
6801 int i;
6802 join_T *p;
6803 int len;
6804 int sumlen = 0;
6805 int first = TRUE;
6806 char_u *tofree;
6807 char_u numbuf[NUMBUFLEN];
6808 listitem_T *item;
6809 char_u *s;
6810
6811 /* Stringify each item in the list. */
6812 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6813 {
6814 if (echo_style)
6815 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6816 else
6817 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6818 if (s == NULL)
6819 return FAIL;
6820
6821 len = (int)STRLEN(s);
6822 sumlen += len;
6823
Bram Moolenaarcde88542015-08-11 19:14:00 +02006824 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006825 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6826 if (tofree != NULL || s != numbuf)
6827 {
6828 p->s = s;
6829 p->tofree = tofree;
6830 }
6831 else
6832 {
6833 p->s = vim_strnsave(s, len);
6834 p->tofree = p->s;
6835 }
6836
6837 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006838 if (did_echo_string_emsg) /* recursion error, bail out */
6839 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006840 }
6841
6842 /* Allocate result buffer with its total size, avoid re-allocation and
6843 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6844 if (join_gap->ga_len >= 2)
6845 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6846 if (ga_grow(gap, sumlen + 2) == FAIL)
6847 return FAIL;
6848
6849 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6850 {
6851 if (first)
6852 first = FALSE;
6853 else
6854 ga_concat(gap, sep);
6855 p = ((join_T *)join_gap->ga_data) + i;
6856
6857 if (p->s != NULL)
6858 ga_concat(gap, p->s);
6859 line_breakcheck();
6860 }
6861
6862 return OK;
6863}
6864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006867 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006868 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006869 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006871list_join(
6872 garray_T *gap,
6873 list_T *l,
6874 char_u *sep,
6875 int echo_style,
6876 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006877{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006878 garray_T join_ga;
6879 int retval;
6880 join_T *p;
6881 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882
Bram Moolenaard39a7512015-04-16 22:51:22 +02006883 if (l->lv_len < 1)
6884 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006885 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6886 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6887
6888 /* Dispose each item in join_ga. */
6889 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006891 p = (join_T *)join_ga.ga_data;
6892 for (i = 0; i < join_ga.ga_len; ++i)
6893 {
6894 vim_free(p->tofree);
6895 ++p;
6896 }
6897 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006898 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006899
6900 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901}
6902
6903/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006904 * Return the next (unique) copy ID.
6905 * Used for serializing nested structures.
6906 */
6907 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006908get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006909{
6910 current_copyID += COPYID_INC;
6911 return current_copyID;
6912}
6913
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006914/* Used by get_func_tv() */
6915static garray_T funcargs = GA_EMPTY;
6916
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006917/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 * Garbage collection for lists and dictionaries.
6919 *
6920 * We use reference counts to be able to free most items right away when they
6921 * are no longer used. But for composite items it's possible that it becomes
6922 * unused while the reference count is > 0: When there is a recursive
6923 * reference. Example:
6924 * :let l = [1, 2, 3]
6925 * :let d = {9: l}
6926 * :let l[1] = d
6927 *
6928 * Since this is quite unusual we handle this with garbage collection: every
6929 * once in a while find out which lists and dicts are not referenced from any
6930 * variable.
6931 *
6932 * Here is a good reference text about garbage collection (refers to Python
6933 * but it applies to all reference-counting mechanisms):
6934 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006935 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006936
6937/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006939 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006941 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006942 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006943garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006944{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947 buf_T *buf;
6948 win_T *wp;
6949 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006950 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006951 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006953#ifdef FEAT_WINDOWS
6954 tabpage_T *tp;
6955#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006957 if (!testing)
6958 {
6959 /* Only do this once. */
6960 want_garbage_collect = FALSE;
6961 may_garbage_collect = FALSE;
6962 garbage_collect_at_exit = FALSE;
6963 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006964
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 /* We advance by two because we add one for items referenced through
6966 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006967 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 /*
6970 * 1. Go through all accessible variables and mark all lists and dicts
6971 * with copyID.
6972 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973
6974 /* Don't free variables in the previous_funccal list unless they are only
6975 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006976 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6978 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6980 NULL);
6981 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6982 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 }
6984
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 /* script-local variables */
6986 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006987 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988
6989 /* buffer-local variables */
6990 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006991 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6992 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
6994 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006995 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6997 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006998#ifdef FEAT_AUTOCMD
6999 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7001 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007002#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007004#ifdef FEAT_WINDOWS
7005 /* tabpage-local variables */
7006 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007007 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7008 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007009#endif
7010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007012 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013
7014 /* function-local variables */
7015 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7016 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7018 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 }
7020
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007021 /* function call arguments, if v:testing is set. */
7022 for (i = 0; i < funcargs.ga_len; ++i)
7023 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7024 copyID, NULL, NULL);
7025
Bram Moolenaard812df62008-11-09 12:46:09 +00007026 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007028
Bram Moolenaar1dced572012-04-05 16:54:08 +02007029#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007031#endif
7032
Bram Moolenaardb913952012-06-29 12:54:53 +02007033#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007035#endif
7036
7037#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007039#endif
7040
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007041#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007042 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007043 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007044#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007045#ifdef FEAT_NETBEANS_INTG
7046 abort = abort || set_ref_in_nb_channel(copyID);
7047#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007048
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007050 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 /*
7052 * 2. Free lists and dictionaries that are not referenced.
7053 */
7054 did_free = free_unref_items(copyID);
7055
7056 /*
7057 * 3. Check if any funccal can be freed now.
7058 */
7059 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007060 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 if (can_free_funccal(*pfc, copyID))
7062 {
7063 fc = *pfc;
7064 *pfc = fc->caller;
7065 free_funccal(fc, TRUE);
7066 did_free = TRUE;
7067 did_free_funccal = TRUE;
7068 }
7069 else
7070 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007071 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 if (did_free_funccal)
7073 /* When a funccal was freed some more items might be garbage
7074 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007075 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007076 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 else if (p_verbose > 0)
7078 {
7079 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7080 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007081
7082 return did_free;
7083}
7084
7085/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007086 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007087 */
7088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007089free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007090{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007091 dict_T *dd, *dd_next;
7092 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007093 int did_free = FALSE;
7094
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007095 /* Let all "free" functions know that we are here. This means no
7096 * dictionaries, lists, channels or jobs are to be freed, because we will
7097 * do that here. */
7098 in_free_unref_items = TRUE;
7099
7100 /*
7101 * PASS 1: free the contents of the items. We don't free the items
7102 * themselves yet, so that it is possible to decrement refcount counters
7103 */
7104
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007106 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007108 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007109 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007111 /* Free the Dictionary and ordinary items it contains, but don't
7112 * recurse into Lists and Dictionaries, they will be in the list
7113 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007114 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117
7118 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007119 * Go through the list of lists and free items without the copyID.
7120 * But don't free a list that has a watcher (used in a for loop), these
7121 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007123 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7124 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7125 && ll->lv_watch == NULL)
7126 {
7127 /* Free the List and ordinary items it contains, but don't recurse
7128 * into Lists and Dictionaries, they will be in the list of dicts
7129 * or list of lists. */
7130 list_free_contents(ll);
7131 did_free = TRUE;
7132 }
7133
7134#ifdef FEAT_JOB_CHANNEL
7135 /* Go through the list of jobs and free items without the copyID. This
7136 * must happen before doing channels, because jobs refer to channels, but
7137 * the reference from the channel to the job isn't tracked. */
7138 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7139
7140 /* Go through the list of channels and free items without the copyID. */
7141 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7142#endif
7143
7144 /*
7145 * PASS 2: free the items themselves.
7146 */
7147 for (dd = first_dict; dd != NULL; dd = dd_next)
7148 {
7149 dd_next = dd->dv_used_next;
7150 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7151 dict_free_dict(dd);
7152 }
7153
7154 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007155 {
7156 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007157 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7158 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007159 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007160 /* Free the List and ordinary items it contains, but don't recurse
7161 * into Lists and Dictionaries, they will be in the list of dicts
7162 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007163 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007165 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007166
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007167#ifdef FEAT_JOB_CHANNEL
7168 /* Go through the list of jobs and free items without the copyID. This
7169 * must happen before doing channels, because jobs refer to channels, but
7170 * the reference from the channel to the job isn't tracked. */
7171 free_unused_jobs(copyID, COPYID_MASK);
7172
7173 /* Go through the list of channels and free items without the copyID. */
7174 free_unused_channels(copyID, COPYID_MASK);
7175#endif
7176
7177 in_free_unref_items = FALSE;
7178
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179 return did_free;
7180}
7181
7182/*
7183 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007184 * "list_stack" is used to add lists to be marked. Can be NULL.
7185 *
7186 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007189set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190{
7191 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007194 hashtab_T *cur_ht;
7195 ht_stack_T *ht_stack = NULL;
7196 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198 cur_ht = ht;
7199 for (;;)
7200 {
7201 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007202 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007203 /* Mark each item in the hashtab. If the item contains a hashtab
7204 * it is added to ht_stack, if it contains a list it is added to
7205 * list_stack. */
7206 todo = (int)cur_ht->ht_used;
7207 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7208 if (!HASHITEM_EMPTY(hi))
7209 {
7210 --todo;
7211 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7212 &ht_stack, list_stack);
7213 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007214 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215
7216 if (ht_stack == NULL)
7217 break;
7218
7219 /* take an item from the stack */
7220 cur_ht = ht_stack->ht;
7221 tempitem = ht_stack;
7222 ht_stack = ht_stack->prev;
7223 free(tempitem);
7224 }
7225
7226 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227}
7228
7229/*
7230 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007231 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7232 *
7233 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007234 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007236set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007237{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007238 listitem_T *li;
7239 int abort = FALSE;
7240 list_T *cur_l;
7241 list_stack_T *list_stack = NULL;
7242 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007244 cur_l = l;
7245 for (;;)
7246 {
7247 if (!abort)
7248 /* Mark each item in the list. If the item contains a hashtab
7249 * it is added to ht_stack, if it contains a list it is added to
7250 * list_stack. */
7251 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7252 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7253 ht_stack, &list_stack);
7254 if (list_stack == NULL)
7255 break;
7256
7257 /* take an item from the stack */
7258 cur_l = list_stack->list;
7259 tempitem = list_stack;
7260 list_stack = list_stack->prev;
7261 free(tempitem);
7262 }
7263
7264 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265}
7266
7267/*
7268 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007269 * "list_stack" is used to add lists to be marked. Can be NULL.
7270 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7271 *
7272 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007273 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007274 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007275set_ref_in_item(
7276 typval_T *tv,
7277 int copyID,
7278 ht_stack_T **ht_stack,
7279 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007281 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007283 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007284 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007285 dict_T *dd = tv->vval.v_dict;
7286
Bram Moolenaara03f2332016-02-06 18:09:59 +01007287 if (dd != NULL && dd->dv_copyID != copyID)
7288 {
7289 /* Didn't see this dict yet. */
7290 dd->dv_copyID = copyID;
7291 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007292 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007293 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7294 }
7295 else
7296 {
7297 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7298 if (newitem == NULL)
7299 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007300 else
7301 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007302 newitem->ht = &dd->dv_hashtab;
7303 newitem->prev = *ht_stack;
7304 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007305 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007306 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007307 }
7308 }
7309 else if (tv->v_type == VAR_LIST)
7310 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007311 list_T *ll = tv->vval.v_list;
7312
Bram Moolenaara03f2332016-02-06 18:09:59 +01007313 if (ll != NULL && ll->lv_copyID != copyID)
7314 {
7315 /* Didn't see this list yet. */
7316 ll->lv_copyID = copyID;
7317 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007318 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007319 abort = set_ref_in_list(ll, copyID, ht_stack);
7320 }
7321 else
7322 {
7323 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007324 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007325 if (newitem == NULL)
7326 abort = TRUE;
7327 else
7328 {
7329 newitem->list = ll;
7330 newitem->prev = *list_stack;
7331 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007332 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007333 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007334 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007335 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007336 else if (tv->v_type == VAR_PARTIAL)
7337 {
7338 partial_T *pt = tv->vval.v_partial;
7339 int i;
7340
7341 /* A partial does not have a copyID, because it cannot contain itself.
7342 */
7343 if (pt != NULL)
7344 {
7345 if (pt->pt_dict != NULL)
7346 {
7347 typval_T dtv;
7348
7349 dtv.v_type = VAR_DICT;
7350 dtv.vval.v_dict = pt->pt_dict;
7351 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7352 }
7353
7354 for (i = 0; i < pt->pt_argc; ++i)
7355 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7356 ht_stack, list_stack);
7357 }
7358 }
7359#ifdef FEAT_JOB_CHANNEL
7360 else if (tv->v_type == VAR_JOB)
7361 {
7362 job_T *job = tv->vval.v_job;
7363 typval_T dtv;
7364
7365 if (job != NULL && job->jv_copyID != copyID)
7366 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007367 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007368 if (job->jv_channel != NULL)
7369 {
7370 dtv.v_type = VAR_CHANNEL;
7371 dtv.vval.v_channel = job->jv_channel;
7372 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7373 }
7374 if (job->jv_exit_partial != NULL)
7375 {
7376 dtv.v_type = VAR_PARTIAL;
7377 dtv.vval.v_partial = job->jv_exit_partial;
7378 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7379 }
7380 }
7381 }
7382 else if (tv->v_type == VAR_CHANNEL)
7383 {
7384 channel_T *ch =tv->vval.v_channel;
7385 int part;
7386 typval_T dtv;
7387 jsonq_T *jq;
7388 cbq_T *cq;
7389
7390 if (ch != NULL && ch->ch_copyID != copyID)
7391 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007392 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007393 for (part = PART_SOCK; part <= PART_IN; ++part)
7394 {
7395 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7396 jq = jq->jq_next)
7397 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7398 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7399 cq = cq->cq_next)
7400 if (cq->cq_partial != NULL)
7401 {
7402 dtv.v_type = VAR_PARTIAL;
7403 dtv.vval.v_partial = cq->cq_partial;
7404 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7405 }
7406 if (ch->ch_part[part].ch_partial != NULL)
7407 {
7408 dtv.v_type = VAR_PARTIAL;
7409 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7410 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7411 }
7412 }
7413 if (ch->ch_partial != NULL)
7414 {
7415 dtv.v_type = VAR_PARTIAL;
7416 dtv.vval.v_partial = ch->ch_partial;
7417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7418 }
7419 if (ch->ch_close_partial != NULL)
7420 {
7421 dtv.v_type = VAR_PARTIAL;
7422 dtv.vval.v_partial = ch->ch_close_partial;
7423 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7424 }
7425 }
7426 }
7427#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007428 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007429}
7430
7431/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 * Allocate an empty header for a dictionary.
7433 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007434 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007435dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436{
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438
Bram Moolenaar33570922005-01-25 22:26:29 +00007439 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007440 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007441 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007442 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007443 if (first_dict != NULL)
7444 first_dict->dv_used_prev = d;
7445 d->dv_used_next = first_dict;
7446 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007447 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007448
Bram Moolenaar33570922005-01-25 22:26:29 +00007449 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007450 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007451 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007452 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007454 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007455 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007456}
7457
7458/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007459 * Allocate an empty dict for a return value.
7460 * Returns OK or FAIL.
7461 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007464{
7465 dict_T *d = dict_alloc();
7466
7467 if (d == NULL)
7468 return FAIL;
7469
7470 rettv->vval.v_dict = d;
7471 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007472 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007473 ++d->dv_refcount;
7474 return OK;
7475}
7476
7477
7478/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 * Unreference a Dictionary: decrement the reference count and free it when it
7480 * becomes zero.
7481 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007483dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007485 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007486 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487}
7488
7489/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007490 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 * Ignores the reference count.
7492 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007493 static void
7494dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007498 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007500 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007501 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007502 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 if (!HASHITEM_EMPTY(hi))
7506 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007507 /* Remove the item before deleting it, just in case there is
7508 * something recursive causing trouble. */
7509 di = HI2DI(hi);
7510 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007511 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007512 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 --todo;
7514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007517}
7518
7519 static void
7520dict_free_dict(dict_T *d)
7521{
7522 /* Remove the dict from the list of dicts for garbage collection. */
7523 if (d->dv_used_prev == NULL)
7524 first_dict = d->dv_used_next;
7525 else
7526 d->dv_used_prev->dv_used_next = d->dv_used_next;
7527 if (d->dv_used_next != NULL)
7528 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 vim_free(d);
7530}
7531
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007532 void
7533dict_free(dict_T *d)
7534{
7535 if (!in_free_unref_items)
7536 {
7537 dict_free_contents(d);
7538 dict_free_dict(d);
7539 }
7540}
7541
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542/*
7543 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 * The "key" is copied to the new item.
7545 * Note that the value of the item "di_tv" still needs to be initialized!
7546 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007548 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550{
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007553 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007557 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560}
7561
7562/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 * Make a copy of a Dictionary item.
7564 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007565 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007566dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567{
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007569
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007570 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7571 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007572 if (di != NULL)
7573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007575 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007576 copy_tv(&org->di_tv, &di->di_tv);
7577 }
7578 return di;
7579}
7580
7581/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 * Remove item "item" from Dictionary "dict" and free it.
7583 */
7584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007585dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586{
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 if (HASHITEM_EMPTY(hi))
7591 EMSG2(_(e_intern2), "dictitem_remove()");
7592 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 dictitem_free(item);
7595}
7596
7597/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 * Free a dict item. Also clears the value.
7599 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007601dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007604 if (item->di_flags & DI_FLAGS_ALLOC)
7605 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606}
7607
7608/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7610 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007612 * Returns NULL when out of memory.
7613 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007614 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007615dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616{
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 dict_T *copy;
7618 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007619 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621
7622 if (orig == NULL)
7623 return NULL;
7624
7625 copy = dict_alloc();
7626 if (copy != NULL)
7627 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007628 if (copyID != 0)
7629 {
7630 orig->dv_copyID = copyID;
7631 orig->dv_copydict = copy;
7632 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007633 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007634 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 --todo;
7639
7640 di = dictitem_alloc(hi->hi_key);
7641 if (di == NULL)
7642 break;
7643 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007644 {
7645 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7646 copyID) == FAIL)
7647 {
7648 vim_free(di);
7649 break;
7650 }
7651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 else
7653 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7654 if (dict_add(copy, di) == FAIL)
7655 {
7656 dictitem_free(di);
7657 break;
7658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007660 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661
Bram Moolenaare9a41262005-01-15 22:18:47 +00007662 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007663 if (todo > 0)
7664 {
7665 dict_unref(copy);
7666 copy = NULL;
7667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 }
7669
7670 return copy;
7671}
7672
7673/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007675 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679{
Bram Moolenaar33570922005-01-25 22:26:29 +00007680 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681}
7682
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007684 * Add a number or string entry to dictionary "d".
7685 * When "str" is NULL use number "nr", otherwise use "str".
7686 * Returns FAIL when out of memory and when key already exists.
7687 */
7688 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007689dict_add_nr_str(
7690 dict_T *d,
7691 char *key,
7692 long nr,
7693 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007694{
7695 dictitem_T *item;
7696
7697 item = dictitem_alloc((char_u *)key);
7698 if (item == NULL)
7699 return FAIL;
7700 item->di_tv.v_lock = 0;
7701 if (str == NULL)
7702 {
7703 item->di_tv.v_type = VAR_NUMBER;
7704 item->di_tv.vval.v_number = nr;
7705 }
7706 else
7707 {
7708 item->di_tv.v_type = VAR_STRING;
7709 item->di_tv.vval.v_string = vim_strsave(str);
7710 }
7711 if (dict_add(d, item) == FAIL)
7712 {
7713 dictitem_free(item);
7714 return FAIL;
7715 }
7716 return OK;
7717}
7718
7719/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007720 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007721 * Returns FAIL when out of memory and when key already exists.
7722 */
7723 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007724dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007725{
7726 dictitem_T *item;
7727
7728 item = dictitem_alloc((char_u *)key);
7729 if (item == NULL)
7730 return FAIL;
7731 item->di_tv.v_lock = 0;
7732 item->di_tv.v_type = VAR_LIST;
7733 item->di_tv.vval.v_list = list;
7734 if (dict_add(d, item) == FAIL)
7735 {
7736 dictitem_free(item);
7737 return FAIL;
7738 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007739 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007740 return OK;
7741}
7742
7743/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007744 * Get the number of items in a Dictionary.
7745 */
7746 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007747dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007748{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007749 if (d == NULL)
7750 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007751 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007752}
7753
7754/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 * Find item "key[len]" in Dictionary "d".
7756 * If "len" is negative use strlen(key).
7757 * Returns NULL when not found.
7758 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007759 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007760dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762#define AKEYLEN 200
7763 char_u buf[AKEYLEN];
7764 char_u *akey;
7765 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007768 if (d == NULL)
7769 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 if (len < 0)
7771 akey = key;
7772 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 tofree = akey = vim_strnsave(key, len);
7775 if (akey == NULL)
7776 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007777 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 else
7779 {
7780 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007781 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 akey = buf;
7783 }
7784
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007786 vim_free(tofree);
7787 if (HASHITEM_EMPTY(hi))
7788 return NULL;
7789 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790}
7791
7792/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007793 * Get a string item from a dictionary.
7794 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007795 * Returns NULL if the entry doesn't exist or out of memory.
7796 */
7797 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007799{
7800 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007801 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007802
7803 di = dict_find(d, key, -1);
7804 if (di == NULL)
7805 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007806 s = get_tv_string(&di->di_tv);
7807 if (save && s != NULL)
7808 s = vim_strsave(s);
7809 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007810}
7811
7812/*
7813 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007814 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007815 */
7816 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007817get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007818{
7819 dictitem_T *di;
7820
7821 di = dict_find(d, key, -1);
7822 if (di == NULL)
7823 return 0;
7824 return get_tv_number(&di->di_tv);
7825}
7826
7827/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 * Return an allocated string with the string representation of a Dictionary.
7829 * May return NULL.
7830 */
7831 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007832dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007833{
7834 garray_T ga;
7835 int first = TRUE;
7836 char_u *tofree;
7837 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007838 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007840 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007841 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007842
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007843 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007844 return NULL;
7845 ga_init2(&ga, (int)sizeof(char), 80);
7846 ga_append(&ga, '{');
7847
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007848 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007849 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007851 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007853 --todo;
7854
7855 if (first)
7856 first = FALSE;
7857 else
7858 ga_concat(&ga, (char_u *)", ");
7859
7860 tofree = string_quote(hi->hi_key, FALSE);
7861 if (tofree != NULL)
7862 {
7863 ga_concat(&ga, tofree);
7864 vim_free(tofree);
7865 }
7866 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007868 if (s != NULL)
7869 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007870 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007871 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007872 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007873 line_breakcheck();
7874
Bram Moolenaar8c711452005-01-14 21:53:12 +00007875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007876 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007877 if (todo > 0)
7878 {
7879 vim_free(ga.ga_data);
7880 return NULL;
7881 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007882
7883 ga_append(&ga, '}');
7884 ga_append(&ga, NUL);
7885 return (char_u *)ga.ga_data;
7886}
7887
7888/*
7889 * Allocate a variable for a Dictionary and fill it from "*arg".
7890 * Return OK or FAIL. Returns NOTDONE for {expr}.
7891 */
7892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007893get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007894{
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 dict_T *d = NULL;
7896 typval_T tvkey;
7897 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007898 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007899 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007900 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007901 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007902
7903 /*
7904 * First check if it's not a curly-braces thing: {expr}.
7905 * Must do this without evaluating, otherwise a function may be called
7906 * twice. Unfortunately this means we need to call eval1() twice for the
7907 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007908 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007909 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 if (*start != '}')
7911 {
7912 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7913 return FAIL;
7914 if (*start == '}')
7915 return NOTDONE;
7916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917
7918 if (evaluate)
7919 {
7920 d = dict_alloc();
7921 if (d == NULL)
7922 return FAIL;
7923 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007924 tvkey.v_type = VAR_UNKNOWN;
7925 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926
7927 *arg = skipwhite(*arg + 1);
7928 while (**arg != '}' && **arg != NUL)
7929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007930 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 goto failret;
7932 if (**arg != ':')
7933 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007934 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007935 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007936 goto failret;
7937 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007938 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007940 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007941 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007942 {
7943 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007944 clear_tv(&tvkey);
7945 goto failret;
7946 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948
7949 *arg = skipwhite(*arg + 1);
7950 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7951 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007952 if (evaluate)
7953 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 goto failret;
7955 }
7956 if (evaluate)
7957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 if (item != NULL)
7960 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007961 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007963 clear_tv(&tv);
7964 goto failret;
7965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007966 item = dictitem_alloc(key);
7967 clear_tv(&tvkey);
7968 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007971 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007972 if (dict_add(d, item) == FAIL)
7973 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974 }
7975 }
7976
7977 if (**arg == '}')
7978 break;
7979 if (**arg != ',')
7980 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007981 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 goto failret;
7983 }
7984 *arg = skipwhite(*arg + 1);
7985 }
7986
7987 if (**arg != '}')
7988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007989 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007990failret:
7991 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007992 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007993 return FAIL;
7994 }
7995
7996 *arg = skipwhite(*arg + 1);
7997 if (evaluate)
7998 {
7999 rettv->v_type = VAR_DICT;
8000 rettv->vval.v_dict = d;
8001 ++d->dv_refcount;
8002 }
8003
8004 return OK;
8005}
8006
Bram Moolenaar17a13432016-01-24 14:22:10 +01008007 static char *
8008get_var_special_name(int nr)
8009{
8010 switch (nr)
8011 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008012 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008013 case VVAL_TRUE: return "v:true";
8014 case VVAL_NONE: return "v:none";
8015 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008016 }
8017 EMSG2(_(e_intern2), "get_var_special_name()");
8018 return "42";
8019}
8020
Bram Moolenaar8c711452005-01-14 21:53:12 +00008021/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022 * Return a string with the string representation of a variable.
8023 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008024 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008025 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008026 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008027 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008028 */
8029 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008030echo_string(
8031 typval_T *tv,
8032 char_u **tofree,
8033 char_u *numbuf,
8034 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008035{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 static int recurse = 0;
8037 char_u *r = NULL;
8038
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008040 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008041 if (!did_echo_string_emsg)
8042 {
8043 /* Only give this message once for a recursive call to avoid
8044 * flooding the user with errors. And stop iterating over lists
8045 * and dicts. */
8046 did_echo_string_emsg = TRUE;
8047 EMSG(_("E724: variable nested too deep for displaying"));
8048 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008049 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008050 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051 }
8052 ++recurse;
8053
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008054 switch (tv->v_type)
8055 {
8056 case VAR_FUNC:
8057 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008058 r = tv->vval.v_string;
8059 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008060
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008061 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008062 {
8063 partial_T *pt = tv->vval.v_partial;
8064 char_u *fname = string_quote(pt == NULL ? NULL
8065 : pt->pt_name, FALSE);
8066 garray_T ga;
8067 int i;
8068 char_u *tf;
8069
8070 ga_init2(&ga, 1, 100);
8071 ga_concat(&ga, (char_u *)"function(");
8072 if (fname != NULL)
8073 {
8074 ga_concat(&ga, fname);
8075 vim_free(fname);
8076 }
8077 if (pt != NULL && pt->pt_argc > 0)
8078 {
8079 ga_concat(&ga, (char_u *)", [");
8080 for (i = 0; i < pt->pt_argc; ++i)
8081 {
8082 if (i > 0)
8083 ga_concat(&ga, (char_u *)", ");
8084 ga_concat(&ga,
8085 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8086 vim_free(tf);
8087 }
8088 ga_concat(&ga, (char_u *)"]");
8089 }
8090 if (pt != NULL && pt->pt_dict != NULL)
8091 {
8092 typval_T dtv;
8093
8094 ga_concat(&ga, (char_u *)", ");
8095 dtv.v_type = VAR_DICT;
8096 dtv.vval.v_dict = pt->pt_dict;
8097 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8098 vim_free(tf);
8099 }
8100 ga_concat(&ga, (char_u *)")");
8101
8102 *tofree = ga.ga_data;
8103 r = *tofree;
8104 break;
8105 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008106
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008107 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008108 if (tv->vval.v_list == NULL)
8109 {
8110 *tofree = NULL;
8111 r = NULL;
8112 }
8113 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8114 {
8115 *tofree = NULL;
8116 r = (char_u *)"[...]";
8117 }
8118 else
8119 {
8120 tv->vval.v_list->lv_copyID = copyID;
8121 *tofree = list2string(tv, copyID);
8122 r = *tofree;
8123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008124 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008125
Bram Moolenaar8c711452005-01-14 21:53:12 +00008126 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008127 if (tv->vval.v_dict == NULL)
8128 {
8129 *tofree = NULL;
8130 r = NULL;
8131 }
8132 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8133 {
8134 *tofree = NULL;
8135 r = (char_u *)"{...}";
8136 }
8137 else
8138 {
8139 tv->vval.v_dict->dv_copyID = copyID;
8140 *tofree = dict2string(tv, copyID);
8141 r = *tofree;
8142 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008143 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 case VAR_STRING:
8146 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008147 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008148 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008149 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008150 *tofree = NULL;
8151 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008152 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008153
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008155#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008156 *tofree = NULL;
8157 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8158 r = numbuf;
8159 break;
8160#endif
8161
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008162 case VAR_SPECIAL:
8163 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008164 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008165 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167
Bram Moolenaar8502c702014-06-17 12:51:16 +02008168 if (--recurse == 0)
8169 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008170 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008171}
8172
8173/*
8174 * Return a string with the string representation of a variable.
8175 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8176 * "numbuf" is used for a number.
8177 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008178 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008179 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008180 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008181tv2string(
8182 typval_T *tv,
8183 char_u **tofree,
8184 char_u *numbuf,
8185 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008186{
8187 switch (tv->v_type)
8188 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008189 case VAR_FUNC:
8190 *tofree = string_quote(tv->vval.v_string, TRUE);
8191 return *tofree;
8192 case VAR_STRING:
8193 *tofree = string_quote(tv->vval.v_string, FALSE);
8194 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008195 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008196#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008197 *tofree = NULL;
8198 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8199 return numbuf;
8200#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008201 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008202 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008203 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008204 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008205 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008206 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008207 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008208 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008209 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008210 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008211 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008212}
8213
8214/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 * Return string "str" in ' quotes, doubling ' characters.
8216 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008217 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008218 */
8219 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008220string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008221{
Bram Moolenaar33570922005-01-25 22:26:29 +00008222 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008223 char_u *p, *r, *s;
8224
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 len = (function ? 13 : 3);
8226 if (str != NULL)
8227 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008228 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008229 for (p = str; *p != NUL; mb_ptr_adv(p))
8230 if (*p == '\'')
8231 ++len;
8232 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008233 s = r = alloc(len);
8234 if (r != NULL)
8235 {
8236 if (function)
8237 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 r += 10;
8240 }
8241 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008242 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008243 if (str != NULL)
8244 for (p = str; *p != NUL; )
8245 {
8246 if (*p == '\'')
8247 *r++ = '\'';
8248 MB_COPY_CHAR(p, r);
8249 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008250 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008251 if (function)
8252 *r++ = ')';
8253 *r++ = NUL;
8254 }
8255 return s;
8256}
8257
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008258#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008259/*
8260 * Convert the string "text" to a floating point number.
8261 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8262 * this always uses a decimal point.
8263 * Returns the length of the text that was consumed.
8264 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008266string2float(
8267 char_u *text,
8268 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269{
8270 char *s = (char *)text;
8271 float_T f;
8272
8273 f = strtod(s, &s);
8274 *value = f;
8275 return (int)((char_u *)s - text);
8276}
8277#endif
8278
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 * Get the value of an environment variable.
8281 * "arg" is pointing to the '$'. It is advanced to after the name.
8282 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008283 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 */
8285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008286get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287{
8288 char_u *string = NULL;
8289 int len;
8290 int cc;
8291 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008292 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294 ++*arg;
8295 name = *arg;
8296 len = get_env_len(arg);
8297 if (evaluate)
8298 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008299 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008300 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008301
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008302 cc = name[len];
8303 name[len] = NUL;
8304 /* first try vim_getenv(), fast for normal environment vars */
8305 string = vim_getenv(name, &mustfree);
8306 if (string != NULL && *string != NUL)
8307 {
8308 if (!mustfree)
8309 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008311 else
8312 {
8313 if (mustfree)
8314 vim_free(string);
8315
8316 /* next try expanding things like $VIM and ${HOME} */
8317 string = expand_env_save(name - 1);
8318 if (string != NULL && *string == '$')
8319 {
8320 vim_free(string);
8321 string = NULL;
8322 }
8323 }
8324 name[len] = cc;
8325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326 rettv->v_type = VAR_STRING;
8327 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
8329
8330 return OK;
8331}
8332
8333/*
8334 * Array with names and number of arguments of all internal functions
8335 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8336 */
8337static struct fst
8338{
8339 char *f_name; /* function name */
8340 char f_min_argc; /* minimal number of arguments */
8341 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008342 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008343 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344} functions[] =
8345{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008346#ifdef FEAT_FLOAT
8347 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008348 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008349#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008350 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008351 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"append", 2, 2, f_append},
8353 {"argc", 0, 0, f_argc},
8354 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008355 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008356 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008357#ifdef FEAT_FLOAT
8358 {"asin", 1, 1, f_asin}, /* WJMc */
8359#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008360 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008361 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008362 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008363 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008364 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008365 {"assert_notequal", 2, 3, f_assert_notequal},
8366 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008367 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#ifdef FEAT_FLOAT
8369 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008370 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008373 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"bufexists", 1, 1, f_bufexists},
8375 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8376 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8377 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8378 {"buflisted", 1, 1, f_buflisted},
8379 {"bufloaded", 1, 1, f_bufloaded},
8380 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008381 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 {"bufwinnr", 1, 1, f_bufwinnr},
8383 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008384 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008385 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#ifdef FEAT_FLOAT
8388 {"ceil", 1, 1, f_ceil},
8389#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008390#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008391 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008392 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8393 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008394 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008395 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008396 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008397 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008398 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008399 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008400 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008401 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008402 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8403 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008404 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008405 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008406#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008407 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008408 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008410 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008412#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008413 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008414 {"complete_add", 1, 1, f_complete_add},
8415 {"complete_check", 0, 0, f_complete_check},
8416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008418 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419#ifdef FEAT_FLOAT
8420 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008421 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008422#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008423 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008425 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008426 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008427 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008429 {"diff_filler", 1, 1, f_diff_filler},
8430 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008431 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008433 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {"eventhandler", 0, 0, f_eventhandler},
8435 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008436 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008438#ifdef FEAT_FLOAT
8439 {"exp", 1, 1, f_exp},
8440#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008441 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008442 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008443 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8445 {"filereadable", 1, 1, f_filereadable},
8446 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008447 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008448 {"finddir", 1, 3, f_finddir},
8449 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008450#ifdef FEAT_FLOAT
8451 {"float2nr", 1, 1, f_float2nr},
8452 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008453 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008454#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008455 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"fnamemodify", 2, 2, f_fnamemodify},
8457 {"foldclosed", 1, 1, f_foldclosed},
8458 {"foldclosedend", 1, 1, f_foldclosedend},
8459 {"foldlevel", 1, 1, f_foldlevel},
8460 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008461 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008463 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008464 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008465 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008466 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008467 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {"getchar", 0, 1, f_getchar},
8469 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008470 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {"getcmdline", 0, 0, f_getcmdline},
8472 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008473 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008474 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008475 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008476 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008477 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008478 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"getfsize", 1, 1, f_getfsize},
8480 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008481 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008483 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008484 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008485 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008486 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008487 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008488 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008490 {"gettabvar", 2, 3, f_gettabvar},
8491 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"getwinposx", 0, 0, f_getwinposx},
8493 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008494 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008495 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008496 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008497 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008499 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008500 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008501 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8503 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8504 {"histadd", 2, 2, f_histadd},
8505 {"histdel", 1, 2, f_histdel},
8506 {"histget", 1, 2, f_histget},
8507 {"histnr", 1, 1, f_histnr},
8508 {"hlID", 1, 1, f_hlID},
8509 {"hlexists", 1, 1, f_hlexists},
8510 {"hostname", 0, 0, f_hostname},
8511 {"iconv", 3, 3, f_iconv},
8512 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008513 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008514 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008516 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"inputrestore", 0, 0, f_inputrestore},
8518 {"inputsave", 0, 0, f_inputsave},
8519 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008520 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008521 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008523 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008524#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8525 {"isnan", 1, 1, f_isnan},
8526#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008527 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008528#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008529 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008530 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008531 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008532 {"job_start", 1, 2, f_job_start},
8533 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008534 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008535#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008536 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008537 {"js_decode", 1, 1, f_js_decode},
8538 {"js_encode", 1, 1, f_js_encode},
8539 {"json_decode", 1, 1, f_json_decode},
8540 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008541 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008543 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {"libcall", 3, 3, f_libcall},
8545 {"libcallnr", 3, 3, f_libcallnr},
8546 {"line", 1, 1, f_line},
8547 {"line2byte", 1, 1, f_line2byte},
8548 {"lispindent", 1, 1, f_lispindent},
8549 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008550#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008551 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552 {"log10", 1, 1, f_log10},
8553#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008554#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008555 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008556#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008557 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008558 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008559 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008560 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008561 {"matchadd", 2, 5, f_matchadd},
8562 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008563 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008564 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008565 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008566 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008567 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008568 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008569 {"max", 1, 1, f_max},
8570 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008571#ifdef vim_mkdir
8572 {"mkdir", 1, 3, f_mkdir},
8573#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008574 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008575#ifdef FEAT_MZSCHEME
8576 {"mzeval", 1, 1, f_mzeval},
8577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008579 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008580 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008581 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008582#ifdef FEAT_PERL
8583 {"perleval", 1, 1, f_perleval},
8584#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008585#ifdef FEAT_FLOAT
8586 {"pow", 2, 2, f_pow},
8587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008589 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008590 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008591#ifdef FEAT_PYTHON3
8592 {"py3eval", 1, 1, f_py3eval},
8593#endif
8594#ifdef FEAT_PYTHON
8595 {"pyeval", 1, 1, f_pyeval},
8596#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008597 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008598 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008599 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008600#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008601 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008602#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008603 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {"remote_expr", 2, 3, f_remote_expr},
8605 {"remote_foreground", 1, 1, f_remote_foreground},
8606 {"remote_peek", 1, 2, f_remote_peek},
8607 {"remote_read", 1, 1, f_remote_read},
8608 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008609 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008611 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008613 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008614#ifdef FEAT_FLOAT
8615 {"round", 1, 1, f_round},
8616#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008617 {"screenattr", 2, 2, f_screenattr},
8618 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008619 {"screencol", 0, 0, f_screencol},
8620 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008621 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008622 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008623 {"searchpair", 3, 7, f_searchpair},
8624 {"searchpairpos", 3, 7, f_searchpairpos},
8625 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"server2client", 2, 2, f_server2client},
8627 {"serverlist", 0, 0, f_serverlist},
8628 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008629 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008631 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008633 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008634 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008635 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008636 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008638 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008639 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008641#ifdef FEAT_CRYPT
8642 {"sha256", 1, 1, f_sha256},
8643#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008644 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008645 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008647#ifdef FEAT_FLOAT
8648 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008649 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008650#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008651 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008652 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008653 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008654 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008655 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008656#ifdef FEAT_FLOAT
8657 {"sqrt", 1, 1, f_sqrt},
8658 {"str2float", 1, 1, f_str2float},
8659#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008660 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008661 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008662 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008663 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664#ifdef HAVE_STRFTIME
8665 {"strftime", 1, 2, f_strftime},
8666#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008667 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008668 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008669 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 {"strlen", 1, 1, f_strlen},
8671 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008672 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008674 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008675 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {"substitute", 4, 4, f_substitute},
8677 {"synID", 3, 3, f_synID},
8678 {"synIDattr", 2, 3, f_synIDattr},
8679 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008680 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008681 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008682 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008683 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008684 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008685 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008686 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008687 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008688 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008689#ifdef FEAT_FLOAT
8690 {"tan", 1, 1, f_tan},
8691 {"tanh", 1, 1, f_tanh},
8692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008694 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8695 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008696 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8697#ifdef FEAT_JOB_CHANNEL
8698 {"test_null_channel", 0, 0, f_test_null_channel},
8699#endif
8700 {"test_null_dict", 0, 0, f_test_null_dict},
8701#ifdef FEAT_JOB_CHANNEL
8702 {"test_null_job", 0, 0, f_test_null_job},
8703#endif
8704 {"test_null_list", 0, 0, f_test_null_list},
8705 {"test_null_partial", 0, 0, f_test_null_partial},
8706 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008707#ifdef FEAT_TIMERS
8708 {"timer_start", 2, 3, f_timer_start},
8709 {"timer_stop", 1, 1, f_timer_stop},
8710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {"tolower", 1, 1, f_tolower},
8712 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008713 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008714#ifdef FEAT_FLOAT
8715 {"trunc", 1, 1, f_trunc},
8716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008718 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008719 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008720 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008721 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {"virtcol", 1, 1, f_virtcol},
8723 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008724 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008725 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008726 {"win_getid", 0, 2, f_win_getid},
8727 {"win_gotoid", 1, 1, f_win_gotoid},
8728 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8729 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {"winbufnr", 1, 1, f_winbufnr},
8731 {"wincol", 0, 0, f_wincol},
8732 {"winheight", 1, 1, f_winheight},
8733 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008734 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008736 {"winrestview", 1, 1, f_winrestview},
8737 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008739 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008740 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008741 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742};
8743
8744#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8745
8746/*
8747 * Function given to ExpandGeneric() to obtain the list of internal
8748 * or user defined function names.
8749 */
8750 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008751get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752{
8753 static int intidx = -1;
8754 char_u *name;
8755
8756 if (idx == 0)
8757 intidx = -1;
8758 if (intidx < 0)
8759 {
8760 name = get_user_func_name(xp, idx);
8761 if (name != NULL)
8762 return name;
8763 }
8764 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8765 {
8766 STRCPY(IObuff, functions[intidx].f_name);
8767 STRCAT(IObuff, "(");
8768 if (functions[intidx].f_max_argc == 0)
8769 STRCAT(IObuff, ")");
8770 return IObuff;
8771 }
8772
8773 return NULL;
8774}
8775
8776/*
8777 * Function given to ExpandGeneric() to obtain the list of internal or
8778 * user defined variable or function names.
8779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008781get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 static int intidx = -1;
8784 char_u *name;
8785
8786 if (idx == 0)
8787 intidx = -1;
8788 if (intidx < 0)
8789 {
8790 name = get_function_name(xp, idx);
8791 if (name != NULL)
8792 return name;
8793 }
8794 return get_user_var_name(xp, ++intidx);
8795}
8796
8797#endif /* FEAT_CMDL_COMPL */
8798
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008799#if defined(EBCDIC) || defined(PROTO)
8800/*
8801 * Compare struct fst by function name.
8802 */
8803 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008804compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008805{
8806 struct fst *p1 = (struct fst *)s1;
8807 struct fst *p2 = (struct fst *)s2;
8808
8809 return STRCMP(p1->f_name, p2->f_name);
8810}
8811
8812/*
8813 * Sort the function table by function name.
8814 * The sorting of the table above is ASCII dependant.
8815 * On machines using EBCDIC we have to sort it.
8816 */
8817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008818sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008819{
8820 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8821
8822 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8823}
8824#endif
8825
8826
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827/*
8828 * Find internal function in table above.
8829 * Return index, or -1 if not found
8830 */
8831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008832find_internal_func(
8833 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 int first = 0;
8836 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8837 int cmp;
8838 int x;
8839
8840 /*
8841 * Find the function name in the table. Binary search.
8842 */
8843 while (first <= last)
8844 {
8845 x = first + ((unsigned)(last - first) >> 1);
8846 cmp = STRCMP(name, functions[x].f_name);
8847 if (cmp < 0)
8848 last = x - 1;
8849 else if (cmp > 0)
8850 first = x + 1;
8851 else
8852 return x;
8853 }
8854 return -1;
8855}
8856
8857/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008858 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8859 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008860 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8861 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008862 */
8863 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008864deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008865{
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008867 int cc;
8868
Bram Moolenaar65639032016-03-16 21:40:30 +01008869 if (partialp != NULL)
8870 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008871
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008872 cc = name[*lenp];
8873 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008874 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008875 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008876 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008877 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008878 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008879 {
8880 *lenp = 0;
8881 return (char_u *)""; /* just in case */
8882 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008883 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008884 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008885 }
8886
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008887 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8888 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008889 partial_T *pt = v->di_tv.vval.v_partial;
8890
8891 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008892 {
8893 *lenp = 0;
8894 return (char_u *)""; /* just in case */
8895 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008896 if (partialp != NULL)
8897 *partialp = pt;
8898 *lenp = (int)STRLEN(pt->pt_name);
8899 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008900 }
8901
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008902 return name;
8903}
8904
8905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 * Allocate a variable for the result of a function.
8907 * Return OK or FAIL.
8908 */
8909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008910get_func_tv(
8911 char_u *name, /* name of the function */
8912 int len, /* length of "name" */
8913 typval_T *rettv,
8914 char_u **arg, /* argument, pointing to the '(' */
8915 linenr_T firstline, /* first line of range */
8916 linenr_T lastline, /* last line of range */
8917 int *doesrange, /* return: function handled range */
8918 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008919 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008920 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 char_u *argp;
8923 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008924 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 int argcount = 0; /* number of arguments found */
8926
8927 /*
8928 * Get the arguments.
8929 */
8930 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008931 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 {
8933 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8934 if (*argp == ')' || *argp == ',' || *argp == NUL)
8935 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8937 {
8938 ret = FAIL;
8939 break;
8940 }
8941 ++argcount;
8942 if (*argp != ',')
8943 break;
8944 }
8945 if (*argp == ')')
8946 ++argp;
8947 else
8948 ret = FAIL;
8949
8950 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008951 {
8952 int i = 0;
8953
8954 if (get_vim_var_nr(VV_TESTING))
8955 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008956 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008957 * what variables are used on the call stack. */
8958 if (funcargs.ga_itemsize == 0)
8959 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8960 for (i = 0; i < argcount; ++i)
8961 if (ga_grow(&funcargs, 1) == OK)
8962 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8963 &argvars[i];
8964 }
8965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008967 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008968
8969 funcargs.ga_len -= i;
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008972 {
8973 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008974 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008976 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
8979 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
8982 *arg = skipwhite(argp);
8983 return ret;
8984}
8985
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008986#define ERROR_UNKNOWN 0
8987#define ERROR_TOOMANY 1
8988#define ERROR_TOOFEW 2
8989#define ERROR_SCRIPT 3
8990#define ERROR_DICT 4
8991#define ERROR_NONE 5
8992#define ERROR_OTHER 6
8993#define FLEN_FIXED 40
8994
8995/*
8996 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8997 * Change <SNR>123_name() to K_SNR 123_name().
8998 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8999 * (slow).
9000 */
9001 static char_u *
9002fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9003{
9004 int llen;
9005 char_u *fname;
9006 int i;
9007
9008 llen = eval_fname_script(name);
9009 if (llen > 0)
9010 {
9011 fname_buf[0] = K_SPECIAL;
9012 fname_buf[1] = KS_EXTRA;
9013 fname_buf[2] = (int)KE_SNR;
9014 i = 3;
9015 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9016 {
9017 if (current_SID <= 0)
9018 *error = ERROR_SCRIPT;
9019 else
9020 {
9021 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9022 i = (int)STRLEN(fname_buf);
9023 }
9024 }
9025 if (i + STRLEN(name + llen) < FLEN_FIXED)
9026 {
9027 STRCPY(fname_buf + i, name + llen);
9028 fname = fname_buf;
9029 }
9030 else
9031 {
9032 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9033 if (fname == NULL)
9034 *error = ERROR_OTHER;
9035 else
9036 {
9037 *tofree = fname;
9038 mch_memmove(fname, fname_buf, (size_t)i);
9039 STRCPY(fname + i, name + llen);
9040 }
9041 }
9042 }
9043 else
9044 fname = name;
9045 return fname;
9046}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
9048/*
9049 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009050 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009051 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009054call_func(
9055 char_u *funcname, /* name of the function */
9056 int len, /* length of "name" */
9057 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009058 int argcount_in, /* number of "argvars" */
9059 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009060 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009061 linenr_T firstline, /* first line of range */
9062 linenr_T lastline, /* last line of range */
9063 int *doesrange, /* return: function handled range */
9064 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009065 partial_T *partial, /* optional, can be NULL */
9066 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
9068 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 int error = ERROR_NONE;
9070 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009073 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009075 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009076 int argcount = argcount_in;
9077 typval_T *argvars = argvars_in;
9078 dict_T *selfdict = selfdict_in;
9079 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9080 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009081
9082 /* Make a copy of the name, if it comes from a funcref variable it could
9083 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009084 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009085 if (name == NULL)
9086 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009088 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090 *doesrange = FALSE;
9091
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009092 if (partial != NULL)
9093 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009094 /* When the function has a partial with a dict and there is a dict
9095 * argument, use the dict argument. That is backwards compatible.
9096 * When the dict was bound explicitly use the one from the partial. */
9097 if (partial->pt_dict != NULL
9098 && (selfdict_in == NULL || !partial->pt_auto))
9099 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009100 if (error == ERROR_NONE && partial->pt_argc > 0)
9101 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009102 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9103 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9104 for (i = 0; i < argcount_in; ++i)
9105 argv[i + argv_clear] = argvars_in[i];
9106 argvars = argv;
9107 argcount = partial->pt_argc + argcount_in;
9108 }
9109 }
9110
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111
9112 /* execute the function if no errors detected and executing */
9113 if (evaluate && error == ERROR_NONE)
9114 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009115 char_u *rfname = fname;
9116
9117 /* Ignore "g:" before a function name. */
9118 if (fname[0] == 'g' && fname[1] == ':')
9119 rfname = fname + 2;
9120
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009121 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9122 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 error = ERROR_UNKNOWN;
9124
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009125 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 {
9127 /*
9128 * User defined function.
9129 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009130 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009131
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009133 /* Trigger FuncUndefined event, may load the function. */
9134 if (fp == NULL
9135 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009136 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009137 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009139 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009140 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 }
9142#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009143 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009144 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009145 {
9146 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009147 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009148 }
9149
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 if (fp != NULL)
9151 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009152 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009154 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009156 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009158 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009159 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 else
9161 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009162 int did_save_redo = FALSE;
9163
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 /*
9165 * Call the user function.
9166 * Save and restore search patterns, script variables and
9167 * redo buffer.
9168 */
9169 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009170#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009171 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009172#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009173 {
9174 saveRedobuff();
9175 did_save_redo = TRUE;
9176 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009177 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009179 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009180 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9181 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9182 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009183 /* Function was unreferenced while being used, free it
9184 * now. */
9185 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009186 if (did_save_redo)
9187 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 restore_search_patterns();
9189 error = ERROR_NONE;
9190 }
9191 }
9192 }
9193 else
9194 {
9195 /*
9196 * Find the function name in the table, call its implementation.
9197 */
9198 i = find_internal_func(fname);
9199 if (i >= 0)
9200 {
9201 if (argcount < functions[i].f_min_argc)
9202 error = ERROR_TOOFEW;
9203 else if (argcount > functions[i].f_max_argc)
9204 error = ERROR_TOOMANY;
9205 else
9206 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009207 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 error = ERROR_NONE;
9210 }
9211 }
9212 }
9213 /*
9214 * The function call (or "FuncUndefined" autocommand sequence) might
9215 * have been aborted by an error, an interrupt, or an explicitly thrown
9216 * exception that has not been caught so far. This situation can be
9217 * tested for by calling aborting(). For an error in an internal
9218 * function or for the "E132" error in call_user_func(), however, the
9219 * throw point at which the "force_abort" flag (temporarily reset by
9220 * emsg()) is normally updated has not been reached yet. We need to
9221 * update that flag first to make aborting() reliable.
9222 */
9223 update_force_abort();
9224 }
9225 if (error == ERROR_NONE)
9226 ret = OK;
9227
9228 /*
9229 * Report an error unless the argument evaluation or function call has been
9230 * cancelled due to an aborting error, an interrupt, or an exception.
9231 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009232 if (!aborting())
9233 {
9234 switch (error)
9235 {
9236 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009237 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009238 break;
9239 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009240 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009241 break;
9242 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009243 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009244 name);
9245 break;
9246 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009247 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009248 name);
9249 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009250 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009251 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009252 name);
9253 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009254 }
9255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009257 while (argv_clear > 0)
9258 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009259 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009260 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262 return ret;
9263}
9264
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009265/*
9266 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009267 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009268 */
9269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009270emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009271{
9272 char_u *p;
9273
9274 if (*name == K_SPECIAL)
9275 p = concat_str((char_u *)"<SNR>", name + 3);
9276 else
9277 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009278 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009279 if (p != name)
9280 vim_free(p);
9281}
9282
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009283/*
9284 * Return TRUE for a non-zero Number and a non-empty String.
9285 */
9286 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009287non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009288{
9289 return ((argvars[0].v_type == VAR_NUMBER
9290 && argvars[0].vval.v_number != 0)
9291 || (argvars[0].v_type == VAR_STRING
9292 && argvars[0].vval.v_string != NULL
9293 && *argvars[0].vval.v_string != NUL));
9294}
9295
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296/*********************************************
9297 * Implementation of the built-in functions
9298 */
9299
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009300#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009301static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009302
9303/*
9304 * Get the float value of "argvars[0]" into "f".
9305 * Returns FAIL when the argument is not a Number or Float.
9306 */
9307 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009309{
9310 if (argvars[0].v_type == VAR_FLOAT)
9311 {
9312 *f = argvars[0].vval.v_float;
9313 return OK;
9314 }
9315 if (argvars[0].v_type == VAR_NUMBER)
9316 {
9317 *f = (float_T)argvars[0].vval.v_number;
9318 return OK;
9319 }
9320 EMSG(_("E808: Number or Float required"));
9321 return FAIL;
9322}
9323
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009324/*
9325 * "abs(expr)" function
9326 */
9327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009328f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009329{
9330 if (argvars[0].v_type == VAR_FLOAT)
9331 {
9332 rettv->v_type = VAR_FLOAT;
9333 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9334 }
9335 else
9336 {
9337 varnumber_T n;
9338 int error = FALSE;
9339
9340 n = get_tv_number_chk(&argvars[0], &error);
9341 if (error)
9342 rettv->vval.v_number = -1;
9343 else if (n > 0)
9344 rettv->vval.v_number = n;
9345 else
9346 rettv->vval.v_number = -n;
9347 }
9348}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009349
9350/*
9351 * "acos()" function
9352 */
9353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009354f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009355{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009356 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009357
9358 rettv->v_type = VAR_FLOAT;
9359 if (get_float_arg(argvars, &f) == OK)
9360 rettv->vval.v_float = acos(f);
9361 else
9362 rettv->vval.v_float = 0.0;
9363}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009364#endif
9365
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009367 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 */
9369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009370f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371{
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009374 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009375 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009377 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009378 && !tv_check_lock(l->lv_lock,
9379 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009380 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009382 }
9383 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009384 EMSG(_(e_listreq));
9385}
9386
9387/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009388 * "and(expr, expr)" function
9389 */
9390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009391f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009392{
9393 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9394 & get_tv_number_chk(&argvars[1], NULL);
9395}
9396
9397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009398 * "append(lnum, string/list)" function
9399 */
9400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009401f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402{
9403 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009405 list_T *l = NULL;
9406 listitem_T *li = NULL;
9407 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009408 long added = 0;
9409
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009410 /* When coming here from Insert mode, sync undo, so that this can be
9411 * undone separately from what was previously inserted. */
9412 if (u_sync_once == 2)
9413 {
9414 u_sync_once = 1; /* notify that u_sync() was called */
9415 u_sync(TRUE);
9416 }
9417
Bram Moolenaar0d660222005-01-07 21:51:51 +00009418 lnum = get_tv_lnum(argvars);
9419 if (lnum >= 0
9420 && lnum <= curbuf->b_ml.ml_line_count
9421 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009422 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009423 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009424 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009425 l = argvars[1].vval.v_list;
9426 if (l == NULL)
9427 return;
9428 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009429 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009430 for (;;)
9431 {
9432 if (l == NULL)
9433 tv = &argvars[1]; /* append a string */
9434 else if (li == NULL)
9435 break; /* end of list */
9436 else
9437 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009438 line = get_tv_string_chk(tv);
9439 if (line == NULL) /* type error */
9440 {
9441 rettv->vval.v_number = 1; /* Failed */
9442 break;
9443 }
9444 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009445 ++added;
9446 if (l == NULL)
9447 break;
9448 li = li->li_next;
9449 }
9450
9451 appended_lines_mark(lnum, added);
9452 if (curwin->w_cursor.lnum > lnum)
9453 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455 else
9456 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457}
9458
9459/*
9460 * "argc()" function
9461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466}
9467
9468/*
9469 * "argidx()" function
9470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009472f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475}
9476
9477/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009478 * "arglistid()" function
9479 */
9480 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009481f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009482{
9483 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009484
9485 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009486 wp = find_tabwin(&argvars[0], &argvars[1]);
9487 if (wp != NULL)
9488 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009489}
9490
9491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 * "argv(nr)" function
9493 */
9494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009495f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497 int idx;
9498
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009499 if (argvars[0].v_type != VAR_UNKNOWN)
9500 {
9501 idx = get_tv_number_chk(&argvars[0], NULL);
9502 if (idx >= 0 && idx < ARGCOUNT)
9503 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9504 else
9505 rettv->vval.v_string = NULL;
9506 rettv->v_type = VAR_STRING;
9507 }
9508 else if (rettv_list_alloc(rettv) == OK)
9509 for (idx = 0; idx < ARGCOUNT; ++idx)
9510 list_append_string(rettv->vval.v_list,
9511 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512}
9513
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009514typedef enum
9515{
9516 ASSERT_EQUAL,
9517 ASSERT_NOTEQUAL,
9518 ASSERT_MATCH,
9519 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009520 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009521} assert_type_T;
9522
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009523static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009524static 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 +01009525static void assert_error(garray_T *gap);
9526static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009527
9528/*
9529 * Prepare "gap" for an assert error and add the sourcing position.
9530 */
9531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009532prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009533{
9534 char buf[NUMBUFLEN];
9535
9536 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009537 if (sourcing_name != NULL)
9538 {
9539 ga_concat(gap, sourcing_name);
9540 if (sourcing_lnum > 0)
9541 ga_concat(gap, (char_u *)" ");
9542 }
9543 if (sourcing_lnum > 0)
9544 {
9545 sprintf(buf, "line %ld", (long)sourcing_lnum);
9546 ga_concat(gap, (char_u *)buf);
9547 }
9548 if (sourcing_name != NULL || sourcing_lnum > 0)
9549 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009550}
9551
9552/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009553 * Append "str" to "gap", escaping unprintable characters.
9554 * Changes NL to \n, CR to \r, etc.
9555 */
9556 static void
9557ga_concat_esc(garray_T *gap, char_u *str)
9558{
9559 char_u *p;
9560 char_u buf[NUMBUFLEN];
9561
Bram Moolenaarf1551962016-03-15 12:55:58 +01009562 if (str == NULL)
9563 {
9564 ga_concat(gap, (char_u *)"NULL");
9565 return;
9566 }
9567
Bram Moolenaar23689172016-02-15 22:37:37 +01009568 for (p = str; *p != NUL; ++p)
9569 switch (*p)
9570 {
9571 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9572 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9573 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9574 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9575 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9576 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9577 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9578 default:
9579 if (*p < ' ')
9580 {
9581 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9582 ga_concat(gap, buf);
9583 }
9584 else
9585 ga_append(gap, *p);
9586 break;
9587 }
9588}
9589
9590/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009591 * Fill "gap" with information about an assert error.
9592 */
9593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009594fill_assert_error(
9595 garray_T *gap,
9596 typval_T *opt_msg_tv,
9597 char_u *exp_str,
9598 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009599 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009600 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009601{
9602 char_u numbuf[NUMBUFLEN];
9603 char_u *tofree;
9604
9605 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9606 {
9607 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9608 vim_free(tofree);
9609 }
9610 else
9611 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009612 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009613 ga_concat(gap, (char_u *)"Pattern ");
9614 else
9615 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009616 if (exp_str == NULL)
9617 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009618 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009619 vim_free(tofree);
9620 }
9621 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009622 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009623 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009624 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009625 else if (atype == ASSERT_NOTMATCH)
9626 ga_concat(gap, (char_u *)" does match ");
9627 else if (atype == ASSERT_NOTEQUAL)
9628 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009629 else
9630 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009631 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009632 vim_free(tofree);
9633 }
9634}
Bram Moolenaar43345542015-11-29 17:35:35 +01009635
9636/*
9637 * Add an assert error to v:errors.
9638 */
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009641{
9642 struct vimvar *vp = &vimvars[VV_ERRORS];
9643
9644 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9645 /* Make sure v:errors is a list. */
9646 set_vim_var_list(VV_ERRORS, list_alloc());
9647 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9648}
9649
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009650 static void
9651assert_equal_common(typval_T *argvars, assert_type_T atype)
9652{
9653 garray_T ga;
9654
9655 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9656 != (atype == ASSERT_EQUAL))
9657 {
9658 prepare_assert_error(&ga);
9659 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9660 atype);
9661 assert_error(&ga);
9662 ga_clear(&ga);
9663 }
9664}
9665
Bram Moolenaar43345542015-11-29 17:35:35 +01009666/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009667 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009668 */
9669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009670f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009671{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009672 assert_equal_common(argvars, ASSERT_EQUAL);
9673}
Bram Moolenaar43345542015-11-29 17:35:35 +01009674
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009675/*
9676 * "assert_notequal(expected, actual[, msg])" function
9677 */
9678 static void
9679f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9680{
9681 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009682}
9683
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009684/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009685 * "assert_exception(string[, msg])" function
9686 */
9687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009688f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009689{
9690 garray_T ga;
9691 char *error;
9692
9693 error = (char *)get_tv_string_chk(&argvars[0]);
9694 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9695 {
9696 prepare_assert_error(&ga);
9697 ga_concat(&ga, (char_u *)"v:exception is not set");
9698 assert_error(&ga);
9699 ga_clear(&ga);
9700 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009701 else if (error != NULL
9702 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009703 {
9704 prepare_assert_error(&ga);
9705 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009706 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009707 assert_error(&ga);
9708 ga_clear(&ga);
9709 }
9710}
9711
9712/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009713 * "assert_fails(cmd [, error])" function
9714 */
9715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009716f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009717{
9718 char_u *cmd = get_tv_string_chk(&argvars[0]);
9719 garray_T ga;
9720
9721 called_emsg = FALSE;
9722 suppress_errthrow = TRUE;
9723 emsg_silent = TRUE;
9724 do_cmdline_cmd(cmd);
9725 if (!called_emsg)
9726 {
9727 prepare_assert_error(&ga);
9728 ga_concat(&ga, (char_u *)"command did not fail: ");
9729 ga_concat(&ga, cmd);
9730 assert_error(&ga);
9731 ga_clear(&ga);
9732 }
9733 else if (argvars[1].v_type != VAR_UNKNOWN)
9734 {
9735 char_u buf[NUMBUFLEN];
9736 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9737
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009738 if (error == NULL
9739 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009740 {
9741 prepare_assert_error(&ga);
9742 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009743 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009744 assert_error(&ga);
9745 ga_clear(&ga);
9746 }
9747 }
9748
9749 called_emsg = FALSE;
9750 suppress_errthrow = FALSE;
9751 emsg_silent = FALSE;
9752 emsg_on_display = FALSE;
9753 set_vim_var_string(VV_ERRMSG, NULL, 0);
9754}
9755
9756/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009757 * Common for assert_true() and assert_false().
9758 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009760assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009761{
9762 int error = FALSE;
9763 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009764
Bram Moolenaar37127922016-02-06 20:29:28 +01009765 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009766 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009767 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009768 if (argvars[0].v_type != VAR_NUMBER
9769 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9770 || error)
9771 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009772 prepare_assert_error(&ga);
9773 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009774 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009775 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009777 ga_clear(&ga);
9778 }
9779}
9780
9781/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009782 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009783 */
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009786{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009787 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009788}
9789
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009790 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009791assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009792{
9793 garray_T ga;
9794 char_u buf1[NUMBUFLEN];
9795 char_u buf2[NUMBUFLEN];
9796 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9797 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9798
Bram Moolenaar72188e92016-03-28 22:48:29 +02009799 if (pat == NULL || text == NULL)
9800 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009801 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009802 {
9803 prepare_assert_error(&ga);
9804 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009805 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009806 assert_error(&ga);
9807 ga_clear(&ga);
9808 }
9809}
9810
9811/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009812 * "assert_match(pattern, actual[, msg])" function
9813 */
9814 static void
9815f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9816{
9817 assert_match_common(argvars, ASSERT_MATCH);
9818}
9819
9820/*
9821 * "assert_notmatch(pattern, actual[, msg])" function
9822 */
9823 static void
9824f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9825{
9826 assert_match_common(argvars, ASSERT_NOTMATCH);
9827}
9828
9829/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009830 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009831 */
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009834{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009835 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009836}
9837
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009838#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009839/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009840 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009841 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009843f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009844{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009845 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009846
9847 rettv->v_type = VAR_FLOAT;
9848 if (get_float_arg(argvars, &f) == OK)
9849 rettv->vval.v_float = asin(f);
9850 else
9851 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009852}
9853
9854/*
9855 * "atan()" function
9856 */
9857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009858f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009859{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009860 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009861
9862 rettv->v_type = VAR_FLOAT;
9863 if (get_float_arg(argvars, &f) == OK)
9864 rettv->vval.v_float = atan(f);
9865 else
9866 rettv->vval.v_float = 0.0;
9867}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009868
9869/*
9870 * "atan2()" function
9871 */
9872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009873f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009874{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009875 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009876
9877 rettv->v_type = VAR_FLOAT;
9878 if (get_float_arg(argvars, &fx) == OK
9879 && get_float_arg(&argvars[1], &fy) == OK)
9880 rettv->vval.v_float = atan2(fx, fy);
9881 else
9882 rettv->vval.v_float = 0.0;
9883}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009884#endif
9885
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886/*
9887 * "browse(save, title, initdir, default)" function
9888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009890f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
9892#ifdef FEAT_BROWSE
9893 int save;
9894 char_u *title;
9895 char_u *initdir;
9896 char_u *defname;
9897 char_u buf[NUMBUFLEN];
9898 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009899 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009901 save = get_tv_number_chk(&argvars[0], &error);
9902 title = get_tv_string_chk(&argvars[1]);
9903 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9904 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009906 if (error || title == NULL || initdir == NULL || defname == NULL)
9907 rettv->vval.v_string = NULL;
9908 else
9909 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009910 do_browse(save ? BROWSE_SAVE : 0,
9911 title, defname, NULL, initdir, NULL, curbuf);
9912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009913 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009915 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009916}
9917
9918/*
9919 * "browsedir(title, initdir)" function
9920 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009923{
9924#ifdef FEAT_BROWSE
9925 char_u *title;
9926 char_u *initdir;
9927 char_u buf[NUMBUFLEN];
9928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 title = get_tv_string_chk(&argvars[0]);
9930 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009932 if (title == NULL || initdir == NULL)
9933 rettv->vval.v_string = NULL;
9934 else
9935 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009936 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009938 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941}
9942
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009943static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009944
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945/*
9946 * Find a buffer by number or exact name.
9947 */
9948 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009949find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950{
9951 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009953 if (avar->v_type == VAR_NUMBER)
9954 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009955 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009957 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009958 if (buf == NULL)
9959 {
9960 /* No full path name match, try a match with a URL or a "nofile"
9961 * buffer, these don't use the full path. */
9962 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9963 if (buf->b_fname != NULL
9964 && (path_with_url(buf->b_fname)
9965#ifdef FEAT_QUICKFIX
9966 || bt_nofile(buf)
9967#endif
9968 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009969 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009970 break;
9971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 }
9973 return buf;
9974}
9975
9976/*
9977 * "bufexists(expr)" function
9978 */
9979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009980f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983}
9984
9985/*
9986 * "buflisted(expr)" function
9987 */
9988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009989f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990{
9991 buf_T *buf;
9992
9993 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995}
9996
9997/*
9998 * "bufloaded(expr)" function
9999 */
10000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010001f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002{
10003 buf_T *buf;
10004
10005 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007}
10008
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010009 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010010buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 int save_magic;
10013 char_u *save_cpo;
10014 buf_T *buf;
10015
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10017 save_magic = p_magic;
10018 p_magic = TRUE;
10019 save_cpo = p_cpo;
10020 p_cpo = (char_u *)"";
10021
10022 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010023 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024
10025 p_magic = save_magic;
10026 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010027 return buf;
10028}
10029
10030/*
10031 * Get buffer by number or pattern.
10032 */
10033 static buf_T *
10034get_buf_tv(typval_T *tv, int curtab_only)
10035{
10036 char_u *name = tv->vval.v_string;
10037 buf_T *buf;
10038
10039 if (tv->v_type == VAR_NUMBER)
10040 return buflist_findnr((int)tv->vval.v_number);
10041 if (tv->v_type != VAR_STRING)
10042 return NULL;
10043 if (name == NULL || *name == NUL)
10044 return curbuf;
10045 if (name[0] == '$' && name[1] == NUL)
10046 return lastbuf;
10047
10048 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
10050 /* If not found, try expanding the name, like done for bufexists(). */
10051 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053
10054 return buf;
10055}
10056
10057/*
10058 * "bufname(expr)" function
10059 */
10060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010061f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 buf_T *buf;
10064
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010065 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010067 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010070 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 --emsg_off;
10074}
10075
10076/*
10077 * "bufnr(expr)" function
10078 */
10079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010080f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010083 int error = FALSE;
10084 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010088 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010089 --emsg_off;
10090
10091 /* If the buffer isn't found and the second argument is not zero create a
10092 * new buffer. */
10093 if (buf == NULL
10094 && argvars[1].v_type != VAR_UNKNOWN
10095 && get_tv_number_chk(&argvars[1], &error) != 0
10096 && !error
10097 && (name = get_tv_string_chk(&argvars[0])) != NULL
10098 && !error)
10099 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10100
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105}
10106
10107/*
10108 * "bufwinnr(nr)" function
10109 */
10110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010111f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
10113#ifdef FEAT_WINDOWS
10114 win_T *wp;
10115 int winnr = 0;
10116#endif
10117 buf_T *buf;
10118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010119 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010121 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122#ifdef FEAT_WINDOWS
10123 for (wp = firstwin; wp; wp = wp->w_next)
10124 {
10125 ++winnr;
10126 if (wp->w_buffer == buf)
10127 break;
10128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010129 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#endif
10133 --emsg_off;
10134}
10135
10136/*
10137 * "byte2line(byte)" function
10138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010140f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141{
10142#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144#else
10145 long boff = 0;
10146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 (linenr_T)0, &boff);
10153#endif
10154}
10155
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010157byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010158{
10159#ifdef FEAT_MBYTE
10160 char_u *t;
10161#endif
10162 char_u *str;
10163 long idx;
10164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 str = get_tv_string_chk(&argvars[0]);
10166 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010169 return;
10170
10171#ifdef FEAT_MBYTE
10172 t = str;
10173 for ( ; idx > 0; idx--)
10174 {
10175 if (*t == NUL) /* EOL reached */
10176 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010177 if (enc_utf8 && comp)
10178 t += utf_ptr2len(t);
10179 else
10180 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010181 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010182 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010183#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010184 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010186#endif
10187}
10188
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010189/*
10190 * "byteidx()" function
10191 */
10192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010193f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010194{
10195 byteidx(argvars, rettv, FALSE);
10196}
10197
10198/*
10199 * "byteidxcomp()" function
10200 */
10201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010202f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010203{
10204 byteidx(argvars, rettv, TRUE);
10205}
10206
Bram Moolenaardb913952012-06-29 12:54:53 +020010207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010208func_call(
10209 char_u *name,
10210 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010211 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212 dict_T *selfdict,
10213 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010214{
10215 listitem_T *item;
10216 typval_T argv[MAX_FUNC_ARGS + 1];
10217 int argc = 0;
10218 int dummy;
10219 int r = 0;
10220
10221 for (item = args->vval.v_list->lv_first; item != NULL;
10222 item = item->li_next)
10223 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010224 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010225 {
10226 EMSG(_("E699: Too many arguments"));
10227 break;
10228 }
10229 /* Make a copy of each argument. This is needed to be able to set
10230 * v_lock to VAR_FIXED in the copy without changing the original list.
10231 */
10232 copy_tv(&item->li_tv, &argv[argc++]);
10233 }
10234
10235 if (item == NULL)
10236 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010238 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010239
10240 /* Free the arguments. */
10241 while (argc > 0)
10242 clear_tv(&argv[--argc]);
10243
10244 return r;
10245}
10246
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010247/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010248 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010249 */
10250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010251f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010252{
10253 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010254 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010255 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010256
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010257 if (argvars[1].v_type != VAR_LIST)
10258 {
10259 EMSG(_(e_listreq));
10260 return;
10261 }
10262 if (argvars[1].vval.v_list == NULL)
10263 return;
10264
10265 if (argvars[0].v_type == VAR_FUNC)
10266 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010267 else if (argvars[0].v_type == VAR_PARTIAL)
10268 {
10269 partial = argvars[0].vval.v_partial;
10270 func = partial->pt_name;
10271 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010272 else
10273 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 if (*func == NUL)
10275 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277 if (argvars[2].v_type != VAR_UNKNOWN)
10278 {
10279 if (argvars[2].v_type != VAR_DICT)
10280 {
10281 EMSG(_(e_dictreq));
10282 return;
10283 }
10284 selfdict = argvars[2].vval.v_dict;
10285 }
10286
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010287 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010288}
10289
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010290#ifdef FEAT_FLOAT
10291/*
10292 * "ceil({float})" function
10293 */
10294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010295f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010296{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010297 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010298
10299 rettv->v_type = VAR_FLOAT;
10300 if (get_float_arg(argvars, &f) == OK)
10301 rettv->vval.v_float = ceil(f);
10302 else
10303 rettv->vval.v_float = 0.0;
10304}
10305#endif
10306
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010307#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010308/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010309 * "ch_close()" function
10310 */
10311 static void
10312f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10313{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010314 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010315
10316 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010317 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010318 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010319 channel_clear(channel);
10320 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010321}
10322
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010323/*
10324 * "ch_getbufnr()" function
10325 */
10326 static void
10327f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10328{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010329 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010330
10331 rettv->vval.v_number = -1;
10332 if (channel != NULL)
10333 {
10334 char_u *what = get_tv_string(&argvars[1]);
10335 int part;
10336
10337 if (STRCMP(what, "err") == 0)
10338 part = PART_ERR;
10339 else if (STRCMP(what, "out") == 0)
10340 part = PART_OUT;
10341 else if (STRCMP(what, "in") == 0)
10342 part = PART_IN;
10343 else
10344 part = PART_SOCK;
10345 if (channel->ch_part[part].ch_buffer != NULL)
10346 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10347 }
10348}
10349
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010350/*
10351 * "ch_getjob()" function
10352 */
10353 static void
10354f_ch_getjob(typval_T *argvars, typval_T *rettv)
10355{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010356 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010357
10358 if (channel != NULL)
10359 {
10360 rettv->v_type = VAR_JOB;
10361 rettv->vval.v_job = channel->ch_job;
10362 if (channel->ch_job != NULL)
10363 ++channel->ch_job->jv_refcount;
10364 }
10365}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010366
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010367/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010368 * "ch_info()" function
10369 */
10370 static void
10371f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10372{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010373 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010374
10375 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10376 channel_info(channel, rettv->vval.v_dict);
10377}
10378
10379/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010380 * "ch_log()" function
10381 */
10382 static void
10383f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10384{
10385 char_u *msg = get_tv_string(&argvars[0]);
10386 channel_T *channel = NULL;
10387
10388 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010389 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010390
10391 ch_log(channel, (char *)msg);
10392}
10393
10394/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010395 * "ch_logfile()" function
10396 */
10397 static void
10398f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10399{
10400 char_u *fname;
10401 char_u *opt = (char_u *)"";
10402 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010403
10404 fname = get_tv_string(&argvars[0]);
10405 if (argvars[1].v_type == VAR_STRING)
10406 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010407 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010408}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010409
10410/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010411 * "ch_open()" function
10412 */
10413 static void
10414f_ch_open(typval_T *argvars, typval_T *rettv)
10415{
Bram Moolenaar77073442016-02-13 23:23:53 +010010416 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010417 if (check_restricted() || check_secure())
10418 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010419 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010420}
10421
10422/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010423 * "ch_read()" function
10424 */
10425 static void
10426f_ch_read(typval_T *argvars, typval_T *rettv)
10427{
10428 common_channel_read(argvars, rettv, FALSE);
10429}
10430
10431/*
10432 * "ch_readraw()" function
10433 */
10434 static void
10435f_ch_readraw(typval_T *argvars, typval_T *rettv)
10436{
10437 common_channel_read(argvars, rettv, TRUE);
10438}
10439
10440/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010441 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010442 */
10443 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010444f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10445{
10446 ch_expr_common(argvars, rettv, TRUE);
10447}
10448
10449/*
10450 * "ch_sendexpr()" function
10451 */
10452 static void
10453f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10454{
10455 ch_expr_common(argvars, rettv, FALSE);
10456}
10457
10458/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010459 * "ch_evalraw()" function
10460 */
10461 static void
10462f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10463{
10464 ch_raw_common(argvars, rettv, TRUE);
10465}
10466
10467/*
10468 * "ch_sendraw()" function
10469 */
10470 static void
10471f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10472{
10473 ch_raw_common(argvars, rettv, FALSE);
10474}
10475
10476/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010477 * "ch_setoptions()" function
10478 */
10479 static void
10480f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10481{
10482 channel_T *channel;
10483 jobopt_T opt;
10484
Bram Moolenaar437905c2016-04-26 19:01:05 +020010485 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010486 if (channel == NULL)
10487 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010488 clear_job_options(&opt);
10489 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010490 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10491 channel_set_options(channel, &opt);
10492 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010493}
10494
10495/*
10496 * "ch_status()" function
10497 */
10498 static void
10499f_ch_status(typval_T *argvars, typval_T *rettv)
10500{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010501 channel_T *channel;
10502
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010503 /* return an empty string by default */
10504 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010505 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010506
Bram Moolenaar437905c2016-04-26 19:01:05 +020010507 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010508 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010509}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010510#endif
10511
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010512/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010513 * "changenr()" function
10514 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010516f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010517{
10518 rettv->vval.v_number = curbuf->b_u_seq_cur;
10519}
10520
10521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 * "char2nr(string)" function
10523 */
10524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010525f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526{
10527#ifdef FEAT_MBYTE
10528 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010529 {
10530 int utf8 = 0;
10531
10532 if (argvars[1].v_type != VAR_UNKNOWN)
10533 utf8 = get_tv_number_chk(&argvars[1], NULL);
10534
10535 if (utf8)
10536 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10537 else
10538 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 else
10541#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543}
10544
10545/*
10546 * "cindent(lnum)" function
10547 */
10548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010549f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550{
10551#ifdef FEAT_CINDENT
10552 pos_T pos;
10553 linenr_T lnum;
10554
10555 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10558 {
10559 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010560 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 curwin->w_cursor = pos;
10562 }
10563 else
10564#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010565 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566}
10567
10568/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010569 * "clearmatches()" function
10570 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010572f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010573{
10574#ifdef FEAT_SEARCH_EXTRA
10575 clear_matches(curwin);
10576#endif
10577}
10578
10579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 * "col(string)" function
10581 */
10582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010583f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584{
10585 colnr_T col = 0;
10586 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010587 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010589 fp = var2fpos(&argvars[0], FALSE, &fnum);
10590 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 {
10592 if (fp->col == MAXCOL)
10593 {
10594 /* '> can be MAXCOL, get the length of the line then */
10595 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010596 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597 else
10598 col = MAXCOL;
10599 }
10600 else
10601 {
10602 col = fp->col + 1;
10603#ifdef FEAT_VIRTUALEDIT
10604 /* col(".") when the cursor is on the NUL at the end of the line
10605 * because of "coladd" can be seen as an extra column. */
10606 if (virtual_active() && fp == &curwin->w_cursor)
10607 {
10608 char_u *p = ml_get_cursor();
10609
10610 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10611 curwin->w_virtcol - curwin->w_cursor.coladd))
10612 {
10613# ifdef FEAT_MBYTE
10614 int l;
10615
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010616 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617 col += l;
10618# else
10619 if (*p != NUL && p[1] == NUL)
10620 ++col;
10621# endif
10622 }
10623 }
10624#endif
10625 }
10626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010627 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628}
10629
Bram Moolenaar572cb562005-08-05 21:35:02 +000010630#if defined(FEAT_INS_EXPAND)
10631/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010632 * "complete()" function
10633 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010635f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010636{
10637 int startcol;
10638
10639 if ((State & INSERT) == 0)
10640 {
10641 EMSG(_("E785: complete() can only be used in Insert mode"));
10642 return;
10643 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010644
10645 /* Check for undo allowed here, because if something was already inserted
10646 * the line was already saved for undo and this check isn't done. */
10647 if (!undo_allowed())
10648 return;
10649
Bram Moolenaarade00832006-03-10 21:46:58 +000010650 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10651 {
10652 EMSG(_(e_invarg));
10653 return;
10654 }
10655
10656 startcol = get_tv_number_chk(&argvars[0], NULL);
10657 if (startcol <= 0)
10658 return;
10659
10660 set_completion(startcol - 1, argvars[1].vval.v_list);
10661}
10662
10663/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010664 * "complete_add()" function
10665 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010667f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010668{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010669 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010670}
10671
10672/*
10673 * "complete_check()" function
10674 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010676f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010677{
10678 int saved = RedrawingDisabled;
10679
10680 RedrawingDisabled = 0;
10681 ins_compl_check_keys(0);
10682 rettv->vval.v_number = compl_interrupted;
10683 RedrawingDisabled = saved;
10684}
10685#endif
10686
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687/*
10688 * "confirm(message, buttons[, default [, type]])" function
10689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010691f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
10693#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10694 char_u *message;
10695 char_u *buttons = NULL;
10696 char_u buf[NUMBUFLEN];
10697 char_u buf2[NUMBUFLEN];
10698 int def = 1;
10699 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 char_u *typestr;
10701 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 message = get_tv_string_chk(&argvars[0]);
10704 if (message == NULL)
10705 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010706 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010708 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10709 if (buttons == NULL)
10710 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010714 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010716 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10717 if (typestr == NULL)
10718 error = TRUE;
10719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 switch (TOUPPER_ASC(*typestr))
10722 {
10723 case 'E': type = VIM_ERROR; break;
10724 case 'Q': type = VIM_QUESTION; break;
10725 case 'I': type = VIM_INFO; break;
10726 case 'W': type = VIM_WARNING; break;
10727 case 'G': type = VIM_GENERIC; break;
10728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 }
10730 }
10731 }
10732 }
10733
10734 if (buttons == NULL || *buttons == NUL)
10735 buttons = (char_u *)_("&Ok");
10736
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010737 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010739 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740#endif
10741}
10742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010743/*
10744 * "copy()" function
10745 */
10746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010747f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010748{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010749 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010750}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010752#ifdef FEAT_FLOAT
10753/*
10754 * "cos()" function
10755 */
10756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010757f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010758{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010759 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010760
10761 rettv->v_type = VAR_FLOAT;
10762 if (get_float_arg(argvars, &f) == OK)
10763 rettv->vval.v_float = cos(f);
10764 else
10765 rettv->vval.v_float = 0.0;
10766}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010767
10768/*
10769 * "cosh()" function
10770 */
10771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010772f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010773{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010774 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010775
10776 rettv->v_type = VAR_FLOAT;
10777 if (get_float_arg(argvars, &f) == OK)
10778 rettv->vval.v_float = cosh(f);
10779 else
10780 rettv->vval.v_float = 0.0;
10781}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010782#endif
10783
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010785 * "count()" function
10786 */
10787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010789{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010790 long n = 0;
10791 int ic = FALSE;
10792
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010794 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010795 listitem_T *li;
10796 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010797 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010798
Bram Moolenaare9a41262005-01-15 22:18:47 +000010799 if ((l = argvars[0].vval.v_list) != NULL)
10800 {
10801 li = l->lv_first;
10802 if (argvars[2].v_type != VAR_UNKNOWN)
10803 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 int error = FALSE;
10805
10806 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010807 if (argvars[3].v_type != VAR_UNKNOWN)
10808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 idx = get_tv_number_chk(&argvars[3], &error);
10810 if (!error)
10811 {
10812 li = list_find(l, idx);
10813 if (li == NULL)
10814 EMSGN(_(e_listidx), idx);
10815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 if (error)
10818 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010819 }
10820
10821 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010822 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010823 ++n;
10824 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010825 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 else if (argvars[0].v_type == VAR_DICT)
10827 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 int todo;
10829 dict_T *d;
10830 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010832 if ((d = argvars[0].vval.v_dict) != NULL)
10833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010834 int error = FALSE;
10835
Bram Moolenaare9a41262005-01-15 22:18:47 +000010836 if (argvars[2].v_type != VAR_UNKNOWN)
10837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 if (argvars[3].v_type != VAR_UNKNOWN)
10840 EMSG(_(e_invarg));
10841 }
10842
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010843 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010844 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010845 {
10846 if (!HASHITEM_EMPTY(hi))
10847 {
10848 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010849 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010850 ++n;
10851 }
10852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010853 }
10854 }
10855 else
10856 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010857 rettv->vval.v_number = n;
10858}
10859
10860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10862 *
10863 * Checks the existence of a cscope connection.
10864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010866f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867{
10868#ifdef FEAT_CSCOPE
10869 int num = 0;
10870 char_u *dbpath = NULL;
10871 char_u *prepend = NULL;
10872 char_u buf[NUMBUFLEN];
10873
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010874 if (argvars[0].v_type != VAR_UNKNOWN
10875 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 num = (int)get_tv_number(&argvars[0]);
10878 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 }
10882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884#endif
10885}
10886
10887/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010888 * "cursor(lnum, col)" function, or
10889 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010891 * Moves the cursor to the specified line and column.
10892 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010895f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896{
10897 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010898#ifdef FEAT_VIRTUALEDIT
10899 long coladd = 0;
10900#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010901 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010903 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010904 if (argvars[1].v_type == VAR_UNKNOWN)
10905 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010906 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010907 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010908
Bram Moolenaar493c1782014-05-28 14:34:46 +020010909 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010910 {
10911 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010912 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010913 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010914 line = pos.lnum;
10915 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010916#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010917 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010918#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010919 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010920 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010921 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010922 set_curswant = FALSE;
10923 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010924 }
10925 else
10926 {
10927 line = get_tv_lnum(argvars);
10928 col = get_tv_number_chk(&argvars[1], NULL);
10929#ifdef FEAT_VIRTUALEDIT
10930 if (argvars[2].v_type != VAR_UNKNOWN)
10931 coladd = get_tv_number_chk(&argvars[2], NULL);
10932#endif
10933 }
10934 if (line < 0 || col < 0
10935#ifdef FEAT_VIRTUALEDIT
10936 || coladd < 0
10937#endif
10938 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 if (line > 0)
10941 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 if (col > 0)
10943 curwin->w_cursor.col = col - 1;
10944#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010945 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946#endif
10947
10948 /* Make sure the cursor is in a valid position. */
10949 check_cursor();
10950#ifdef FEAT_MBYTE
10951 /* Correct cursor for multi-byte character. */
10952 if (has_mbyte)
10953 mb_adjust_cursor();
10954#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010955
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010956 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010957 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958}
10959
10960/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010961 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 */
10963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010964f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010966 int noref = 0;
10967
10968 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010970 if (noref < 0 || noref > 1)
10971 EMSG(_(e_invarg));
10972 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010973 {
10974 current_copyID += COPYID_INC;
10975 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
10979/*
10980 * "delete()" function
10981 */
10982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010983f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010985 char_u nbuf[NUMBUFLEN];
10986 char_u *name;
10987 char_u *flags;
10988
10989 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010991 return;
10992
10993 name = get_tv_string(&argvars[0]);
10994 if (name == NULL || *name == NUL)
10995 {
10996 EMSG(_(e_invarg));
10997 return;
10998 }
10999
11000 if (argvars[1].v_type != VAR_UNKNOWN)
11001 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011003 flags = (char_u *)"";
11004
11005 if (*flags == NUL)
11006 /* delete a file */
11007 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11008 else if (STRCMP(flags, "d") == 0)
11009 /* delete an empty directory */
11010 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11011 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011012 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011013 rettv->vval.v_number = delete_recursive(name);
11014 else
11015 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016}
11017
11018/*
11019 * "did_filetype()" function
11020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011022f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026#endif
11027}
11028
11029/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011030 * "diff_filler()" function
11031 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011033f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011034{
11035#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011037#endif
11038}
11039
11040/*
11041 * "diff_hlID()" function
11042 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011044f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011045{
11046#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011048 static linenr_T prev_lnum = 0;
11049 static int changedtick = 0;
11050 static int fnum = 0;
11051 static int change_start = 0;
11052 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011053 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011054 int filler_lines;
11055 int col;
11056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057 if (lnum < 0) /* ignore type error in {lnum} arg */
11058 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011059 if (lnum != prev_lnum
11060 || changedtick != curbuf->b_changedtick
11061 || fnum != curbuf->b_fnum)
11062 {
11063 /* New line, buffer, change: need to get the values. */
11064 filler_lines = diff_check(curwin, lnum);
11065 if (filler_lines < 0)
11066 {
11067 if (filler_lines == -1)
11068 {
11069 change_start = MAXCOL;
11070 change_end = -1;
11071 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11072 hlID = HLF_ADD; /* added line */
11073 else
11074 hlID = HLF_CHD; /* changed line */
11075 }
11076 else
11077 hlID = HLF_ADD; /* added line */
11078 }
11079 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011080 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081 prev_lnum = lnum;
11082 changedtick = curbuf->b_changedtick;
11083 fnum = curbuf->b_fnum;
11084 }
11085
11086 if (hlID == HLF_CHD || hlID == HLF_TXD)
11087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011089 if (col >= change_start && col <= change_end)
11090 hlID = HLF_TXD; /* changed text */
11091 else
11092 hlID = HLF_CHD; /* changed line */
11093 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011094 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011095#endif
11096}
11097
11098/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011099 * "empty({expr})" function
11100 */
11101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011102f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011103{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011104 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011105
11106 switch (argvars[0].v_type)
11107 {
11108 case VAR_STRING:
11109 case VAR_FUNC:
11110 n = argvars[0].vval.v_string == NULL
11111 || *argvars[0].vval.v_string == NUL;
11112 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011113 case VAR_PARTIAL:
11114 n = FALSE;
11115 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011116 case VAR_NUMBER:
11117 n = argvars[0].vval.v_number == 0;
11118 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011119 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011120#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011121 n = argvars[0].vval.v_float == 0.0;
11122 break;
11123#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011124 case VAR_LIST:
11125 n = argvars[0].vval.v_list == NULL
11126 || argvars[0].vval.v_list->lv_first == NULL;
11127 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011128 case VAR_DICT:
11129 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011130 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011131 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011132 case VAR_SPECIAL:
11133 n = argvars[0].vval.v_number != VVAL_TRUE;
11134 break;
11135
Bram Moolenaar835dc632016-02-07 14:27:38 +010011136 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011138 n = argvars[0].vval.v_job == NULL
11139 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11140 break;
11141#endif
11142 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011144 n = argvars[0].vval.v_channel == NULL
11145 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011146 break;
11147#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011148 case VAR_UNKNOWN:
11149 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11150 n = TRUE;
11151 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011152 }
11153
11154 rettv->vval.v_number = n;
11155}
11156
11157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 * "escape({string}, {chars})" function
11159 */
11160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011161f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162{
11163 char_u buf[NUMBUFLEN];
11164
Bram Moolenaar758711c2005-02-02 23:11:38 +000011165 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11166 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168}
11169
11170/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011171 * "eval()" function
11172 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011174f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011175{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011176 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011177
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 s = get_tv_string_chk(&argvars[0]);
11179 if (s != NULL)
11180 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011181
Bram Moolenaar615b9972015-01-14 17:15:05 +010011182 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11184 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011185 if (p != NULL && !aborting())
11186 EMSG2(_(e_invexpr2), p);
11187 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011189 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011191 else if (*s != NUL)
11192 EMSG(_(e_trailing));
11193}
11194
11195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 * "eventhandler()" function
11197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011199f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011201 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202}
11203
11204/*
11205 * "executable()" function
11206 */
11207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011208f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011210 char_u *name = get_tv_string(&argvars[0]);
11211
11212 /* Check in $PATH and also check directly if there is a directory name. */
11213 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11214 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011215}
11216
11217/*
11218 * "exepath()" function
11219 */
11220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011221f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011222{
11223 char_u *p = NULL;
11224
Bram Moolenaarb5971142015-03-21 17:32:19 +010011225 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011226 rettv->v_type = VAR_STRING;
11227 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228}
11229
11230/*
11231 * "exists()" function
11232 */
11233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011234f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235{
11236 char_u *p;
11237 char_u *name;
11238 int n = FALSE;
11239 int len = 0;
11240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011241 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 if (*p == '$') /* environment variable */
11243 {
11244 /* first try "normal" environment variables (fast) */
11245 if (mch_getenv(p + 1) != NULL)
11246 n = TRUE;
11247 else
11248 {
11249 /* try expanding things like $VIM and ${HOME} */
11250 p = expand_env_save(p);
11251 if (p != NULL && *p != '$')
11252 n = TRUE;
11253 vim_free(p);
11254 }
11255 }
11256 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011259 if (*skipwhite(p) != NUL)
11260 n = FALSE; /* trailing garbage */
11261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 else if (*p == '*') /* internal or user defined function */
11263 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011264 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 }
11266 else if (*p == ':')
11267 {
11268 n = cmd_exists(p + 1);
11269 }
11270 else if (*p == '#')
11271 {
11272#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011273 if (p[1] == '#')
11274 n = autocmd_supported(p + 2);
11275 else
11276 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277#endif
11278 }
11279 else /* internal variable */
11280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011281 char_u *tofree;
11282 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011284 /* get_name_len() takes care of expanding curly braces */
11285 name = p;
11286 len = get_name_len(&p, &tofree, TRUE, FALSE);
11287 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011289 if (tofree != NULL)
11290 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011291 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011292 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011294 /* handle d.key, l[idx], f(expr) */
11295 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11296 if (n)
11297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 }
11299 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011300 if (*p != NUL)
11301 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011303 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 }
11305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011306 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307}
11308
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011309#ifdef FEAT_FLOAT
11310/*
11311 * "exp()" function
11312 */
11313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011314f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011315{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011316 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011317
11318 rettv->v_type = VAR_FLOAT;
11319 if (get_float_arg(argvars, &f) == OK)
11320 rettv->vval.v_float = exp(f);
11321 else
11322 rettv->vval.v_float = 0.0;
11323}
11324#endif
11325
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326/*
11327 * "expand()" function
11328 */
11329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011330f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
11332 char_u *s;
11333 int len;
11334 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011335 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011338 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011340 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011341 if (argvars[1].v_type != VAR_UNKNOWN
11342 && argvars[2].v_type != VAR_UNKNOWN
11343 && get_tv_number_chk(&argvars[2], &error)
11344 && !error)
11345 {
11346 rettv->v_type = VAR_LIST;
11347 rettv->vval.v_list = NULL;
11348 }
11349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 if (*s == '%' || *s == '#' || *s == '<')
11352 {
11353 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011354 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011356 if (rettv->v_type == VAR_LIST)
11357 {
11358 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11359 list_append_string(rettv->vval.v_list, result, -1);
11360 else
11361 vim_free(result);
11362 }
11363 else
11364 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 }
11366 else
11367 {
11368 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011369 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 if (argvars[1].v_type != VAR_UNKNOWN
11371 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011372 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 if (!error)
11374 {
11375 ExpandInit(&xpc);
11376 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011377 if (p_wic)
11378 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011379 if (rettv->v_type == VAR_STRING)
11380 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11381 options, WILD_ALL);
11382 else if (rettv_list_alloc(rettv) != FAIL)
11383 {
11384 int i;
11385
11386 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11387 for (i = 0; i < xpc.xp_numfiles; i++)
11388 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11389 ExpandCleanup(&xpc);
11390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 }
11392 else
11393 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 }
11395}
11396
11397/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011398 * Go over all entries in "d2" and add them to "d1".
11399 * When "action" is "error" then a duplicate key is an error.
11400 * When "action" is "force" then a duplicate key is overwritten.
11401 * Otherwise duplicate keys are ignored ("action" is "keep").
11402 */
11403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011404dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011405{
11406 dictitem_T *di1;
11407 hashitem_T *hi2;
11408 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011409 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011410
11411 todo = (int)d2->dv_hashtab.ht_used;
11412 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11413 {
11414 if (!HASHITEM_EMPTY(hi2))
11415 {
11416 --todo;
11417 di1 = dict_find(d1, hi2->hi_key, -1);
11418 if (d1->dv_scope != 0)
11419 {
11420 /* Disallow replacing a builtin function in l: and g:.
11421 * Check the key to be valid when adding to any
11422 * scope. */
11423 if (d1->dv_scope == VAR_DEF_SCOPE
11424 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11425 && var_check_func_name(hi2->hi_key,
11426 di1 == NULL))
11427 break;
11428 if (!valid_varname(hi2->hi_key))
11429 break;
11430 }
11431 if (di1 == NULL)
11432 {
11433 di1 = dictitem_copy(HI2DI(hi2));
11434 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11435 dictitem_free(di1);
11436 }
11437 else if (*action == 'e')
11438 {
11439 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11440 break;
11441 }
11442 else if (*action == 'f' && HI2DI(hi2) != di1)
11443 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011444 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11445 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011446 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011447 clear_tv(&di1->di_tv);
11448 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11449 }
11450 }
11451 }
11452}
11453
11454/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011455 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011456 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011457 */
11458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011459f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011460{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011461 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011462
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011464 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011465 list_T *l1, *l2;
11466 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011467 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011469
Bram Moolenaare9a41262005-01-15 22:18:47 +000011470 l1 = argvars[0].vval.v_list;
11471 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011472 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011473 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011474 {
11475 if (argvars[2].v_type != VAR_UNKNOWN)
11476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011477 before = get_tv_number_chk(&argvars[2], &error);
11478 if (error)
11479 return; /* type error; errmsg already given */
11480
Bram Moolenaar758711c2005-02-02 23:11:38 +000011481 if (before == l1->lv_len)
11482 item = NULL;
11483 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011484 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011485 item = list_find(l1, before);
11486 if (item == NULL)
11487 {
11488 EMSGN(_(e_listidx), before);
11489 return;
11490 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 }
11492 }
11493 else
11494 item = NULL;
11495 list_extend(l1, l2, item);
11496
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 copy_tv(&argvars[0], rettv);
11498 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011499 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011500 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11501 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011502 dict_T *d1, *d2;
11503 char_u *action;
11504 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505
11506 d1 = argvars[0].vval.v_dict;
11507 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011508 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011509 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 {
11511 /* Check the third argument. */
11512 if (argvars[2].v_type != VAR_UNKNOWN)
11513 {
11514 static char *(av[]) = {"keep", "force", "error"};
11515
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011516 action = get_tv_string_chk(&argvars[2]);
11517 if (action == NULL)
11518 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011519 for (i = 0; i < 3; ++i)
11520 if (STRCMP(action, av[i]) == 0)
11521 break;
11522 if (i == 3)
11523 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011524 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 return;
11526 }
11527 }
11528 else
11529 action = (char_u *)"force";
11530
Bram Moolenaara9922d62013-05-30 13:01:18 +020011531 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011532
Bram Moolenaare9a41262005-01-15 22:18:47 +000011533 copy_tv(&argvars[0], rettv);
11534 }
11535 }
11536 else
11537 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011538}
11539
11540/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011541 * "feedkeys()" function
11542 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011544f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011545{
11546 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011547 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011548 char_u *keys, *flags;
11549 char_u nbuf[NUMBUFLEN];
11550 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011551 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011552 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011553 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011554
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011555 /* This is not allowed in the sandbox. If the commands would still be
11556 * executed in the sandbox it would be OK, but it probably happens later,
11557 * when "sandbox" is no longer set. */
11558 if (check_secure())
11559 return;
11560
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011561 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011562
11563 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011564 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011565 flags = get_tv_string_buf(&argvars[1], nbuf);
11566 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011567 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011568 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011569 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011570 case 'n': remap = FALSE; break;
11571 case 'm': remap = TRUE; break;
11572 case 't': typed = TRUE; break;
11573 case 'i': insert = TRUE; break;
11574 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011575 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011576 }
11577 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011578 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011579
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011580 if (*keys != NUL || execute)
11581 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011582 /* Need to escape K_SPECIAL and CSI before putting the string in the
11583 * typeahead buffer. */
11584 keys_esc = vim_strsave_escape_csi(keys);
11585 if (keys_esc != NULL)
11586 {
11587 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011588 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011589 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011590 if (vgetc_busy)
11591 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011592 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011593 {
11594 int save_msg_scroll = msg_scroll;
11595
11596 /* Avoid a 1 second delay when the keys start Insert mode. */
11597 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011598
Bram Moolenaar245c4102016-04-20 17:37:41 +020011599 if (!dangerous)
11600 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011601 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011602 if (!dangerous)
11603 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011604 msg_scroll |= save_msg_scroll;
11605 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011606 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011607 }
11608}
11609
11610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 * "filereadable()" function
11612 */
11613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011614f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011616 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617 char_u *p;
11618 int n;
11619
Bram Moolenaarc236c162008-07-13 17:41:49 +000011620#ifndef O_NONBLOCK
11621# define O_NONBLOCK 0
11622#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011624 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11625 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 {
11627 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011628 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 }
11630 else
11631 n = FALSE;
11632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634}
11635
11636/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011637 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 * rights to write into.
11639 */
11640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011641f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011643 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644}
11645
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011646 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011647findfilendir(
11648 typval_T *argvars UNUSED,
11649 typval_T *rettv,
11650 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011651{
11652#ifdef FEAT_SEARCHPATH
11653 char_u *fname;
11654 char_u *fresult = NULL;
11655 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11656 char_u *p;
11657 char_u pathbuf[NUMBUFLEN];
11658 int count = 1;
11659 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011660 int error = FALSE;
11661#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011662
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011663 rettv->vval.v_string = NULL;
11664 rettv->v_type = VAR_STRING;
11665
11666#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011668
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011671 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11672 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011673 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674 else
11675 {
11676 if (*p != NUL)
11677 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011680 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011682 }
11683
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011684 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11685 error = TRUE;
11686
11687 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 do
11690 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011691 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011692 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 fresult = find_file_in_path_option(first ? fname : NULL,
11694 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011695 0, first, path,
11696 find_what,
11697 curbuf->b_ffname,
11698 find_what == FINDFILE_DIR
11699 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011700 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011701
11702 if (fresult != NULL && rettv->v_type == VAR_LIST)
11703 list_append_string(rettv->vval.v_list, fresult, -1);
11704
11705 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011707
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011708 if (rettv->v_type == VAR_STRING)
11709 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011711}
11712
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011713static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11714static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011715
11716/*
11717 * Implementation of map() and filter().
11718 */
11719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011720filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011721{
11722 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011724 listitem_T *li, *nli;
11725 list_T *l = NULL;
11726 dictitem_T *di;
11727 hashtab_T *ht;
11728 hashitem_T *hi;
11729 dict_T *d = NULL;
11730 typval_T save_val;
11731 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011733 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011734 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011735 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011736 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011737 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011738 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011739
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011741 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011742 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011743 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 return;
11745 }
11746 else if (argvars[0].v_type == VAR_DICT)
11747 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011748 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011749 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 return;
11751 }
11752 else
11753 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011754 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 return;
11756 }
11757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011758 expr = get_tv_string_buf_chk(&argvars[1], buf);
11759 /* On type errors, the preceding call has already displayed an error
11760 * message. Avoid a misleading error message for an empty string that
11761 * was not passed as argument. */
11762 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 prepare_vimvar(VV_VAL, &save_val);
11765 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011766
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011767 /* We reset "did_emsg" to be able to detect whether an error
11768 * occurred during evaluation of the expression. */
11769 save_did_emsg = did_emsg;
11770 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011771
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011772 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775 vimvars[VV_KEY].vv_type = VAR_STRING;
11776
11777 ht = &d->dv_hashtab;
11778 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011779 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 if (!HASHITEM_EMPTY(hi))
11783 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011784 int r;
11785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 --todo;
11787 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011788 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011789 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11790 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011791 break;
11792 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011793 r = filter_map_one(&di->di_tv, expr, map, &rem);
11794 clear_tv(&vimvars[VV_KEY].vv_tv);
11795 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796 break;
11797 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011798 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011799 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11800 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011801 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011803 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 }
11805 }
11806 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011807 }
11808 else
11809 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011810 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 for (li = l->lv_first; li != NULL; li = nli)
11813 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011814 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011815 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011817 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011818 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011819 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011820 break;
11821 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011823 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011824 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011826
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011827 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011829
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011830 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011832
11833 copy_tv(&argvars[0], rettv);
11834}
11835
11836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011837filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011838{
Bram Moolenaar33570922005-01-25 22:26:29 +000011839 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011840 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011841 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011842
Bram Moolenaar33570922005-01-25 22:26:29 +000011843 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011844 s = expr;
11845 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011846 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011847 if (*s != NUL) /* check for trailing chars after expr */
11848 {
11849 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011850 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011851 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852 }
11853 if (map)
11854 {
11855 /* map(): replace the list item value */
11856 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011857 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 *tv = rettv;
11859 }
11860 else
11861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011862 int error = FALSE;
11863
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011867 /* On type error, nothing has been removed; return FAIL to stop the
11868 * loop. The error message was given by get_tv_number_chk(). */
11869 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011870 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011871 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011872 retval = OK;
11873theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011874 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011875 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011876}
11877
11878/*
11879 * "filter()" function
11880 */
11881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011882f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011883{
11884 filter_map(argvars, rettv, FALSE);
11885}
11886
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011888 * "finddir({fname}[, {path}[, {count}]])" function
11889 */
11890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011891f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011892{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011893 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011894}
11895
11896/*
11897 * "findfile({fname}[, {path}[, {count}]])" function
11898 */
11899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011900f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011901{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011902 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011903}
11904
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011905#ifdef FEAT_FLOAT
11906/*
11907 * "float2nr({float})" function
11908 */
11909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011910f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011911{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011912 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011913
11914 if (get_float_arg(argvars, &f) == OK)
11915 {
11916 if (f < -0x7fffffff)
11917 rettv->vval.v_number = -0x7fffffff;
11918 else if (f > 0x7fffffff)
11919 rettv->vval.v_number = 0x7fffffff;
11920 else
11921 rettv->vval.v_number = (varnumber_T)f;
11922 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011923}
11924
11925/*
11926 * "floor({float})" function
11927 */
11928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011929f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011930{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011931 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011932
11933 rettv->v_type = VAR_FLOAT;
11934 if (get_float_arg(argvars, &f) == OK)
11935 rettv->vval.v_float = floor(f);
11936 else
11937 rettv->vval.v_float = 0.0;
11938}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011939
11940/*
11941 * "fmod()" function
11942 */
11943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011944f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011945{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011946 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011947
11948 rettv->v_type = VAR_FLOAT;
11949 if (get_float_arg(argvars, &fx) == OK
11950 && get_float_arg(&argvars[1], &fy) == OK)
11951 rettv->vval.v_float = fmod(fx, fy);
11952 else
11953 rettv->vval.v_float = 0.0;
11954}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011955#endif
11956
Bram Moolenaar0d660222005-01-07 21:51:51 +000011957/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011958 * "fnameescape({string})" function
11959 */
11960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011961f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011962{
11963 rettv->vval.v_string = vim_strsave_fnameescape(
11964 get_tv_string(&argvars[0]), FALSE);
11965 rettv->v_type = VAR_STRING;
11966}
11967
11968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 * "fnamemodify({fname}, {mods})" function
11970 */
11971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011972f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973{
11974 char_u *fname;
11975 char_u *mods;
11976 int usedlen = 0;
11977 int len;
11978 char_u *fbuf = NULL;
11979 char_u buf[NUMBUFLEN];
11980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 fname = get_tv_string_chk(&argvars[0]);
11982 mods = get_tv_string_buf_chk(&argvars[1], buf);
11983 if (fname == NULL || mods == NULL)
11984 fname = NULL;
11985 else
11986 {
11987 len = (int)STRLEN(fname);
11988 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011993 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011995 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011996 vim_free(fbuf);
11997}
11998
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011999static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000
12001/*
12002 * "foldclosed()" function
12003 */
12004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012005foldclosed_both(
12006 typval_T *argvars UNUSED,
12007 typval_T *rettv,
12008 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009{
12010#ifdef FEAT_FOLDING
12011 linenr_T lnum;
12012 linenr_T first, last;
12013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012014 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12016 {
12017 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12018 {
12019 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012020 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012022 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 return;
12024 }
12025 }
12026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028}
12029
12030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031 * "foldclosed()" function
12032 */
12033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012034f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035{
12036 foldclosed_both(argvars, rettv, FALSE);
12037}
12038
12039/*
12040 * "foldclosedend()" function
12041 */
12042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012044{
12045 foldclosed_both(argvars, rettv, TRUE);
12046}
12047
12048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 * "foldlevel()" function
12050 */
12051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012052f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053{
12054#ifdef FEAT_FOLDING
12055 linenr_T lnum;
12056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012058 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012059 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061}
12062
12063/*
12064 * "foldtext()" function
12065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012067f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012068{
12069#ifdef FEAT_FOLDING
12070 linenr_T lnum;
12071 char_u *s;
12072 char_u *r;
12073 int len;
12074 char *txt;
12075#endif
12076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077 rettv->v_type = VAR_STRING;
12078 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012079#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012080 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12081 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12082 <= curbuf->b_ml.ml_line_count
12083 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 {
12085 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012086 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12087 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088 {
12089 if (!linewhite(lnum))
12090 break;
12091 ++lnum;
12092 }
12093
12094 /* Find interesting text in this line. */
12095 s = skipwhite(ml_get(lnum));
12096 /* skip C comment-start */
12097 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012100 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012101 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012102 {
12103 s = skipwhite(ml_get(lnum + 1));
12104 if (*s == '*')
12105 s = skipwhite(s + 1);
12106 }
12107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 txt = _("+-%s%3ld lines: ");
12109 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012110 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 + 20 /* for %3ld */
12112 + STRLEN(s))); /* concatenated */
12113 if (r != NULL)
12114 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012115 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12116 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12117 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 len = (int)STRLEN(r);
12119 STRCAT(r, s);
12120 /* remove 'foldmarker' and 'commentstring' */
12121 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 }
12124 }
12125#endif
12126}
12127
12128/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012129 * "foldtextresult(lnum)" function
12130 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012132f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012133{
12134#ifdef FEAT_FOLDING
12135 linenr_T lnum;
12136 char_u *text;
12137 char_u buf[51];
12138 foldinfo_T foldinfo;
12139 int fold_count;
12140#endif
12141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->v_type = VAR_STRING;
12143 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012144#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012145 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 /* treat illegal types and illegal string values for {lnum} the same */
12147 if (lnum < 0)
12148 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012149 fold_count = foldedCount(curwin, lnum, &foldinfo);
12150 if (fold_count > 0)
12151 {
12152 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12153 &foldinfo, buf);
12154 if (text == buf)
12155 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012156 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012157 }
12158#endif
12159}
12160
12161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 * "foreground()" function
12163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012165f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167#ifdef FEAT_GUI
12168 if (gui.in_use)
12169 gui_mch_set_foreground();
12170#else
12171# ifdef WIN32
12172 win32_set_foreground();
12173# endif
12174#endif
12175}
12176
12177/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012178 * "function()" function
12179 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012181f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012182{
12183 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012184 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012185 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012186 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012187
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012188 if (argvars[0].v_type == VAR_FUNC)
12189 {
12190 /* function(MyFunc, [arg], dict) */
12191 s = argvars[0].vval.v_string;
12192 }
12193 else if (argvars[0].v_type == VAR_PARTIAL
12194 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012195 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012196 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012197 arg_pt = argvars[0].vval.v_partial;
12198 s = arg_pt->pt_name;
12199 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012200 else
12201 {
12202 /* function('MyFunc', [arg], dict) */
12203 s = get_tv_string(&argvars[0]);
12204 use_string = TRUE;
12205 }
12206
12207 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012208 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012209 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012210 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12211 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012212 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012213 else
12214 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012215 int dict_idx = 0;
12216 int arg_idx = 0;
12217 list_T *list = NULL;
12218
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012219 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012220 {
12221 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012222 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012223
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012224 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12225 * also be called from another script. Using trans_function_name()
12226 * would also work, but some plugins depend on the name being
12227 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012228 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012229 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12230 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012231 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012232 STRCPY(name, sid_buf);
12233 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012234 }
12235 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012236 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012237 name = vim_strsave(s);
12238
12239 if (argvars[1].v_type != VAR_UNKNOWN)
12240 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012241 if (argvars[2].v_type != VAR_UNKNOWN)
12242 {
12243 /* function(name, [args], dict) */
12244 arg_idx = 1;
12245 dict_idx = 2;
12246 }
12247 else if (argvars[1].v_type == VAR_DICT)
12248 /* function(name, dict) */
12249 dict_idx = 1;
12250 else
12251 /* function(name, [args]) */
12252 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012253 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012254 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012255 if (argvars[dict_idx].v_type != VAR_DICT)
12256 {
12257 EMSG(_("E922: expected a dict"));
12258 vim_free(name);
12259 return;
12260 }
12261 if (argvars[dict_idx].vval.v_dict == NULL)
12262 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012263 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012264 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012265 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012266 if (argvars[arg_idx].v_type != VAR_LIST)
12267 {
12268 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12269 vim_free(name);
12270 return;
12271 }
12272 list = argvars[arg_idx].vval.v_list;
12273 if (list == NULL || list->lv_len == 0)
12274 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012275 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012276 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012277 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012278 {
12279 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012280
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012281 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012282 if (pt == NULL)
12283 vim_free(name);
12284 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012285 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012286 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012287 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012288 listitem_T *li;
12289 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012290 int arg_len = 0;
12291 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012293 if (arg_pt != NULL)
12294 arg_len = arg_pt->pt_argc;
12295 if (list != NULL)
12296 lv_len = list->lv_len;
12297 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012298 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012299 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012300 if (pt->pt_argv == NULL)
12301 {
12302 vim_free(pt);
12303 vim_free(name);
12304 return;
12305 }
12306 else
12307 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012308 for (i = 0; i < arg_len; i++)
12309 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12310 if (lv_len > 0)
12311 for (li = list->lv_first; li != NULL;
12312 li = li->li_next)
12313 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012314 }
12315 }
12316
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012317 /* For "function(dict.func, [], dict)" and "func" is a partial
12318 * use "dict". That is backwards compatible. */
12319 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012320 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012321 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012322 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12323 ++pt->pt_dict->dv_refcount;
12324 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012325 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012326 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012327 /* If the dict was bound automatically the result is also
12328 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012329 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012330 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012331 if (pt->pt_dict != NULL)
12332 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012333 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012334
12335 pt->pt_refcount = 1;
12336 pt->pt_name = name;
12337 func_ref(pt->pt_name);
12338 }
12339 rettv->v_type = VAR_PARTIAL;
12340 rettv->vval.v_partial = pt;
12341 }
12342 else
12343 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012344 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012345 rettv->v_type = VAR_FUNC;
12346 rettv->vval.v_string = name;
12347 func_ref(name);
12348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012349 }
12350}
12351
12352/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012353 * "garbagecollect()" function
12354 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012357{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012358 /* This is postponed until we are back at the toplevel, because we may be
12359 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12360 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012361
12362 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12363 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012364}
12365
12366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012367 * "get()" function
12368 */
12369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012371{
Bram Moolenaar33570922005-01-25 22:26:29 +000012372 listitem_T *li;
12373 list_T *l;
12374 dictitem_T *di;
12375 dict_T *d;
12376 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012377
Bram Moolenaare9a41262005-01-15 22:18:47 +000012378 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012380 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 int error = FALSE;
12383
12384 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12385 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012386 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012387 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012389 else if (argvars[0].v_type == VAR_DICT)
12390 {
12391 if ((d = argvars[0].vval.v_dict) != NULL)
12392 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012393 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012394 if (di != NULL)
12395 tv = &di->di_tv;
12396 }
12397 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012398 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012399 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012400 partial_T *pt;
12401 partial_T fref_pt;
12402
12403 if (argvars[0].v_type == VAR_PARTIAL)
12404 pt = argvars[0].vval.v_partial;
12405 else
12406 {
12407 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12408 fref_pt.pt_name = argvars[0].vval.v_string;
12409 pt = &fref_pt;
12410 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012411
12412 if (pt != NULL)
12413 {
12414 char_u *what = get_tv_string(&argvars[1]);
12415
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012416 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012417 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012418 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012419 if (pt->pt_name == NULL)
12420 rettv->vval.v_string = NULL;
12421 else
12422 rettv->vval.v_string = vim_strsave(pt->pt_name);
12423 }
12424 else if (STRCMP(what, "dict") == 0)
12425 {
12426 rettv->v_type = VAR_DICT;
12427 rettv->vval.v_dict = pt->pt_dict;
12428 if (pt->pt_dict != NULL)
12429 ++pt->pt_dict->dv_refcount;
12430 }
12431 else if (STRCMP(what, "args") == 0)
12432 {
12433 rettv->v_type = VAR_LIST;
12434 if (rettv_list_alloc(rettv) == OK)
12435 {
12436 int i;
12437
12438 for (i = 0; i < pt->pt_argc; ++i)
12439 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12440 }
12441 }
12442 else
12443 EMSG2(_(e_invarg2), what);
12444 return;
12445 }
12446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012447 else
12448 EMSG2(_(e_listdictarg), "get()");
12449
12450 if (tv == NULL)
12451 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012453 copy_tv(&argvars[2], rettv);
12454 }
12455 else
12456 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457}
12458
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012459static 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 +000012460
12461/*
12462 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012463 * Return a range (from start to end) of lines in rettv from the specified
12464 * buffer.
12465 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012466 */
12467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012468get_buffer_lines(
12469 buf_T *buf,
12470 linenr_T start,
12471 linenr_T end,
12472 int retlist,
12473 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012474{
12475 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012476
Bram Moolenaar959a1432013-12-14 12:17:38 +010012477 rettv->v_type = VAR_STRING;
12478 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012479 if (retlist && rettv_list_alloc(rettv) == FAIL)
12480 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012481
12482 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12483 return;
12484
12485 if (!retlist)
12486 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012487 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12488 p = ml_get_buf(buf, start, FALSE);
12489 else
12490 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012491 rettv->vval.v_string = vim_strsave(p);
12492 }
12493 else
12494 {
12495 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012496 return;
12497
12498 if (start < 1)
12499 start = 1;
12500 if (end > buf->b_ml.ml_line_count)
12501 end = buf->b_ml.ml_line_count;
12502 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012503 if (list_append_string(rettv->vval.v_list,
12504 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012505 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012506 }
12507}
12508
12509/*
12510 * "getbufline()" function
12511 */
12512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012513f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012514{
12515 linenr_T lnum;
12516 linenr_T end;
12517 buf_T *buf;
12518
12519 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12520 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012521 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012522 --emsg_off;
12523
Bram Moolenaar661b1822005-07-28 22:36:45 +000012524 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012525 if (argvars[2].v_type == VAR_UNKNOWN)
12526 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012527 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012528 end = get_tv_lnum_buf(&argvars[2], buf);
12529
Bram Moolenaar342337a2005-07-21 21:11:17 +000012530 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012531}
12532
Bram Moolenaar0d660222005-01-07 21:51:51 +000012533/*
12534 * "getbufvar()" function
12535 */
12536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012537f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538{
12539 buf_T *buf;
12540 buf_T *save_curbuf;
12541 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012543 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12546 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012548 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012550 rettv->v_type = VAR_STRING;
12551 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552
12553 if (buf != NULL && varname != NULL)
12554 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012555 /* set curbuf to be our buf, temporarily */
12556 save_curbuf = curbuf;
12557 curbuf = buf;
12558
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012560 {
12561 if (get_option_tv(&varname, rettv, TRUE) == OK)
12562 done = TRUE;
12563 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012564 else if (STRCMP(varname, "changedtick") == 0)
12565 {
12566 rettv->v_type = VAR_NUMBER;
12567 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012568 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 else
12571 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012572 /* Look up the variable. */
12573 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12574 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12575 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012578 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012579 done = TRUE;
12580 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012582
12583 /* restore previous notion of curbuf */
12584 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 }
12586
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012587 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12588 /* use the default value */
12589 copy_tv(&argvars[2], rettv);
12590
Bram Moolenaar0d660222005-01-07 21:51:51 +000012591 --emsg_off;
12592}
12593
12594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 * "getchar()" function
12596 */
12597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012598f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599{
12600 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012603 /* Position the cursor. Needed after a message that ends in a space. */
12604 windgoto(msg_row, msg_col);
12605
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 ++no_mapping;
12607 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012608 for (;;)
12609 {
12610 if (argvars[0].v_type == VAR_UNKNOWN)
12611 /* getchar(): blocking wait. */
12612 n = safe_vgetc();
12613 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12614 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012615 n = vpeekc_any();
12616 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012617 /* illegal argument or getchar(0) and no char avail: return zero */
12618 n = 0;
12619 else
12620 /* getchar(0) and char avail: return char */
12621 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012622
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012623 if (n == K_IGNORE)
12624 continue;
12625 break;
12626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 --no_mapping;
12628 --allow_keys;
12629
Bram Moolenaar219b8702006-11-01 14:32:36 +000012630 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12631 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12632 vimvars[VV_MOUSE_COL].vv_nr = 0;
12633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 if (IS_SPECIAL(n) || mod_mask != 0)
12636 {
12637 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12638 int i = 0;
12639
12640 /* Turn a special key into three bytes, plus modifier. */
12641 if (mod_mask != 0)
12642 {
12643 temp[i++] = K_SPECIAL;
12644 temp[i++] = KS_MODIFIER;
12645 temp[i++] = mod_mask;
12646 }
12647 if (IS_SPECIAL(n))
12648 {
12649 temp[i++] = K_SPECIAL;
12650 temp[i++] = K_SECOND(n);
12651 temp[i++] = K_THIRD(n);
12652 }
12653#ifdef FEAT_MBYTE
12654 else if (has_mbyte)
12655 i += (*mb_char2bytes)(n, temp + i);
12656#endif
12657 else
12658 temp[i++] = n;
12659 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012660 rettv->v_type = VAR_STRING;
12661 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012662
12663#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012664 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012665 {
12666 int row = mouse_row;
12667 int col = mouse_col;
12668 win_T *win;
12669 linenr_T lnum;
12670# ifdef FEAT_WINDOWS
12671 win_T *wp;
12672# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012673 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012674
12675 if (row >= 0 && col >= 0)
12676 {
12677 /* Find the window at the mouse coordinates and compute the
12678 * text position. */
12679 win = mouse_find_win(&row, &col);
12680 (void)mouse_comp_pos(win, &row, &col, &lnum);
12681# ifdef FEAT_WINDOWS
12682 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012683 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012684# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012685 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012686 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12687 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12688 }
12689 }
12690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 }
12692}
12693
12694/*
12695 * "getcharmod()" function
12696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012698f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701}
12702
12703/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012704 * "getcharsearch()" function
12705 */
12706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012707f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012708{
12709 if (rettv_dict_alloc(rettv) != FAIL)
12710 {
12711 dict_T *dict = rettv->vval.v_dict;
12712
12713 dict_add_nr_str(dict, "char", 0L, last_csearch());
12714 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12715 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12716 }
12717}
12718
12719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 * "getcmdline()" function
12721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012723f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725 rettv->v_type = VAR_STRING;
12726 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727}
12728
12729/*
12730 * "getcmdpos()" function
12731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012733f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736}
12737
12738/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012739 * "getcmdtype()" function
12740 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012742f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012743{
12744 rettv->v_type = VAR_STRING;
12745 rettv->vval.v_string = alloc(2);
12746 if (rettv->vval.v_string != NULL)
12747 {
12748 rettv->vval.v_string[0] = get_cmdline_type();
12749 rettv->vval.v_string[1] = NUL;
12750 }
12751}
12752
12753/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012754 * "getcmdwintype()" function
12755 */
12756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012757f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012758{
12759 rettv->v_type = VAR_STRING;
12760 rettv->vval.v_string = NULL;
12761#ifdef FEAT_CMDWIN
12762 rettv->vval.v_string = alloc(2);
12763 if (rettv->vval.v_string != NULL)
12764 {
12765 rettv->vval.v_string[0] = cmdwin_type;
12766 rettv->vval.v_string[1] = NUL;
12767 }
12768#endif
12769}
12770
12771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 * "getcwd()" function
12773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012775f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012777 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012778 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012780 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012781 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012782
12783 wp = find_tabwin(&argvars[0], &argvars[1]);
12784 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012786 if (wp->w_localdir != NULL)
12787 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12788 else if(globaldir != NULL)
12789 rettv->vval.v_string = vim_strsave(globaldir);
12790 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012791 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012792 cwd = alloc(MAXPATHL);
12793 if (cwd != NULL)
12794 {
12795 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12796 rettv->vval.v_string = vim_strsave(cwd);
12797 vim_free(cwd);
12798 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012799 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012800#ifdef BACKSLASH_IN_FILENAME
12801 if (rettv->vval.v_string != NULL)
12802 slash_adjust(rettv->vval.v_string);
12803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 }
12805}
12806
12807/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012808 * "getfontname()" function
12809 */
12810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012811f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 rettv->v_type = VAR_STRING;
12814 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012815#ifdef FEAT_GUI
12816 if (gui.in_use)
12817 {
12818 GuiFont font;
12819 char_u *name = NULL;
12820
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012821 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012822 {
12823 /* Get the "Normal" font. Either the name saved by
12824 * hl_set_font_name() or from the font ID. */
12825 font = gui.norm_font;
12826 name = hl_get_font_name();
12827 }
12828 else
12829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012831 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12832 return;
12833 font = gui_mch_get_font(name, FALSE);
12834 if (font == NOFONT)
12835 return; /* Invalid font name, return empty string. */
12836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012838 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012839 gui_mch_free_font(font);
12840 }
12841#endif
12842}
12843
12844/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012845 * "getfperm({fname})" function
12846 */
12847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012848f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012849{
12850 char_u *fname;
12851 struct stat st;
12852 char_u *perm = NULL;
12853 char_u flags[] = "rwx";
12854 int i;
12855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012856 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012857
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012859 if (mch_stat((char *)fname, &st) >= 0)
12860 {
12861 perm = vim_strsave((char_u *)"---------");
12862 if (perm != NULL)
12863 {
12864 for (i = 0; i < 9; i++)
12865 {
12866 if (st.st_mode & (1 << (8 - i)))
12867 perm[i] = flags[i % 3];
12868 }
12869 }
12870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012872}
12873
12874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 * "getfsize({fname})" function
12876 */
12877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012878f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879{
12880 char_u *fname;
12881 struct stat st;
12882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
12887 if (mch_stat((char *)fname, &st) >= 0)
12888 {
12889 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012890 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012892 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012894
12895 /* non-perfect check for overflow */
12896 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12897 rettv->vval.v_number = -2;
12898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 }
12900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902}
12903
12904/*
12905 * "getftime({fname})" function
12906 */
12907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012908f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909{
12910 char_u *fname;
12911 struct stat st;
12912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914
12915 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012916 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919}
12920
12921/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012922 * "getftype({fname})" function
12923 */
12924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012925f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012926{
12927 char_u *fname;
12928 struct stat st;
12929 char_u *type = NULL;
12930 char *t;
12931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012935 if (mch_lstat((char *)fname, &st) >= 0)
12936 {
12937#ifdef S_ISREG
12938 if (S_ISREG(st.st_mode))
12939 t = "file";
12940 else if (S_ISDIR(st.st_mode))
12941 t = "dir";
12942# ifdef S_ISLNK
12943 else if (S_ISLNK(st.st_mode))
12944 t = "link";
12945# endif
12946# ifdef S_ISBLK
12947 else if (S_ISBLK(st.st_mode))
12948 t = "bdev";
12949# endif
12950# ifdef S_ISCHR
12951 else if (S_ISCHR(st.st_mode))
12952 t = "cdev";
12953# endif
12954# ifdef S_ISFIFO
12955 else if (S_ISFIFO(st.st_mode))
12956 t = "fifo";
12957# endif
12958# ifdef S_ISSOCK
12959 else if (S_ISSOCK(st.st_mode))
12960 t = "fifo";
12961# endif
12962 else
12963 t = "other";
12964#else
12965# ifdef S_IFMT
12966 switch (st.st_mode & S_IFMT)
12967 {
12968 case S_IFREG: t = "file"; break;
12969 case S_IFDIR: t = "dir"; break;
12970# ifdef S_IFLNK
12971 case S_IFLNK: t = "link"; break;
12972# endif
12973# ifdef S_IFBLK
12974 case S_IFBLK: t = "bdev"; break;
12975# endif
12976# ifdef S_IFCHR
12977 case S_IFCHR: t = "cdev"; break;
12978# endif
12979# ifdef S_IFIFO
12980 case S_IFIFO: t = "fifo"; break;
12981# endif
12982# ifdef S_IFSOCK
12983 case S_IFSOCK: t = "socket"; break;
12984# endif
12985 default: t = "other";
12986 }
12987# else
12988 if (mch_isdir(fname))
12989 t = "dir";
12990 else
12991 t = "file";
12992# endif
12993#endif
12994 type = vim_strsave((char_u *)t);
12995 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012996 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012997}
12998
12999/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013000 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001 */
13002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013003f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013004{
13005 linenr_T lnum;
13006 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013007 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013008
13009 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013010 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013011 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013012 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013013 retlist = FALSE;
13014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013015 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013016 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013017 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013018 retlist = TRUE;
13019 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013020
Bram Moolenaar342337a2005-07-21 21:11:17 +000013021 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013022}
13023
13024/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013025 * "getmatches()" function
13026 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013028f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013029{
13030#ifdef FEAT_SEARCH_EXTRA
13031 dict_T *dict;
13032 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013033 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013034
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013035 if (rettv_list_alloc(rettv) == OK)
13036 {
13037 while (cur != NULL)
13038 {
13039 dict = dict_alloc();
13040 if (dict == NULL)
13041 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013042 if (cur->match.regprog == NULL)
13043 {
13044 /* match added with matchaddpos() */
13045 for (i = 0; i < MAXPOSMATCH; ++i)
13046 {
13047 llpos_T *llpos;
13048 char buf[6];
13049 list_T *l;
13050
13051 llpos = &cur->pos.pos[i];
13052 if (llpos->lnum == 0)
13053 break;
13054 l = list_alloc();
13055 if (l == NULL)
13056 break;
13057 list_append_number(l, (varnumber_T)llpos->lnum);
13058 if (llpos->col > 0)
13059 {
13060 list_append_number(l, (varnumber_T)llpos->col);
13061 list_append_number(l, (varnumber_T)llpos->len);
13062 }
13063 sprintf(buf, "pos%d", i + 1);
13064 dict_add_list(dict, buf, l);
13065 }
13066 }
13067 else
13068 {
13069 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13070 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013071 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013072 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13073 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013074# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013075 if (cur->conceal_char)
13076 {
13077 char_u buf[MB_MAXBYTES + 1];
13078
13079 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13080 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13081 }
13082# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013083 list_append_dict(rettv->vval.v_list, dict);
13084 cur = cur->next;
13085 }
13086 }
13087#endif
13088}
13089
13090/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013091 * "getpid()" function
13092 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013094f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013095{
13096 rettv->vval.v_number = mch_get_pid();
13097}
13098
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013100getpos_both(
13101 typval_T *argvars,
13102 typval_T *rettv,
13103 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013104{
Bram Moolenaara5525202006-03-02 22:52:09 +000013105 pos_T *fp;
13106 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013107 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013108
13109 if (rettv_list_alloc(rettv) == OK)
13110 {
13111 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013112 if (getcurpos)
13113 fp = &curwin->w_cursor;
13114 else
13115 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013116 if (fnum != -1)
13117 list_append_number(l, (varnumber_T)fnum);
13118 else
13119 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013120 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13121 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013122 list_append_number(l, (fp != NULL)
13123 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013124 : (varnumber_T)0);
13125 list_append_number(l,
13126#ifdef FEAT_VIRTUALEDIT
13127 (fp != NULL) ? (varnumber_T)fp->coladd :
13128#endif
13129 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013130 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013131 {
13132 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013133 list_append_number(l, curwin->w_curswant == MAXCOL ?
13134 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013135 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013136 }
13137 else
13138 rettv->vval.v_number = FALSE;
13139}
13140
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013141
13142/*
13143 * "getcurpos()" function
13144 */
13145 static void
13146f_getcurpos(typval_T *argvars, typval_T *rettv)
13147{
13148 getpos_both(argvars, rettv, TRUE);
13149}
13150
13151/*
13152 * "getpos(string)" function
13153 */
13154 static void
13155f_getpos(typval_T *argvars, typval_T *rettv)
13156{
13157 getpos_both(argvars, rettv, FALSE);
13158}
13159
Bram Moolenaara5525202006-03-02 22:52:09 +000013160/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013161 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013162 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013164f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013165{
13166#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013167 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013168#endif
13169
Bram Moolenaar2641f772005-03-25 21:58:17 +000013170#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013171 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013172 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013173 wp = NULL;
13174 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13175 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013176 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013177 if (wp == NULL)
13178 return;
13179 }
13180
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013181 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013182 }
13183#endif
13184}
13185
13186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 * "getreg()" function
13188 */
13189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013190f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191{
13192 char_u *strregname;
13193 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013194 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013195 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013196 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200 strregname = get_tv_string_chk(&argvars[0]);
13201 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013202 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013203 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013204 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013205 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13206 return_list = get_tv_number_chk(&argvars[2], &error);
13207 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013210 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013211
13212 if (error)
13213 return;
13214
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 regname = (strregname == NULL ? '"' : *strregname);
13216 if (regname == 0)
13217 regname = '"';
13218
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013219 if (return_list)
13220 {
13221 rettv->v_type = VAR_LIST;
13222 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13223 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013224 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013225 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013226 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013227 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013228 }
13229 else
13230 {
13231 rettv->v_type = VAR_STRING;
13232 rettv->vval.v_string = get_reg_contents(regname,
13233 arg2 ? GREG_EXPR_SRC : 0);
13234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235}
13236
13237/*
13238 * "getregtype()" function
13239 */
13240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013241f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242{
13243 char_u *strregname;
13244 int regname;
13245 char_u buf[NUMBUFLEN + 2];
13246 long reglen = 0;
13247
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013248 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013249 {
13250 strregname = get_tv_string_chk(&argvars[0]);
13251 if (strregname == NULL) /* type error; errmsg already given */
13252 {
13253 rettv->v_type = VAR_STRING;
13254 rettv->vval.v_string = NULL;
13255 return;
13256 }
13257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 else
13259 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013260 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261
13262 regname = (strregname == NULL ? '"' : *strregname);
13263 if (regname == 0)
13264 regname = '"';
13265
13266 buf[0] = NUL;
13267 buf[1] = NUL;
13268 switch (get_reg_type(regname, &reglen))
13269 {
13270 case MLINE: buf[0] = 'V'; break;
13271 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 case MBLOCK:
13273 buf[0] = Ctrl_V;
13274 sprintf((char *)buf + 1, "%ld", reglen + 1);
13275 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->v_type = VAR_STRING;
13278 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279}
13280
13281/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013282 * "gettabvar()" function
13283 */
13284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013285f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013286{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013287 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013288 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013289 dictitem_T *v;
13290 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013291 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013292
13293 rettv->v_type = VAR_STRING;
13294 rettv->vval.v_string = NULL;
13295
13296 varname = get_tv_string_chk(&argvars[1]);
13297 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13298 if (tp != NULL && varname != NULL)
13299 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013300 /* Set tp to be our tabpage, temporarily. Also set the window to the
13301 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013302 if (switch_win(&oldcurwin, &oldtabpage,
13303 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013304 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013305 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013306 /* look up the variable */
13307 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13308 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13309 if (v != NULL)
13310 {
13311 copy_tv(&v->di_tv, rettv);
13312 done = TRUE;
13313 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013314 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013315
13316 /* restore previous notion of curwin */
13317 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013318 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013319
13320 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013321 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013322}
13323
13324/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013325 * "gettabwinvar()" function
13326 */
13327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013328f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013329{
13330 getwinvar(argvars, rettv, 1);
13331}
13332
13333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 * "getwinposx()" function
13335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013337f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340#ifdef FEAT_GUI
13341 if (gui.in_use)
13342 {
13343 int x, y;
13344
13345 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 }
13348#endif
13349}
13350
13351/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013352 * "win_findbuf()" function
13353 */
13354 static void
13355f_win_findbuf(typval_T *argvars, typval_T *rettv)
13356{
13357 if (rettv_list_alloc(rettv) != FAIL)
13358 win_findbuf(argvars, rettv->vval.v_list);
13359}
13360
13361/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013362 * "win_getid()" function
13363 */
13364 static void
13365f_win_getid(typval_T *argvars, typval_T *rettv)
13366{
13367 rettv->vval.v_number = win_getid(argvars);
13368}
13369
13370/*
13371 * "win_gotoid()" function
13372 */
13373 static void
13374f_win_gotoid(typval_T *argvars, typval_T *rettv)
13375{
13376 rettv->vval.v_number = win_gotoid(argvars);
13377}
13378
13379/*
13380 * "win_id2tabwin()" function
13381 */
13382 static void
13383f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13384{
13385 if (rettv_list_alloc(rettv) != FAIL)
13386 win_id2tabwin(argvars, rettv->vval.v_list);
13387}
13388
13389/*
13390 * "win_id2win()" function
13391 */
13392 static void
13393f_win_id2win(typval_T *argvars, typval_T *rettv)
13394{
13395 rettv->vval.v_number = win_id2win(argvars);
13396}
13397
13398/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 * "getwinposy()" function
13400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013402f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405#ifdef FEAT_GUI
13406 if (gui.in_use)
13407 {
13408 int x, y;
13409
13410 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013411 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 }
13413#endif
13414}
13415
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013416/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013417 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013418 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013419 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013420find_win_by_nr(
13421 typval_T *vp,
13422 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013423{
13424#ifdef FEAT_WINDOWS
13425 win_T *wp;
13426#endif
13427 int nr;
13428
13429 nr = get_tv_number_chk(vp, NULL);
13430
13431#ifdef FEAT_WINDOWS
13432 if (nr < 0)
13433 return NULL;
13434 if (nr == 0)
13435 return curwin;
13436
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013437 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13438 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013439 if (--nr <= 0)
13440 break;
13441 return wp;
13442#else
13443 if (nr == 0 || nr == 1)
13444 return curwin;
13445 return NULL;
13446#endif
13447}
13448
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013450 * Find window specified by "wvp" in tabpage "tvp".
13451 */
13452 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013453find_tabwin(
13454 typval_T *wvp, /* VAR_UNKNOWN for current window */
13455 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013456{
13457 win_T *wp = NULL;
13458 tabpage_T *tp = NULL;
13459 long n;
13460
13461 if (wvp->v_type != VAR_UNKNOWN)
13462 {
13463 if (tvp->v_type != VAR_UNKNOWN)
13464 {
13465 n = get_tv_number(tvp);
13466 if (n >= 0)
13467 tp = find_tabpage(n);
13468 }
13469 else
13470 tp = curtab;
13471
13472 if (tp != NULL)
13473 wp = find_win_by_nr(wvp, tp);
13474 }
13475 else
13476 wp = curwin;
13477
13478 return wp;
13479}
13480
13481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 * "getwinvar()" function
13483 */
13484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013485f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013487 getwinvar(argvars, rettv, 0);
13488}
13489
13490/*
13491 * getwinvar() and gettabwinvar()
13492 */
13493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013494getwinvar(
13495 typval_T *argvars,
13496 typval_T *rettv,
13497 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013498{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013499 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013501 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013502 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013503 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013504#ifdef FEAT_WINDOWS
13505 win_T *oldcurwin;
13506 tabpage_T *oldtabpage;
13507 int need_switch_win;
13508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013510#ifdef FEAT_WINDOWS
13511 if (off == 1)
13512 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13513 else
13514 tp = curtab;
13515#endif
13516 win = find_win_by_nr(&argvars[off], tp);
13517 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013518 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013520 rettv->v_type = VAR_STRING;
13521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522
13523 if (win != NULL && varname != NULL)
13524 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013525#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013526 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013527 * otherwise the window is not valid. Only do this when needed,
13528 * autocommands get blocked. */
13529 need_switch_win = !(tp == curtab && win == curwin);
13530 if (!need_switch_win
13531 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13532#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013533 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013534 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013535 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013536 if (get_option_tv(&varname, rettv, 1) == OK)
13537 done = TRUE;
13538 }
13539 else
13540 {
13541 /* Look up the variable. */
13542 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13543 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13544 varname, FALSE);
13545 if (v != NULL)
13546 {
13547 copy_tv(&v->di_tv, rettv);
13548 done = TRUE;
13549 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013552
Bram Moolenaarba117c22015-09-29 16:53:22 +020013553#ifdef FEAT_WINDOWS
13554 if (need_switch_win)
13555 /* restore previous notion of curwin */
13556 restore_win(oldcurwin, oldtabpage, TRUE);
13557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558 }
13559
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013560 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13561 /* use the default return value */
13562 copy_tv(&argvars[off + 2], rettv);
13563
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 --emsg_off;
13565}
13566
13567/*
13568 * "glob()" function
13569 */
13570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013571f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013573 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013575 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013577 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013578 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013580 if (argvars[1].v_type != VAR_UNKNOWN)
13581 {
13582 if (get_tv_number_chk(&argvars[1], &error))
13583 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013585 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013586 if (get_tv_number_chk(&argvars[2], &error))
13587 {
13588 rettv->v_type = VAR_LIST;
13589 rettv->vval.v_list = NULL;
13590 }
13591 if (argvars[3].v_type != VAR_UNKNOWN
13592 && get_tv_number_chk(&argvars[3], &error))
13593 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013594 }
13595 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013596 if (!error)
13597 {
13598 ExpandInit(&xpc);
13599 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013600 if (p_wic)
13601 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013602 if (rettv->v_type == VAR_STRING)
13603 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013604 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013605 else if (rettv_list_alloc(rettv) != FAIL)
13606 {
13607 int i;
13608
13609 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13610 NULL, options, WILD_ALL_KEEP);
13611 for (i = 0; i < xpc.xp_numfiles; i++)
13612 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13613
13614 ExpandCleanup(&xpc);
13615 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013616 }
13617 else
13618 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619}
13620
13621/*
13622 * "globpath()" function
13623 */
13624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013625f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013627 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013629 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013630 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013631 garray_T ga;
13632 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013634 /* When the optional second argument is non-zero, don't remove matches
13635 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013637 if (argvars[2].v_type != VAR_UNKNOWN)
13638 {
13639 if (get_tv_number_chk(&argvars[2], &error))
13640 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013641 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013642 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013643 if (get_tv_number_chk(&argvars[3], &error))
13644 {
13645 rettv->v_type = VAR_LIST;
13646 rettv->vval.v_list = NULL;
13647 }
13648 if (argvars[4].v_type != VAR_UNKNOWN
13649 && get_tv_number_chk(&argvars[4], &error))
13650 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013651 }
13652 }
13653 if (file != NULL && !error)
13654 {
13655 ga_init2(&ga, (int)sizeof(char_u *), 10);
13656 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13657 if (rettv->v_type == VAR_STRING)
13658 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13659 else if (rettv_list_alloc(rettv) != FAIL)
13660 for (i = 0; i < ga.ga_len; ++i)
13661 list_append_string(rettv->vval.v_list,
13662 ((char_u **)(ga.ga_data))[i], -1);
13663 ga_clear_strings(&ga);
13664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013665 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667}
13668
13669/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013670 * "glob2regpat()" function
13671 */
13672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013673f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013674{
13675 char_u *pat = get_tv_string_chk(&argvars[0]);
13676
13677 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013678 rettv->vval.v_string = (pat == NULL)
13679 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013680}
13681
13682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 * "has()" function
13684 */
13685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013686f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687{
13688 int i;
13689 char_u *name;
13690 int n = FALSE;
13691 static char *(has_list[]) =
13692 {
13693#ifdef AMIGA
13694 "amiga",
13695# ifdef FEAT_ARP
13696 "arp",
13697# endif
13698#endif
13699#ifdef __BEOS__
13700 "beos",
13701#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013702#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 "mac",
13704#endif
13705#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013706 "macunix", /* built with 'darwin' enabled */
13707#endif
13708#if defined(__APPLE__) && __APPLE__ == 1
13709 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711#ifdef __QNX__
13712 "qnx",
13713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714#ifdef UNIX
13715 "unix",
13716#endif
13717#ifdef VMS
13718 "vms",
13719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef WIN32
13721 "win32",
13722#endif
13723#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13724 "win32unix",
13725#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013726#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 "win64",
13728#endif
13729#ifdef EBCDIC
13730 "ebcdic",
13731#endif
13732#ifndef CASE_INSENSITIVE_FILENAME
13733 "fname_case",
13734#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013735#ifdef HAVE_ACL
13736 "acl",
13737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738#ifdef FEAT_ARABIC
13739 "arabic",
13740#endif
13741#ifdef FEAT_AUTOCMD
13742 "autocmd",
13743#endif
13744#ifdef FEAT_BEVAL
13745 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013746# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13747 "balloon_multiline",
13748# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#endif
13750#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13751 "builtin_terms",
13752# ifdef ALL_BUILTIN_TCAPS
13753 "all_builtin_terms",
13754# endif
13755#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013756#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13757 || defined(FEAT_GUI_W32) \
13758 || defined(FEAT_GUI_MOTIF))
13759 "browsefilter",
13760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761#ifdef FEAT_BYTEOFF
13762 "byte_offset",
13763#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013764#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013765 "channel",
13766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#ifdef FEAT_CINDENT
13768 "cindent",
13769#endif
13770#ifdef FEAT_CLIENTSERVER
13771 "clientserver",
13772#endif
13773#ifdef FEAT_CLIPBOARD
13774 "clipboard",
13775#endif
13776#ifdef FEAT_CMDL_COMPL
13777 "cmdline_compl",
13778#endif
13779#ifdef FEAT_CMDHIST
13780 "cmdline_hist",
13781#endif
13782#ifdef FEAT_COMMENTS
13783 "comments",
13784#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013785#ifdef FEAT_CONCEAL
13786 "conceal",
13787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788#ifdef FEAT_CRYPT
13789 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013790 "crypt-blowfish",
13791 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#endif
13793#ifdef FEAT_CSCOPE
13794 "cscope",
13795#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013796#ifdef FEAT_CURSORBIND
13797 "cursorbind",
13798#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013799#ifdef CURSOR_SHAPE
13800 "cursorshape",
13801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802#ifdef DEBUG
13803 "debug",
13804#endif
13805#ifdef FEAT_CON_DIALOG
13806 "dialog_con",
13807#endif
13808#ifdef FEAT_GUI_DIALOG
13809 "dialog_gui",
13810#endif
13811#ifdef FEAT_DIFF
13812 "diff",
13813#endif
13814#ifdef FEAT_DIGRAPHS
13815 "digraphs",
13816#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013817#ifdef FEAT_DIRECTX
13818 "directx",
13819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820#ifdef FEAT_DND
13821 "dnd",
13822#endif
13823#ifdef FEAT_EMACS_TAGS
13824 "emacs_tags",
13825#endif
13826 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013827 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828#ifdef FEAT_SEARCH_EXTRA
13829 "extra_search",
13830#endif
13831#ifdef FEAT_FKMAP
13832 "farsi",
13833#endif
13834#ifdef FEAT_SEARCHPATH
13835 "file_in_path",
13836#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013837#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013838 "filterpipe",
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef FEAT_FIND_ID
13841 "find_in_path",
13842#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013843#ifdef FEAT_FLOAT
13844 "float",
13845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846#ifdef FEAT_FOLDING
13847 "folding",
13848#endif
13849#ifdef FEAT_FOOTER
13850 "footer",
13851#endif
13852#if !defined(USE_SYSTEM) && defined(UNIX)
13853 "fork",
13854#endif
13855#ifdef FEAT_GETTEXT
13856 "gettext",
13857#endif
13858#ifdef FEAT_GUI
13859 "gui",
13860#endif
13861#ifdef FEAT_GUI_ATHENA
13862# ifdef FEAT_GUI_NEXTAW
13863 "gui_neXtaw",
13864# else
13865 "gui_athena",
13866# endif
13867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868#ifdef FEAT_GUI_GTK
13869 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013870# ifdef USE_GTK3
13871 "gui_gtk3",
13872# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013876#ifdef FEAT_GUI_GNOME
13877 "gui_gnome",
13878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879#ifdef FEAT_GUI_MAC
13880 "gui_mac",
13881#endif
13882#ifdef FEAT_GUI_MOTIF
13883 "gui_motif",
13884#endif
13885#ifdef FEAT_GUI_PHOTON
13886 "gui_photon",
13887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888#ifdef FEAT_GUI_W32
13889 "gui_win32",
13890#endif
13891#ifdef FEAT_HANGULIN
13892 "hangul_input",
13893#endif
13894#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13895 "iconv",
13896#endif
13897#ifdef FEAT_INS_EXPAND
13898 "insert_expand",
13899#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013900#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013901 "job",
13902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903#ifdef FEAT_JUMPLIST
13904 "jumplist",
13905#endif
13906#ifdef FEAT_KEYMAP
13907 "keymap",
13908#endif
13909#ifdef FEAT_LANGMAP
13910 "langmap",
13911#endif
13912#ifdef FEAT_LIBCALL
13913 "libcall",
13914#endif
13915#ifdef FEAT_LINEBREAK
13916 "linebreak",
13917#endif
13918#ifdef FEAT_LISP
13919 "lispindent",
13920#endif
13921#ifdef FEAT_LISTCMDS
13922 "listcmds",
13923#endif
13924#ifdef FEAT_LOCALMAP
13925 "localmap",
13926#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013927#ifdef FEAT_LUA
13928# ifndef DYNAMIC_LUA
13929 "lua",
13930# endif
13931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932#ifdef FEAT_MENU
13933 "menu",
13934#endif
13935#ifdef FEAT_SESSION
13936 "mksession",
13937#endif
13938#ifdef FEAT_MODIFY_FNAME
13939 "modify_fname",
13940#endif
13941#ifdef FEAT_MOUSE
13942 "mouse",
13943#endif
13944#ifdef FEAT_MOUSESHAPE
13945 "mouseshape",
13946#endif
13947#if defined(UNIX) || defined(VMS)
13948# ifdef FEAT_MOUSE_DEC
13949 "mouse_dec",
13950# endif
13951# ifdef FEAT_MOUSE_GPM
13952 "mouse_gpm",
13953# endif
13954# ifdef FEAT_MOUSE_JSB
13955 "mouse_jsbterm",
13956# endif
13957# ifdef FEAT_MOUSE_NET
13958 "mouse_netterm",
13959# endif
13960# ifdef FEAT_MOUSE_PTERM
13961 "mouse_pterm",
13962# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013963# ifdef FEAT_MOUSE_SGR
13964 "mouse_sgr",
13965# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013966# ifdef FEAT_SYSMOUSE
13967 "mouse_sysmouse",
13968# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013969# ifdef FEAT_MOUSE_URXVT
13970 "mouse_urxvt",
13971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972# ifdef FEAT_MOUSE_XTERM
13973 "mouse_xterm",
13974# endif
13975#endif
13976#ifdef FEAT_MBYTE
13977 "multi_byte",
13978#endif
13979#ifdef FEAT_MBYTE_IME
13980 "multi_byte_ime",
13981#endif
13982#ifdef FEAT_MULTI_LANG
13983 "multi_lang",
13984#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013985#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013986#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013987 "mzscheme",
13988#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990#ifdef FEAT_OLE
13991 "ole",
13992#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013993 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994#ifdef FEAT_PATH_EXTRA
13995 "path_extra",
13996#endif
13997#ifdef FEAT_PERL
13998#ifndef DYNAMIC_PERL
13999 "perl",
14000#endif
14001#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014002#ifdef FEAT_PERSISTENT_UNDO
14003 "persistent_undo",
14004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005#ifdef FEAT_PYTHON
14006#ifndef DYNAMIC_PYTHON
14007 "python",
14008#endif
14009#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014010#ifdef FEAT_PYTHON3
14011#ifndef DYNAMIC_PYTHON3
14012 "python3",
14013#endif
14014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015#ifdef FEAT_POSTSCRIPT
14016 "postscript",
14017#endif
14018#ifdef FEAT_PRINTER
14019 "printer",
14020#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014021#ifdef FEAT_PROFILE
14022 "profile",
14023#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014024#ifdef FEAT_RELTIME
14025 "reltime",
14026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027#ifdef FEAT_QUICKFIX
14028 "quickfix",
14029#endif
14030#ifdef FEAT_RIGHTLEFT
14031 "rightleft",
14032#endif
14033#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14034 "ruby",
14035#endif
14036#ifdef FEAT_SCROLLBIND
14037 "scrollbind",
14038#endif
14039#ifdef FEAT_CMDL_INFO
14040 "showcmd",
14041 "cmdline_info",
14042#endif
14043#ifdef FEAT_SIGNS
14044 "signs",
14045#endif
14046#ifdef FEAT_SMARTINDENT
14047 "smartindent",
14048#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014049#ifdef STARTUPTIME
14050 "startuptime",
14051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052#ifdef FEAT_STL_OPT
14053 "statusline",
14054#endif
14055#ifdef FEAT_SUN_WORKSHOP
14056 "sun_workshop",
14057#endif
14058#ifdef FEAT_NETBEANS_INTG
14059 "netbeans_intg",
14060#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014061#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014062 "spell",
14063#endif
14064#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 "syntax",
14066#endif
14067#if defined(USE_SYSTEM) || !defined(UNIX)
14068 "system",
14069#endif
14070#ifdef FEAT_TAG_BINS
14071 "tag_binary",
14072#endif
14073#ifdef FEAT_TAG_OLDSTATIC
14074 "tag_old_static",
14075#endif
14076#ifdef FEAT_TAG_ANYWHITE
14077 "tag_any_white",
14078#endif
14079#ifdef FEAT_TCL
14080# ifndef DYNAMIC_TCL
14081 "tcl",
14082# endif
14083#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014084#ifdef FEAT_TERMGUICOLORS
14085 "termguicolors",
14086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087#ifdef TERMINFO
14088 "terminfo",
14089#endif
14090#ifdef FEAT_TERMRESPONSE
14091 "termresponse",
14092#endif
14093#ifdef FEAT_TEXTOBJ
14094 "textobjects",
14095#endif
14096#ifdef HAVE_TGETENT
14097 "tgetent",
14098#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014099#ifdef FEAT_TIMERS
14100 "timers",
14101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102#ifdef FEAT_TITLE
14103 "title",
14104#endif
14105#ifdef FEAT_TOOLBAR
14106 "toolbar",
14107#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014108#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14109 "unnamedplus",
14110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111#ifdef FEAT_USR_CMDS
14112 "user-commands", /* was accidentally included in 5.4 */
14113 "user_commands",
14114#endif
14115#ifdef FEAT_VIMINFO
14116 "viminfo",
14117#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014118#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 "vertsplit",
14120#endif
14121#ifdef FEAT_VIRTUALEDIT
14122 "virtualedit",
14123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#ifdef FEAT_VISUALEXTRA
14126 "visualextra",
14127#endif
14128#ifdef FEAT_VREPLACE
14129 "vreplace",
14130#endif
14131#ifdef FEAT_WILDIGN
14132 "wildignore",
14133#endif
14134#ifdef FEAT_WILDMENU
14135 "wildmenu",
14136#endif
14137#ifdef FEAT_WINDOWS
14138 "windows",
14139#endif
14140#ifdef FEAT_WAK
14141 "winaltkeys",
14142#endif
14143#ifdef FEAT_WRITEBACKUP
14144 "writebackup",
14145#endif
14146#ifdef FEAT_XIM
14147 "xim",
14148#endif
14149#ifdef FEAT_XFONTSET
14150 "xfontset",
14151#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014152#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014153 "xpm",
14154 "xpm_w32", /* for backward compatibility */
14155#else
14156# if defined(HAVE_XPM)
14157 "xpm",
14158# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160#ifdef USE_XSMP
14161 "xsmp",
14162#endif
14163#ifdef USE_XSMP_INTERACT
14164 "xsmp_interact",
14165#endif
14166#ifdef FEAT_XCLIPBOARD
14167 "xterm_clipboard",
14168#endif
14169#ifdef FEAT_XTERM_SAVE
14170 "xterm_save",
14171#endif
14172#if defined(UNIX) && defined(FEAT_X11)
14173 "X11",
14174#endif
14175 NULL
14176 };
14177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014178 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 for (i = 0; has_list[i] != NULL; ++i)
14180 if (STRICMP(name, has_list[i]) == 0)
14181 {
14182 n = TRUE;
14183 break;
14184 }
14185
14186 if (n == FALSE)
14187 {
14188 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014189 {
14190 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014191 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014192 && vim_isdigit(name[6])
14193 && vim_isdigit(name[8])
14194 && vim_isdigit(name[10]))
14195 {
14196 int major = atoi((char *)name + 6);
14197 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014198
14199 /* Expect "patch-9.9.01234". */
14200 n = (major < VIM_VERSION_MAJOR
14201 || (major == VIM_VERSION_MAJOR
14202 && (minor < VIM_VERSION_MINOR
14203 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014204 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014205 }
14206 else
14207 n = has_patch(atoi((char *)name + 5));
14208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 else if (STRICMP(name, "vim_starting") == 0)
14210 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014211#ifdef FEAT_MBYTE
14212 else if (STRICMP(name, "multi_byte_encoding") == 0)
14213 n = has_mbyte;
14214#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014215#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14216 else if (STRICMP(name, "balloon_multiline") == 0)
14217 n = multiline_balloon_available();
14218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#ifdef DYNAMIC_TCL
14220 else if (STRICMP(name, "tcl") == 0)
14221 n = tcl_enabled(FALSE);
14222#endif
14223#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14224 else if (STRICMP(name, "iconv") == 0)
14225 n = iconv_enabled(FALSE);
14226#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014227#ifdef DYNAMIC_LUA
14228 else if (STRICMP(name, "lua") == 0)
14229 n = lua_enabled(FALSE);
14230#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014231#ifdef DYNAMIC_MZSCHEME
14232 else if (STRICMP(name, "mzscheme") == 0)
14233 n = mzscheme_enabled(FALSE);
14234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235#ifdef DYNAMIC_RUBY
14236 else if (STRICMP(name, "ruby") == 0)
14237 n = ruby_enabled(FALSE);
14238#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014239#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240#ifdef DYNAMIC_PYTHON
14241 else if (STRICMP(name, "python") == 0)
14242 n = python_enabled(FALSE);
14243#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014244#endif
14245#ifdef FEAT_PYTHON3
14246#ifdef DYNAMIC_PYTHON3
14247 else if (STRICMP(name, "python3") == 0)
14248 n = python3_enabled(FALSE);
14249#endif
14250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251#ifdef DYNAMIC_PERL
14252 else if (STRICMP(name, "perl") == 0)
14253 n = perl_enabled(FALSE);
14254#endif
14255#ifdef FEAT_GUI
14256 else if (STRICMP(name, "gui_running") == 0)
14257 n = (gui.in_use || gui.starting);
14258# ifdef FEAT_GUI_W32
14259 else if (STRICMP(name, "gui_win32s") == 0)
14260 n = gui_is_win32s();
14261# endif
14262# ifdef FEAT_BROWSE
14263 else if (STRICMP(name, "browse") == 0)
14264 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14265# endif
14266#endif
14267#ifdef FEAT_SYN_HL
14268 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014269 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270#endif
14271#if defined(WIN3264)
14272 else if (STRICMP(name, "win95") == 0)
14273 n = mch_windows95();
14274#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014275#ifdef FEAT_NETBEANS_INTG
14276 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014277 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279 }
14280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282}
14283
14284/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014285 * "has_key()" function
14286 */
14287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014289{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014290 if (argvars[0].v_type != VAR_DICT)
14291 {
14292 EMSG(_(e_dictreq));
14293 return;
14294 }
14295 if (argvars[0].vval.v_dict == NULL)
14296 return;
14297
14298 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014299 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014300}
14301
14302/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014303 * "haslocaldir()" function
14304 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014307{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014308 win_T *wp = NULL;
14309
14310 wp = find_tabwin(&argvars[0], &argvars[1]);
14311 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014312}
14313
14314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 * "hasmapto()" function
14316 */
14317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014318f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319{
14320 char_u *name;
14321 char_u *mode;
14322 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014323 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014326 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 mode = (char_u *)"nvo";
14328 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014331 if (argvars[2].v_type != VAR_UNKNOWN)
14332 abbr = get_tv_number(&argvars[2]);
14333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334
Bram Moolenaar2c932302006-03-18 21:42:09 +000014335 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014338 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339}
14340
14341/*
14342 * "histadd()" function
14343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014345f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346{
14347#ifdef FEAT_CMDHIST
14348 int histype;
14349 char_u *str;
14350 char_u buf[NUMBUFLEN];
14351#endif
14352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 if (check_restricted() || check_secure())
14355 return;
14356#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14358 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 if (histype >= 0)
14360 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 if (*str != NUL)
14363 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014364 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 return;
14368 }
14369 }
14370#endif
14371}
14372
14373/*
14374 * "histdel()" function
14375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014377f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378{
14379#ifdef FEAT_CMDHIST
14380 int n;
14381 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014382 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14385 if (str == NULL)
14386 n = 0;
14387 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014390 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 else
14395 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 get_tv_string_buf(&argvars[1], buf));
14398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399#endif
14400}
14401
14402/*
14403 * "histget()" function
14404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014406f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
14408#ifdef FEAT_CMDHIST
14409 int type;
14410 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014413 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14414 if (str == NULL)
14415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 {
14418 type = get_histtype(str);
14419 if (argvars[1].v_type == VAR_UNKNOWN)
14420 idx = get_history_idx(type);
14421 else
14422 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14423 /* -1 on type error */
14424 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430}
14431
14432/*
14433 * "histnr()" function
14434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014436f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437{
14438 int i;
14439
14440#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 char_u *history = get_tv_string_chk(&argvars[0]);
14442
14443 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 if (i >= HIST_CMD && i < HIST_COUNT)
14445 i = get_history_idx(i);
14446 else
14447#endif
14448 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450}
14451
14452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 * "highlightID(name)" function
14454 */
14455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014456f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459}
14460
14461/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014462 * "highlight_exists()" function
14463 */
14464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014465f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014466{
14467 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14468}
14469
14470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 * "hostname()" function
14472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 char_u hostname[256];
14477
14478 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->v_type = VAR_STRING;
14480 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481}
14482
14483/*
14484 * iconv() function
14485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014487f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488{
14489#ifdef FEAT_MBYTE
14490 char_u buf1[NUMBUFLEN];
14491 char_u buf2[NUMBUFLEN];
14492 char_u *from, *to, *str;
14493 vimconv_T vimconv;
14494#endif
14495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->v_type = VAR_STRING;
14497 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498
14499#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 str = get_tv_string(&argvars[0]);
14501 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14502 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 vimconv.vc_type = CONV_NONE;
14504 convert_setup(&vimconv, from, to);
14505
14506 /* If the encodings are equal, no conversion needed. */
14507 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014510 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511
14512 convert_setup(&vimconv, NULL, NULL);
14513 vim_free(from);
14514 vim_free(to);
14515#endif
14516}
14517
14518/*
14519 * "indent()" function
14520 */
14521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014522f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523{
14524 linenr_T lnum;
14525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531}
14532
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014533/*
14534 * "index()" function
14535 */
14536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014537f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014538{
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 list_T *l;
14540 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014541 long idx = 0;
14542 int ic = FALSE;
14543
14544 rettv->vval.v_number = -1;
14545 if (argvars[0].v_type != VAR_LIST)
14546 {
14547 EMSG(_(e_listreq));
14548 return;
14549 }
14550 l = argvars[0].vval.v_list;
14551 if (l != NULL)
14552 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014553 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014554 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014556 int error = FALSE;
14557
Bram Moolenaar758711c2005-02-02 23:11:38 +000014558 /* Start at specified item. Use the cached index that list_find()
14559 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014561 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 ic = get_tv_number_chk(&argvars[3], &error);
14564 if (error)
14565 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014567
Bram Moolenaar758711c2005-02-02 23:11:38 +000014568 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014569 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014570 {
14571 rettv->vval.v_number = idx;
14572 break;
14573 }
14574 }
14575}
14576
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577static int inputsecret_flag = 0;
14578
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014579static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014580
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014582 * This function is used by f_input() and f_inputdialog() functions. The third
14583 * argument to f_input() specifies the type of completion to use at the
14584 * prompt. The third argument to f_inputdialog() specifies the value to return
14585 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586 */
14587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014588get_user_input(
14589 typval_T *argvars,
14590 typval_T *rettv,
14591 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 char_u *p = NULL;
14595 int c;
14596 char_u buf[NUMBUFLEN];
14597 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014599 int xp_type = EXPAND_NOTHING;
14600 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014603 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604
14605#ifdef NO_CONSOLE_INPUT
14606 /* While starting up, there is no place to enter text. */
14607 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609#endif
14610
14611 cmd_silent = FALSE; /* Want to see the prompt. */
14612 if (prompt != NULL)
14613 {
14614 /* Only the part of the message after the last NL is considered as
14615 * prompt for the command line */
14616 p = vim_strrchr(prompt, '\n');
14617 if (p == NULL)
14618 p = prompt;
14619 else
14620 {
14621 ++p;
14622 c = *p;
14623 *p = NUL;
14624 msg_start();
14625 msg_clr_eos();
14626 msg_puts_attr(prompt, echo_attr);
14627 msg_didout = FALSE;
14628 msg_starthere();
14629 *p = c;
14630 }
14631 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 if (argvars[1].v_type != VAR_UNKNOWN)
14634 {
14635 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14636 if (defstr != NULL)
14637 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014639 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014640 {
14641 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014642 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014643 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014644
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014645 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014646 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014647
Bram Moolenaar4463f292005-09-25 22:20:24 +000014648 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14649 if (xp_name == NULL)
14650 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014651
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014652 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014653
Bram Moolenaar4463f292005-09-25 22:20:24 +000014654 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14655 &xp_arg) == FAIL)
14656 return;
14657 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014658 }
14659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014661 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014662 int save_ex_normal_busy = ex_normal_busy;
14663 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014665 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14666 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014667 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014668 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014669 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014670 && argvars[1].v_type != VAR_UNKNOWN
14671 && argvars[2].v_type != VAR_UNKNOWN)
14672 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14673 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014674
14675 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 /* since the user typed this, no need to wait for return */
14678 need_wait_return = FALSE;
14679 msg_didout = FALSE;
14680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014681 cmd_silent = cmd_silent_save;
14682}
14683
14684/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014685 * "input()" function
14686 * Also handles inputsecret() when inputsecret is set.
14687 */
14688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014689f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014690{
14691 get_user_input(argvars, rettv, FALSE);
14692}
14693
14694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 * "inputdialog()" function
14696 */
14697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014698f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699{
14700#if defined(FEAT_GUI_TEXTDIALOG)
14701 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14702 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14703 {
14704 char_u *message;
14705 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014708 message = get_tv_string_chk(&argvars[0]);
14709 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014710 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014711 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 else
14713 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014714 if (message != NULL && defstr != NULL
14715 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014716 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014717 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 else
14719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014720 if (message != NULL && defstr != NULL
14721 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014722 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014723 rettv->vval.v_string = vim_strsave(
14724 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014728 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 }
14730 else
14731#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014732 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733}
14734
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014735/*
14736 * "inputlist()" function
14737 */
14738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014739f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014740{
14741 listitem_T *li;
14742 int selected;
14743 int mouse_used;
14744
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014745#ifdef NO_CONSOLE_INPUT
14746 /* While starting up, there is no place to enter text. */
14747 if (no_console_input())
14748 return;
14749#endif
14750 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14751 {
14752 EMSG2(_(e_listarg), "inputlist()");
14753 return;
14754 }
14755
14756 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014757 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014758 lines_left = Rows; /* avoid more prompt */
14759 msg_scroll = TRUE;
14760 msg_clr_eos();
14761
14762 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14763 {
14764 msg_puts(get_tv_string(&li->li_tv));
14765 msg_putchar('\n');
14766 }
14767
14768 /* Ask for choice. */
14769 selected = prompt_for_number(&mouse_used);
14770 if (mouse_used)
14771 selected -= lines_left;
14772
14773 rettv->vval.v_number = selected;
14774}
14775
14776
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14778
14779/*
14780 * "inputrestore()" function
14781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014783f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784{
14785 if (ga_userinput.ga_len > 0)
14786 {
14787 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14789 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014790 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 }
14792 else if (p_verbose > 1)
14793 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014794 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 }
14797}
14798
14799/*
14800 * "inputsave()" function
14801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014805 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 if (ga_grow(&ga_userinput, 1) == OK)
14807 {
14808 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14809 + ga_userinput.ga_len);
14810 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014811 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812 }
14813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014814 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815}
14816
14817/*
14818 * "inputsecret()" function
14819 */
14820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
14823 ++cmdline_star;
14824 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 --cmdline_star;
14827 --inputsecret_flag;
14828}
14829
14830/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831 * "insert()" function
14832 */
14833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014834f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014835{
14836 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014837 listitem_T *item;
14838 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014840
14841 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014842 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014843 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014844 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014845 {
14846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 before = get_tv_number_chk(&argvars[2], &error);
14848 if (error)
14849 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014850
Bram Moolenaar758711c2005-02-02 23:11:38 +000014851 if (before == l->lv_len)
14852 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853 else
14854 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014855 item = list_find(l, before);
14856 if (item == NULL)
14857 {
14858 EMSGN(_(e_listidx), before);
14859 l = NULL;
14860 }
14861 }
14862 if (l != NULL)
14863 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014864 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014865 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014866 }
14867 }
14868}
14869
14870/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014871 * "invert(expr)" function
14872 */
14873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014874f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014875{
14876 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14877}
14878
14879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 * "isdirectory()" function
14881 */
14882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014883f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886}
14887
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014888/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014889 * "islocked()" function
14890 */
14891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014892f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014893{
14894 lval_T lv;
14895 char_u *end;
14896 dictitem_T *di;
14897
14898 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014899 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14900 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014901 if (end != NULL && lv.ll_name != NULL)
14902 {
14903 if (*end != NUL)
14904 EMSG(_(e_trailing));
14905 else
14906 {
14907 if (lv.ll_tv == NULL)
14908 {
14909 if (check_changedtick(lv.ll_name))
14910 rettv->vval.v_number = 1; /* always locked */
14911 else
14912 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014913 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014914 if (di != NULL)
14915 {
14916 /* Consider a variable locked when:
14917 * 1. the variable itself is locked
14918 * 2. the value of the variable is locked.
14919 * 3. the List or Dict value is locked.
14920 */
14921 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14922 || tv_islocked(&di->di_tv));
14923 }
14924 }
14925 }
14926 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014927 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014928 else if (lv.ll_newkey != NULL)
14929 EMSG2(_(e_dictkey), lv.ll_newkey);
14930 else if (lv.ll_list != NULL)
14931 /* List item. */
14932 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14933 else
14934 /* Dictionary item. */
14935 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14936 }
14937 }
14938
14939 clear_lval(&lv);
14940}
14941
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014942#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14943/*
14944 * "isnan()" function
14945 */
14946 static void
14947f_isnan(typval_T *argvars, typval_T *rettv)
14948{
14949 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14950 && isnan(argvars[0].vval.v_float);
14951}
14952#endif
14953
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014954static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955
14956/*
14957 * Turn a dict into a list:
14958 * "what" == 0: list of keys
14959 * "what" == 1: list of values
14960 * "what" == 2: list of items
14961 */
14962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014963dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014964{
Bram Moolenaar33570922005-01-25 22:26:29 +000014965 list_T *l2;
14966 dictitem_T *di;
14967 hashitem_T *hi;
14968 listitem_T *li;
14969 listitem_T *li2;
14970 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014971 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014972
Bram Moolenaar8c711452005-01-14 21:53:12 +000014973 if (argvars[0].v_type != VAR_DICT)
14974 {
14975 EMSG(_(e_dictreq));
14976 return;
14977 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014978 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014979 return;
14980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014982 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014983
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014984 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014985 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014987 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014988 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014989 --todo;
14990 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014991
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014992 li = listitem_alloc();
14993 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014994 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014995 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014996
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014997 if (what == 0)
14998 {
14999 /* keys() */
15000 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015001 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015002 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15003 }
15004 else if (what == 1)
15005 {
15006 /* values() */
15007 copy_tv(&di->di_tv, &li->li_tv);
15008 }
15009 else
15010 {
15011 /* items() */
15012 l2 = list_alloc();
15013 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015014 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015015 li->li_tv.vval.v_list = l2;
15016 if (l2 == NULL)
15017 break;
15018 ++l2->lv_refcount;
15019
15020 li2 = listitem_alloc();
15021 if (li2 == NULL)
15022 break;
15023 list_append(l2, li2);
15024 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015025 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015026 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15027
15028 li2 = listitem_alloc();
15029 if (li2 == NULL)
15030 break;
15031 list_append(l2, li2);
15032 copy_tv(&di->di_tv, &li2->li_tv);
15033 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015034 }
15035 }
15036}
15037
15038/*
15039 * "items(dict)" function
15040 */
15041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015042f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015043{
15044 dict_list(argvars, rettv, 2);
15045}
15046
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015047#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015048/*
15049 * Get the job from the argument.
15050 * Returns NULL if the job is invalid.
15051 */
15052 static job_T *
15053get_job_arg(typval_T *tv)
15054{
15055 job_T *job;
15056
15057 if (tv->v_type != VAR_JOB)
15058 {
15059 EMSG2(_(e_invarg2), get_tv_string(tv));
15060 return NULL;
15061 }
15062 job = tv->vval.v_job;
15063
15064 if (job == NULL)
15065 EMSG(_("E916: not a valid job"));
15066 return job;
15067}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015068
Bram Moolenaar835dc632016-02-07 14:27:38 +010015069/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015070 * "job_getchannel()" function
15071 */
15072 static void
15073f_job_getchannel(typval_T *argvars, typval_T *rettv)
15074{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015075 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015076
Bram Moolenaar65edff82016-02-21 16:40:11 +010015077 if (job != NULL)
15078 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015079 rettv->v_type = VAR_CHANNEL;
15080 rettv->vval.v_channel = job->jv_channel;
15081 if (job->jv_channel != NULL)
15082 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015083 }
15084}
15085
15086/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015087 * "job_info()" function
15088 */
15089 static void
15090f_job_info(typval_T *argvars, typval_T *rettv)
15091{
15092 job_T *job = get_job_arg(&argvars[0]);
15093
15094 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15095 job_info(job, rettv->vval.v_dict);
15096}
15097
15098/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015099 * "job_setoptions()" function
15100 */
15101 static void
15102f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15103{
15104 job_T *job = get_job_arg(&argvars[0]);
15105 jobopt_T opt;
15106
15107 if (job == NULL)
15108 return;
15109 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015110 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15111 job_set_options(job, &opt);
15112 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015113}
15114
15115/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015116 * "job_start()" function
15117 */
15118 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015119f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015120{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015121 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015122 if (check_restricted() || check_secure())
15123 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015124 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015125}
15126
15127/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015128 * "job_status()" function
15129 */
15130 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015131f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015132{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015133 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015134
Bram Moolenaar65edff82016-02-21 16:40:11 +010015135 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015136 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015137 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015138 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015139 }
15140}
15141
15142/*
15143 * "job_stop()" function
15144 */
15145 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015146f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015147{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015148 job_T *job = get_job_arg(&argvars[0]);
15149
15150 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015151 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015152}
15153#endif
15154
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015156 * "join()" function
15157 */
15158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015159f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015160{
15161 garray_T ga;
15162 char_u *sep;
15163
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015164 if (argvars[0].v_type != VAR_LIST)
15165 {
15166 EMSG(_(e_listreq));
15167 return;
15168 }
15169 if (argvars[0].vval.v_list == NULL)
15170 return;
15171 if (argvars[1].v_type == VAR_UNKNOWN)
15172 sep = (char_u *)" ";
15173 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015174 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015175
15176 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015177
15178 if (sep != NULL)
15179 {
15180 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015181 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 ga_append(&ga, NUL);
15183 rettv->vval.v_string = (char_u *)ga.ga_data;
15184 }
15185 else
15186 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015187}
15188
15189/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015190 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015191 */
15192 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015193f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015194{
15195 js_read_T reader;
15196
15197 reader.js_buf = get_tv_string(&argvars[0]);
15198 reader.js_fill = NULL;
15199 reader.js_used = 0;
15200 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15201 EMSG(_(e_invarg));
15202}
15203
15204/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015205 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015206 */
15207 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015208f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015209{
15210 rettv->v_type = VAR_STRING;
15211 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15212}
15213
15214/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015215 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015216 */
15217 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015218f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015219{
15220 js_read_T reader;
15221
15222 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015223 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015224 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015225 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015226 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015227}
15228
15229/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015230 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015231 */
15232 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015233f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015234{
15235 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015236 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015237}
15238
15239/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 * "keys()" function
15241 */
15242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015243f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244{
15245 dict_list(argvars, rettv, 0);
15246}
15247
15248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 * "last_buffer_nr()" function.
15250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015252f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253{
15254 int n = 0;
15255 buf_T *buf;
15256
15257 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15258 if (n < buf->b_fnum)
15259 n = buf->b_fnum;
15260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015261 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015262}
15263
15264/*
15265 * "len()" function
15266 */
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015269{
15270 switch (argvars[0].v_type)
15271 {
15272 case VAR_STRING:
15273 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015274 rettv->vval.v_number = (varnumber_T)STRLEN(
15275 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276 break;
15277 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015280 case VAR_DICT:
15281 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15282 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015283 case VAR_UNKNOWN:
15284 case VAR_SPECIAL:
15285 case VAR_FLOAT:
15286 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015287 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015288 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015289 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015290 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015291 break;
15292 }
15293}
15294
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015295static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015296
15297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015298libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015299{
15300#ifdef FEAT_LIBCALL
15301 char_u *string_in;
15302 char_u **string_result;
15303 int nr_result;
15304#endif
15305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015307 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309
15310 if (check_restricted() || check_secure())
15311 return;
15312
15313#ifdef FEAT_LIBCALL
15314 /* The first two args must be strings, otherwise its meaningless */
15315 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15316 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015317 string_in = NULL;
15318 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015319 string_in = argvars[2].vval.v_string;
15320 if (type == VAR_NUMBER)
15321 string_result = NULL;
15322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015323 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015324 if (mch_libcall(argvars[0].vval.v_string,
15325 argvars[1].vval.v_string,
15326 string_in,
15327 argvars[2].vval.v_number,
15328 string_result,
15329 &nr_result) == OK
15330 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015331 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015332 }
15333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334}
15335
15336/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015337 * "libcall()" function
15338 */
15339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015340f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015341{
15342 libcall_common(argvars, rettv, VAR_STRING);
15343}
15344
15345/*
15346 * "libcallnr()" function
15347 */
15348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015349f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350{
15351 libcall_common(argvars, rettv, VAR_NUMBER);
15352}
15353
15354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 * "line(string)" function
15356 */
15357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015358f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359{
15360 linenr_T lnum = 0;
15361 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015362 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015364 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 if (fp != NULL)
15366 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368}
15369
15370/*
15371 * "line2byte(lnum)" function
15372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015374f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375{
15376#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378#else
15379 linenr_T lnum;
15380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15386 if (rettv->vval.v_number >= 0)
15387 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388#endif
15389}
15390
15391/*
15392 * "lispindent(lnum)" function
15393 */
15394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015395f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396{
15397#ifdef FEAT_LISP
15398 pos_T pos;
15399 linenr_T lnum;
15400
15401 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15404 {
15405 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 curwin->w_cursor = pos;
15408 }
15409 else
15410#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412}
15413
15414/*
15415 * "localtime()" function
15416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015418f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421}
15422
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015423static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424
15425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015426get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427{
15428 char_u *keys;
15429 char_u *which;
15430 char_u buf[NUMBUFLEN];
15431 char_u *keys_buf = NULL;
15432 char_u *rhs;
15433 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015434 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015435 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015436 mapblock_T *mp;
15437 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
15439 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 rettv->v_type = VAR_STRING;
15441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 if (*keys == NUL)
15445 return;
15446
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015449 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015451 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015452 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015453 if (argvars[3].v_type != VAR_UNKNOWN)
15454 get_dict = get_tv_number(&argvars[3]);
15455 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 else
15458 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015459 if (which == NULL)
15460 return;
15461
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 mode = get_map_mode(&which, 0);
15463
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015464 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015465 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015467
15468 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015470 /* Return a string. */
15471 if (rhs != NULL)
15472 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473
Bram Moolenaarbd743252010-10-20 21:23:33 +020015474 }
15475 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15476 {
15477 /* Return a dictionary. */
15478 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15479 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15480 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481
Bram Moolenaarbd743252010-10-20 21:23:33 +020015482 dict_add_nr_str(dict, "lhs", 0L, lhs);
15483 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15484 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15485 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15486 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15487 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15488 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015489 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015490 dict_add_nr_str(dict, "mode", 0L, mapmode);
15491
15492 vim_free(lhs);
15493 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 }
15495}
15496
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015497#ifdef FEAT_FLOAT
15498/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015499 * "log()" function
15500 */
15501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015502f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015503{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015504 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015505
15506 rettv->v_type = VAR_FLOAT;
15507 if (get_float_arg(argvars, &f) == OK)
15508 rettv->vval.v_float = log(f);
15509 else
15510 rettv->vval.v_float = 0.0;
15511}
15512
15513/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015514 * "log10()" function
15515 */
15516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015517f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015518{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015519 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015520
15521 rettv->v_type = VAR_FLOAT;
15522 if (get_float_arg(argvars, &f) == OK)
15523 rettv->vval.v_float = log10(f);
15524 else
15525 rettv->vval.v_float = 0.0;
15526}
15527#endif
15528
Bram Moolenaar1dced572012-04-05 16:54:08 +020015529#ifdef FEAT_LUA
15530/*
15531 * "luaeval()" function
15532 */
15533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015534f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015535{
15536 char_u *str;
15537 char_u buf[NUMBUFLEN];
15538
15539 str = get_tv_string_buf(&argvars[0], buf);
15540 do_luaeval(str, argvars + 1, rettv);
15541}
15542#endif
15543
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015545 * "map()" function
15546 */
15547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015548f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015549{
15550 filter_map(argvars, rettv, TRUE);
15551}
15552
15553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015554 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555 */
15556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015557f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015559 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560}
15561
15562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015563 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564 */
15565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015566f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015568 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569}
15570
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015571static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572
15573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015574find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015576 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015577 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015578 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 char_u *pat;
15580 regmatch_T regmatch;
15581 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015582 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 char_u *save_cpo;
15584 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015585 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015586 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015587 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015588 list_T *l = NULL;
15589 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015590 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015591 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592
15593 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15594 save_cpo = p_cpo;
15595 p_cpo = (char_u *)"";
15596
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015598 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015600 /* type 3: return empty list when there are no matches.
15601 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015602 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015603 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015604 if (type == 4
15605 && (list_append_string(rettv->vval.v_list,
15606 (char_u *)"", 0) == FAIL
15607 || list_append_number(rettv->vval.v_list,
15608 (varnumber_T)-1) == FAIL
15609 || list_append_number(rettv->vval.v_list,
15610 (varnumber_T)-1) == FAIL
15611 || list_append_number(rettv->vval.v_list,
15612 (varnumber_T)-1) == FAIL))
15613 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015614 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015615 rettv->vval.v_list = NULL;
15616 goto theend;
15617 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015618 }
15619 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015621 rettv->v_type = VAR_STRING;
15622 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015625 if (argvars[0].v_type == VAR_LIST)
15626 {
15627 if ((l = argvars[0].vval.v_list) == NULL)
15628 goto theend;
15629 li = l->lv_first;
15630 }
15631 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015632 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015633 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015634 len = (long)STRLEN(str);
15635 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15638 if (pat == NULL)
15639 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015640
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015641 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015643 int error = FALSE;
15644
15645 start = get_tv_number_chk(&argvars[2], &error);
15646 if (error)
15647 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015648 if (l != NULL)
15649 {
15650 li = list_find(l, start);
15651 if (li == NULL)
15652 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015653 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015654 }
15655 else
15656 {
15657 if (start < 0)
15658 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015659 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015660 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015661 /* When "count" argument is there ignore matches before "start",
15662 * otherwise skip part of the string. Differs when pattern is "^"
15663 * or "\<". */
15664 if (argvars[3].v_type != VAR_UNKNOWN)
15665 startcol = start;
15666 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015667 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015668 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015669 len -= start;
15670 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015672
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015673 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 nth = get_tv_number_chk(&argvars[3], &error);
15675 if (error)
15676 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 }
15678
15679 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15680 if (regmatch.regprog != NULL)
15681 {
15682 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015683
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015684 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015685 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686 if (l != NULL)
15687 {
15688 if (li == NULL)
15689 {
15690 match = FALSE;
15691 break;
15692 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015693 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015694 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015695 if (str == NULL)
15696 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015697 }
15698
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015699 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015700
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015701 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015702 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015703 if (l == NULL && !match)
15704 break;
15705
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015706 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015707 if (l != NULL)
15708 {
15709 li = li->li_next;
15710 ++idx;
15711 }
15712 else
15713 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015714#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015715 startcol = (colnr_T)(regmatch.startp[0]
15716 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015717#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015718 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015719#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015720 if (startcol > (colnr_T)len
15721 || str + startcol <= regmatch.startp[0])
15722 {
15723 match = FALSE;
15724 break;
15725 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015726 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015727 }
15728
15729 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015731 if (type == 4)
15732 {
15733 listitem_T *li1 = rettv->vval.v_list->lv_first;
15734 listitem_T *li2 = li1->li_next;
15735 listitem_T *li3 = li2->li_next;
15736 listitem_T *li4 = li3->li_next;
15737
15738 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15739 (int)(regmatch.endp[0] - regmatch.startp[0]));
15740 li3->li_tv.vval.v_number =
15741 (varnumber_T)(regmatch.startp[0] - expr);
15742 li4->li_tv.vval.v_number =
15743 (varnumber_T)(regmatch.endp[0] - expr);
15744 if (l != NULL)
15745 li2->li_tv.vval.v_number = (varnumber_T)idx;
15746 }
15747 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015748 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015749 int i;
15750
15751 /* return list with matched string and submatches */
15752 for (i = 0; i < NSUBEXP; ++i)
15753 {
15754 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015755 {
15756 if (list_append_string(rettv->vval.v_list,
15757 (char_u *)"", 0) == FAIL)
15758 break;
15759 }
15760 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015761 regmatch.startp[i],
15762 (int)(regmatch.endp[i] - regmatch.startp[i]))
15763 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015764 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015765 }
15766 }
15767 else if (type == 2)
15768 {
15769 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770 if (l != NULL)
15771 copy_tv(&li->li_tv, rettv);
15772 else
15773 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015775 }
15776 else if (l != NULL)
15777 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 else
15779 {
15780 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015781 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 (varnumber_T)(regmatch.startp[0] - str);
15783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015786 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015787 }
15788 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015789 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 }
15791
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015792 if (type == 4 && l == NULL)
15793 /* matchstrpos() without a list: drop the second item. */
15794 listitem_remove(rettv->vval.v_list,
15795 rettv->vval.v_list->lv_first->li_next);
15796
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015798 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 p_cpo = save_cpo;
15800}
15801
15802/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015803 * "match()" function
15804 */
15805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015806f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015807{
15808 find_some_match(argvars, rettv, 1);
15809}
15810
15811/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015812 * "matchadd()" function
15813 */
15814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015815f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015816{
15817#ifdef FEAT_SEARCH_EXTRA
15818 char_u buf[NUMBUFLEN];
15819 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15820 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15821 int prio = 10; /* default priority */
15822 int id = -1;
15823 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015824 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015825
15826 rettv->vval.v_number = -1;
15827
15828 if (grp == NULL || pat == NULL)
15829 return;
15830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015831 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015832 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015833 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015834 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015835 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015836 if (argvars[4].v_type != VAR_UNKNOWN)
15837 {
15838 if (argvars[4].v_type != VAR_DICT)
15839 {
15840 EMSG(_(e_dictreq));
15841 return;
15842 }
15843 if (dict_find(argvars[4].vval.v_dict,
15844 (char_u *)"conceal", -1) != NULL)
15845 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15846 (char_u *)"conceal", FALSE);
15847 }
15848 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015849 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015850 if (error == TRUE)
15851 return;
15852 if (id >= 1 && id <= 3)
15853 {
15854 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15855 return;
15856 }
15857
Bram Moolenaar6561d522015-07-21 15:48:27 +020015858 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15859 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015860#endif
15861}
15862
15863/*
15864 * "matchaddpos()" function
15865 */
15866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015867f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015868{
15869#ifdef FEAT_SEARCH_EXTRA
15870 char_u buf[NUMBUFLEN];
15871 char_u *group;
15872 int prio = 10;
15873 int id = -1;
15874 int error = FALSE;
15875 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015876 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015877
15878 rettv->vval.v_number = -1;
15879
15880 group = get_tv_string_buf_chk(&argvars[0], buf);
15881 if (group == NULL)
15882 return;
15883
15884 if (argvars[1].v_type != VAR_LIST)
15885 {
15886 EMSG2(_(e_listarg), "matchaddpos()");
15887 return;
15888 }
15889 l = argvars[1].vval.v_list;
15890 if (l == NULL)
15891 return;
15892
15893 if (argvars[2].v_type != VAR_UNKNOWN)
15894 {
15895 prio = get_tv_number_chk(&argvars[2], &error);
15896 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015897 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015898 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015899 if (argvars[4].v_type != VAR_UNKNOWN)
15900 {
15901 if (argvars[4].v_type != VAR_DICT)
15902 {
15903 EMSG(_(e_dictreq));
15904 return;
15905 }
15906 if (dict_find(argvars[4].vval.v_dict,
15907 (char_u *)"conceal", -1) != NULL)
15908 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15909 (char_u *)"conceal", FALSE);
15910 }
15911 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015912 }
15913 if (error == TRUE)
15914 return;
15915
15916 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15917 if (id == 1 || id == 2)
15918 {
15919 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15920 return;
15921 }
15922
Bram Moolenaar6561d522015-07-21 15:48:27 +020015923 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15924 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015925#endif
15926}
15927
15928/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015929 * "matcharg()" function
15930 */
15931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015932f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015933{
15934 if (rettv_list_alloc(rettv) == OK)
15935 {
15936#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015937 int id = get_tv_number(&argvars[0]);
15938 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015939
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015940 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015941 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015942 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15943 {
15944 list_append_string(rettv->vval.v_list,
15945 syn_id2name(m->hlg_id), -1);
15946 list_append_string(rettv->vval.v_list, m->pattern, -1);
15947 }
15948 else
15949 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015950 list_append_string(rettv->vval.v_list, NULL, -1);
15951 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015952 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015953 }
15954#endif
15955 }
15956}
15957
15958/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015959 * "matchdelete()" function
15960 */
15961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015962f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015963{
15964#ifdef FEAT_SEARCH_EXTRA
15965 rettv->vval.v_number = match_delete(curwin,
15966 (int)get_tv_number(&argvars[0]), TRUE);
15967#endif
15968}
15969
15970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015971 * "matchend()" function
15972 */
15973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015974f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015975{
15976 find_some_match(argvars, rettv, 0);
15977}
15978
15979/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015980 * "matchlist()" function
15981 */
15982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015983f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015984{
15985 find_some_match(argvars, rettv, 3);
15986}
15987
15988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015989 * "matchstr()" function
15990 */
15991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015992f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993{
15994 find_some_match(argvars, rettv, 2);
15995}
15996
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015997/*
15998 * "matchstrpos()" function
15999 */
16000 static void
16001f_matchstrpos(typval_T *argvars, typval_T *rettv)
16002{
16003 find_some_match(argvars, rettv, 4);
16004}
16005
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016006static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016007
16008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016009max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016010{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016011 long n = 0;
16012 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016013 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016014
16015 if (argvars[0].v_type == VAR_LIST)
16016 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016017 list_T *l;
16018 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016019
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016020 l = argvars[0].vval.v_list;
16021 if (l != NULL)
16022 {
16023 li = l->lv_first;
16024 if (li != NULL)
16025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016027 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016028 {
16029 li = li->li_next;
16030 if (li == NULL)
16031 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016032 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016033 if (domax ? i > n : i < n)
16034 n = i;
16035 }
16036 }
16037 }
16038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016039 else if (argvars[0].v_type == VAR_DICT)
16040 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016041 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016042 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016043 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016044 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016045
16046 d = argvars[0].vval.v_dict;
16047 if (d != NULL)
16048 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016049 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016050 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016051 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016052 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016053 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016054 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016055 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016056 if (first)
16057 {
16058 n = i;
16059 first = FALSE;
16060 }
16061 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016062 n = i;
16063 }
16064 }
16065 }
16066 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016067 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016068 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016069 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016070}
16071
16072/*
16073 * "max()" function
16074 */
16075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016076f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016077{
16078 max_min(argvars, rettv, TRUE);
16079}
16080
16081/*
16082 * "min()" function
16083 */
16084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016085f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016086{
16087 max_min(argvars, rettv, FALSE);
16088}
16089
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016090static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016091
16092/*
16093 * Create the directory in which "dir" is located, and higher levels when
16094 * needed.
16095 */
16096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016097mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016098{
16099 char_u *p;
16100 char_u *updir;
16101 int r = FAIL;
16102
16103 /* Get end of directory name in "dir".
16104 * We're done when it's "/" or "c:/". */
16105 p = gettail_sep(dir);
16106 if (p <= get_past_head(dir))
16107 return OK;
16108
16109 /* If the directory exists we're done. Otherwise: create it.*/
16110 updir = vim_strnsave(dir, (int)(p - dir));
16111 if (updir == NULL)
16112 return FAIL;
16113 if (mch_isdir(updir))
16114 r = OK;
16115 else if (mkdir_recurse(updir, prot) == OK)
16116 r = vim_mkdir_emsg(updir, prot);
16117 vim_free(updir);
16118 return r;
16119}
16120
16121#ifdef vim_mkdir
16122/*
16123 * "mkdir()" function
16124 */
16125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016126f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016127{
16128 char_u *dir;
16129 char_u buf[NUMBUFLEN];
16130 int prot = 0755;
16131
16132 rettv->vval.v_number = FAIL;
16133 if (check_restricted() || check_secure())
16134 return;
16135
16136 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016137 if (*dir == NUL)
16138 rettv->vval.v_number = FAIL;
16139 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016140 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016141 if (*gettail(dir) == NUL)
16142 /* remove trailing slashes */
16143 *gettail_sep(dir) = NUL;
16144
16145 if (argvars[1].v_type != VAR_UNKNOWN)
16146 {
16147 if (argvars[2].v_type != VAR_UNKNOWN)
16148 prot = get_tv_number_chk(&argvars[2], NULL);
16149 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16150 mkdir_recurse(dir, prot);
16151 }
16152 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016153 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016154}
16155#endif
16156
Bram Moolenaar0d660222005-01-07 21:51:51 +000016157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 * "mode()" function
16159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016161f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016162{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016163 char_u buf[3];
16164
16165 buf[1] = NUL;
16166 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016167
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 if (VIsual_active)
16169 {
16170 if (VIsual_select)
16171 buf[0] = VIsual_mode + 's' - 'v';
16172 else
16173 buf[0] = VIsual_mode;
16174 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016175 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016176 || State == CONFIRM)
16177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016179 if (State == ASKMORE)
16180 buf[1] = 'm';
16181 else if (State == CONFIRM)
16182 buf[1] = '?';
16183 }
16184 else if (State == EXTERNCMD)
16185 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 else if (State & INSERT)
16187 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016188#ifdef FEAT_VREPLACE
16189 if (State & VREPLACE_FLAG)
16190 {
16191 buf[0] = 'R';
16192 buf[1] = 'v';
16193 }
16194 else
16195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 if (State & REPLACE_FLAG)
16197 buf[0] = 'R';
16198 else
16199 buf[0] = 'i';
16200 }
16201 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016204 if (exmode_active)
16205 buf[1] = 'v';
16206 }
16207 else if (exmode_active)
16208 {
16209 buf[0] = 'c';
16210 buf[1] = 'e';
16211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016215 if (finish_op)
16216 buf[1] = 'o';
16217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016219 /* Clear out the minor mode when the argument is not a non-zero number or
16220 * non-empty string. */
16221 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016222 buf[1] = NUL;
16223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016224 rettv->vval.v_string = vim_strsave(buf);
16225 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226}
16227
Bram Moolenaar429fa852013-04-15 12:27:36 +020016228#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016229/*
16230 * "mzeval()" function
16231 */
16232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016233f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016234{
16235 char_u *str;
16236 char_u buf[NUMBUFLEN];
16237
16238 str = get_tv_string_buf(&argvars[0], buf);
16239 do_mzeval(str, rettv);
16240}
Bram Moolenaar75676462013-01-30 14:55:42 +010016241
16242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016244{
16245 typval_T argvars[3];
16246
16247 argvars[0].v_type = VAR_STRING;
16248 argvars[0].vval.v_string = name;
16249 copy_tv(args, &argvars[1]);
16250 argvars[2].v_type = VAR_UNKNOWN;
16251 f_call(argvars, rettv);
16252 clear_tv(&argvars[1]);
16253}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016254#endif
16255
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016257 * "nextnonblank()" function
16258 */
16259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016260f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261{
16262 linenr_T lnum;
16263
16264 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016266 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267 {
16268 lnum = 0;
16269 break;
16270 }
16271 if (*skipwhite(ml_get(lnum)) != NUL)
16272 break;
16273 }
16274 rettv->vval.v_number = lnum;
16275}
16276
16277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 * "nr2char()" function
16279 */
16280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016281f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282{
16283 char_u buf[NUMBUFLEN];
16284
16285#ifdef FEAT_MBYTE
16286 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016287 {
16288 int utf8 = 0;
16289
16290 if (argvars[1].v_type != VAR_UNKNOWN)
16291 utf8 = get_tv_number_chk(&argvars[1], NULL);
16292 if (utf8)
16293 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16294 else
16295 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297 else
16298#endif
16299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016300 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 buf[1] = NUL;
16302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016303 rettv->v_type = VAR_STRING;
16304 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016305}
16306
16307/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016308 * "or(expr, expr)" function
16309 */
16310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016311f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016312{
16313 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16314 | get_tv_number_chk(&argvars[1], NULL);
16315}
16316
16317/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016318 * "pathshorten()" function
16319 */
16320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016321f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016322{
16323 char_u *p;
16324
16325 rettv->v_type = VAR_STRING;
16326 p = get_tv_string_chk(&argvars[0]);
16327 if (p == NULL)
16328 rettv->vval.v_string = NULL;
16329 else
16330 {
16331 p = vim_strsave(p);
16332 rettv->vval.v_string = p;
16333 if (p != NULL)
16334 shorten_dir(p);
16335 }
16336}
16337
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016338#ifdef FEAT_PERL
16339/*
16340 * "perleval()" function
16341 */
16342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016343f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016344{
16345 char_u *str;
16346 char_u buf[NUMBUFLEN];
16347
16348 str = get_tv_string_buf(&argvars[0], buf);
16349 do_perleval(str, rettv);
16350}
16351#endif
16352
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016353#ifdef FEAT_FLOAT
16354/*
16355 * "pow()" function
16356 */
16357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016358f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016359{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016360 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016361
16362 rettv->v_type = VAR_FLOAT;
16363 if (get_float_arg(argvars, &fx) == OK
16364 && get_float_arg(&argvars[1], &fy) == OK)
16365 rettv->vval.v_float = pow(fx, fy);
16366 else
16367 rettv->vval.v_float = 0.0;
16368}
16369#endif
16370
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016371/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 * "prevnonblank()" function
16373 */
16374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016375f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376{
16377 linenr_T lnum;
16378
16379 lnum = get_tv_lnum(argvars);
16380 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16381 lnum = 0;
16382 else
16383 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16384 --lnum;
16385 rettv->vval.v_number = lnum;
16386}
16387
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016388/* This dummy va_list is here because:
16389 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16390 * - locally in the function results in a "used before set" warning
16391 * - using va_start() to initialize it gives "function with fixed args" error */
16392static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016393
Bram Moolenaar8c711452005-01-14 21:53:12 +000016394/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016395 * "printf()" function
16396 */
16397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016398f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016399{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016400 char_u buf[NUMBUFLEN];
16401 int len;
16402 char_u *s;
16403 int saved_did_emsg = did_emsg;
16404 char *fmt;
16405
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016406 rettv->v_type = VAR_STRING;
16407 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016408
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016409 /* Get the required length, allocate the buffer and do it for real. */
16410 did_emsg = FALSE;
16411 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16412 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16413 if (!did_emsg)
16414 {
16415 s = alloc(len + 1);
16416 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016417 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016418 rettv->vval.v_string = s;
16419 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016420 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016421 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016422 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016423}
16424
16425/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016426 * "pumvisible()" function
16427 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016429f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016430{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016431#ifdef FEAT_INS_EXPAND
16432 if (pum_visible())
16433 rettv->vval.v_number = 1;
16434#endif
16435}
16436
Bram Moolenaardb913952012-06-29 12:54:53 +020016437#ifdef FEAT_PYTHON3
16438/*
16439 * "py3eval()" function
16440 */
16441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016442f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016443{
16444 char_u *str;
16445 char_u buf[NUMBUFLEN];
16446
16447 str = get_tv_string_buf(&argvars[0], buf);
16448 do_py3eval(str, rettv);
16449}
16450#endif
16451
16452#ifdef FEAT_PYTHON
16453/*
16454 * "pyeval()" function
16455 */
16456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016458{
16459 char_u *str;
16460 char_u buf[NUMBUFLEN];
16461
16462 str = get_tv_string_buf(&argvars[0], buf);
16463 do_pyeval(str, rettv);
16464}
16465#endif
16466
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016467/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016468 * "range()" function
16469 */
16470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016471f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016472{
16473 long start;
16474 long end;
16475 long stride = 1;
16476 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016478
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016480 if (argvars[1].v_type == VAR_UNKNOWN)
16481 {
16482 end = start - 1;
16483 start = 0;
16484 }
16485 else
16486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016489 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016490 }
16491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016492 if (error)
16493 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016494 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016495 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016496 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016497 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016498 else
16499 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016500 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016501 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016502 if (list_append_number(rettv->vval.v_list,
16503 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016504 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016505 }
16506}
16507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016508/*
16509 * "readfile()" function
16510 */
16511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016512f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016513{
16514 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016515 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016516 char_u *fname;
16517 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016518 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16519 int io_size = sizeof(buf);
16520 int readlen; /* size of last fread() */
16521 char_u *prev = NULL; /* previously read bytes, if any */
16522 long prevlen = 0; /* length of data in prev */
16523 long prevsize = 0; /* size of prev buffer */
16524 long maxline = MAXLNUM;
16525 long cnt = 0;
16526 char_u *p; /* position in buf */
16527 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016528
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016529 if (argvars[1].v_type != VAR_UNKNOWN)
16530 {
16531 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16532 binary = TRUE;
16533 if (argvars[2].v_type != VAR_UNKNOWN)
16534 maxline = get_tv_number(&argvars[2]);
16535 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016536
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016537 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016538 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016539
16540 /* Always open the file in binary mode, library functions have a mind of
16541 * their own about CR-LF conversion. */
16542 fname = get_tv_string(&argvars[0]);
16543 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16544 {
16545 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16546 return;
16547 }
16548
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016549 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016551 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016552
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016553 /* This for loop processes what was read, but is also entered at end
16554 * of file so that either:
16555 * - an incomplete line gets written
16556 * - a "binary" file gets an empty line at the end if it ends in a
16557 * newline. */
16558 for (p = buf, start = buf;
16559 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16560 ++p)
16561 {
16562 if (*p == '\n' || readlen <= 0)
16563 {
16564 listitem_T *li;
16565 char_u *s = NULL;
16566 long_u len = p - start;
16567
16568 /* Finished a line. Remove CRs before NL. */
16569 if (readlen > 0 && !binary)
16570 {
16571 while (len > 0 && start[len - 1] == '\r')
16572 --len;
16573 /* removal may cross back to the "prev" string */
16574 if (len == 0)
16575 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16576 --prevlen;
16577 }
16578 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016579 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016580 else
16581 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016582 /* Change "prev" buffer to be the right size. This way
16583 * the bytes are only copied once, and very long lines are
16584 * allocated only once. */
16585 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016586 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016587 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016588 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016589 prev = NULL; /* the list will own the string */
16590 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016591 }
16592 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016593 if (s == NULL)
16594 {
16595 do_outofmem_msg((long_u) prevlen + len + 1);
16596 failed = TRUE;
16597 break;
16598 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016600 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016601 {
16602 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016603 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016604 break;
16605 }
16606 li->li_tv.v_type = VAR_STRING;
16607 li->li_tv.v_lock = 0;
16608 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016609 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016610
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 start = p + 1; /* step over newline */
16612 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016613 break;
16614 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016615 else if (*p == NUL)
16616 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016617#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016618 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16619 * when finding the BF and check the previous two bytes. */
16620 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016621 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016622 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16623 * + 1, these may be in the "prev" string. */
16624 char_u back1 = p >= buf + 1 ? p[-1]
16625 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16626 char_u back2 = p >= buf + 2 ? p[-2]
16627 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16628 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016630 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016631 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016632 char_u *dest = p - 2;
16633
16634 /* Usually a BOM is at the beginning of a file, and so at
16635 * the beginning of a line; then we can just step over it.
16636 */
16637 if (start == dest)
16638 start = p + 1;
16639 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016640 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016641 /* have to shuffle buf to close gap */
16642 int adjust_prevlen = 0;
16643
16644 if (dest < buf)
16645 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016646 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016647 dest = buf;
16648 }
16649 if (readlen > p - buf + 1)
16650 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16651 readlen -= 3 - adjust_prevlen;
16652 prevlen -= adjust_prevlen;
16653 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016654 }
16655 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016656 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016657#endif
16658 } /* for */
16659
16660 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16661 break;
16662 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016663 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016664 /* There's part of a line in buf, store it in "prev". */
16665 if (p - start + prevlen >= prevsize)
16666 {
16667 /* need bigger "prev" buffer */
16668 char_u *newprev;
16669
16670 /* A common use case is ordinary text files and "prev" gets a
16671 * fragment of a line, so the first allocation is made
16672 * small, to avoid repeatedly 'allocing' large and
16673 * 'reallocing' small. */
16674 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016675 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016676 else
16677 {
16678 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016679 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016680 prevsize = grow50pc > growmin ? grow50pc : growmin;
16681 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016682 newprev = prev == NULL ? alloc(prevsize)
16683 : vim_realloc(prev, prevsize);
16684 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016685 {
16686 do_outofmem_msg((long_u)prevsize);
16687 failed = TRUE;
16688 break;
16689 }
16690 prev = newprev;
16691 }
16692 /* Add the line part to end of "prev". */
16693 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016694 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016695 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016696 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016697
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016698 /*
16699 * For a negative line count use only the lines at the end of the file,
16700 * free the rest.
16701 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016702 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016703 while (cnt > -maxline)
16704 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016705 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016706 --cnt;
16707 }
16708
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016709 if (failed)
16710 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016711 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016712 /* readfile doc says an empty list is returned on error */
16713 rettv->vval.v_list = list_alloc();
16714 }
16715
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016716 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016717 fclose(fd);
16718}
16719
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016720#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016721static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016722
16723/*
16724 * Convert a List to proftime_T.
16725 * Return FAIL when there is something wrong.
16726 */
16727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016728list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016729{
16730 long n1, n2;
16731 int error = FALSE;
16732
16733 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16734 || arg->vval.v_list->lv_len != 2)
16735 return FAIL;
16736 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16737 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16738# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016739 tm->HighPart = n1;
16740 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016741# else
16742 tm->tv_sec = n1;
16743 tm->tv_usec = n2;
16744# endif
16745 return error ? FAIL : OK;
16746}
16747#endif /* FEAT_RELTIME */
16748
16749/*
16750 * "reltime()" function
16751 */
16752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016753f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016754{
16755#ifdef FEAT_RELTIME
16756 proftime_T res;
16757 proftime_T start;
16758
16759 if (argvars[0].v_type == VAR_UNKNOWN)
16760 {
16761 /* No arguments: get current time. */
16762 profile_start(&res);
16763 }
16764 else if (argvars[1].v_type == VAR_UNKNOWN)
16765 {
16766 if (list2proftime(&argvars[0], &res) == FAIL)
16767 return;
16768 profile_end(&res);
16769 }
16770 else
16771 {
16772 /* Two arguments: compute the difference. */
16773 if (list2proftime(&argvars[0], &start) == FAIL
16774 || list2proftime(&argvars[1], &res) == FAIL)
16775 return;
16776 profile_sub(&res, &start);
16777 }
16778
16779 if (rettv_list_alloc(rettv) == OK)
16780 {
16781 long n1, n2;
16782
16783# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016784 n1 = res.HighPart;
16785 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016786# else
16787 n1 = res.tv_sec;
16788 n2 = res.tv_usec;
16789# endif
16790 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16791 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16792 }
16793#endif
16794}
16795
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016796#ifdef FEAT_FLOAT
16797/*
16798 * "reltimefloat()" function
16799 */
16800 static void
16801f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16802{
16803# ifdef FEAT_RELTIME
16804 proftime_T tm;
16805# endif
16806
16807 rettv->v_type = VAR_FLOAT;
16808 rettv->vval.v_float = 0;
16809# ifdef FEAT_RELTIME
16810 if (list2proftime(&argvars[0], &tm) == OK)
16811 rettv->vval.v_float = profile_float(&tm);
16812# endif
16813}
16814#endif
16815
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016816/*
16817 * "reltimestr()" function
16818 */
16819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016820f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016821{
16822#ifdef FEAT_RELTIME
16823 proftime_T tm;
16824#endif
16825
16826 rettv->v_type = VAR_STRING;
16827 rettv->vval.v_string = NULL;
16828#ifdef FEAT_RELTIME
16829 if (list2proftime(&argvars[0], &tm) == OK)
16830 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16831#endif
16832}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016833
Bram Moolenaar0d660222005-01-07 21:51:51 +000016834#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016835static void make_connection(void);
16836static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016837
16838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016839make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016840{
16841 if (X_DISPLAY == NULL
16842# ifdef FEAT_GUI
16843 && !gui.in_use
16844# endif
16845 )
16846 {
16847 x_force_connect = TRUE;
16848 setup_term_clip();
16849 x_force_connect = FALSE;
16850 }
16851}
16852
16853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016854check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855{
16856 make_connection();
16857 if (X_DISPLAY == NULL)
16858 {
16859 EMSG(_("E240: No connection to Vim server"));
16860 return FAIL;
16861 }
16862 return OK;
16863}
16864#endif
16865
16866#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016868remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016869{
16870 char_u *server_name;
16871 char_u *keys;
16872 char_u *r = NULL;
16873 char_u buf[NUMBUFLEN];
16874# ifdef WIN32
16875 HWND w;
16876# else
16877 Window w;
16878# endif
16879
16880 if (check_restricted() || check_secure())
16881 return;
16882
16883# ifdef FEAT_X11
16884 if (check_connection() == FAIL)
16885 return;
16886# endif
16887
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016888 server_name = get_tv_string_chk(&argvars[0]);
16889 if (server_name == NULL)
16890 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 keys = get_tv_string_buf(&argvars[1], buf);
16892# ifdef WIN32
16893 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16894# else
16895 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16896 < 0)
16897# endif
16898 {
16899 if (r != NULL)
16900 EMSG(r); /* sending worked but evaluation failed */
16901 else
16902 EMSG2(_("E241: Unable to send to %s"), server_name);
16903 return;
16904 }
16905
16906 rettv->vval.v_string = r;
16907
16908 if (argvars[2].v_type != VAR_UNKNOWN)
16909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016911 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016912 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016914 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016915 v.di_tv.v_type = VAR_STRING;
16916 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 idvar = get_tv_string_chk(&argvars[2]);
16918 if (idvar != NULL)
16919 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016920 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016921 }
16922}
16923#endif
16924
16925/*
16926 * "remote_expr()" function
16927 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016929f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930{
16931 rettv->v_type = VAR_STRING;
16932 rettv->vval.v_string = NULL;
16933#ifdef FEAT_CLIENTSERVER
16934 remote_common(argvars, rettv, TRUE);
16935#endif
16936}
16937
16938/*
16939 * "remote_foreground()" function
16940 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016942f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944#ifdef FEAT_CLIENTSERVER
16945# ifdef WIN32
16946 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016947 {
16948 char_u *server_name = get_tv_string_chk(&argvars[0]);
16949
16950 if (server_name != NULL)
16951 serverForeground(server_name);
16952 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016953# else
16954 /* Send a foreground() expression to the server. */
16955 argvars[1].v_type = VAR_STRING;
16956 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16957 argvars[2].v_type = VAR_UNKNOWN;
16958 remote_common(argvars, rettv, TRUE);
16959 vim_free(argvars[1].vval.v_string);
16960# endif
16961#endif
16962}
16963
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016965f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966{
16967#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016968 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 char_u *s = NULL;
16970# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016971 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016973 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974
16975 if (check_restricted() || check_secure())
16976 {
16977 rettv->vval.v_number = -1;
16978 return;
16979 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016980 serverid = get_tv_string_chk(&argvars[0]);
16981 if (serverid == NULL)
16982 {
16983 rettv->vval.v_number = -1;
16984 return; /* type error; errmsg already given */
16985 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016987 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988 if (n == 0)
16989 rettv->vval.v_number = -1;
16990 else
16991 {
16992 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16993 rettv->vval.v_number = (s != NULL);
16994 }
16995# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996 if (check_connection() == FAIL)
16997 return;
16998
16999 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017000 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001# endif
17002
17003 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 char_u *retvar;
17006
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 v.di_tv.v_type = VAR_STRING;
17008 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 retvar = get_tv_string_chk(&argvars[1]);
17010 if (retvar != NULL)
17011 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017012 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013 }
17014#else
17015 rettv->vval.v_number = -1;
17016#endif
17017}
17018
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017020f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021{
17022 char_u *r = NULL;
17023
17024#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017025 char_u *serverid = get_tv_string_chk(&argvars[0]);
17026
17027 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028 {
17029# ifdef WIN32
17030 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017031 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017033 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 if (n != 0)
17035 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17036 if (r == NULL)
17037# else
17038 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040# endif
17041 EMSG(_("E277: Unable to read a server reply"));
17042 }
17043#endif
17044 rettv->v_type = VAR_STRING;
17045 rettv->vval.v_string = r;
17046}
17047
17048/*
17049 * "remote_send()" function
17050 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017052f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053{
17054 rettv->v_type = VAR_STRING;
17055 rettv->vval.v_string = NULL;
17056#ifdef FEAT_CLIENTSERVER
17057 remote_common(argvars, rettv, FALSE);
17058#endif
17059}
17060
17061/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017062 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017063 */
17064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017065f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017066{
Bram Moolenaar33570922005-01-25 22:26:29 +000017067 list_T *l;
17068 listitem_T *item, *item2;
17069 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017070 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017071 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017072 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 dict_T *d;
17074 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017075 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017076
Bram Moolenaar8c711452005-01-14 21:53:12 +000017077 if (argvars[0].v_type == VAR_DICT)
17078 {
17079 if (argvars[2].v_type != VAR_UNKNOWN)
17080 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017081 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017082 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 key = get_tv_string_chk(&argvars[1]);
17085 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017087 di = dict_find(d, key, -1);
17088 if (di == NULL)
17089 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017090 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17091 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 {
17093 *rettv = di->di_tv;
17094 init_tv(&di->di_tv);
17095 dictitem_remove(d, di);
17096 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017097 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017098 }
17099 }
17100 else if (argvars[0].v_type != VAR_LIST)
17101 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017102 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017103 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017105 int error = FALSE;
17106
17107 idx = get_tv_number_chk(&argvars[1], &error);
17108 if (error)
17109 ; /* type error: do nothing, errmsg already given */
17110 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017111 EMSGN(_(e_listidx), idx);
17112 else
17113 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017114 if (argvars[2].v_type == VAR_UNKNOWN)
17115 {
17116 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017117 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017118 *rettv = item->li_tv;
17119 vim_free(item);
17120 }
17121 else
17122 {
17123 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017124 end = get_tv_number_chk(&argvars[2], &error);
17125 if (error)
17126 ; /* type error: do nothing */
17127 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017128 EMSGN(_(e_listidx), end);
17129 else
17130 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017131 int cnt = 0;
17132
17133 for (li = item; li != NULL; li = li->li_next)
17134 {
17135 ++cnt;
17136 if (li == item2)
17137 break;
17138 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017139 if (li == NULL) /* didn't find "item2" after "item" */
17140 EMSG(_(e_invrange));
17141 else
17142 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017143 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017145 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017146 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017147 l->lv_first = item;
17148 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017149 item->li_prev = NULL;
17150 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017151 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017152 }
17153 }
17154 }
17155 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017156 }
17157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158}
17159
17160/*
17161 * "rename({from}, {to})" function
17162 */
17163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017164f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165{
17166 char_u buf[NUMBUFLEN];
17167
17168 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017171 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17172 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173}
17174
17175/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017176 * "repeat()" function
17177 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017179f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017180{
17181 char_u *p;
17182 int n;
17183 int slen;
17184 int len;
17185 char_u *r;
17186 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017187
17188 n = get_tv_number(&argvars[1]);
17189 if (argvars[0].v_type == VAR_LIST)
17190 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017191 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017192 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017193 if (list_extend(rettv->vval.v_list,
17194 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017195 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017196 }
17197 else
17198 {
17199 p = get_tv_string(&argvars[0]);
17200 rettv->v_type = VAR_STRING;
17201 rettv->vval.v_string = NULL;
17202
17203 slen = (int)STRLEN(p);
17204 len = slen * n;
17205 if (len <= 0)
17206 return;
17207
17208 r = alloc(len + 1);
17209 if (r != NULL)
17210 {
17211 for (i = 0; i < n; i++)
17212 mch_memmove(r + i * slen, p, (size_t)slen);
17213 r[len] = NUL;
17214 }
17215
17216 rettv->vval.v_string = r;
17217 }
17218}
17219
17220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 * "resolve()" function
17222 */
17223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017224f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225{
17226 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017227#ifdef HAVE_READLINK
17228 char_u *buf = NULL;
17229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017231 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232#ifdef FEAT_SHORTCUT
17233 {
17234 char_u *v = NULL;
17235
17236 v = mch_resolve_shortcut(p);
17237 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017238 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017240 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 }
17242#else
17243# ifdef HAVE_READLINK
17244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017245 char_u *cpy;
17246 int len;
17247 char_u *remain = NULL;
17248 char_u *q;
17249 int is_relative_to_current = FALSE;
17250 int has_trailing_pathsep = FALSE;
17251 int limit = 100;
17252
17253 p = vim_strsave(p);
17254
17255 if (p[0] == '.' && (vim_ispathsep(p[1])
17256 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17257 is_relative_to_current = TRUE;
17258
17259 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017260 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017262 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017263 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265
17266 q = getnextcomp(p);
17267 if (*q != NUL)
17268 {
17269 /* Separate the first path component in "p", and keep the
17270 * remainder (beginning with the path separator). */
17271 remain = vim_strsave(q - 1);
17272 q[-1] = NUL;
17273 }
17274
Bram Moolenaard9462e32011-04-11 21:35:11 +020017275 buf = alloc(MAXPATHL + 1);
17276 if (buf == NULL)
17277 goto fail;
17278
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 for (;;)
17280 {
17281 for (;;)
17282 {
17283 len = readlink((char *)p, (char *)buf, MAXPATHL);
17284 if (len <= 0)
17285 break;
17286 buf[len] = NUL;
17287
17288 if (limit-- == 0)
17289 {
17290 vim_free(p);
17291 vim_free(remain);
17292 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017293 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 goto fail;
17295 }
17296
17297 /* Ensure that the result will have a trailing path separator
17298 * if the argument has one. */
17299 if (remain == NULL && has_trailing_pathsep)
17300 add_pathsep(buf);
17301
17302 /* Separate the first path component in the link value and
17303 * concatenate the remainders. */
17304 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17305 if (*q != NUL)
17306 {
17307 if (remain == NULL)
17308 remain = vim_strsave(q - 1);
17309 else
17310 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017311 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 if (cpy != NULL)
17313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 vim_free(remain);
17315 remain = cpy;
17316 }
17317 }
17318 q[-1] = NUL;
17319 }
17320
17321 q = gettail(p);
17322 if (q > p && *q == NUL)
17323 {
17324 /* Ignore trailing path separator. */
17325 q[-1] = NUL;
17326 q = gettail(p);
17327 }
17328 if (q > p && !mch_isFullName(buf))
17329 {
17330 /* symlink is relative to directory of argument */
17331 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17332 if (cpy != NULL)
17333 {
17334 STRCPY(cpy, p);
17335 STRCPY(gettail(cpy), buf);
17336 vim_free(p);
17337 p = cpy;
17338 }
17339 }
17340 else
17341 {
17342 vim_free(p);
17343 p = vim_strsave(buf);
17344 }
17345 }
17346
17347 if (remain == NULL)
17348 break;
17349
17350 /* Append the first path component of "remain" to "p". */
17351 q = getnextcomp(remain + 1);
17352 len = q - remain - (*q != NUL);
17353 cpy = vim_strnsave(p, STRLEN(p) + len);
17354 if (cpy != NULL)
17355 {
17356 STRNCAT(cpy, remain, len);
17357 vim_free(p);
17358 p = cpy;
17359 }
17360 /* Shorten "remain". */
17361 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017362 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 else
17364 {
17365 vim_free(remain);
17366 remain = NULL;
17367 }
17368 }
17369
17370 /* If the result is a relative path name, make it explicitly relative to
17371 * the current directory if and only if the argument had this form. */
17372 if (!vim_ispathsep(*p))
17373 {
17374 if (is_relative_to_current
17375 && *p != NUL
17376 && !(p[0] == '.'
17377 && (p[1] == NUL
17378 || vim_ispathsep(p[1])
17379 || (p[1] == '.'
17380 && (p[2] == NUL
17381 || vim_ispathsep(p[2]))))))
17382 {
17383 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017384 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 if (cpy != NULL)
17386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 vim_free(p);
17388 p = cpy;
17389 }
17390 }
17391 else if (!is_relative_to_current)
17392 {
17393 /* Strip leading "./". */
17394 q = p;
17395 while (q[0] == '.' && vim_ispathsep(q[1]))
17396 q += 2;
17397 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017398 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 }
17400 }
17401
17402 /* Ensure that the result will have no trailing path separator
17403 * if the argument had none. But keep "/" or "//". */
17404 if (!has_trailing_pathsep)
17405 {
17406 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017407 if (after_pathsep(p, q))
17408 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017409 }
17410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017411 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 }
17413# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017414 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415# endif
17416#endif
17417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
17420#ifdef HAVE_READLINK
17421fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017422 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425}
17426
17427/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017428 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 */
17430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017431f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432{
Bram Moolenaar33570922005-01-25 22:26:29 +000017433 list_T *l;
17434 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435
Bram Moolenaar0d660222005-01-07 21:51:51 +000017436 if (argvars[0].v_type != VAR_LIST)
17437 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017438 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017439 && !tv_check_lock(l->lv_lock,
17440 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 {
17442 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017443 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017444 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017445 while (li != NULL)
17446 {
17447 ni = li->li_prev;
17448 list_append(l, li);
17449 li = ni;
17450 }
17451 rettv->vval.v_list = l;
17452 rettv->v_type = VAR_LIST;
17453 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017454 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456}
17457
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017458#define SP_NOMOVE 0x01 /* don't move cursor */
17459#define SP_REPEAT 0x02 /* repeat to find outer pair */
17460#define SP_RETCOUNT 0x04 /* return matchcount */
17461#define SP_SETPCMARK 0x08 /* set previous context mark */
17462#define SP_START 0x10 /* accept match at start position */
17463#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17464#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017465#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017466
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017467static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017468
17469/*
17470 * Get flags for a search function.
17471 * Possibly sets "p_ws".
17472 * Returns BACKWARD, FORWARD or zero (for an error).
17473 */
17474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017475get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017476{
17477 int dir = FORWARD;
17478 char_u *flags;
17479 char_u nbuf[NUMBUFLEN];
17480 int mask;
17481
17482 if (varp->v_type != VAR_UNKNOWN)
17483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017484 flags = get_tv_string_buf_chk(varp, nbuf);
17485 if (flags == NULL)
17486 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017487 while (*flags != NUL)
17488 {
17489 switch (*flags)
17490 {
17491 case 'b': dir = BACKWARD; break;
17492 case 'w': p_ws = TRUE; break;
17493 case 'W': p_ws = FALSE; break;
17494 default: mask = 0;
17495 if (flagsp != NULL)
17496 switch (*flags)
17497 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017498 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017499 case 'e': mask = SP_END; break;
17500 case 'm': mask = SP_RETCOUNT; break;
17501 case 'n': mask = SP_NOMOVE; break;
17502 case 'p': mask = SP_SUBPAT; break;
17503 case 'r': mask = SP_REPEAT; break;
17504 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017505 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506 }
17507 if (mask == 0)
17508 {
17509 EMSG2(_(e_invarg2), flags);
17510 dir = 0;
17511 }
17512 else
17513 *flagsp |= mask;
17514 }
17515 if (dir == 0)
17516 break;
17517 ++flags;
17518 }
17519 }
17520 return dir;
17521}
17522
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017524 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017527search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017529 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 char_u *pat;
17531 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017532 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533 int save_p_ws = p_ws;
17534 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017535 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017536 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017537 proftime_T tm;
17538#ifdef FEAT_RELTIME
17539 long time_limit = 0;
17540#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017541 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017542 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017544 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017545 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017546 if (dir == 0)
17547 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017548 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017549 if (flags & SP_START)
17550 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017551 if (flags & SP_END)
17552 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017553 if (flags & SP_COLUMN)
17554 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017555
Bram Moolenaar76929292008-01-06 19:07:36 +000017556 /* Optional arguments: line number to stop searching and timeout. */
17557 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017558 {
17559 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17560 if (lnum_stop < 0)
17561 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017562#ifdef FEAT_RELTIME
17563 if (argvars[3].v_type != VAR_UNKNOWN)
17564 {
17565 time_limit = get_tv_number_chk(&argvars[3], NULL);
17566 if (time_limit < 0)
17567 goto theend;
17568 }
17569#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017570 }
17571
Bram Moolenaar76929292008-01-06 19:07:36 +000017572#ifdef FEAT_RELTIME
17573 /* Set the time limit, if there is one. */
17574 profile_setlimit(time_limit, &tm);
17575#endif
17576
Bram Moolenaar231334e2005-07-25 20:46:57 +000017577 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017578 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017579 * Check to make sure only those flags are set.
17580 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17581 * flags cannot be set. Check for that condition also.
17582 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017583 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017584 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017587 goto theend;
17588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017590 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017591 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017592 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017593 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017595 if (flags & SP_SUBPAT)
17596 retval = subpatnum;
17597 else
17598 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017599 if (flags & SP_SETPCMARK)
17600 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017602 if (match_pos != NULL)
17603 {
17604 /* Store the match cursor position */
17605 match_pos->lnum = pos.lnum;
17606 match_pos->col = pos.col + 1;
17607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017608 /* "/$" will put the cursor after the end of the line, may need to
17609 * correct that here */
17610 check_cursor();
17611 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017612
17613 /* If 'n' flag is used: restore cursor position. */
17614 if (flags & SP_NOMOVE)
17615 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017616 else
17617 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017618theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017620
17621 return retval;
17622}
17623
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017624#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017625
17626/*
17627 * round() is not in C90, use ceil() or floor() instead.
17628 */
17629 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017630vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017631{
17632 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17633}
17634
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017635/*
17636 * "round({float})" function
17637 */
17638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017639f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017640{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017641 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017642
17643 rettv->v_type = VAR_FLOAT;
17644 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017645 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017646 else
17647 rettv->vval.v_float = 0.0;
17648}
17649#endif
17650
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017651/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017652 * "screenattr()" function
17653 */
17654 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017655f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017656{
17657 int row;
17658 int col;
17659 int c;
17660
17661 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17662 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17663 if (row < 0 || row >= screen_Rows
17664 || col < 0 || col >= screen_Columns)
17665 c = -1;
17666 else
17667 c = ScreenAttrs[LineOffset[row] + col];
17668 rettv->vval.v_number = c;
17669}
17670
17671/*
17672 * "screenchar()" function
17673 */
17674 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017675f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017676{
17677 int row;
17678 int col;
17679 int off;
17680 int c;
17681
17682 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17683 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17684 if (row < 0 || row >= screen_Rows
17685 || col < 0 || col >= screen_Columns)
17686 c = -1;
17687 else
17688 {
17689 off = LineOffset[row] + col;
17690#ifdef FEAT_MBYTE
17691 if (enc_utf8 && ScreenLinesUC[off] != 0)
17692 c = ScreenLinesUC[off];
17693 else
17694#endif
17695 c = ScreenLines[off];
17696 }
17697 rettv->vval.v_number = c;
17698}
17699
17700/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017701 * "screencol()" function
17702 *
17703 * First column is 1 to be consistent with virtcol().
17704 */
17705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017706f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017707{
17708 rettv->vval.v_number = screen_screencol() + 1;
17709}
17710
17711/*
17712 * "screenrow()" function
17713 */
17714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017715f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017716{
17717 rettv->vval.v_number = screen_screenrow() + 1;
17718}
17719
17720/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017721 * "search()" function
17722 */
17723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017724f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017725{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017726 int flags = 0;
17727
17728 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729}
17730
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017732 * "searchdecl()" function
17733 */
17734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017735f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017736{
17737 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017738 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017739 int error = FALSE;
17740 char_u *name;
17741
17742 rettv->vval.v_number = 1; /* default: FAIL */
17743
17744 name = get_tv_string_chk(&argvars[0]);
17745 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017746 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017747 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017748 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17749 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17750 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017751 if (!error && name != NULL)
17752 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017753 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017754}
17755
17756/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017757 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017760searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
17762 char_u *spat, *mpat, *epat;
17763 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 int dir;
17766 int flags = 0;
17767 char_u nbuf1[NUMBUFLEN];
17768 char_u nbuf2[NUMBUFLEN];
17769 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017770 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017771 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017772 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017775 spat = get_tv_string_chk(&argvars[0]);
17776 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17777 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17778 if (spat == NULL || mpat == NULL || epat == NULL)
17779 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781 /* Handle the optional fourth argument: flags */
17782 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017783 if (dir == 0)
17784 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017785
17786 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017787 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17788 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017789 if ((flags & (SP_END | SP_SUBPAT)) != 0
17790 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017791 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017792 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017793 goto theend;
17794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017796 /* Using 'r' implies 'W', otherwise it doesn't work. */
17797 if (flags & SP_REPEAT)
17798 p_ws = FALSE;
17799
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017801 if (argvars[3].v_type == VAR_UNKNOWN
17802 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 skip = (char_u *)"";
17804 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017806 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017807 if (argvars[5].v_type != VAR_UNKNOWN)
17808 {
17809 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17810 if (lnum_stop < 0)
17811 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017812#ifdef FEAT_RELTIME
17813 if (argvars[6].v_type != VAR_UNKNOWN)
17814 {
17815 time_limit = get_tv_number_chk(&argvars[6], NULL);
17816 if (time_limit < 0)
17817 goto theend;
17818 }
17819#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017820 }
17821 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017822 if (skip == NULL)
17823 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017825 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017826 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017827
17828theend:
17829 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017830
17831 return retval;
17832}
17833
17834/*
17835 * "searchpair()" function
17836 */
17837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017838f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017839{
17840 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17841}
17842
17843/*
17844 * "searchpairpos()" function
17845 */
17846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017847f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017848{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017849 pos_T match_pos;
17850 int lnum = 0;
17851 int col = 0;
17852
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017853 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017854 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017855
17856 if (searchpair_cmn(argvars, &match_pos) > 0)
17857 {
17858 lnum = match_pos.lnum;
17859 col = match_pos.col;
17860 }
17861
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017862 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17863 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017864}
17865
17866/*
17867 * Search for a start/middle/end thing.
17868 * Used by searchpair(), see its documentation for the details.
17869 * Returns 0 or -1 for no match,
17870 */
17871 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017872do_searchpair(
17873 char_u *spat, /* start pattern */
17874 char_u *mpat, /* middle pattern */
17875 char_u *epat, /* end pattern */
17876 int dir, /* BACKWARD or FORWARD */
17877 char_u *skip, /* skip expression */
17878 int flags, /* SP_SETPCMARK and other SP_ values */
17879 pos_T *match_pos,
17880 linenr_T lnum_stop, /* stop at this line if not zero */
17881 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017882{
17883 char_u *save_cpo;
17884 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17885 long retval = 0;
17886 pos_T pos;
17887 pos_T firstpos;
17888 pos_T foundpos;
17889 pos_T save_cursor;
17890 pos_T save_pos;
17891 int n;
17892 int r;
17893 int nest = 1;
17894 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017895 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017896 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017897
17898 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17899 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017900 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017901
Bram Moolenaar76929292008-01-06 19:07:36 +000017902#ifdef FEAT_RELTIME
17903 /* Set the time limit, if there is one. */
17904 profile_setlimit(time_limit, &tm);
17905#endif
17906
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017907 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17908 * start/middle/end (pat3, for the top pair). */
17909 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17910 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17911 if (pat2 == NULL || pat3 == NULL)
17912 goto theend;
17913 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17914 if (*mpat == NUL)
17915 STRCPY(pat3, pat2);
17916 else
17917 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17918 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017919 if (flags & SP_START)
17920 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017921
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 save_cursor = curwin->w_cursor;
17923 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017924 clearpos(&firstpos);
17925 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926 pat = pat3;
17927 for (;;)
17928 {
17929 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017930 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17932 /* didn't find it or found the first match again: FAIL */
17933 break;
17934
17935 if (firstpos.lnum == 0)
17936 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017937 if (equalpos(pos, foundpos))
17938 {
17939 /* Found the same position again. Can happen with a pattern that
17940 * has "\zs" at the end and searching backwards. Advance one
17941 * character and try again. */
17942 if (dir == BACKWARD)
17943 decl(&pos);
17944 else
17945 incl(&pos);
17946 }
17947 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017949 /* clear the start flag to avoid getting stuck here */
17950 options &= ~SEARCH_START;
17951
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 /* If the skip pattern matches, ignore this match. */
17953 if (*skip != NUL)
17954 {
17955 save_pos = curwin->w_cursor;
17956 curwin->w_cursor = pos;
17957 r = eval_to_bool(skip, &err, NULL, FALSE);
17958 curwin->w_cursor = save_pos;
17959 if (err)
17960 {
17961 /* Evaluating {skip} caused an error, break here. */
17962 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017963 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 break;
17965 }
17966 if (r)
17967 continue;
17968 }
17969
17970 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17971 {
17972 /* Found end when searching backwards or start when searching
17973 * forward: nested pair. */
17974 ++nest;
17975 pat = pat2; /* nested, don't search for middle */
17976 }
17977 else
17978 {
17979 /* Found end when searching forward or start when searching
17980 * backward: end of (nested) pair; or found middle in outer pair. */
17981 if (--nest == 1)
17982 pat = pat3; /* outer level, search for middle */
17983 }
17984
17985 if (nest == 0)
17986 {
17987 /* Found the match: return matchcount or line number. */
17988 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017989 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017991 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017992 if (flags & SP_SETPCMARK)
17993 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 curwin->w_cursor = pos;
17995 if (!(flags & SP_REPEAT))
17996 break;
17997 nest = 1; /* search for next unmatched */
17998 }
17999 }
18000
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018001 if (match_pos != NULL)
18002 {
18003 /* Store the match cursor position */
18004 match_pos->lnum = curwin->w_cursor.lnum;
18005 match_pos->col = curwin->w_cursor.col + 1;
18006 }
18007
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018009 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 curwin->w_cursor = save_cursor;
18011
18012theend:
18013 vim_free(pat2);
18014 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018015 if (p_cpo == empty_option)
18016 p_cpo = save_cpo;
18017 else
18018 /* Darn, evaluating the {skip} expression changed the value. */
18019 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018020
18021 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022}
18023
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018024/*
18025 * "searchpos()" function
18026 */
18027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018028f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018029{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018030 pos_T match_pos;
18031 int lnum = 0;
18032 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018033 int n;
18034 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018035
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018036 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018037 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018038
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018039 n = search_cmn(argvars, &match_pos, &flags);
18040 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018041 {
18042 lnum = match_pos.lnum;
18043 col = match_pos.col;
18044 }
18045
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018046 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18047 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018048 if (flags & SP_SUBPAT)
18049 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018050}
18051
Bram Moolenaar0d660222005-01-07 21:51:51 +000018052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018053f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018055#ifdef FEAT_CLIENTSERVER
18056 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018057 char_u *server = get_tv_string_chk(&argvars[0]);
18058 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059
Bram Moolenaar0d660222005-01-07 21:51:51 +000018060 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018061 if (server == NULL || reply == NULL)
18062 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018063 if (check_restricted() || check_secure())
18064 return;
18065# ifdef FEAT_X11
18066 if (check_connection() == FAIL)
18067 return;
18068# endif
18069
18070 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018072 EMSG(_("E258: Unable to send to client"));
18073 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018075 rettv->vval.v_number = 0;
18076#else
18077 rettv->vval.v_number = -1;
18078#endif
18079}
18080
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018082f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083{
18084 char_u *r = NULL;
18085
18086#ifdef FEAT_CLIENTSERVER
18087# ifdef WIN32
18088 r = serverGetVimNames();
18089# else
18090 make_connection();
18091 if (X_DISPLAY != NULL)
18092 r = serverGetVimNames(X_DISPLAY);
18093# endif
18094#endif
18095 rettv->v_type = VAR_STRING;
18096 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097}
18098
18099/*
18100 * "setbufvar()" function
18101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018103f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104{
18105 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018108 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 char_u nbuf[NUMBUFLEN];
18110
18111 if (check_restricted() || check_secure())
18112 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18114 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018115 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 varp = &argvars[2];
18117
18118 if (buf != NULL && varname != NULL && varp != NULL)
18119 {
18120 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122
18123 if (*varname == '&')
18124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018125 long numval;
18126 char_u *strval;
18127 int error = FALSE;
18128
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018130 numval = get_tv_number_chk(varp, &error);
18131 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018132 if (!error && strval != NULL)
18133 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 }
18135 else
18136 {
18137 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18138 if (bufvarname != NULL)
18139 {
18140 STRCPY(bufvarname, "b:");
18141 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018142 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018143 vim_free(bufvarname);
18144 }
18145 }
18146
18147 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150}
18151
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018153f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018154{
18155 dict_T *d;
18156 dictitem_T *di;
18157 char_u *csearch;
18158
18159 if (argvars[0].v_type != VAR_DICT)
18160 {
18161 EMSG(_(e_dictreq));
18162 return;
18163 }
18164
18165 if ((d = argvars[0].vval.v_dict) != NULL)
18166 {
18167 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18168 if (csearch != NULL)
18169 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018170#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018171 if (enc_utf8)
18172 {
18173 int pcc[MAX_MCO];
18174 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018175
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018176 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18177 }
18178 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018179#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018180 set_last_csearch(PTR2CHAR(csearch),
18181 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018182 }
18183
18184 di = dict_find(d, (char_u *)"forward", -1);
18185 if (di != NULL)
18186 set_csearch_direction(get_tv_number(&di->di_tv)
18187 ? FORWARD : BACKWARD);
18188
18189 di = dict_find(d, (char_u *)"until", -1);
18190 if (di != NULL)
18191 set_csearch_until(!!get_tv_number(&di->di_tv));
18192 }
18193}
18194
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195/*
18196 * "setcmdpos()" function
18197 */
18198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018199f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018201 int pos = (int)get_tv_number(&argvars[0]) - 1;
18202
18203 if (pos >= 0)
18204 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205}
18206
18207/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018208 * "setfperm({fname}, {mode})" function
18209 */
18210 static void
18211f_setfperm(typval_T *argvars, typval_T *rettv)
18212{
18213 char_u *fname;
18214 char_u modebuf[NUMBUFLEN];
18215 char_u *mode_str;
18216 int i;
18217 int mask;
18218 int mode = 0;
18219
18220 rettv->vval.v_number = 0;
18221 fname = get_tv_string_chk(&argvars[0]);
18222 if (fname == NULL)
18223 return;
18224 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18225 if (mode_str == NULL)
18226 return;
18227 if (STRLEN(mode_str) != 9)
18228 {
18229 EMSG2(_(e_invarg2), mode_str);
18230 return;
18231 }
18232
18233 mask = 1;
18234 for (i = 8; i >= 0; --i)
18235 {
18236 if (mode_str[i] != '-')
18237 mode |= mask;
18238 mask = mask << 1;
18239 }
18240 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18241}
18242
18243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 * "setline()" function
18245 */
18246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018247f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248{
18249 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018250 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018251 list_T *l = NULL;
18252 listitem_T *li = NULL;
18253 long added = 0;
18254 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018256 lnum = get_tv_lnum(&argvars[0]);
18257 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018259 l = argvars[1].vval.v_list;
18260 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018262 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018264
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018265 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018266 for (;;)
18267 {
18268 if (l != NULL)
18269 {
18270 /* list argument, get next string */
18271 if (li == NULL)
18272 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018273 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018274 li = li->li_next;
18275 }
18276
18277 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018278 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018279 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018280
18281 /* When coming here from Insert mode, sync undo, so that this can be
18282 * undone separately from what was previously inserted. */
18283 if (u_sync_once == 2)
18284 {
18285 u_sync_once = 1; /* notify that u_sync() was called */
18286 u_sync(TRUE);
18287 }
18288
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018289 if (lnum <= curbuf->b_ml.ml_line_count)
18290 {
18291 /* existing line, replace it */
18292 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18293 {
18294 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018295 if (lnum == curwin->w_cursor.lnum)
18296 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018297 rettv->vval.v_number = 0; /* OK */
18298 }
18299 }
18300 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18301 {
18302 /* lnum is one past the last line, append the line */
18303 ++added;
18304 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18305 rettv->vval.v_number = 0; /* OK */
18306 }
18307
18308 if (l == NULL) /* only one string argument */
18309 break;
18310 ++lnum;
18311 }
18312
18313 if (added > 0)
18314 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315}
18316
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018317static 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 +000018318
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018320 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018321 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018323set_qf_ll_list(
18324 win_T *wp UNUSED,
18325 typval_T *list_arg UNUSED,
18326 typval_T *action_arg UNUSED,
18327 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018328{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018329#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018330 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018331 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018332 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018333#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018334
Bram Moolenaar2641f772005-03-25 21:58:17 +000018335 rettv->vval.v_number = -1;
18336
18337#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018338 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018339 EMSG(_(e_listreq));
18340 else
18341 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018342 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018343
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018344 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018345 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018346 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018347 if (act == NULL)
18348 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018349 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018350 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018351 else
18352 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018353 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018354 else if (action_arg->v_type == VAR_UNKNOWN)
18355 action = ' ';
18356 else
18357 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018358
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018359 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018360 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018361 rettv->vval.v_number = 0;
18362 }
18363#endif
18364}
18365
18366/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018367 * "setloclist()" function
18368 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018370f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018371{
18372 win_T *win;
18373
18374 rettv->vval.v_number = -1;
18375
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018376 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018377 if (win != NULL)
18378 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18379}
18380
18381/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018382 * "setmatches()" function
18383 */
18384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018386{
18387#ifdef FEAT_SEARCH_EXTRA
18388 list_T *l;
18389 listitem_T *li;
18390 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018391 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018392
18393 rettv->vval.v_number = -1;
18394 if (argvars[0].v_type != VAR_LIST)
18395 {
18396 EMSG(_(e_listreq));
18397 return;
18398 }
18399 if ((l = argvars[0].vval.v_list) != NULL)
18400 {
18401
18402 /* To some extent make sure that we are dealing with a list from
18403 * "getmatches()". */
18404 li = l->lv_first;
18405 while (li != NULL)
18406 {
18407 if (li->li_tv.v_type != VAR_DICT
18408 || (d = li->li_tv.vval.v_dict) == NULL)
18409 {
18410 EMSG(_(e_invarg));
18411 return;
18412 }
18413 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018414 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18415 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018416 && dict_find(d, (char_u *)"priority", -1) != NULL
18417 && dict_find(d, (char_u *)"id", -1) != NULL))
18418 {
18419 EMSG(_(e_invarg));
18420 return;
18421 }
18422 li = li->li_next;
18423 }
18424
18425 clear_matches(curwin);
18426 li = l->lv_first;
18427 while (li != NULL)
18428 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018429 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018430 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018431 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018432 char_u *group;
18433 int priority;
18434 int id;
18435 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018436
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018437 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018438 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18439 {
18440 if (s == NULL)
18441 {
18442 s = list_alloc();
18443 if (s == NULL)
18444 return;
18445 }
18446
18447 /* match from matchaddpos() */
18448 for (i = 1; i < 9; i++)
18449 {
18450 sprintf((char *)buf, (char *)"pos%d", i);
18451 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18452 {
18453 if (di->di_tv.v_type != VAR_LIST)
18454 return;
18455
18456 list_append_tv(s, &di->di_tv);
18457 s->lv_refcount++;
18458 }
18459 else
18460 break;
18461 }
18462 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018463
18464 group = get_dict_string(d, (char_u *)"group", FALSE);
18465 priority = (int)get_dict_number(d, (char_u *)"priority");
18466 id = (int)get_dict_number(d, (char_u *)"id");
18467 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18468 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18469 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018470 if (i == 0)
18471 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018472 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018473 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018474 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018475 }
18476 else
18477 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018478 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018479 list_unref(s);
18480 s = NULL;
18481 }
18482
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018483 li = li->li_next;
18484 }
18485 rettv->vval.v_number = 0;
18486 }
18487#endif
18488}
18489
18490/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018491 * "setpos()" function
18492 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018494f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018495{
18496 pos_T pos;
18497 int fnum;
18498 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018499 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018500
Bram Moolenaar08250432008-02-13 11:42:46 +000018501 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018502 name = get_tv_string_chk(argvars);
18503 if (name != NULL)
18504 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018505 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018506 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018507 if (--pos.col < 0)
18508 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018509 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018510 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018511 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018512 if (fnum == curbuf->b_fnum)
18513 {
18514 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018515 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018516 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018517 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018518 curwin->w_set_curswant = FALSE;
18519 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018520 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018521 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018522 }
18523 else
18524 EMSG(_(e_invarg));
18525 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018526 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18527 {
18528 /* set mark */
18529 if (setmark_pos(name[1], &pos, fnum) == OK)
18530 rettv->vval.v_number = 0;
18531 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018532 else
18533 EMSG(_(e_invarg));
18534 }
18535 }
18536}
18537
18538/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018539 * "setqflist()" function
18540 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018542f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018543{
18544 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18545}
18546
18547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 * "setreg()" function
18549 */
18550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018551f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552{
18553 int regname;
18554 char_u *strregname;
18555 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018556 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 int append;
18558 char_u yank_type;
18559 long block_len;
18560
18561 block_len = -1;
18562 yank_type = MAUTO;
18563 append = FALSE;
18564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018565 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018568 if (strregname == NULL)
18569 return; /* type error; errmsg already given */
18570 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 if (regname == 0 || regname == '@')
18572 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018574 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018576 stropt = get_tv_string_chk(&argvars[2]);
18577 if (stropt == NULL)
18578 return; /* type error */
18579 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 switch (*stropt)
18581 {
18582 case 'a': case 'A': /* append */
18583 append = TRUE;
18584 break;
18585 case 'v': case 'c': /* character-wise selection */
18586 yank_type = MCHAR;
18587 break;
18588 case 'V': case 'l': /* line-wise selection */
18589 yank_type = MLINE;
18590 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 case 'b': case Ctrl_V: /* block-wise selection */
18592 yank_type = MBLOCK;
18593 if (VIM_ISDIGIT(stropt[1]))
18594 {
18595 ++stropt;
18596 block_len = getdigits(&stropt) - 1;
18597 --stropt;
18598 }
18599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 }
18601 }
18602
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018603 if (argvars[1].v_type == VAR_LIST)
18604 {
18605 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018606 char_u **allocval;
18607 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018608 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018609 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018610 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018611 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018612 int len;
18613
18614 /* If the list is NULL handle like an empty list. */
18615 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018616
Bram Moolenaar7d647822014-04-05 21:28:56 +020018617 /* First half: use for pointers to result lines; second half: use for
18618 * pointers to allocated copies. */
18619 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018620 if (lstval == NULL)
18621 return;
18622 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018623 allocval = lstval + len + 2;
18624 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018625
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018626 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018627 li = li->li_next)
18628 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018629 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018630 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018631 goto free_lstval;
18632 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018633 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018634 /* Need to make a copy, next get_tv_string_buf_chk() will
18635 * overwrite the string. */
18636 strval = vim_strsave(buf);
18637 if (strval == NULL)
18638 goto free_lstval;
18639 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018640 }
18641 *curval++ = strval;
18642 }
18643 *curval++ = NULL;
18644
18645 write_reg_contents_lst(regname, lstval, -1,
18646 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018647free_lstval:
18648 while (curallocval > allocval)
18649 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018650 vim_free(lstval);
18651 }
18652 else
18653 {
18654 strval = get_tv_string_chk(&argvars[1]);
18655 if (strval == NULL)
18656 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018657 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018660 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661}
18662
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018663/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018664 * "settabvar()" function
18665 */
18666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018667f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018668{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018669#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018670 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018671 tabpage_T *tp;
18672#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018673 char_u *varname, *tabvarname;
18674 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018675
18676 rettv->vval.v_number = 0;
18677
18678 if (check_restricted() || check_secure())
18679 return;
18680
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018681#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018682 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018683#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018684 varname = get_tv_string_chk(&argvars[1]);
18685 varp = &argvars[2];
18686
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018687 if (varname != NULL && varp != NULL
18688#ifdef FEAT_WINDOWS
18689 && tp != NULL
18690#endif
18691 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018692 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018693#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018694 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018695 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018696#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018697
18698 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18699 if (tabvarname != NULL)
18700 {
18701 STRCPY(tabvarname, "t:");
18702 STRCPY(tabvarname + 2, varname);
18703 set_var(tabvarname, varp, TRUE);
18704 vim_free(tabvarname);
18705 }
18706
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018707#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018708 /* Restore current tabpage */
18709 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018710 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018711#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018712 }
18713}
18714
18715/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018716 * "settabwinvar()" function
18717 */
18718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018719f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018720{
18721 setwinvar(argvars, rettv, 1);
18722}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723
18724/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018725 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018728f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018730 setwinvar(argvars, rettv, 0);
18731}
18732
18733/*
18734 * "setwinvar()" and "settabwinvar()" functions
18735 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018736
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018738setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018739{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 win_T *win;
18741#ifdef FEAT_WINDOWS
18742 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018743 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018744 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745#endif
18746 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018747 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018749 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750
18751 if (check_restricted() || check_secure())
18752 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018753
18754#ifdef FEAT_WINDOWS
18755 if (off == 1)
18756 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18757 else
18758 tp = curtab;
18759#endif
18760 win = find_win_by_nr(&argvars[off], tp);
18761 varname = get_tv_string_chk(&argvars[off + 1]);
18762 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763
18764 if (win != NULL && varname != NULL && varp != NULL)
18765 {
18766#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018767 need_switch_win = !(tp == curtab && win == curwin);
18768 if (!need_switch_win
18769 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018772 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018774 long numval;
18775 char_u *strval;
18776 int error = FALSE;
18777
18778 ++varname;
18779 numval = get_tv_number_chk(varp, &error);
18780 strval = get_tv_string_buf_chk(varp, nbuf);
18781 if (!error && strval != NULL)
18782 set_option_value(varname, numval, strval, OPT_LOCAL);
18783 }
18784 else
18785 {
18786 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18787 if (winvarname != NULL)
18788 {
18789 STRCPY(winvarname, "w:");
18790 STRCPY(winvarname + 2, varname);
18791 set_var(winvarname, varp, TRUE);
18792 vim_free(winvarname);
18793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 }
18795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018797 if (need_switch_win)
18798 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799#endif
18800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018803#ifdef FEAT_CRYPT
18804/*
18805 * "sha256({string})" function
18806 */
18807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018808f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018809{
18810 char_u *p;
18811
18812 p = get_tv_string(&argvars[0]);
18813 rettv->vval.v_string = vim_strsave(
18814 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18815 rettv->v_type = VAR_STRING;
18816}
18817#endif /* FEAT_CRYPT */
18818
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018820 * "shellescape({string})" function
18821 */
18822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018823f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018824{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018825 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018826 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018827 rettv->v_type = VAR_STRING;
18828}
18829
18830/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018831 * shiftwidth() function
18832 */
18833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018834f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018835{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018836 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018837}
18838
18839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 */
18842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018843f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846
Bram Moolenaar0d660222005-01-07 21:51:51 +000018847 p = get_tv_string(&argvars[0]);
18848 rettv->vval.v_string = vim_strsave(p);
18849 simplify_filename(rettv->vval.v_string); /* simplify in place */
18850 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851}
18852
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018853#ifdef FEAT_FLOAT
18854/*
18855 * "sin()" function
18856 */
18857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018858f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018859{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018860 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018861
18862 rettv->v_type = VAR_FLOAT;
18863 if (get_float_arg(argvars, &f) == OK)
18864 rettv->vval.v_float = sin(f);
18865 else
18866 rettv->vval.v_float = 0.0;
18867}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018868
18869/*
18870 * "sinh()" function
18871 */
18872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018873f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018874{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018875 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018876
18877 rettv->v_type = VAR_FLOAT;
18878 if (get_float_arg(argvars, &f) == OK)
18879 rettv->vval.v_float = sinh(f);
18880 else
18881 rettv->vval.v_float = 0.0;
18882}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018883#endif
18884
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885static int
18886#ifdef __BORLANDC__
18887 _RTLENTRYF
18888#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018889 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018890static int
18891#ifdef __BORLANDC__
18892 _RTLENTRYF
18893#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018894 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018895
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018896/* struct used in the array that's given to qsort() */
18897typedef struct
18898{
18899 listitem_T *item;
18900 int idx;
18901} sortItem_T;
18902
Bram Moolenaar0b962472016-02-22 22:51:33 +010018903/* struct storing information about current sort */
18904typedef struct
18905{
18906 int item_compare_ic;
18907 int item_compare_numeric;
18908 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018909#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018910 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018911#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018912 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018913 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018914 dict_T *item_compare_selfdict;
18915 int item_compare_func_err;
18916 int item_compare_keep_zero;
18917} sortinfo_T;
18918static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018919static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920#define ITEM_COMPARE_FAIL 999
18921
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018923 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 static int
18926#ifdef __BORLANDC__
18927_RTLENTRYF
18928#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018929item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018931 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018932 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018933 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018934 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935 int res;
18936 char_u numbuf1[NUMBUFLEN];
18937 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018939 si1 = (sortItem_T *)s1;
18940 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018941 tv1 = &si1->item->li_tv;
18942 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018943
Bram Moolenaar0b962472016-02-22 22:51:33 +010018944 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018945 {
18946 long v1 = get_tv_number(tv1);
18947 long v2 = get_tv_number(tv2);
18948
18949 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18950 }
18951
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018952#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018953 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018954 {
18955 float_T v1 = get_tv_float(tv1);
18956 float_T v2 = get_tv_float(tv2);
18957
18958 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18959 }
18960#endif
18961
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018962 /* tv2string() puts quotes around a string and allocates memory. Don't do
18963 * that for string variables. Use a single quote when comparing with a
18964 * non-string to do what the docs promise. */
18965 if (tv1->v_type == VAR_STRING)
18966 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018967 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018968 p1 = (char_u *)"'";
18969 else
18970 p1 = tv1->vval.v_string;
18971 }
18972 else
18973 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18974 if (tv2->v_type == VAR_STRING)
18975 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018976 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018977 p2 = (char_u *)"'";
18978 else
18979 p2 = tv2->vval.v_string;
18980 }
18981 else
18982 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018983 if (p1 == NULL)
18984 p1 = (char_u *)"";
18985 if (p2 == NULL)
18986 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018987 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018988 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018989 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018990 res = STRICMP(p1, p2);
18991 else
18992 res = STRCMP(p1, p2);
18993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018994 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018995 {
18996 double n1, n2;
18997 n1 = strtod((char *)p1, (char **)&p1);
18998 n2 = strtod((char *)p2, (char **)&p2);
18999 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19000 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019001
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019002 /* When the result would be zero, compare the item indexes. Makes the
19003 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019004 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019005 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019006
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007 vim_free(tofree1);
19008 vim_free(tofree2);
19009 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010}
19011
19012 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013#ifdef __BORLANDC__
19014_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019016item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019018 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019019 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019020 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019021 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019022 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019023 char_u *func_name;
19024 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019026 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019027 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019028 return 0;
19029
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019030 si1 = (sortItem_T *)s1;
19031 si2 = (sortItem_T *)s2;
19032
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019033 if (partial == NULL)
19034 func_name = sortinfo->item_compare_func;
19035 else
19036 func_name = partial->pt_name;
19037
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019038 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019039 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019040 copy_tv(&si1->item->li_tv, &argv[0]);
19041 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042
19043 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019044 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019045 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019046 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047 clear_tv(&argv[0]);
19048 clear_tv(&argv[1]);
19049
19050 if (res == FAIL)
19051 res = ITEM_COMPARE_FAIL;
19052 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019053 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19054 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019055 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019057
19058 /* When the result would be zero, compare the pointers themselves. Makes
19059 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019060 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019061 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019062
Bram Moolenaar0d660222005-01-07 21:51:51 +000019063 return res;
19064}
19065
19066/*
19067 * "sort({list})" function
19068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019070do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071{
Bram Moolenaar33570922005-01-25 22:26:29 +000019072 list_T *l;
19073 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019074 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019075 sortinfo_T *old_sortinfo;
19076 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 long len;
19078 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079
Bram Moolenaar0b962472016-02-22 22:51:33 +010019080 /* Pointer to current info struct used in compare function. Save and
19081 * restore the current one for nested calls. */
19082 old_sortinfo = sortinfo;
19083 sortinfo = &info;
19084
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019086 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 else
19088 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019090 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019091 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19092 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019093 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019094 rettv->vval.v_list = l;
19095 rettv->v_type = VAR_LIST;
19096 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 len = list_len(l);
19099 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 info.item_compare_ic = FALSE;
19103 info.item_compare_numeric = FALSE;
19104 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019105#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019106 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019107#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019108 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019109 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019110 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111 if (argvars[1].v_type != VAR_UNKNOWN)
19112 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019113 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019116 else if (argvars[1].v_type == VAR_PARTIAL)
19117 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118 else
19119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019120 int error = FALSE;
19121
19122 i = get_tv_number_chk(&argvars[1], &error);
19123 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019125 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019127 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019128 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019129 else if (i != 0)
19130 {
19131 EMSG(_(e_invarg));
19132 goto theend;
19133 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019134 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019135 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019136 if (*info.item_compare_func == NUL)
19137 {
19138 /* empty string means default sort */
19139 info.item_compare_func = NULL;
19140 }
19141 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019142 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019143 info.item_compare_func = NULL;
19144 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019145 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019146 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019147 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 info.item_compare_func = NULL;
19149 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019150 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019151#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019153 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019154 info.item_compare_func = NULL;
19155 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019156 }
19157#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019159 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019160 info.item_compare_func = NULL;
19161 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019162 }
19163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019164 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019165
19166 if (argvars[2].v_type != VAR_UNKNOWN)
19167 {
19168 /* optional third argument: {dict} */
19169 if (argvars[2].v_type != VAR_DICT)
19170 {
19171 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019173 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019179 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182
Bram Moolenaar327aa022014-03-25 18:24:23 +010019183 i = 0;
19184 if (sort)
19185 {
19186 /* sort(): ptrs will be the list to sort */
19187 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019188 {
19189 ptrs[i].item = li;
19190 ptrs[i].idx = i;
19191 ++i;
19192 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019193
Bram Moolenaar0b962472016-02-22 22:51:33 +010019194 info.item_compare_func_err = FALSE;
19195 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019196 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019197 if ((info.item_compare_func != NULL
19198 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019199 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019200 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019201 EMSG(_("E702: Sort compare function failed"));
19202 else
19203 {
19204 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019205 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019206 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019207 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019209
Bram Moolenaar0b962472016-02-22 22:51:33 +010019210 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019211 {
19212 /* Clear the List and append the items in sorted order. */
19213 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19214 l->lv_len = 0;
19215 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019216 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019217 }
19218 }
19219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019221 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019222 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019223
19224 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019225 info.item_compare_func_err = FALSE;
19226 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019227 item_compare_func_ptr = info.item_compare_func != NULL
19228 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019229 ? item_compare2 : item_compare;
19230
19231 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19232 li = li->li_next)
19233 {
19234 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19235 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019236 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019237 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019238 {
19239 EMSG(_("E882: Uniq compare function failed"));
19240 break;
19241 }
19242 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019243
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019245 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019246 while (--i >= 0)
19247 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019248 li = ptrs[i].item->li_next;
19249 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019250 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019251 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019252 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019253 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019254 list_fix_watch(l, li);
19255 listitem_free(li);
19256 l->lv_len--;
19257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019258 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019259 }
19260
19261 vim_free(ptrs);
19262 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019263theend:
19264 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019265}
19266
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019267/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019268 * "sort({list})" function
19269 */
19270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019271f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019272{
19273 do_sort_uniq(argvars, rettv, TRUE);
19274}
19275
19276/*
19277 * "uniq({list})" function
19278 */
19279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019280f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019281{
19282 do_sort_uniq(argvars, rettv, FALSE);
19283}
19284
19285/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019286 * "soundfold({word})" function
19287 */
19288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019289f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019290{
19291 char_u *s;
19292
19293 rettv->v_type = VAR_STRING;
19294 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019295#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019296 rettv->vval.v_string = eval_soundfold(s);
19297#else
19298 rettv->vval.v_string = vim_strsave(s);
19299#endif
19300}
19301
19302/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019303 * "spellbadword()" function
19304 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019306f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019307{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019308 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019309 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019310 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019311
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019312 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019313 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019314
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019315#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019316 if (argvars[0].v_type == VAR_UNKNOWN)
19317 {
19318 /* Find the start and length of the badly spelled word. */
19319 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19320 if (len != 0)
19321 word = ml_get_cursor();
19322 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019323 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019324 {
19325 char_u *str = get_tv_string_chk(&argvars[0]);
19326 int capcol = -1;
19327
19328 if (str != NULL)
19329 {
19330 /* Check the argument for spelling. */
19331 while (*str != NUL)
19332 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019333 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019334 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019335 {
19336 word = str;
19337 break;
19338 }
19339 str += len;
19340 }
19341 }
19342 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019345 list_append_string(rettv->vval.v_list, word, len);
19346 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019347 attr == HLF_SPB ? "bad" :
19348 attr == HLF_SPR ? "rare" :
19349 attr == HLF_SPL ? "local" :
19350 attr == HLF_SPC ? "caps" :
19351 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019352}
19353
19354/*
19355 * "spellsuggest()" function
19356 */
19357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019358f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019359{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019360#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019361 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019362 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019363 int maxcount;
19364 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019365 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019366 listitem_T *li;
19367 int need_capital = FALSE;
19368#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019369
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019370 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019371 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019372
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019373#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019374 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019375 {
19376 str = get_tv_string(&argvars[0]);
19377 if (argvars[1].v_type != VAR_UNKNOWN)
19378 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019379 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019380 if (maxcount <= 0)
19381 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019382 if (argvars[2].v_type != VAR_UNKNOWN)
19383 {
19384 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19385 if (typeerr)
19386 return;
19387 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019388 }
19389 else
19390 maxcount = 25;
19391
Bram Moolenaar4770d092006-01-12 23:22:24 +000019392 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019393
19394 for (i = 0; i < ga.ga_len; ++i)
19395 {
19396 str = ((char_u **)ga.ga_data)[i];
19397
19398 li = listitem_alloc();
19399 if (li == NULL)
19400 vim_free(str);
19401 else
19402 {
19403 li->li_tv.v_type = VAR_STRING;
19404 li->li_tv.v_lock = 0;
19405 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019406 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019407 }
19408 }
19409 ga_clear(&ga);
19410 }
19411#endif
19412}
19413
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019415f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019416{
19417 char_u *str;
19418 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019419 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420 regmatch_T regmatch;
19421 char_u patbuf[NUMBUFLEN];
19422 char_u *save_cpo;
19423 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019425 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019426 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427
19428 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19429 save_cpo = p_cpo;
19430 p_cpo = (char_u *)"";
19431
19432 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019433 if (argvars[1].v_type != VAR_UNKNOWN)
19434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019435 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19436 if (pat == NULL)
19437 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019439 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019440 }
19441 if (pat == NULL || *pat == NUL)
19442 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019444 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019446 if (typeerr)
19447 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448
Bram Moolenaar0d660222005-01-07 21:51:51 +000019449 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19450 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019452 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019453 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019454 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019455 if (*str == NUL)
19456 match = FALSE; /* empty item at the end */
19457 else
19458 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019459 if (match)
19460 end = regmatch.startp[0];
19461 else
19462 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019463 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19464 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019466 if (list_append_string(rettv->vval.v_list, str,
19467 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019468 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019469 }
19470 if (!match)
19471 break;
19472 /* Advance to just after the match. */
19473 if (regmatch.endp[0] > str)
19474 col = 0;
19475 else
19476 {
19477 /* Don't get stuck at the same match. */
19478#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019479 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019480#else
19481 col = 1;
19482#endif
19483 }
19484 str = regmatch.endp[0];
19485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486
Bram Moolenaar473de612013-06-08 18:19:48 +020019487 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489
Bram Moolenaar0d660222005-01-07 21:51:51 +000019490 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491}
19492
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019493#ifdef FEAT_FLOAT
19494/*
19495 * "sqrt()" function
19496 */
19497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019498f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019499{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019500 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019501
19502 rettv->v_type = VAR_FLOAT;
19503 if (get_float_arg(argvars, &f) == OK)
19504 rettv->vval.v_float = sqrt(f);
19505 else
19506 rettv->vval.v_float = 0.0;
19507}
19508
19509/*
19510 * "str2float()" function
19511 */
19512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019513f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019514{
19515 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19516
19517 if (*p == '+')
19518 p = skipwhite(p + 1);
19519 (void)string2float(p, &rettv->vval.v_float);
19520 rettv->v_type = VAR_FLOAT;
19521}
19522#endif
19523
Bram Moolenaar2c932302006-03-18 21:42:09 +000019524/*
19525 * "str2nr()" function
19526 */
19527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019528f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019529{
19530 int base = 10;
19531 char_u *p;
19532 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019533 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019534
19535 if (argvars[1].v_type != VAR_UNKNOWN)
19536 {
19537 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019538 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019539 {
19540 EMSG(_(e_invarg));
19541 return;
19542 }
19543 }
19544
19545 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019546 if (*p == '+')
19547 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019548 switch (base)
19549 {
19550 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19551 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19552 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19553 default: what = 0;
19554 }
19555 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019556 rettv->vval.v_number = n;
19557}
19558
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559#ifdef HAVE_STRFTIME
19560/*
19561 * "strftime({format}[, {time}])" function
19562 */
19563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019564f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565{
19566 char_u result_buf[256];
19567 struct tm *curtime;
19568 time_t seconds;
19569 char_u *p;
19570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019574 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 seconds = time(NULL);
19576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 curtime = localtime(&seconds);
19579 /* MSVC returns NULL for an invalid value of seconds. */
19580 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019581 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 else
19583 {
19584# ifdef FEAT_MBYTE
19585 vimconv_T conv;
19586 char_u *enc;
19587
19588 conv.vc_type = CONV_NONE;
19589 enc = enc_locale();
19590 convert_setup(&conv, p_enc, enc);
19591 if (conv.vc_type != CONV_NONE)
19592 p = string_convert(&conv, p, NULL);
19593# endif
19594 if (p != NULL)
19595 (void)strftime((char *)result_buf, sizeof(result_buf),
19596 (char *)p, curtime);
19597 else
19598 result_buf[0] = NUL;
19599
19600# ifdef FEAT_MBYTE
19601 if (conv.vc_type != CONV_NONE)
19602 vim_free(p);
19603 convert_setup(&conv, enc, p_enc);
19604 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 else
19607# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019608 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
19610# ifdef FEAT_MBYTE
19611 /* Release conversion descriptors */
19612 convert_setup(&conv, NULL, NULL);
19613 vim_free(enc);
19614# endif
19615 }
19616}
19617#endif
19618
19619/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019620 * "strgetchar()" function
19621 */
19622 static void
19623f_strgetchar(typval_T *argvars, typval_T *rettv)
19624{
19625 char_u *str;
19626 int len;
19627 int error = FALSE;
19628 int charidx;
19629
19630 rettv->vval.v_number = -1;
19631 str = get_tv_string_chk(&argvars[0]);
19632 if (str == NULL)
19633 return;
19634 len = (int)STRLEN(str);
19635 charidx = get_tv_number_chk(&argvars[1], &error);
19636 if (error)
19637 return;
19638#ifdef FEAT_MBYTE
19639 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019640 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019641
19642 while (charidx >= 0 && byteidx < len)
19643 {
19644 if (charidx == 0)
19645 {
19646 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19647 break;
19648 }
19649 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019650 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019651 }
19652 }
19653#else
19654 if (charidx < len)
19655 rettv->vval.v_number = str[charidx];
19656#endif
19657}
19658
19659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 * "stridx()" function
19661 */
19662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019663f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664{
19665 char_u buf[NUMBUFLEN];
19666 char_u *needle;
19667 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019668 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019672 needle = get_tv_string_chk(&argvars[1]);
19673 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019674 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019675 if (needle == NULL || haystack == NULL)
19676 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677
Bram Moolenaar33570922005-01-25 22:26:29 +000019678 if (argvars[2].v_type != VAR_UNKNOWN)
19679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019680 int error = FALSE;
19681
19682 start_idx = get_tv_number_chk(&argvars[2], &error);
19683 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019685 if (start_idx >= 0)
19686 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 }
19688
19689 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19690 if (pos != NULL)
19691 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692}
19693
19694/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019695 * "string()" function
19696 */
19697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019698f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019699{
19700 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019701 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019703 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019704 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19705 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019706 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019707 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019708 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709}
19710
19711/*
19712 * "strlen()" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->vval.v_number = (varnumber_T)(STRLEN(
19718 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719}
19720
19721/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019722 * "strchars()" function
19723 */
19724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019725f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019726{
19727 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019728 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019729#ifdef FEAT_MBYTE
19730 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019731 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019732#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019733
19734 if (argvars[1].v_type != VAR_UNKNOWN)
19735 skipcc = get_tv_number_chk(&argvars[1], NULL);
19736 if (skipcc < 0 || skipcc > 1)
19737 EMSG(_(e_invarg));
19738 else
19739 {
19740#ifdef FEAT_MBYTE
19741 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19742 while (*s != NUL)
19743 {
19744 func_mb_ptr2char_adv(&s);
19745 ++len;
19746 }
19747 rettv->vval.v_number = len;
19748#else
19749 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19750#endif
19751 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019752}
19753
19754/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019755 * "strdisplaywidth()" function
19756 */
19757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019758f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019759{
19760 char_u *s = get_tv_string(&argvars[0]);
19761 int col = 0;
19762
19763 if (argvars[1].v_type != VAR_UNKNOWN)
19764 col = get_tv_number(&argvars[1]);
19765
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019766 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019767}
19768
19769/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019770 * "strwidth()" function
19771 */
19772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019773f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019774{
19775 char_u *s = get_tv_string(&argvars[0]);
19776
19777 rettv->vval.v_number = (varnumber_T)(
19778#ifdef FEAT_MBYTE
19779 mb_string2cells(s, -1)
19780#else
19781 STRLEN(s)
19782#endif
19783 );
19784}
19785
19786/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019787 * "strcharpart()" function
19788 */
19789 static void
19790f_strcharpart(typval_T *argvars, typval_T *rettv)
19791{
19792#ifdef FEAT_MBYTE
19793 char_u *p;
19794 int nchar;
19795 int nbyte = 0;
19796 int charlen;
19797 int len = 0;
19798 int slen;
19799 int error = FALSE;
19800
19801 p = get_tv_string(&argvars[0]);
19802 slen = (int)STRLEN(p);
19803
19804 nchar = get_tv_number_chk(&argvars[1], &error);
19805 if (!error)
19806 {
19807 if (nchar > 0)
19808 while (nchar > 0 && nbyte < slen)
19809 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019810 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019811 --nchar;
19812 }
19813 else
19814 nbyte = nchar;
19815 if (argvars[2].v_type != VAR_UNKNOWN)
19816 {
19817 charlen = get_tv_number(&argvars[2]);
19818 while (charlen > 0 && nbyte + len < slen)
19819 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019820 int off = nbyte + len;
19821
19822 if (off < 0)
19823 len += 1;
19824 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019825 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019826 --charlen;
19827 }
19828 }
19829 else
19830 len = slen - nbyte; /* default: all bytes that are available. */
19831 }
19832
19833 /*
19834 * Only return the overlap between the specified part and the actual
19835 * string.
19836 */
19837 if (nbyte < 0)
19838 {
19839 len += nbyte;
19840 nbyte = 0;
19841 }
19842 else if (nbyte > slen)
19843 nbyte = slen;
19844 if (len < 0)
19845 len = 0;
19846 else if (nbyte + len > slen)
19847 len = slen - nbyte;
19848
19849 rettv->v_type = VAR_STRING;
19850 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19851#else
19852 f_strpart(argvars, rettv);
19853#endif
19854}
19855
19856/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 * "strpart()" function
19858 */
19859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019860f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861{
19862 char_u *p;
19863 int n;
19864 int len;
19865 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019866 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 slen = (int)STRLEN(p);
19870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019871 n = get_tv_number_chk(&argvars[1], &error);
19872 if (error)
19873 len = 0;
19874 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 else
19877 len = slen - n; /* default len: all bytes that are available. */
19878
19879 /*
19880 * Only return the overlap between the specified part and the actual
19881 * string.
19882 */
19883 if (n < 0)
19884 {
19885 len += n;
19886 n = 0;
19887 }
19888 else if (n > slen)
19889 n = slen;
19890 if (len < 0)
19891 len = 0;
19892 else if (n + len > slen)
19893 len = slen - n;
19894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019895 rettv->v_type = VAR_STRING;
19896 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897}
19898
19899/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019900 * "strridx()" function
19901 */
19902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019903f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019904{
19905 char_u buf[NUMBUFLEN];
19906 char_u *needle;
19907 char_u *haystack;
19908 char_u *rest;
19909 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019910 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019912 needle = get_tv_string_chk(&argvars[1]);
19913 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019914
19915 rettv->vval.v_number = -1;
19916 if (needle == NULL || haystack == NULL)
19917 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019918
19919 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019920 if (argvars[2].v_type != VAR_UNKNOWN)
19921 {
19922 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019923 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019924 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019925 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019926 }
19927 else
19928 end_idx = haystack_len;
19929
Bram Moolenaar0d660222005-01-07 21:51:51 +000019930 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019931 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019932 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019933 lastmatch = haystack + end_idx;
19934 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019935 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019936 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019937 for (rest = haystack; *rest != '\0'; ++rest)
19938 {
19939 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019940 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019941 break;
19942 lastmatch = rest;
19943 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019944 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019945
19946 if (lastmatch == NULL)
19947 rettv->vval.v_number = -1;
19948 else
19949 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19950}
19951
19952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 * "strtrans()" function
19954 */
19955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019956f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958 rettv->v_type = VAR_STRING;
19959 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960}
19961
19962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019963 * "submatch()" function
19964 */
19965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019966f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019967{
Bram Moolenaar41571762014-04-02 19:00:58 +020019968 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019969 int no;
19970 int retList = 0;
19971
19972 no = (int)get_tv_number_chk(&argvars[0], &error);
19973 if (error)
19974 return;
19975 error = FALSE;
19976 if (argvars[1].v_type != VAR_UNKNOWN)
19977 retList = get_tv_number_chk(&argvars[1], &error);
19978 if (error)
19979 return;
19980
19981 if (retList == 0)
19982 {
19983 rettv->v_type = VAR_STRING;
19984 rettv->vval.v_string = reg_submatch(no);
19985 }
19986 else
19987 {
19988 rettv->v_type = VAR_LIST;
19989 rettv->vval.v_list = reg_submatch_list(no);
19990 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019991}
19992
19993/*
19994 * "substitute()" function
19995 */
19996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019997f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019998{
19999 char_u patbuf[NUMBUFLEN];
20000 char_u subbuf[NUMBUFLEN];
20001 char_u flagsbuf[NUMBUFLEN];
20002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020003 char_u *str = get_tv_string_chk(&argvars[0]);
20004 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20005 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20006 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20007
Bram Moolenaar0d660222005-01-07 21:51:51 +000020008 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020009 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20010 rettv->vval.v_string = NULL;
20011 else
20012 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020013}
20014
20015/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020016 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020019f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020{
20021 int id = 0;
20022#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020023 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020024 long col;
20025 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020026 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020028 lnum = get_tv_lnum(argvars); /* -1 on type error */
20029 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20030 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020033 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020034 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035#endif
20036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020037 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038}
20039
20040/*
20041 * "synIDattr(id, what [, mode])" function
20042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020044f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045{
20046 char_u *p = NULL;
20047#ifdef FEAT_SYN_HL
20048 int id;
20049 char_u *what;
20050 char_u *mode;
20051 char_u modebuf[NUMBUFLEN];
20052 int modec;
20053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020054 id = get_tv_number(&argvars[0]);
20055 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020058 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020060 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 modec = 0; /* replace invalid with current */
20062 }
20063 else
20064 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020065#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020066 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 modec = 'g';
20068 else
20069#endif
20070 if (t_colors > 1)
20071 modec = 'c';
20072 else
20073 modec = 't';
20074 }
20075
20076
20077 switch (TOLOWER_ASC(what[0]))
20078 {
20079 case 'b':
20080 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20081 p = highlight_color(id, what, modec);
20082 else /* bold */
20083 p = highlight_has_attr(id, HL_BOLD, modec);
20084 break;
20085
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020086 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 p = highlight_color(id, what, modec);
20088 break;
20089
20090 case 'i':
20091 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20092 p = highlight_has_attr(id, HL_INVERSE, modec);
20093 else /* italic */
20094 p = highlight_has_attr(id, HL_ITALIC, modec);
20095 break;
20096
20097 case 'n': /* name */
20098 p = get_highlight_name(NULL, id - 1);
20099 break;
20100
20101 case 'r': /* reverse */
20102 p = highlight_has_attr(id, HL_INVERSE, modec);
20103 break;
20104
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020105 case 's':
20106 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20107 p = highlight_color(id, what, modec);
20108 else /* standout */
20109 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 break;
20111
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020112 case 'u':
20113 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20114 /* underline */
20115 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20116 else
20117 /* undercurl */
20118 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 break;
20120 }
20121
20122 if (p != NULL)
20123 p = vim_strsave(p);
20124#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020125 rettv->v_type = VAR_STRING;
20126 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127}
20128
20129/*
20130 * "synIDtrans(id)" function
20131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020133f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134{
20135 int id;
20136
20137#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139
20140 if (id > 0)
20141 id = syn_get_final_id(id);
20142 else
20143#endif
20144 id = 0;
20145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147}
20148
20149/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020150 * "synconcealed(lnum, col)" function
20151 */
20152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020153f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020154{
20155#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20156 long lnum;
20157 long col;
20158 int syntax_flags = 0;
20159 int cchar;
20160 int matchid = 0;
20161 char_u str[NUMBUFLEN];
20162#endif
20163
20164 rettv->v_type = VAR_LIST;
20165 rettv->vval.v_list = NULL;
20166
20167#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20168 lnum = get_tv_lnum(argvars); /* -1 on type error */
20169 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20170
20171 vim_memset(str, NUL, sizeof(str));
20172
20173 if (rettv_list_alloc(rettv) != FAIL)
20174 {
20175 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20176 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20177 && curwin->w_p_cole > 0)
20178 {
20179 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20180 syntax_flags = get_syntax_info(&matchid);
20181
20182 /* get the conceal character */
20183 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20184 {
20185 cchar = syn_get_sub_char();
20186 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20187 cchar = lcs_conceal;
20188 if (cchar != NUL)
20189 {
20190# ifdef FEAT_MBYTE
20191 if (has_mbyte)
20192 (*mb_char2bytes)(cchar, str);
20193 else
20194# endif
20195 str[0] = cchar;
20196 }
20197 }
20198 }
20199
20200 list_append_number(rettv->vval.v_list,
20201 (syntax_flags & HL_CONCEAL) != 0);
20202 /* -1 to auto-determine strlen */
20203 list_append_string(rettv->vval.v_list, str, -1);
20204 list_append_number(rettv->vval.v_list, matchid);
20205 }
20206#endif
20207}
20208
20209/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020210 * "synstack(lnum, col)" function
20211 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020213f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020214{
20215#ifdef FEAT_SYN_HL
20216 long lnum;
20217 long col;
20218 int i;
20219 int id;
20220#endif
20221
20222 rettv->v_type = VAR_LIST;
20223 rettv->vval.v_list = NULL;
20224
20225#ifdef FEAT_SYN_HL
20226 lnum = get_tv_lnum(argvars); /* -1 on type error */
20227 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20228
20229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020230 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020231 && rettv_list_alloc(rettv) != FAIL)
20232 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020233 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020234 for (i = 0; ; ++i)
20235 {
20236 id = syn_get_stack_item(i);
20237 if (id < 0)
20238 break;
20239 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20240 break;
20241 }
20242 }
20243#endif
20244}
20245
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020247get_cmd_output_as_rettv(
20248 typval_T *argvars,
20249 typval_T *rettv,
20250 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020252 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020254 char_u *infile = NULL;
20255 char_u buf[NUMBUFLEN];
20256 int err = FALSE;
20257 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020258 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020259 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020261 rettv->v_type = VAR_STRING;
20262 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020263 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020264 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020265
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020266 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020267 {
20268 /*
20269 * Write the string to a temp file, to be used for input of the shell
20270 * command.
20271 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020272 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020273 {
20274 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020275 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020276 }
20277
20278 fd = mch_fopen((char *)infile, WRITEBIN);
20279 if (fd == NULL)
20280 {
20281 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020282 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020283 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020284 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020285 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020286 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20287 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020288 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020289 else
20290 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020291 size_t len;
20292
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020293 p = get_tv_string_buf_chk(&argvars[1], buf);
20294 if (p == NULL)
20295 {
20296 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020297 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020298 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020299 len = STRLEN(p);
20300 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020301 err = TRUE;
20302 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020303 if (fclose(fd) != 0)
20304 err = TRUE;
20305 if (err)
20306 {
20307 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020308 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020309 }
20310 }
20311
Bram Moolenaar52a72462014-08-29 15:53:52 +020020312 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20313 * echoes typeahead, that messes up the display. */
20314 if (!msg_silent)
20315 flags += SHELL_COOKED;
20316
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020317 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020319 int len;
20320 listitem_T *li;
20321 char_u *s = NULL;
20322 char_u *start;
20323 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020324 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325
Bram Moolenaar52a72462014-08-29 15:53:52 +020020326 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020327 if (res == NULL)
20328 goto errret;
20329
20330 list = list_alloc();
20331 if (list == NULL)
20332 goto errret;
20333
20334 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020336 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020337 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020338 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020339 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020340
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020341 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020342 if (s == NULL)
20343 goto errret;
20344
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020345 for (p = s; start < end; ++p, ++start)
20346 *p = *start == NUL ? NL : *start;
20347 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020348
20349 li = listitem_alloc();
20350 if (li == NULL)
20351 {
20352 vim_free(s);
20353 goto errret;
20354 }
20355 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020356 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020357 li->li_tv.vval.v_string = s;
20358 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020360
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020361 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020362 rettv->v_type = VAR_LIST;
20363 rettv->vval.v_list = list;
20364 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366 else
20367 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020368 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020369#ifdef USE_CR
20370 /* translate <CR> into <NL> */
20371 if (res != NULL)
20372 {
20373 char_u *s;
20374
20375 for (s = res; *s; ++s)
20376 {
20377 if (*s == CAR)
20378 *s = NL;
20379 }
20380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381#else
20382# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020383 /* translate <CR><NL> into <NL> */
20384 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386 char_u *s, *d;
20387
20388 d = res;
20389 for (s = res; *s; ++s)
20390 {
20391 if (s[0] == CAR && s[1] == NL)
20392 ++s;
20393 *d++ = *s;
20394 }
20395 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397# endif
20398#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020399 rettv->vval.v_string = res;
20400 res = NULL;
20401 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020402
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020403errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020404 if (infile != NULL)
20405 {
20406 mch_remove(infile);
20407 vim_free(infile);
20408 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020409 if (res != NULL)
20410 vim_free(res);
20411 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020412 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020413}
20414
20415/*
20416 * "system()" function
20417 */
20418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020419f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020420{
20421 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20422}
20423
20424/*
20425 * "systemlist()" function
20426 */
20427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020428f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020429{
20430 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431}
20432
20433/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020434 * "tabpagebuflist()" function
20435 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020437f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020438{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020439#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020440 tabpage_T *tp;
20441 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020442
20443 if (argvars[0].v_type == VAR_UNKNOWN)
20444 wp = firstwin;
20445 else
20446 {
20447 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20448 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020449 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020450 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020451 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020453 for (; wp != NULL; wp = wp->w_next)
20454 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020455 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020456 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020457 }
20458#endif
20459}
20460
20461
20462/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020463 * "tabpagenr()" function
20464 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020467{
20468 int nr = 1;
20469#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020470 char_u *arg;
20471
20472 if (argvars[0].v_type != VAR_UNKNOWN)
20473 {
20474 arg = get_tv_string_chk(&argvars[0]);
20475 nr = 0;
20476 if (arg != NULL)
20477 {
20478 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020479 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020480 else
20481 EMSG2(_(e_invexpr2), arg);
20482 }
20483 }
20484 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020485 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020486#endif
20487 rettv->vval.v_number = nr;
20488}
20489
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020490
20491#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020492static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020493
20494/*
20495 * Common code for tabpagewinnr() and winnr().
20496 */
20497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020498get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020499{
20500 win_T *twin;
20501 int nr = 1;
20502 win_T *wp;
20503 char_u *arg;
20504
20505 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20506 if (argvar->v_type != VAR_UNKNOWN)
20507 {
20508 arg = get_tv_string_chk(argvar);
20509 if (arg == NULL)
20510 nr = 0; /* type error; errmsg already given */
20511 else if (STRCMP(arg, "$") == 0)
20512 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20513 else if (STRCMP(arg, "#") == 0)
20514 {
20515 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20516 if (twin == NULL)
20517 nr = 0;
20518 }
20519 else
20520 {
20521 EMSG2(_(e_invexpr2), arg);
20522 nr = 0;
20523 }
20524 }
20525
20526 if (nr > 0)
20527 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20528 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020529 {
20530 if (wp == NULL)
20531 {
20532 /* didn't find it in this tabpage */
20533 nr = 0;
20534 break;
20535 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020536 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020537 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020538 return nr;
20539}
20540#endif
20541
20542/*
20543 * "tabpagewinnr()" function
20544 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020546f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020547{
20548 int nr = 1;
20549#ifdef FEAT_WINDOWS
20550 tabpage_T *tp;
20551
20552 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20553 if (tp == NULL)
20554 nr = 0;
20555 else
20556 nr = get_winnr(tp, &argvars[1]);
20557#endif
20558 rettv->vval.v_number = nr;
20559}
20560
20561
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020562/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020563 * "tagfiles()" function
20564 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020566f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020567{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020568 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020569 tagname_T tn;
20570 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020571
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020572 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020573 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020574 fname = alloc(MAXPATHL);
20575 if (fname == NULL)
20576 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020577
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020578 for (first = TRUE; ; first = FALSE)
20579 if (get_tagfname(&tn, first, fname) == FAIL
20580 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020581 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020582 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020583 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020584}
20585
20586/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020587 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020588 */
20589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020590f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020591{
20592 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020593
20594 tag_pattern = get_tv_string(&argvars[0]);
20595
20596 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020597 if (*tag_pattern == NUL)
20598 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020599
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020600 if (rettv_list_alloc(rettv) == OK)
20601 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020602}
20603
20604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 * "tempname()" function
20606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020608f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609{
20610 static int x = 'A';
20611
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020612 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020613 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614
20615 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20616 * names. Skip 'I' and 'O', they are used for shell redirection. */
20617 do
20618 {
20619 if (x == 'Z')
20620 x = '0';
20621 else if (x == '9')
20622 x = 'A';
20623 else
20624 {
20625#ifdef EBCDIC
20626 if (x == 'I')
20627 x = 'J';
20628 else if (x == 'R')
20629 x = 'S';
20630 else
20631#endif
20632 ++x;
20633 }
20634 } while (x == 'I' || x == 'O');
20635}
20636
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020637#ifdef FEAT_FLOAT
20638/*
20639 * "tan()" function
20640 */
20641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020642f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020643{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020644 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020645
20646 rettv->v_type = VAR_FLOAT;
20647 if (get_float_arg(argvars, &f) == OK)
20648 rettv->vval.v_float = tan(f);
20649 else
20650 rettv->vval.v_float = 0.0;
20651}
20652
20653/*
20654 * "tanh()" function
20655 */
20656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020657f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020658{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020659 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020660
20661 rettv->v_type = VAR_FLOAT;
20662 if (get_float_arg(argvars, &f) == OK)
20663 rettv->vval.v_float = tanh(f);
20664 else
20665 rettv->vval.v_float = 0.0;
20666}
20667#endif
20668
Bram Moolenaar574860b2016-05-24 17:33:34 +020020669/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020670 * "test_alloc_fail(id, countdown, repeat)" function
20671 */
20672 static void
20673f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20674{
20675 if (argvars[0].v_type != VAR_NUMBER
20676 || argvars[0].vval.v_number <= 0
20677 || argvars[1].v_type != VAR_NUMBER
20678 || argvars[1].vval.v_number < 0
20679 || argvars[2].v_type != VAR_NUMBER)
20680 EMSG(_(e_invarg));
20681 else
20682 {
20683 alloc_fail_id = argvars[0].vval.v_number;
20684 if (alloc_fail_id >= aid_last)
20685 EMSG(_(e_invarg));
20686 alloc_fail_countdown = argvars[1].vval.v_number;
20687 alloc_fail_repeat = argvars[2].vval.v_number;
20688 did_outofmem_msg = FALSE;
20689 }
20690}
20691
20692/*
20693 * "test_disable_char_avail({expr})" function
20694 */
20695 static void
20696f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20697{
20698 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20699}
20700
20701/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020702 * "test_garbagecollect_now()" function
20703 */
20704 static void
20705f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20706{
20707 /* This is dangerous, any Lists and Dicts used internally may be freed
20708 * while still in use. */
20709 garbage_collect(TRUE);
20710}
20711
20712#ifdef FEAT_JOB_CHANNEL
20713 static void
20714f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20715{
20716 rettv->v_type = VAR_CHANNEL;
20717 rettv->vval.v_channel = NULL;
20718}
20719#endif
20720
20721 static void
20722f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20723{
20724 rettv->v_type = VAR_DICT;
20725 rettv->vval.v_dict = NULL;
20726}
20727
20728#ifdef FEAT_JOB_CHANNEL
20729 static void
20730f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20731{
20732 rettv->v_type = VAR_JOB;
20733 rettv->vval.v_job = NULL;
20734}
20735#endif
20736
20737 static void
20738f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20739{
20740 rettv->v_type = VAR_LIST;
20741 rettv->vval.v_list = NULL;
20742}
20743
20744 static void
20745f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20746{
20747 rettv->v_type = VAR_PARTIAL;
20748 rettv->vval.v_partial = NULL;
20749}
20750
20751 static void
20752f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20753{
20754 rettv->v_type = VAR_STRING;
20755 rettv->vval.v_string = NULL;
20756}
20757
Bram Moolenaar975b5272016-03-15 23:10:59 +010020758#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20759/*
20760 * Get a callback from "arg". It can be a Funcref or a function name.
20761 * When "arg" is zero return an empty string.
20762 * Return NULL for an invalid argument.
20763 */
20764 char_u *
20765get_callback(typval_T *arg, partial_T **pp)
20766{
20767 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20768 {
20769 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020770 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020771 return (*pp)->pt_name;
20772 }
20773 *pp = NULL;
20774 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20775 return arg->vval.v_string;
20776 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20777 return (char_u *)"";
20778 EMSG(_("E921: Invalid callback argument"));
20779 return NULL;
20780}
20781#endif
20782
20783#ifdef FEAT_TIMERS
20784/*
20785 * "timer_start(time, callback [, options])" function
20786 */
20787 static void
20788f_timer_start(typval_T *argvars, typval_T *rettv)
20789{
20790 long msec = get_tv_number(&argvars[0]);
20791 timer_T *timer;
20792 int repeat = 0;
20793 char_u *callback;
20794 dict_T *dict;
20795
Bram Moolenaar38499922016-04-22 20:46:52 +020020796 if (check_secure())
20797 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020798 if (argvars[2].v_type != VAR_UNKNOWN)
20799 {
20800 if (argvars[2].v_type != VAR_DICT
20801 || (dict = argvars[2].vval.v_dict) == NULL)
20802 {
20803 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20804 return;
20805 }
20806 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20807 repeat = get_dict_number(dict, (char_u *)"repeat");
20808 }
20809
20810 timer = create_timer(msec, repeat);
20811 callback = get_callback(&argvars[1], &timer->tr_partial);
20812 if (callback == NULL)
20813 {
20814 stop_timer(timer);
20815 rettv->vval.v_number = -1;
20816 }
20817 else
20818 {
20819 timer->tr_callback = vim_strsave(callback);
20820 rettv->vval.v_number = timer->tr_id;
20821 }
20822}
20823
20824/*
20825 * "timer_stop(timer)" function
20826 */
20827 static void
20828f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20829{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020830 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020831
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020832 if (argvars[0].v_type != VAR_NUMBER)
20833 {
20834 EMSG(_(e_number_exp));
20835 return;
20836 }
20837 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020838 if (timer != NULL)
20839 stop_timer(timer);
20840}
20841#endif
20842
Bram Moolenaard52d9742005-08-21 22:20:28 +000020843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 * "tolower(string)" function
20845 */
20846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020847f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848{
20849 char_u *p;
20850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020851 p = vim_strsave(get_tv_string(&argvars[0]));
20852 rettv->v_type = VAR_STRING;
20853 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854
20855 if (p != NULL)
20856 while (*p != NUL)
20857 {
20858#ifdef FEAT_MBYTE
20859 int l;
20860
20861 if (enc_utf8)
20862 {
20863 int c, lc;
20864
20865 c = utf_ptr2char(p);
20866 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020867 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 /* TODO: reallocate string when byte count changes. */
20869 if (utf_char2len(lc) == l)
20870 utf_char2bytes(lc, p);
20871 p += l;
20872 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020873 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 p += l; /* skip multi-byte character */
20875 else
20876#endif
20877 {
20878 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20879 ++p;
20880 }
20881 }
20882}
20883
20884/*
20885 * "toupper(string)" function
20886 */
20887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020888f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020890 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020891 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892}
20893
20894/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020895 * "tr(string, fromstr, tostr)" function
20896 */
20897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020898f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020900 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020901 char_u *fromstr;
20902 char_u *tostr;
20903 char_u *p;
20904#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020905 int inlen;
20906 int fromlen;
20907 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020908 int idx;
20909 char_u *cpstr;
20910 int cplen;
20911 int first = TRUE;
20912#endif
20913 char_u buf[NUMBUFLEN];
20914 char_u buf2[NUMBUFLEN];
20915 garray_T ga;
20916
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020917 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020918 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20919 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020920
20921 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020922 rettv->v_type = VAR_STRING;
20923 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020924 if (fromstr == NULL || tostr == NULL)
20925 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020926 ga_init2(&ga, (int)sizeof(char), 80);
20927
20928#ifdef FEAT_MBYTE
20929 if (!has_mbyte)
20930#endif
20931 /* not multi-byte: fromstr and tostr must be the same length */
20932 if (STRLEN(fromstr) != STRLEN(tostr))
20933 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020934#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020935error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020936#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020937 EMSG2(_(e_invarg2), fromstr);
20938 ga_clear(&ga);
20939 return;
20940 }
20941
20942 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020943 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020944 {
20945#ifdef FEAT_MBYTE
20946 if (has_mbyte)
20947 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020948 inlen = (*mb_ptr2len)(in_str);
20949 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020950 cplen = inlen;
20951 idx = 0;
20952 for (p = fromstr; *p != NUL; p += fromlen)
20953 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020954 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020955 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020956 {
20957 for (p = tostr; *p != NUL; p += tolen)
20958 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020959 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020960 if (idx-- == 0)
20961 {
20962 cplen = tolen;
20963 cpstr = p;
20964 break;
20965 }
20966 }
20967 if (*p == NUL) /* tostr is shorter than fromstr */
20968 goto error;
20969 break;
20970 }
20971 ++idx;
20972 }
20973
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020974 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020975 {
20976 /* Check that fromstr and tostr have the same number of
20977 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020978 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020979 first = FALSE;
20980 for (p = tostr; *p != NUL; p += tolen)
20981 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020982 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020983 --idx;
20984 }
20985 if (idx != 0)
20986 goto error;
20987 }
20988
Bram Moolenaarcde88542015-08-11 19:14:00 +020020989 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020990 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020991 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020992
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020993 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020994 }
20995 else
20996#endif
20997 {
20998 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020999 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021000 if (p != NULL)
21001 ga_append(&ga, tostr[p - fromstr]);
21002 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021003 ga_append(&ga, *in_str);
21004 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021005 }
21006 }
21007
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021008 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021009 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021010 ga_append(&ga, NUL);
21011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021012 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021013}
21014
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021015#ifdef FEAT_FLOAT
21016/*
21017 * "trunc({float})" function
21018 */
21019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021020f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021021{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021022 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021023
21024 rettv->v_type = VAR_FLOAT;
21025 if (get_float_arg(argvars, &f) == OK)
21026 /* trunc() is not in C90, use floor() or ceil() instead. */
21027 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21028 else
21029 rettv->vval.v_float = 0.0;
21030}
21031#endif
21032
Bram Moolenaar8299df92004-07-10 09:47:34 +000021033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 * "type(expr)" function
21035 */
21036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021039 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021040
21041 switch (argvars[0].v_type)
21042 {
21043 case VAR_NUMBER: n = 0; break;
21044 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021045 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021046 case VAR_FUNC: n = 2; break;
21047 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021048 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021049 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021050 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021051 if (argvars[0].vval.v_number == VVAL_FALSE
21052 || argvars[0].vval.v_number == VVAL_TRUE)
21053 n = 6;
21054 else
21055 n = 7;
21056 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021057 case VAR_JOB: n = 8; break;
21058 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021059 case VAR_UNKNOWN:
21060 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21061 n = -1;
21062 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021063 }
21064 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021065}
21066
21067/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021068 * "undofile(name)" function
21069 */
21070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021072{
21073 rettv->v_type = VAR_STRING;
21074#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021075 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021076 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021077
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021078 if (*fname == NUL)
21079 {
21080 /* If there is no file name there will be no undo file. */
21081 rettv->vval.v_string = NULL;
21082 }
21083 else
21084 {
21085 char_u *ffname = FullName_save(fname, FALSE);
21086
21087 if (ffname != NULL)
21088 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21089 vim_free(ffname);
21090 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021091 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021092#else
21093 rettv->vval.v_string = NULL;
21094#endif
21095}
21096
21097/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021098 * "undotree()" function
21099 */
21100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021101f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021102{
21103 if (rettv_dict_alloc(rettv) == OK)
21104 {
21105 dict_T *dict = rettv->vval.v_dict;
21106 list_T *list;
21107
Bram Moolenaar730cde92010-06-27 05:18:54 +020021108 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021109 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021110 dict_add_nr_str(dict, "save_last",
21111 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021112 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21113 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021114 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021115
21116 list = list_alloc();
21117 if (list != NULL)
21118 {
21119 u_eval_tree(curbuf->b_u_oldhead, list);
21120 dict_add_list(dict, "entries", list);
21121 }
21122 }
21123}
21124
21125/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021126 * "values(dict)" function
21127 */
21128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021129f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021130{
21131 dict_list(argvars, rettv, 1);
21132}
21133
21134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 * "virtcol(string)" function
21136 */
21137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021138f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139{
21140 colnr_T vcol = 0;
21141 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021142 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021144 fp = var2fpos(&argvars[0], FALSE, &fnum);
21145 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21146 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147 {
21148 getvvcol(curwin, fp, NULL, NULL, &vcol);
21149 ++vcol;
21150 }
21151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021152 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153}
21154
21155/*
21156 * "visualmode()" function
21157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021159f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 char_u str[2];
21162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 str[0] = curbuf->b_visual_mode_eval;
21165 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021166 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167
21168 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021169 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171}
21172
21173/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021174 * "wildmenumode()" function
21175 */
21176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021177f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021178{
21179#ifdef FEAT_WILDMENU
21180 if (wild_menu_showing)
21181 rettv->vval.v_number = 1;
21182#endif
21183}
21184
21185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 * "winbufnr(nr)" function
21187 */
21188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021189f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190{
21191 win_T *wp;
21192
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021193 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021197 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198}
21199
21200/*
21201 * "wincol()" function
21202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021204f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205{
21206 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021207 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208}
21209
21210/*
21211 * "winheight(nr)" function
21212 */
21213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021214f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215{
21216 win_T *wp;
21217
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021218 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021222 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223}
21224
21225/*
21226 * "winline()" function
21227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230{
21231 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021232 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233}
21234
21235/*
21236 * "winnr()" function
21237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021239f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240{
21241 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021242
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021244 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247}
21248
21249/*
21250 * "winrestcmd()" function
21251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021253f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254{
21255#ifdef FEAT_WINDOWS
21256 win_T *wp;
21257 int winnr = 1;
21258 garray_T ga;
21259 char_u buf[50];
21260
21261 ga_init2(&ga, (int)sizeof(char), 70);
21262 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21263 {
21264 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21265 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21267 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 ++winnr;
21269 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021270 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021272 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021274 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021276 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277}
21278
21279/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021280 * "winrestview()" function
21281 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021284{
21285 dict_T *dict;
21286
21287 if (argvars[0].v_type != VAR_DICT
21288 || (dict = argvars[0].vval.v_dict) == NULL)
21289 EMSG(_(e_invarg));
21290 else
21291 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021292 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21293 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21294 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21295 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021296#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021297 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21298 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021299#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021300 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21301 {
21302 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21303 curwin->w_set_curswant = FALSE;
21304 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021305
Bram Moolenaar82c25852014-05-28 16:47:16 +020021306 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21307 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021308#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021309 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21310 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021311#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021312 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21313 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21314 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21315 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021316
21317 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021318 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021319# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021320 win_new_width(curwin, W_WIDTH(curwin));
21321# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021322 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021323
Bram Moolenaarb851a962014-10-31 15:45:52 +010021324 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021325 curwin->w_topline = 1;
21326 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21327 curwin->w_topline = curbuf->b_ml.ml_line_count;
21328#ifdef FEAT_DIFF
21329 check_topfill(curwin, TRUE);
21330#endif
21331 }
21332}
21333
21334/*
21335 * "winsaveview()" function
21336 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021338f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021339{
21340 dict_T *dict;
21341
Bram Moolenaara800b422010-06-27 01:15:55 +020021342 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021343 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021344 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021345
21346 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21347 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21348#ifdef FEAT_VIRTUALEDIT
21349 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21350#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021351 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021352 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21353
21354 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21355#ifdef FEAT_DIFF
21356 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21357#endif
21358 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21359 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21360}
21361
21362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 * "winwidth(nr)" function
21364 */
21365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021366f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367{
21368 win_T *wp;
21369
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021370 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021372 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021374#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021375 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378#endif
21379}
21380
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021382 * "wordcount()" function
21383 */
21384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021385f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021386{
21387 if (rettv_dict_alloc(rettv) == FAIL)
21388 return;
21389 cursor_pos_info(rettv->vval.v_dict);
21390}
21391
21392/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021393 * Write list of strings to file
21394 */
21395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021396write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021397{
21398 listitem_T *li;
21399 int c;
21400 int ret = OK;
21401 char_u *s;
21402
21403 for (li = list->lv_first; li != NULL; li = li->li_next)
21404 {
21405 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21406 {
21407 if (*s == '\n')
21408 c = putc(NUL, fd);
21409 else
21410 c = putc(*s, fd);
21411 if (c == EOF)
21412 {
21413 ret = FAIL;
21414 break;
21415 }
21416 }
21417 if (!binary || li->li_next != NULL)
21418 if (putc('\n', fd) == EOF)
21419 {
21420 ret = FAIL;
21421 break;
21422 }
21423 if (ret == FAIL)
21424 {
21425 EMSG(_(e_write));
21426 break;
21427 }
21428 }
21429 return ret;
21430}
21431
21432/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021433 * "writefile()" function
21434 */
21435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021436f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021437{
21438 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021439 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021440 char_u *fname;
21441 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021442 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021443
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021444 if (check_restricted() || check_secure())
21445 return;
21446
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021447 if (argvars[0].v_type != VAR_LIST)
21448 {
21449 EMSG2(_(e_listarg), "writefile()");
21450 return;
21451 }
21452 if (argvars[0].vval.v_list == NULL)
21453 return;
21454
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021455 if (argvars[2].v_type != VAR_UNKNOWN)
21456 {
21457 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21458 binary = TRUE;
21459 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21460 append = TRUE;
21461 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021462
21463 /* Always open the file in binary mode, library functions have a mind of
21464 * their own about CR-LF conversion. */
21465 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021466 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21467 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021468 {
21469 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21470 ret = -1;
21471 }
21472 else
21473 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021474 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21475 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021476 fclose(fd);
21477 }
21478
21479 rettv->vval.v_number = ret;
21480}
21481
21482/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021483 * "xor(expr, expr)" function
21484 */
21485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021486f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021487{
21488 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21489 ^ get_tv_number_chk(&argvars[1], NULL);
21490}
21491
21492
21493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021495 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 */
21497 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021498var2fpos(
21499 typval_T *varp,
21500 int dollar_lnum, /* TRUE when $ is last line */
21501 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021503 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021505 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506
Bram Moolenaara5525202006-03-02 22:52:09 +000021507 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021508 if (varp->v_type == VAR_LIST)
21509 {
21510 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021511 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021512 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021513 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021514
21515 l = varp->vval.v_list;
21516 if (l == NULL)
21517 return NULL;
21518
21519 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021520 pos.lnum = list_find_nr(l, 0L, &error);
21521 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021522 return NULL; /* invalid line number */
21523
21524 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021525 pos.col = list_find_nr(l, 1L, &error);
21526 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021527 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021528 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021529
21530 /* We accept "$" for the column number: last column. */
21531 li = list_find(l, 1L);
21532 if (li != NULL && li->li_tv.v_type == VAR_STRING
21533 && li->li_tv.vval.v_string != NULL
21534 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21535 pos.col = len + 1;
21536
Bram Moolenaara5525202006-03-02 22:52:09 +000021537 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021538 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021539 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021540 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021541
Bram Moolenaara5525202006-03-02 22:52:09 +000021542#ifdef FEAT_VIRTUALEDIT
21543 /* Get the virtual offset. Defaults to zero. */
21544 pos.coladd = list_find_nr(l, 2L, &error);
21545 if (error)
21546 pos.coladd = 0;
21547#endif
21548
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021549 return &pos;
21550 }
21551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021552 name = get_tv_string_chk(varp);
21553 if (name == NULL)
21554 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021555 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021557 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21558 {
21559 if (VIsual_active)
21560 return &VIsual;
21561 return &curwin->w_cursor;
21562 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021563 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021565 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21567 return NULL;
21568 return pp;
21569 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021570
21571#ifdef FEAT_VIRTUALEDIT
21572 pos.coladd = 0;
21573#endif
21574
Bram Moolenaar477933c2007-07-17 14:32:23 +000021575 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021576 {
21577 pos.col = 0;
21578 if (name[1] == '0') /* "w0": first visible line */
21579 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021580 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021581 pos.lnum = curwin->w_topline;
21582 return &pos;
21583 }
21584 else if (name[1] == '$') /* "w$": last visible line */
21585 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021586 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021587 pos.lnum = curwin->w_botline - 1;
21588 return &pos;
21589 }
21590 }
21591 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021593 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 {
21595 pos.lnum = curbuf->b_ml.ml_line_count;
21596 pos.col = 0;
21597 }
21598 else
21599 {
21600 pos.lnum = curwin->w_cursor.lnum;
21601 pos.col = (colnr_T)STRLEN(ml_get_curline());
21602 }
21603 return &pos;
21604 }
21605 return NULL;
21606}
21607
21608/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021609 * Convert list in "arg" into a position and optional file number.
21610 * When "fnump" is NULL there is no file number, only 3 items.
21611 * Note that the column is passed on as-is, the caller may want to decrement
21612 * it to use 1 for the first column.
21613 * Return FAIL when conversion is not possible, doesn't check the position for
21614 * validity.
21615 */
21616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021617list2fpos(
21618 typval_T *arg,
21619 pos_T *posp,
21620 int *fnump,
21621 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021622{
21623 list_T *l = arg->vval.v_list;
21624 long i = 0;
21625 long n;
21626
Bram Moolenaar493c1782014-05-28 14:34:46 +020021627 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21628 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021629 if (arg->v_type != VAR_LIST
21630 || l == NULL
21631 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021632 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021633 return FAIL;
21634
21635 if (fnump != NULL)
21636 {
21637 n = list_find_nr(l, i++, NULL); /* fnum */
21638 if (n < 0)
21639 return FAIL;
21640 if (n == 0)
21641 n = curbuf->b_fnum; /* current buffer */
21642 *fnump = n;
21643 }
21644
21645 n = list_find_nr(l, i++, NULL); /* lnum */
21646 if (n < 0)
21647 return FAIL;
21648 posp->lnum = n;
21649
21650 n = list_find_nr(l, i++, NULL); /* col */
21651 if (n < 0)
21652 return FAIL;
21653 posp->col = n;
21654
21655#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021656 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021657 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021658 posp->coladd = 0;
21659 else
21660 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021661#endif
21662
Bram Moolenaar493c1782014-05-28 14:34:46 +020021663 if (curswantp != NULL)
21664 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21665
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021666 return OK;
21667}
21668
21669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 * Get the length of an environment variable name.
21671 * Advance "arg" to the first character after the name.
21672 * Return 0 for error.
21673 */
21674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021675get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676{
21677 char_u *p;
21678 int len;
21679
21680 for (p = *arg; vim_isIDc(*p); ++p)
21681 ;
21682 if (p == *arg) /* no name found */
21683 return 0;
21684
21685 len = (int)(p - *arg);
21686 *arg = p;
21687 return len;
21688}
21689
21690/*
21691 * Get the length of the name of a function or internal variable.
21692 * "arg" is advanced to the first non-white character after the name.
21693 * Return 0 if something is wrong.
21694 */
21695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021696get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697{
21698 char_u *p;
21699 int len;
21700
21701 /* Find the end of the name. */
21702 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021703 {
21704 if (*p == ':')
21705 {
21706 /* "s:" is start of "s:var", but "n:" is not and can be used in
21707 * slice "[n:]". Also "xx:" is not a namespace. */
21708 len = (int)(p - *arg);
21709 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21710 || len > 1)
21711 break;
21712 }
21713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 if (p == *arg) /* no name found */
21715 return 0;
21716
21717 len = (int)(p - *arg);
21718 *arg = skipwhite(p);
21719
21720 return len;
21721}
21722
21723/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021724 * Get the length of the name of a variable or function.
21725 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021727 * Return -1 if curly braces expansion failed.
21728 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 * If the name contains 'magic' {}'s, expand them and return the
21730 * expanded name in an allocated string via 'alias' - caller must free.
21731 */
21732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021733get_name_len(
21734 char_u **arg,
21735 char_u **alias,
21736 int evaluate,
21737 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738{
21739 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 char_u *p;
21741 char_u *expr_start;
21742 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
21744 *alias = NULL; /* default to no alias */
21745
21746 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21747 && (*arg)[2] == (int)KE_SNR)
21748 {
21749 /* hard coded <SNR>, already translated */
21750 *arg += 3;
21751 return get_id_len(arg) + 3;
21752 }
21753 len = eval_fname_script(*arg);
21754 if (len > 0)
21755 {
21756 /* literal "<SID>", "s:" or "<SNR>" */
21757 *arg += len;
21758 }
21759
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021763 p = find_name_end(*arg, &expr_start, &expr_end,
21764 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 if (expr_start != NULL)
21766 {
21767 char_u *temp_string;
21768
21769 if (!evaluate)
21770 {
21771 len += (int)(p - *arg);
21772 *arg = skipwhite(p);
21773 return len;
21774 }
21775
21776 /*
21777 * Include any <SID> etc in the expanded string:
21778 * Thus the -len here.
21779 */
21780 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21781 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021782 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 *alias = temp_string;
21784 *arg = skipwhite(p);
21785 return (int)STRLEN(temp_string);
21786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787
21788 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021789 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 EMSG2(_(e_invexpr2), *arg);
21791
21792 return len;
21793}
21794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021795/*
21796 * Find the end of a variable or function name, taking care of magic braces.
21797 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21798 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021799 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021800 * Return a pointer to just after the name. Equal to "arg" if there is no
21801 * valid name.
21802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021804find_name_end(
21805 char_u *arg,
21806 char_u **expr_start,
21807 char_u **expr_end,
21808 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021810 int mb_nest = 0;
21811 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021813 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021815 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 *expr_start = NULL;
21818 *expr_end = NULL;
21819 }
21820
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021821 /* Quick check for valid starting character. */
21822 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21823 return arg;
21824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021825 for (p = arg; *p != NUL
21826 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021827 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021828 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021829 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021830 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021831 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021832 if (*p == '\'')
21833 {
21834 /* skip over 'string' to avoid counting [ and ] inside it. */
21835 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21836 ;
21837 if (*p == NUL)
21838 break;
21839 }
21840 else if (*p == '"')
21841 {
21842 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21843 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21844 if (*p == '\\' && p[1] != NUL)
21845 ++p;
21846 if (*p == NUL)
21847 break;
21848 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021849 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21850 {
21851 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021852 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021853 len = (int)(p - arg);
21854 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021855 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021856 break;
21857 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021859 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021861 if (*p == '[')
21862 ++br_nest;
21863 else if (*p == ']')
21864 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021867 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021869 if (*p == '{')
21870 {
21871 mb_nest++;
21872 if (expr_start != NULL && *expr_start == NULL)
21873 *expr_start = p;
21874 }
21875 else if (*p == '}')
21876 {
21877 mb_nest--;
21878 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21879 *expr_end = p;
21880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 }
21883
21884 return p;
21885}
21886
21887/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021888 * Expands out the 'magic' {}'s in a variable/function name.
21889 * Note that this can call itself recursively, to deal with
21890 * constructs like foo{bar}{baz}{bam}
21891 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21892 * "in_start" ^
21893 * "expr_start" ^
21894 * "expr_end" ^
21895 * "in_end" ^
21896 *
21897 * Returns a new allocated string, which the caller must free.
21898 * Returns NULL for failure.
21899 */
21900 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021901make_expanded_name(
21902 char_u *in_start,
21903 char_u *expr_start,
21904 char_u *expr_end,
21905 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021906{
21907 char_u c1;
21908 char_u *retval = NULL;
21909 char_u *temp_result;
21910 char_u *nextcmd = NULL;
21911
21912 if (expr_end == NULL || in_end == NULL)
21913 return NULL;
21914 *expr_start = NUL;
21915 *expr_end = NUL;
21916 c1 = *in_end;
21917 *in_end = NUL;
21918
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021919 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021920 if (temp_result != NULL && nextcmd == NULL)
21921 {
21922 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21923 + (in_end - expr_end) + 1));
21924 if (retval != NULL)
21925 {
21926 STRCPY(retval, in_start);
21927 STRCAT(retval, temp_result);
21928 STRCAT(retval, expr_end + 1);
21929 }
21930 }
21931 vim_free(temp_result);
21932
21933 *in_end = c1; /* put char back for error messages */
21934 *expr_start = '{';
21935 *expr_end = '}';
21936
21937 if (retval != NULL)
21938 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021939 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021940 if (expr_start != NULL)
21941 {
21942 /* Further expansion! */
21943 temp_result = make_expanded_name(retval, expr_start,
21944 expr_end, temp_result);
21945 vim_free(retval);
21946 retval = temp_result;
21947 }
21948 }
21949
21950 return retval;
21951}
21952
21953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021955 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 */
21957 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021958eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021960 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21961}
21962
21963/*
21964 * Return TRUE if character "c" can be used as the first character in a
21965 * variable or function name (excluding '{' and '}').
21966 */
21967 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021968eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021969{
21970 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971}
21972
21973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 * Set number v: variable to "val".
21975 */
21976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021977set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021979 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980}
21981
21982/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021983 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 */
21985 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021986get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021988 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989}
21990
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021991/*
21992 * Get string v: variable value. Uses a static buffer, can only be used once.
21993 */
21994 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021995get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021996{
21997 return get_tv_string(&vimvars[idx].vv_tv);
21998}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021999
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022001 * Get List v: variable value. Caller must take care of reference count when
22002 * needed.
22003 */
22004 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022005get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022006{
22007 return vimvars[idx].vv_list;
22008}
22009
22010/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022011 * Set v:char to character "c".
22012 */
22013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022014set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022015{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022016 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022017
22018#ifdef FEAT_MBYTE
22019 if (has_mbyte)
22020 buf[(*mb_char2bytes)(c, buf)] = NUL;
22021 else
22022#endif
22023 {
22024 buf[0] = c;
22025 buf[1] = NUL;
22026 }
22027 set_vim_var_string(VV_CHAR, buf, -1);
22028}
22029
22030/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022031 * Set v:count to "count" and v:count1 to "count1".
22032 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 */
22034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022035set_vcount(
22036 long count,
22037 long count1,
22038 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022040 if (set_prevcount)
22041 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022042 vimvars[VV_COUNT].vv_nr = count;
22043 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044}
22045
22046/*
22047 * Set string v: variable to a copy of "val".
22048 */
22049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022050set_vim_var_string(
22051 int idx,
22052 char_u *val,
22053 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054{
Bram Moolenaara542c682016-01-31 16:28:04 +010022055 clear_tv(&vimvars[idx].vv_di.di_tv);
22056 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022058 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022060 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022062 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063}
22064
22065/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022066 * Set List v: variable to "val".
22067 */
22068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022069set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022070{
Bram Moolenaara542c682016-01-31 16:28:04 +010022071 clear_tv(&vimvars[idx].vv_di.di_tv);
22072 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022073 vimvars[idx].vv_list = val;
22074 if (val != NULL)
22075 ++val->lv_refcount;
22076}
22077
22078/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022079 * Set Dictionary v: variable to "val".
22080 */
22081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022082set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022083{
22084 int todo;
22085 hashitem_T *hi;
22086
Bram Moolenaara542c682016-01-31 16:28:04 +010022087 clear_tv(&vimvars[idx].vv_di.di_tv);
22088 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022089 vimvars[idx].vv_dict = val;
22090 if (val != NULL)
22091 {
22092 ++val->dv_refcount;
22093
22094 /* Set readonly */
22095 todo = (int)val->dv_hashtab.ht_used;
22096 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22097 {
22098 if (HASHITEM_EMPTY(hi))
22099 continue;
22100 --todo;
22101 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22102 }
22103 }
22104}
22105
22106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 * Set v:register if needed.
22108 */
22109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022110set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111{
22112 char_u regname;
22113
22114 if (c == 0 || c == ' ')
22115 regname = '"';
22116 else
22117 regname = c;
22118 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022119 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 set_vim_var_string(VV_REG, &regname, 1);
22121}
22122
22123/*
22124 * Get or set v:exception. If "oldval" == NULL, return the current value.
22125 * Otherwise, restore the value to "oldval" and return NULL.
22126 * Must always be called in pairs to save and restore v:exception! Does not
22127 * take care of memory allocations.
22128 */
22129 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022130v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131{
22132 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022133 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134
Bram Moolenaare9a41262005-01-15 22:18:47 +000022135 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022136 return NULL;
22137}
22138
22139/*
22140 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22141 * Otherwise, restore the value to "oldval" and return NULL.
22142 * Must always be called in pairs to save and restore v:throwpoint! Does not
22143 * take care of memory allocations.
22144 */
22145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022146v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147{
22148 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022149 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150
Bram Moolenaare9a41262005-01-15 22:18:47 +000022151 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022152 return NULL;
22153}
22154
22155#if defined(FEAT_AUTOCMD) || defined(PROTO)
22156/*
22157 * Set v:cmdarg.
22158 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22159 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22160 * Must always be called in pairs!
22161 */
22162 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022163set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164{
22165 char_u *oldval;
22166 char_u *newval;
22167 unsigned len;
22168
Bram Moolenaare9a41262005-01-15 22:18:47 +000022169 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022170 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022172 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022173 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022174 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 }
22176
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022177 if (eap->force_bin == FORCE_BIN)
22178 len = 6;
22179 else if (eap->force_bin == FORCE_NOBIN)
22180 len = 8;
22181 else
22182 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022183
22184 if (eap->read_edit)
22185 len += 7;
22186
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022187 if (eap->force_ff != 0)
22188 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22189# ifdef FEAT_MBYTE
22190 if (eap->force_enc != 0)
22191 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022192 if (eap->bad_char != 0)
22193 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022194# endif
22195
22196 newval = alloc(len + 1);
22197 if (newval == NULL)
22198 return NULL;
22199
22200 if (eap->force_bin == FORCE_BIN)
22201 sprintf((char *)newval, " ++bin");
22202 else if (eap->force_bin == FORCE_NOBIN)
22203 sprintf((char *)newval, " ++nobin");
22204 else
22205 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022206
22207 if (eap->read_edit)
22208 STRCAT(newval, " ++edit");
22209
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022210 if (eap->force_ff != 0)
22211 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22212 eap->cmd + eap->force_ff);
22213# ifdef FEAT_MBYTE
22214 if (eap->force_enc != 0)
22215 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22216 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022217 if (eap->bad_char == BAD_KEEP)
22218 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22219 else if (eap->bad_char == BAD_DROP)
22220 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22221 else if (eap->bad_char != 0)
22222 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022223# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022224 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022225 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226}
22227#endif
22228
22229/*
22230 * Get the value of internal variable "name".
22231 * Return OK or FAIL.
22232 */
22233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022234get_var_tv(
22235 char_u *name,
22236 int len, /* length of "name" */
22237 typval_T *rettv, /* NULL when only checking existence */
22238 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22239 int verbose, /* may give error message */
22240 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241{
22242 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022243 typval_T *tv = NULL;
22244 typval_T atv;
22245 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247
22248 /* truncate the name, so that we can use strcmp() */
22249 cc = name[len];
22250 name[len] = NUL;
22251
22252 /*
22253 * Check for "b:changedtick".
22254 */
22255 if (STRCMP(name, "b:changedtick") == 0)
22256 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022257 atv.v_type = VAR_NUMBER;
22258 atv.vval.v_number = curbuf->b_changedtick;
22259 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 }
22261
22262 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 * Check for user-defined variables.
22264 */
22265 else
22266 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022267 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022268 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022269 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022271 if (dip != NULL)
22272 *dip = v;
22273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 }
22275
Bram Moolenaare9a41262005-01-15 22:18:47 +000022276 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022278 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022279 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 ret = FAIL;
22281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022282 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022283 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284
22285 name[len] = cc;
22286
22287 return ret;
22288}
22289
22290/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022291 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22292 * Also handle function call with Funcref variable: func(expr)
22293 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22294 */
22295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022296handle_subscript(
22297 char_u **arg,
22298 typval_T *rettv,
22299 int evaluate, /* do more than finding the end */
22300 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022301{
22302 int ret = OK;
22303 dict_T *selfdict = NULL;
22304 char_u *s;
22305 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022306 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022307
22308 while (ret == OK
22309 && (**arg == '['
22310 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022311 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22312 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022313 && !vim_iswhite(*(*arg - 1)))
22314 {
22315 if (**arg == '(')
22316 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022317 partial_T *pt = NULL;
22318
Bram Moolenaard9fba312005-06-26 22:34:35 +000022319 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022320 if (evaluate)
22321 {
22322 functv = *rettv;
22323 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022324
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022325 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022326 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022327 {
22328 pt = functv.vval.v_partial;
22329 s = pt->pt_name;
22330 }
22331 else
22332 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022333 }
22334 else
22335 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022336 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022337 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022338 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022339
22340 /* Clear the funcref afterwards, so that deleting it while
22341 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022342 if (evaluate)
22343 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022344
22345 /* Stop the expression evaluation when immediately aborting on
22346 * error, or when an interrupt occurred or an exception was thrown
22347 * but not caught. */
22348 if (aborting())
22349 {
22350 if (ret == OK)
22351 clear_tv(rettv);
22352 ret = FAIL;
22353 }
22354 dict_unref(selfdict);
22355 selfdict = NULL;
22356 }
22357 else /* **arg == '[' || **arg == '.' */
22358 {
22359 dict_unref(selfdict);
22360 if (rettv->v_type == VAR_DICT)
22361 {
22362 selfdict = rettv->vval.v_dict;
22363 if (selfdict != NULL)
22364 ++selfdict->dv_refcount;
22365 }
22366 else
22367 selfdict = NULL;
22368 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22369 {
22370 clear_tv(rettv);
22371 ret = FAIL;
22372 }
22373 }
22374 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022375
Bram Moolenaar1d429612016-05-24 15:44:17 +020022376 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22377 * Don't do this when "Func" is already a partial that was bound
22378 * explicitly (pt_auto is FALSE). */
22379 if (selfdict != NULL
22380 && (rettv->v_type == VAR_FUNC
22381 || (rettv->v_type == VAR_PARTIAL
22382 && (rettv->vval.v_partial->pt_auto
22383 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022384 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022385 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22386 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022387 char_u *tofree = NULL;
22388 ufunc_T *fp;
22389 char_u fname_buf[FLEN_FIXED + 1];
22390 int error;
22391
22392 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022393 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022394 fp = find_func(fname);
22395 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022396
Bram Moolenaar65639032016-03-16 21:40:30 +010022397 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022398 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022399 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22400
22401 if (pt != NULL)
22402 {
22403 pt->pt_refcount = 1;
22404 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022405 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022406 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022407 if (rettv->v_type == VAR_FUNC)
22408 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022409 /* Just a function: Take over the function name and use
22410 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022411 pt->pt_name = rettv->vval.v_string;
22412 }
22413 else
22414 {
22415 partial_T *ret_pt = rettv->vval.v_partial;
22416 int i;
22417
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022418 /* Partial: copy the function name, use selfdict and copy
22419 * args. Can't take over name or args, the partial might
22420 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022421 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022422 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022423 if (ret_pt->pt_argc > 0)
22424 {
22425 pt->pt_argv = (typval_T *)alloc(
22426 sizeof(typval_T) * ret_pt->pt_argc);
22427 if (pt->pt_argv == NULL)
22428 /* out of memory: drop the arguments */
22429 pt->pt_argc = 0;
22430 else
22431 {
22432 pt->pt_argc = ret_pt->pt_argc;
22433 for (i = 0; i < pt->pt_argc; i++)
22434 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22435 }
22436 }
22437 partial_unref(ret_pt);
22438 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022439 rettv->v_type = VAR_PARTIAL;
22440 rettv->vval.v_partial = pt;
22441 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022442 }
22443 }
22444
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022445 dict_unref(selfdict);
22446 return ret;
22447}
22448
22449/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022450 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022451 * value).
22452 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022453 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022454alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022455{
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022457}
22458
22459/*
22460 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 * The string "s" must have been allocated, it is consumed.
22462 * Return NULL for out of memory, the variable otherwise.
22463 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022464 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022465alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466{
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022469 rettv = alloc_tv();
22470 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022472 rettv->v_type = VAR_STRING;
22473 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 }
22475 else
22476 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022477 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478}
22479
22480/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022484free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485{
22486 if (varp != NULL)
22487 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022488 switch (varp->v_type)
22489 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022490 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022491 func_unref(varp->vval.v_string);
22492 /*FALLTHROUGH*/
22493 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022494 vim_free(varp->vval.v_string);
22495 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022496 case VAR_PARTIAL:
22497 partial_unref(varp->vval.v_partial);
22498 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 case VAR_LIST:
22500 list_unref(varp->vval.v_list);
22501 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022502 case VAR_DICT:
22503 dict_unref(varp->vval.v_dict);
22504 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022505 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022507 job_unref(varp->vval.v_job);
22508 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022509#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022510 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022512 channel_unref(varp->vval.v_channel);
22513 break;
22514#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022515 case VAR_NUMBER:
22516 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022517 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022518 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 vim_free(varp);
22522 }
22523}
22524
22525/*
22526 * Free the memory for a variable value and set the value to NULL or 0.
22527 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022528 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022529clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530{
22531 if (varp != NULL)
22532 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022533 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022535 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022536 func_unref(varp->vval.v_string);
22537 /*FALLTHROUGH*/
22538 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022539 vim_free(varp->vval.v_string);
22540 varp->vval.v_string = NULL;
22541 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022542 case VAR_PARTIAL:
22543 partial_unref(varp->vval.v_partial);
22544 varp->vval.v_partial = NULL;
22545 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022546 case VAR_LIST:
22547 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022548 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022549 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022550 case VAR_DICT:
22551 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022552 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022553 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022554 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022555 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022556 varp->vval.v_number = 0;
22557 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022558 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022559#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022560 varp->vval.v_float = 0.0;
22561 break;
22562#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022563 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022564#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022565 job_unref(varp->vval.v_job);
22566 varp->vval.v_job = NULL;
22567#endif
22568 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022569 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022571 channel_unref(varp->vval.v_channel);
22572 varp->vval.v_channel = NULL;
22573#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022574 case VAR_UNKNOWN:
22575 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022577 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 }
22579}
22580
22581/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022582 * Set the value of a variable to NULL without freeing items.
22583 */
22584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022585init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022586{
22587 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022589}
22590
22591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 * Get the number value of a variable.
22593 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022594 * For incompatible types, return 0.
22595 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22596 * caller of incompatible types: it sets *denote to TRUE if "denote"
22597 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022599 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022600get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022602 int error = FALSE;
22603
22604 return get_tv_number_chk(varp, &error); /* return 0L on error */
22605}
22606
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022607 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022608get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022609{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022610 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022612 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022614 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022615 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022616 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022617#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022618 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022619 break;
22620#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022621 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022622 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022623 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022624 break;
22625 case VAR_STRING:
22626 if (varp->vval.v_string != NULL)
22627 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022628 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022629 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022630 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022631 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022632 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022633 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022634 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022635 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022636 case VAR_SPECIAL:
22637 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22638 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022639 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022640#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022641 EMSG(_("E910: Using a Job as a Number"));
22642 break;
22643#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022644 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022646 EMSG(_("E913: Using a Channel as a Number"));
22647 break;
22648#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022649 case VAR_UNKNOWN:
22650 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022653 if (denote == NULL) /* useful for values that must be unsigned */
22654 n = -1;
22655 else
22656 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022657 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658}
22659
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022660#ifdef FEAT_FLOAT
22661 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022662get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022663{
22664 switch (varp->v_type)
22665 {
22666 case VAR_NUMBER:
22667 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022668 case VAR_FLOAT:
22669 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022670 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022671 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022672 EMSG(_("E891: Using a Funcref as a Float"));
22673 break;
22674 case VAR_STRING:
22675 EMSG(_("E892: Using a String as a Float"));
22676 break;
22677 case VAR_LIST:
22678 EMSG(_("E893: Using a List as a Float"));
22679 break;
22680 case VAR_DICT:
22681 EMSG(_("E894: Using a Dictionary as a Float"));
22682 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022683 case VAR_SPECIAL:
22684 EMSG(_("E907: Using a special value as a Float"));
22685 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022686 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022687# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022688 EMSG(_("E911: Using a Job as a Float"));
22689 break;
22690# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022691 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022692# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022693 EMSG(_("E914: Using a Channel as a Float"));
22694 break;
22695# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022696 case VAR_UNKNOWN:
22697 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022698 break;
22699 }
22700 return 0;
22701}
22702#endif
22703
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022705 * Get the lnum from the first argument.
22706 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022707 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 */
22709 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022710get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711{
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713 linenr_T lnum;
22714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022715 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 if (lnum == 0) /* no valid number, try using line() */
22717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022718 rettv.v_type = VAR_NUMBER;
22719 f_line(argvars, &rettv);
22720 lnum = rettv.vval.v_number;
22721 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 }
22723 return lnum;
22724}
22725
22726/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022727 * Get the lnum from the first argument.
22728 * Also accepts "$", then "buf" is used.
22729 * Returns 0 on error.
22730 */
22731 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022732get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022733{
22734 if (argvars[0].v_type == VAR_STRING
22735 && argvars[0].vval.v_string != NULL
22736 && argvars[0].vval.v_string[0] == '$'
22737 && buf != NULL)
22738 return buf->b_ml.ml_line_count;
22739 return get_tv_number_chk(&argvars[0], NULL);
22740}
22741
22742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 * Get the string value of a variable.
22744 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022745 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22746 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747 * If the String variable has never been set, return an empty string.
22748 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022749 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22750 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022752 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022753get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754{
22755 static char_u mybuf[NUMBUFLEN];
22756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022757 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758}
22759
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022760 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022761get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022763 char_u *res = get_tv_string_buf_chk(varp, buf);
22764
22765 return res != NULL ? res : (char_u *)"";
22766}
22767
Bram Moolenaar7d647822014-04-05 21:28:56 +020022768/*
22769 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22770 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022771 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022773{
22774 static char_u mybuf[NUMBUFLEN];
22775
22776 return get_tv_string_buf_chk(varp, mybuf);
22777}
22778
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022779 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022780get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022781{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022782 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022784 case VAR_NUMBER:
22785 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22786 return buf;
22787 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022788 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022789 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022790 break;
22791 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022792 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022793 break;
22794 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022795 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022796 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022797 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022798#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022799 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022800 break;
22801#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022802 case VAR_STRING:
22803 if (varp->vval.v_string != NULL)
22804 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022805 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022806 case VAR_SPECIAL:
22807 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22808 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022809 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022810#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022811 {
22812 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022813 char *status;
22814
22815 if (job == NULL)
22816 return (char_u *)"no process";
22817 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022818 : job->jv_status == JOB_ENDED ? "dead"
22819 : "run";
22820# ifdef UNIX
22821 vim_snprintf((char *)buf, NUMBUFLEN,
22822 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022823# elif defined(WIN32)
22824 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022825 "process %ld %s",
22826 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022827 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022828# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022829 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022830 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22831# endif
22832 return buf;
22833 }
22834#endif
22835 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022836 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022837#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022838 {
22839 channel_T *channel = varp->vval.v_channel;
22840 char *status = channel_status(channel);
22841
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022842 if (channel == NULL)
22843 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22844 else
22845 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022846 "channel %d %s", channel->ch_id, status);
22847 return buf;
22848 }
22849#endif
22850 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022851 case VAR_UNKNOWN:
22852 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022855 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856}
22857
22858/*
22859 * Find variable "name" in the list of variables.
22860 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022861 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022862 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022863 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022865 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022866find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022869 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870
Bram Moolenaara7043832005-01-21 11:56:39 +000022871 ht = find_var_ht(name, &varname);
22872 if (htp != NULL)
22873 *htp = ht;
22874 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022876 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877}
22878
22879/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022880 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022881 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022883 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022884find_var_in_ht(
22885 hashtab_T *ht,
22886 int htname,
22887 char_u *varname,
22888 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022889{
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 hashitem_T *hi;
22891
22892 if (*varname == NUL)
22893 {
22894 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022895 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022897 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022898 case 'g': return &globvars_var;
22899 case 'v': return &vimvars_var;
22900 case 'b': return &curbuf->b_bufvar;
22901 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022902#ifdef FEAT_WINDOWS
22903 case 't': return &curtab->tp_winvar;
22904#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022905 case 'l': return current_funccal == NULL
22906 ? NULL : &current_funccal->l_vars_var;
22907 case 'a': return current_funccal == NULL
22908 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 }
22910 return NULL;
22911 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022912
22913 hi = hash_find(ht, varname);
22914 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022915 {
22916 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022917 * worked find the variable again. Don't auto-load a script if it was
22918 * loaded already, otherwise it would be loaded every time when
22919 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022920 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022921 {
22922 /* Note: script_autoload() may make "hi" invalid. It must either
22923 * be obtained again or not used. */
22924 if (!script_autoload(varname, FALSE) || aborting())
22925 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022926 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022927 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022928 if (HASHITEM_EMPTY(hi))
22929 return NULL;
22930 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022931 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022932}
22933
22934/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022936 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022937 * Set "varname" to the start of name without ':'.
22938 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022939 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022940find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022942 hashitem_T *hi;
22943
Bram Moolenaar73627d02015-08-11 15:46:09 +020022944 if (name[0] == NUL)
22945 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 if (name[1] != ':')
22947 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022948 /* The name must not start with a colon or #. */
22949 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 return NULL;
22951 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022952
22953 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022954 hi = hash_find(&compat_hashtab, name);
22955 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022956 return &compat_hashtab;
22957
Bram Moolenaar071d4272004-06-13 20:20:40 +000022958 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022959 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022960 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 }
22962 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022963 if (*name == 'g') /* global variable */
22964 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022965 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22966 */
22967 if (vim_strchr(name + 2, ':') != NULL
22968 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022969 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022971 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022973 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022974#ifdef FEAT_WINDOWS
22975 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022976 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022977#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022978 if (*name == 'v') /* v: variable */
22979 return &vimvarht;
22980 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022981 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022983 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 if (*name == 's' /* script variable */
22985 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22986 return &SCRIPT_VARS(current_SID);
22987 return NULL;
22988}
22989
22990/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022991 * Get function call environment based on bactrace debug level
22992 */
22993 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022994get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022995{
22996 int i;
22997 funccall_T *funccal;
22998 funccall_T *temp_funccal;
22999
23000 funccal = current_funccal;
23001 if (debug_backtrace_level > 0)
23002 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023003 for (i = 0; i < debug_backtrace_level; i++)
23004 {
23005 temp_funccal = funccal->caller;
23006 if (temp_funccal)
23007 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023008 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023009 /* backtrace level overflow. reset to max */
23010 debug_backtrace_level = i;
23011 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023012 }
23013 return funccal;
23014}
23015
23016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023018 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 * Returns NULL when it doesn't exist.
23020 */
23021 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023022get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023{
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023026 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 if (v == NULL)
23028 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023029 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030}
23031
23032/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023033 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 * sourcing this script and when executing functions defined in the script.
23035 */
23036 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023037new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038{
Bram Moolenaara7043832005-01-21 11:56:39 +000023039 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 hashtab_T *ht;
23041 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023042
Bram Moolenaar071d4272004-06-13 20:20:40 +000023043 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23044 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023045 /* Re-allocating ga_data means that an ht_array pointing to
23046 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023048 for (i = 1; i <= ga_scripts.ga_len; ++i)
23049 {
23050 ht = &SCRIPT_VARS(i);
23051 if (ht->ht_mask == HT_INIT_SIZE - 1)
23052 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023053 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023054 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023055 }
23056
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 while (ga_scripts.ga_len < id)
23058 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023059 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023060 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023061 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 }
23064 }
23065}
23066
23067/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023068 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23069 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 */
23071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023072init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073{
Bram Moolenaar33570922005-01-25 22:26:29 +000023074 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023075 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023076 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023077 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023078 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023079 dict_var->di_tv.vval.v_dict = dict;
23080 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023081 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023082 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23083 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084}
23085
23086/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023087 * Unreference a dictionary initialized by init_var_dict().
23088 */
23089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023090unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023091{
23092 /* Now the dict needs to be freed if no one else is using it, go back to
23093 * normal reference counting. */
23094 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23095 dict_unref(dict);
23096}
23097
23098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023100 * Frees all allocated variables and the value they contain.
23101 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 */
23103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023104vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023105{
23106 vars_clear_ext(ht, TRUE);
23107}
23108
23109/*
23110 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23111 */
23112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023113vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114{
Bram Moolenaara7043832005-01-21 11:56:39 +000023115 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 hashitem_T *hi;
23117 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118
Bram Moolenaar33570922005-01-25 22:26:29 +000023119 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023120 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023121 for (hi = ht->ht_array; todo > 0; ++hi)
23122 {
23123 if (!HASHITEM_EMPTY(hi))
23124 {
23125 --todo;
23126
Bram Moolenaar33570922005-01-25 22:26:29 +000023127 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023128 * ht_array might change then. hash_clear() takes care of it
23129 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023130 v = HI2DI(hi);
23131 if (free_val)
23132 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023133 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023135 }
23136 }
23137 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023138 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139}
23140
Bram Moolenaara7043832005-01-21 11:56:39 +000023141/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023142 * Delete a variable from hashtab "ht" at item "hi".
23143 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023146delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147{
Bram Moolenaar33570922005-01-25 22:26:29 +000023148 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023149
23150 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023151 clear_tv(&di->di_tv);
23152 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153}
23154
23155/*
23156 * List the value of one internal variable.
23157 */
23158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023159list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023161 char_u *tofree;
23162 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023163 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023164
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023165 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023166 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023167 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023168 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169}
23170
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023172list_one_var_a(
23173 char_u *prefix,
23174 char_u *name,
23175 int type,
23176 char_u *string,
23177 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178{
Bram Moolenaar31859182007-08-14 20:41:13 +000023179 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23180 msg_start();
23181 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 if (name != NULL) /* "a:" vars don't have a name stored */
23183 msg_puts(name);
23184 msg_putchar(' ');
23185 msg_advance(22);
23186 if (type == VAR_NUMBER)
23187 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023188 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023189 msg_putchar('*');
23190 else if (type == VAR_LIST)
23191 {
23192 msg_putchar('[');
23193 if (*string == '[')
23194 ++string;
23195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023196 else if (type == VAR_DICT)
23197 {
23198 msg_putchar('{');
23199 if (*string == '{')
23200 ++string;
23201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 else
23203 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023204
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023206
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023207 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023208 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023209 if (*first)
23210 {
23211 msg_clr_eos();
23212 *first = FALSE;
23213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214}
23215
23216/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023217 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 * If the variable already exists, the value is updated.
23219 * Otherwise the variable is created.
23220 */
23221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023222set_var(
23223 char_u *name,
23224 typval_T *tv,
23225 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226{
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023231 ht = find_var_ht(name, &varname);
23232 if (ht == NULL || *varname == NUL)
23233 {
23234 EMSG2(_(e_illvar), name);
23235 return;
23236 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023237 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023238
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023239 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23240 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023241 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023242
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023245 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023246 if (var_check_ro(v->di_flags, name, FALSE)
23247 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023248 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023249
23250 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023251 * Handle setting internal v: variables separately where needed to
23252 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023253 */
23254 if (ht == &vimvarht)
23255 {
23256 if (v->di_tv.v_type == VAR_STRING)
23257 {
23258 vim_free(v->di_tv.vval.v_string);
23259 if (copy || tv->v_type != VAR_STRING)
23260 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23261 else
23262 {
23263 /* Take over the string to avoid an extra alloc/free. */
23264 v->di_tv.vval.v_string = tv->vval.v_string;
23265 tv->vval.v_string = NULL;
23266 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023267 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023268 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023269 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023270 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023271 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023272 if (STRCMP(varname, "searchforward") == 0)
23273 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023274#ifdef FEAT_SEARCH_EXTRA
23275 else if (STRCMP(varname, "hlsearch") == 0)
23276 {
23277 no_hlsearch = !v->di_tv.vval.v_number;
23278 redraw_all_later(SOME_VALID);
23279 }
23280#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023281 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023282 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023283 else if (v->di_tv.v_type != tv->v_type)
23284 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023285 }
23286
23287 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 }
23289 else /* add a new variable */
23290 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023291 /* Can't add "v:" variable. */
23292 if (ht == &vimvarht)
23293 {
23294 EMSG2(_(e_illvar), name);
23295 return;
23296 }
23297
Bram Moolenaar92124a32005-06-17 22:03:40 +000023298 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023299 if (!valid_varname(varname))
23300 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023301
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023302 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23303 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023304 if (v == NULL)
23305 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023306 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023309 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 return;
23311 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023312 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023314
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023315 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023316 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023317 else
23318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023319 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023320 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023321 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323}
23324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023325/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023326 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023327 * Also give an error message.
23328 */
23329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023330var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023331{
23332 if (flags & DI_FLAGS_RO)
23333 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023334 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023335 return TRUE;
23336 }
23337 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23338 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023339 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023340 return TRUE;
23341 }
23342 return FALSE;
23343}
23344
23345/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023346 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23347 * Also give an error message.
23348 */
23349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023350var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023351{
23352 if (flags & DI_FLAGS_FIX)
23353 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023354 EMSG2(_("E795: Cannot delete variable %s"),
23355 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023356 return TRUE;
23357 }
23358 return FALSE;
23359}
23360
23361/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023362 * Check if a funcref is assigned to a valid variable name.
23363 * Return TRUE and give an error if not.
23364 */
23365 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023366var_check_func_name(
23367 char_u *name, /* points to start of variable name */
23368 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023369{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023370 /* Allow for w: b: s: and t:. */
23371 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023372 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23373 ? name[2] : name[0]))
23374 {
23375 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23376 name);
23377 return TRUE;
23378 }
23379 /* Don't allow hiding a function. When "v" is not NULL we might be
23380 * assigning another function to the same var, the type is checked
23381 * below. */
23382 if (new_var && function_exists(name))
23383 {
23384 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23385 name);
23386 return TRUE;
23387 }
23388 return FALSE;
23389}
23390
23391/*
23392 * Check if a variable name is valid.
23393 * Return FALSE and give an error if not.
23394 */
23395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023396valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023397{
23398 char_u *p;
23399
23400 for (p = varname; *p != NUL; ++p)
23401 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23402 && *p != AUTOLOAD_CHAR)
23403 {
23404 EMSG2(_(e_illvar), varname);
23405 return FALSE;
23406 }
23407 return TRUE;
23408}
23409
23410/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023411 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023412 * Also give an error message, using "name" or _("name") when use_gettext is
23413 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023414 */
23415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023416tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023417{
23418 if (lock & VAR_LOCKED)
23419 {
23420 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023421 name == NULL ? (char_u *)_("Unknown")
23422 : use_gettext ? (char_u *)_(name)
23423 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023424 return TRUE;
23425 }
23426 if (lock & VAR_FIXED)
23427 {
23428 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023429 name == NULL ? (char_u *)_("Unknown")
23430 : use_gettext ? (char_u *)_(name)
23431 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023432 return TRUE;
23433 }
23434 return FALSE;
23435}
23436
23437/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023439 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023440 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023441 * It is OK for "from" and "to" to point to the same item. This is used to
23442 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023443 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023445copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023447 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023448 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023449 switch (from->v_type)
23450 {
23451 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023452 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023453 to->vval.v_number = from->vval.v_number;
23454 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023455 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023456#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023457 to->vval.v_float = from->vval.v_float;
23458 break;
23459#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023460 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023461#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023462 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023463 if (to->vval.v_job != NULL)
23464 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023465 break;
23466#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023467 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023468#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023469 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023470 if (to->vval.v_channel != NULL)
23471 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023472 break;
23473#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023474 case VAR_STRING:
23475 case VAR_FUNC:
23476 if (from->vval.v_string == NULL)
23477 to->vval.v_string = NULL;
23478 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023480 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023481 if (from->v_type == VAR_FUNC)
23482 func_ref(to->vval.v_string);
23483 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023484 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023485 case VAR_PARTIAL:
23486 if (from->vval.v_partial == NULL)
23487 to->vval.v_partial = NULL;
23488 else
23489 {
23490 to->vval.v_partial = from->vval.v_partial;
23491 ++to->vval.v_partial->pt_refcount;
23492 }
23493 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023494 case VAR_LIST:
23495 if (from->vval.v_list == NULL)
23496 to->vval.v_list = NULL;
23497 else
23498 {
23499 to->vval.v_list = from->vval.v_list;
23500 ++to->vval.v_list->lv_refcount;
23501 }
23502 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023503 case VAR_DICT:
23504 if (from->vval.v_dict == NULL)
23505 to->vval.v_dict = NULL;
23506 else
23507 {
23508 to->vval.v_dict = from->vval.v_dict;
23509 ++to->vval.v_dict->dv_refcount;
23510 }
23511 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023512 case VAR_UNKNOWN:
23513 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023514 break;
23515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516}
23517
23518/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023519 * Make a copy of an item.
23520 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023521 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23522 * reference to an already copied list/dict can be used.
23523 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023524 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023525 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023526item_copy(
23527 typval_T *from,
23528 typval_T *to,
23529 int deep,
23530 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023531{
23532 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023533 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023534
Bram Moolenaar33570922005-01-25 22:26:29 +000023535 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023536 {
23537 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023538 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023539 }
23540 ++recurse;
23541
23542 switch (from->v_type)
23543 {
23544 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023545 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023546 case VAR_STRING:
23547 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023548 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023549 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023550 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023551 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023552 copy_tv(from, to);
23553 break;
23554 case VAR_LIST:
23555 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023556 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023557 if (from->vval.v_list == NULL)
23558 to->vval.v_list = NULL;
23559 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23560 {
23561 /* use the copy made earlier */
23562 to->vval.v_list = from->vval.v_list->lv_copylist;
23563 ++to->vval.v_list->lv_refcount;
23564 }
23565 else
23566 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23567 if (to->vval.v_list == NULL)
23568 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023569 break;
23570 case VAR_DICT:
23571 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023572 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023573 if (from->vval.v_dict == NULL)
23574 to->vval.v_dict = NULL;
23575 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23576 {
23577 /* use the copy made earlier */
23578 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23579 ++to->vval.v_dict->dv_refcount;
23580 }
23581 else
23582 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23583 if (to->vval.v_dict == NULL)
23584 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023585 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023586 case VAR_UNKNOWN:
23587 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023588 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023589 }
23590 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023591 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023592}
23593
23594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 * ":echo expr1 ..." print each argument separated with a space, add a
23596 * newline at the end.
23597 * ":echon expr1 ..." print each argument plain.
23598 */
23599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023600ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601{
23602 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023603 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023604 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605 char_u *p;
23606 int needclr = TRUE;
23607 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023608 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609
23610 if (eap->skip)
23611 ++emsg_skip;
23612 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23613 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023614 /* If eval1() causes an error message the text from the command may
23615 * still need to be cleared. E.g., "echo 22,44". */
23616 need_clr_eos = needclr;
23617
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023619 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 {
23621 /*
23622 * Report the invalid expression unless the expression evaluation
23623 * has been cancelled due to an aborting error, an interrupt, or an
23624 * exception.
23625 */
23626 if (!aborting())
23627 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023628 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 break;
23630 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023631 need_clr_eos = FALSE;
23632
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 if (!eap->skip)
23634 {
23635 if (atstart)
23636 {
23637 atstart = FALSE;
23638 /* Call msg_start() after eval1(), evaluating the expression
23639 * may cause a message to appear. */
23640 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023641 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023642 /* Mark the saved text as finishing the line, so that what
23643 * follows is displayed on a new line when scrolling back
23644 * at the more prompt. */
23645 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 }
23649 else if (eap->cmdidx == CMD_echo)
23650 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023651 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023652 if (p != NULL)
23653 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023655 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023657 if (*p != TAB && needclr)
23658 {
23659 /* remove any text still there from the command */
23660 msg_clr_eos();
23661 needclr = FALSE;
23662 }
23663 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 }
23665 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023666 {
23667#ifdef FEAT_MBYTE
23668 if (has_mbyte)
23669 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023670 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023671
23672 (void)msg_outtrans_len_attr(p, i, echo_attr);
23673 p += i - 1;
23674 }
23675 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023677 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023680 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023682 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 arg = skipwhite(arg);
23684 }
23685 eap->nextcmd = check_nextcmd(arg);
23686
23687 if (eap->skip)
23688 --emsg_skip;
23689 else
23690 {
23691 /* remove text that may still be there from the command */
23692 if (needclr)
23693 msg_clr_eos();
23694 if (eap->cmdidx == CMD_echo)
23695 msg_end();
23696 }
23697}
23698
23699/*
23700 * ":echohl {name}".
23701 */
23702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023703ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704{
23705 int id;
23706
23707 id = syn_name2id(eap->arg);
23708 if (id == 0)
23709 echo_attr = 0;
23710 else
23711 echo_attr = syn_id2attr(id);
23712}
23713
23714/*
23715 * ":execute expr1 ..." execute the result of an expression.
23716 * ":echomsg expr1 ..." Print a message
23717 * ":echoerr expr1 ..." Print an error
23718 * Each gets spaces around each argument and a newline at the end for
23719 * echo commands
23720 */
23721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023722ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723{
23724 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023725 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 int ret = OK;
23727 char_u *p;
23728 garray_T ga;
23729 int len;
23730 int save_did_emsg;
23731
23732 ga_init2(&ga, 1, 80);
23733
23734 if (eap->skip)
23735 ++emsg_skip;
23736 while (*arg != NUL && *arg != '|' && *arg != '\n')
23737 {
23738 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023739 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 {
23741 /*
23742 * Report the invalid expression unless the expression evaluation
23743 * has been cancelled due to an aborting error, an interrupt, or an
23744 * exception.
23745 */
23746 if (!aborting())
23747 EMSG2(_(e_invexpr2), p);
23748 ret = FAIL;
23749 break;
23750 }
23751
23752 if (!eap->skip)
23753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023754 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 len = (int)STRLEN(p);
23756 if (ga_grow(&ga, len + 2) == FAIL)
23757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023758 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 ret = FAIL;
23760 break;
23761 }
23762 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 ga.ga_len += len;
23766 }
23767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023768 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023769 arg = skipwhite(arg);
23770 }
23771
23772 if (ret != FAIL && ga.ga_data != NULL)
23773 {
23774 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023777 out_flush();
23778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 else if (eap->cmdidx == CMD_echoerr)
23780 {
23781 /* We don't want to abort following commands, restore did_emsg. */
23782 save_did_emsg = did_emsg;
23783 EMSG((char_u *)ga.ga_data);
23784 if (!force_abort)
23785 did_emsg = save_did_emsg;
23786 }
23787 else if (eap->cmdidx == CMD_execute)
23788 do_cmdline((char_u *)ga.ga_data,
23789 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23790 }
23791
23792 ga_clear(&ga);
23793
23794 if (eap->skip)
23795 --emsg_skip;
23796
23797 eap->nextcmd = check_nextcmd(arg);
23798}
23799
23800/*
23801 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23802 * "arg" points to the "&" or '+' when called, to "option" when returning.
23803 * Returns NULL when no option name found. Otherwise pointer to the char
23804 * after the option name.
23805 */
23806 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023807find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808{
23809 char_u *p = *arg;
23810
23811 ++p;
23812 if (*p == 'g' && p[1] == ':')
23813 {
23814 *opt_flags = OPT_GLOBAL;
23815 p += 2;
23816 }
23817 else if (*p == 'l' && p[1] == ':')
23818 {
23819 *opt_flags = OPT_LOCAL;
23820 p += 2;
23821 }
23822 else
23823 *opt_flags = 0;
23824
23825 if (!ASCII_ISALPHA(*p))
23826 return NULL;
23827 *arg = p;
23828
23829 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23830 p += 4; /* termcap option */
23831 else
23832 while (ASCII_ISALPHA(*p))
23833 ++p;
23834 return p;
23835}
23836
23837/*
23838 * ":function"
23839 */
23840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023841ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842{
23843 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023844 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 int j;
23846 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023848 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 char_u *name = NULL;
23850 char_u *p;
23851 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023852 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853 garray_T newargs;
23854 garray_T newlines;
23855 int varargs = FALSE;
23856 int mustend = FALSE;
23857 int flags = 0;
23858 ufunc_T *fp;
23859 int indent;
23860 int nesting;
23861 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023862 dictitem_T *v;
23863 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023864 static int func_nr = 0; /* number for nameless function */
23865 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023866 hashtab_T *ht;
23867 int todo;
23868 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023869 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870
23871 /*
23872 * ":function" without argument: list functions.
23873 */
23874 if (ends_excmd(*eap->arg))
23875 {
23876 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023877 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023878 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023879 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023880 {
23881 if (!HASHITEM_EMPTY(hi))
23882 {
23883 --todo;
23884 fp = HI2UF(hi);
23885 if (!isdigit(*fp->uf_name))
23886 list_func_head(fp, FALSE);
23887 }
23888 }
23889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 eap->nextcmd = check_nextcmd(eap->arg);
23891 return;
23892 }
23893
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023894 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023895 * ":function /pat": list functions matching pattern.
23896 */
23897 if (*eap->arg == '/')
23898 {
23899 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23900 if (!eap->skip)
23901 {
23902 regmatch_T regmatch;
23903
23904 c = *p;
23905 *p = NUL;
23906 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23907 *p = c;
23908 if (regmatch.regprog != NULL)
23909 {
23910 regmatch.rm_ic = p_ic;
23911
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023912 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023913 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23914 {
23915 if (!HASHITEM_EMPTY(hi))
23916 {
23917 --todo;
23918 fp = HI2UF(hi);
23919 if (!isdigit(*fp->uf_name)
23920 && vim_regexec(&regmatch, fp->uf_name, 0))
23921 list_func_head(fp, FALSE);
23922 }
23923 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023924 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023925 }
23926 }
23927 if (*p == '/')
23928 ++p;
23929 eap->nextcmd = check_nextcmd(p);
23930 return;
23931 }
23932
23933 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023934 * Get the function name. There are these situations:
23935 * func normal function name
23936 * "name" == func, "fudi.fd_dict" == NULL
23937 * dict.func new dictionary entry
23938 * "name" == NULL, "fudi.fd_dict" set,
23939 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23940 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023941 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023942 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23943 * dict.func existing dict entry that's not a Funcref
23944 * "name" == NULL, "fudi.fd_dict" set,
23945 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023946 * s:func script-local function name
23947 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023950 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023951 paren = (vim_strchr(p, '(') != NULL);
23952 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 {
23954 /*
23955 * Return on an invalid expression in braces, unless the expression
23956 * evaluation has been cancelled due to an aborting error, an
23957 * interrupt, or an exception.
23958 */
23959 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023961 if (!eap->skip && fudi.fd_newkey != NULL)
23962 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023963 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 else
23967 eap->skip = TRUE;
23968 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023969
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970 /* An error in a function call during evaluation of an expression in magic
23971 * braces should not cause the function not to be defined. */
23972 saved_did_emsg = did_emsg;
23973 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974
23975 /*
23976 * ":function func" with only function name: list function.
23977 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023978 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 {
23980 if (!ends_excmd(*skipwhite(p)))
23981 {
23982 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023983 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 }
23985 eap->nextcmd = check_nextcmd(p);
23986 if (eap->nextcmd != NULL)
23987 *p = NUL;
23988 if (!eap->skip && !got_int)
23989 {
23990 fp = find_func(name);
23991 if (fp != NULL)
23992 {
23993 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023994 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023996 if (FUNCLINE(fp, j) == NULL)
23997 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 msg_putchar('\n');
23999 msg_outnum((long)(j + 1));
24000 if (j < 9)
24001 msg_putchar(' ');
24002 if (j < 99)
24003 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024004 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 out_flush(); /* show a line at a time */
24006 ui_breakcheck();
24007 }
24008 if (!got_int)
24009 {
24010 msg_putchar('\n');
24011 msg_puts((char_u *)" endfunction");
24012 }
24013 }
24014 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024015 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024017 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 }
24019
24020 /*
24021 * ":function name(arg1, arg2)" Define function.
24022 */
24023 p = skipwhite(p);
24024 if (*p != '(')
24025 {
24026 if (!eap->skip)
24027 {
24028 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024029 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 }
24031 /* attempt to continue by skipping some text */
24032 if (vim_strchr(p, '(') != NULL)
24033 p = vim_strchr(p, '(');
24034 }
24035 p = skipwhite(p + 1);
24036
24037 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24038 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24039
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024040 if (!eap->skip)
24041 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024042 /* Check the name of the function. Unless it's a dictionary function
24043 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024044 if (name != NULL)
24045 arg = name;
24046 else
24047 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024048 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024049 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24050 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024051 {
24052 if (*arg == K_SPECIAL)
24053 j = 3;
24054 else
24055 j = 0;
24056 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24057 : eval_isnamec(arg[j])))
24058 ++j;
24059 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024060 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024061 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024062 /* Disallow using the g: dict. */
24063 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24064 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024065 }
24066
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 /*
24068 * Isolate the arguments: "arg1, arg2, ...)"
24069 */
24070 while (*p != ')')
24071 {
24072 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24073 {
24074 varargs = TRUE;
24075 p += 3;
24076 mustend = TRUE;
24077 }
24078 else
24079 {
24080 arg = p;
24081 while (ASCII_ISALNUM(*p) || *p == '_')
24082 ++p;
24083 if (arg == p || isdigit(*arg)
24084 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24085 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24086 {
24087 if (!eap->skip)
24088 EMSG2(_("E125: Illegal argument: %s"), arg);
24089 break;
24090 }
24091 if (ga_grow(&newargs, 1) == FAIL)
24092 goto erret;
24093 c = *p;
24094 *p = NUL;
24095 arg = vim_strsave(arg);
24096 if (arg == NULL)
24097 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024098
24099 /* Check for duplicate argument name. */
24100 for (i = 0; i < newargs.ga_len; ++i)
24101 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24102 {
24103 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024104 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024105 goto erret;
24106 }
24107
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24109 *p = c;
24110 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 if (*p == ',')
24112 ++p;
24113 else
24114 mustend = TRUE;
24115 }
24116 p = skipwhite(p);
24117 if (mustend && *p != ')')
24118 {
24119 if (!eap->skip)
24120 EMSG2(_(e_invarg2), eap->arg);
24121 break;
24122 }
24123 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024124 if (*p != ')')
24125 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 ++p; /* skip the ')' */
24127
Bram Moolenaare9a41262005-01-15 22:18:47 +000024128 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 for (;;)
24130 {
24131 p = skipwhite(p);
24132 if (STRNCMP(p, "range", 5) == 0)
24133 {
24134 flags |= FC_RANGE;
24135 p += 5;
24136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024137 else if (STRNCMP(p, "dict", 4) == 0)
24138 {
24139 flags |= FC_DICT;
24140 p += 4;
24141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 else if (STRNCMP(p, "abort", 5) == 0)
24143 {
24144 flags |= FC_ABORT;
24145 p += 5;
24146 }
24147 else
24148 break;
24149 }
24150
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024151 /* When there is a line break use what follows for the function body.
24152 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24153 if (*p == '\n')
24154 line_arg = p + 1;
24155 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 EMSG(_(e_trailing));
24157
24158 /*
24159 * Read the body of the function, until ":endfunction" is found.
24160 */
24161 if (KeyTyped)
24162 {
24163 /* Check if the function already exists, don't let the user type the
24164 * whole function before telling him it doesn't work! For a script we
24165 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024166 if (!eap->skip && !eap->forceit)
24167 {
24168 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24169 EMSG(_(e_funcdict));
24170 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024171 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024174 if (!eap->skip && did_emsg)
24175 goto erret;
24176
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 msg_putchar('\n'); /* don't overwrite the function name */
24178 cmdline_row = msg_row;
24179 }
24180
24181 indent = 2;
24182 nesting = 0;
24183 for (;;)
24184 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024185 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024186 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024187 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024188 saved_wait_return = FALSE;
24189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024191 sourcing_lnum_off = sourcing_lnum;
24192
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024193 if (line_arg != NULL)
24194 {
24195 /* Use eap->arg, split up in parts by line breaks. */
24196 theline = line_arg;
24197 p = vim_strchr(theline, '\n');
24198 if (p == NULL)
24199 line_arg += STRLEN(line_arg);
24200 else
24201 {
24202 *p = NUL;
24203 line_arg = p + 1;
24204 }
24205 }
24206 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 theline = getcmdline(':', 0L, indent);
24208 else
24209 theline = eap->getline(':', eap->cookie, indent);
24210 if (KeyTyped)
24211 lines_left = Rows - 1;
24212 if (theline == NULL)
24213 {
24214 EMSG(_("E126: Missing :endfunction"));
24215 goto erret;
24216 }
24217
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024218 /* Detect line continuation: sourcing_lnum increased more than one. */
24219 if (sourcing_lnum > sourcing_lnum_off + 1)
24220 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24221 else
24222 sourcing_lnum_off = 0;
24223
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 if (skip_until != NULL)
24225 {
24226 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24227 * don't check for ":endfunc". */
24228 if (STRCMP(theline, skip_until) == 0)
24229 {
24230 vim_free(skip_until);
24231 skip_until = NULL;
24232 }
24233 }
24234 else
24235 {
24236 /* skip ':' and blanks*/
24237 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24238 ;
24239
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024240 /* Check for "endfunction". */
24241 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024243 if (line_arg == NULL)
24244 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 break;
24246 }
24247
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024248 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 * at "end". */
24250 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24251 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024252 else if (STRNCMP(p, "if", 2) == 0
24253 || STRNCMP(p, "wh", 2) == 0
24254 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 || STRNCMP(p, "try", 3) == 0)
24256 indent += 2;
24257
24258 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024259 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024261 if (*p == '!')
24262 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024264 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024265 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024267 ++nesting;
24268 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 }
24270 }
24271
24272 /* Check for ":append" or ":insert". */
24273 p = skip_range(p, NULL);
24274 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24275 || (p[0] == 'i'
24276 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24277 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24278 skip_until = vim_strsave((char_u *)".");
24279
24280 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24281 arg = skipwhite(skiptowhite(p));
24282 if (arg[0] == '<' && arg[1] =='<'
24283 && ((p[0] == 'p' && p[1] == 'y'
24284 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24285 || (p[0] == 'p' && p[1] == 'e'
24286 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24287 || (p[0] == 't' && p[1] == 'c'
24288 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024289 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24290 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024291 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24292 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024293 || (p[0] == 'm' && p[1] == 'z'
24294 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 ))
24296 {
24297 /* ":python <<" continues until a dot, like ":append" */
24298 p = skipwhite(arg + 2);
24299 if (*p == NUL)
24300 skip_until = vim_strsave((char_u *)".");
24301 else
24302 skip_until = vim_strsave(p);
24303 }
24304 }
24305
24306 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024307 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024308 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024309 if (line_arg == NULL)
24310 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024312 }
24313
24314 /* Copy the line to newly allocated memory. get_one_sourceline()
24315 * allocates 250 bytes per line, this saves 80% on average. The cost
24316 * is an extra alloc/free. */
24317 p = vim_strsave(theline);
24318 if (p != NULL)
24319 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024320 if (line_arg == NULL)
24321 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024322 theline = p;
24323 }
24324
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024325 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24326
24327 /* Add NULL lines for continuation lines, so that the line count is
24328 * equal to the index in the growarray. */
24329 while (sourcing_lnum_off-- > 0)
24330 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024331
24332 /* Check for end of eap->arg. */
24333 if (line_arg != NULL && *line_arg == NUL)
24334 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 }
24336
24337 /* Don't define the function when skipping commands or when an error was
24338 * detected. */
24339 if (eap->skip || did_emsg)
24340 goto erret;
24341
24342 /*
24343 * If there are no errors, add the function
24344 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024345 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024346 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024347 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024348 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024349 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024350 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024351 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024352 goto erret;
24353 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024355 fp = find_func(name);
24356 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024358 if (!eap->forceit)
24359 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024360 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024361 goto erret;
24362 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024363 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024364 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024365 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024366 name);
24367 goto erret;
24368 }
24369 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024370 ga_clear_strings(&(fp->uf_args));
24371 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372 vim_free(name);
24373 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 }
24376 else
24377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024378 char numbuf[20];
24379
24380 fp = NULL;
24381 if (fudi.fd_newkey == NULL && !eap->forceit)
24382 {
24383 EMSG(_(e_funcdict));
24384 goto erret;
24385 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024386 if (fudi.fd_di == NULL)
24387 {
24388 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024389 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024390 goto erret;
24391 }
24392 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024393 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024394 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024395
24396 /* Give the function a sequential number. Can only be used with a
24397 * Funcref! */
24398 vim_free(name);
24399 sprintf(numbuf, "%d", ++func_nr);
24400 name = vim_strsave((char_u *)numbuf);
24401 if (name == NULL)
24402 goto erret;
24403 }
24404
24405 if (fp == NULL)
24406 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024407 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024408 {
24409 int slen, plen;
24410 char_u *scriptname;
24411
24412 /* Check that the autoload name matches the script name. */
24413 j = FAIL;
24414 if (sourcing_name != NULL)
24415 {
24416 scriptname = autoload_name(name);
24417 if (scriptname != NULL)
24418 {
24419 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024420 plen = (int)STRLEN(p);
24421 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024422 if (slen > plen && fnamecmp(p,
24423 sourcing_name + slen - plen) == 0)
24424 j = OK;
24425 vim_free(scriptname);
24426 }
24427 }
24428 if (j == FAIL)
24429 {
24430 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24431 goto erret;
24432 }
24433 }
24434
24435 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436 if (fp == NULL)
24437 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024438
24439 if (fudi.fd_dict != NULL)
24440 {
24441 if (fudi.fd_di == NULL)
24442 {
24443 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024444 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024445 if (fudi.fd_di == NULL)
24446 {
24447 vim_free(fp);
24448 goto erret;
24449 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024450 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24451 {
24452 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024453 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024454 goto erret;
24455 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024456 }
24457 else
24458 /* overwrite existing dict entry */
24459 clear_tv(&fudi.fd_di->di_tv);
24460 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024461 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024462 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024463 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024464
24465 /* behave like "dict" was used */
24466 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024467 }
24468
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024471 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24472 {
24473 vim_free(fp);
24474 goto erret;
24475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024477 fp->uf_args = newargs;
24478 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024479#ifdef FEAT_PROFILE
24480 fp->uf_tml_count = NULL;
24481 fp->uf_tml_total = NULL;
24482 fp->uf_tml_self = NULL;
24483 fp->uf_profiling = FALSE;
24484 if (prof_def_func())
24485 func_do_profile(fp);
24486#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024487 fp->uf_varargs = varargs;
24488 fp->uf_flags = flags;
24489 fp->uf_calls = 0;
24490 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024491 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492
24493erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494 ga_clear_strings(&newargs);
24495 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024496ret_free:
24497 vim_free(skip_until);
24498 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024501 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502}
24503
24504/*
24505 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024506 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024508 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024509 * TFN_INT: internal function name OK
24510 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024511 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 * Advances "pp" to just after the function name (if no error).
24513 */
24514 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024515trans_function_name(
24516 char_u **pp,
24517 int skip, /* only find the end, don't evaluate */
24518 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024519 funcdict_T *fdp, /* return: info about dictionary used */
24520 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024522 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 char_u *start;
24524 char_u *end;
24525 int lead;
24526 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024528 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024529
24530 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024533
24534 /* Check for hard coded <SNR>: already translated function ID (from a user
24535 * command). */
24536 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24537 && (*pp)[2] == (int)KE_SNR)
24538 {
24539 *pp += 3;
24540 len = get_id_len(pp) + 3;
24541 return vim_strnsave(start, len);
24542 }
24543
24544 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24545 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024547 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024548 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024549
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024550 /* Note that TFN_ flags use the same values as GLV_ flags. */
24551 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024552 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024553 if (end == start)
24554 {
24555 if (!skip)
24556 EMSG(_("E129: Function name required"));
24557 goto theend;
24558 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024559 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024560 {
24561 /*
24562 * Report an invalid expression in braces, unless the expression
24563 * evaluation has been cancelled due to an aborting error, an
24564 * interrupt, or an exception.
24565 */
24566 if (!aborting())
24567 {
24568 if (end != NULL)
24569 EMSG2(_(e_invarg2), start);
24570 }
24571 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024572 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024573 goto theend;
24574 }
24575
24576 if (lv.ll_tv != NULL)
24577 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024578 if (fdp != NULL)
24579 {
24580 fdp->fd_dict = lv.ll_dict;
24581 fdp->fd_newkey = lv.ll_newkey;
24582 lv.ll_newkey = NULL;
24583 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024584 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024585 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24586 {
24587 name = vim_strsave(lv.ll_tv->vval.v_string);
24588 *pp = end;
24589 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024590 else if (lv.ll_tv->v_type == VAR_PARTIAL
24591 && lv.ll_tv->vval.v_partial != NULL)
24592 {
24593 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24594 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024595 if (partial != NULL)
24596 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024598 else
24599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024600 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24601 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024602 EMSG(_(e_funcref));
24603 else
24604 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024605 name = NULL;
24606 }
24607 goto theend;
24608 }
24609
24610 if (lv.ll_name == NULL)
24611 {
24612 /* Error found, but continue after the function name. */
24613 *pp = end;
24614 goto theend;
24615 }
24616
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024617 /* Check if the name is a Funcref. If so, use the value. */
24618 if (lv.ll_exp_name != NULL)
24619 {
24620 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024621 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024622 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024623 if (name == lv.ll_exp_name)
24624 name = NULL;
24625 }
24626 else
24627 {
24628 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024629 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024630 if (name == *pp)
24631 name = NULL;
24632 }
24633 if (name != NULL)
24634 {
24635 name = vim_strsave(name);
24636 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024637 if (STRNCMP(name, "<SNR>", 5) == 0)
24638 {
24639 /* Change "<SNR>" to the byte sequence. */
24640 name[0] = K_SPECIAL;
24641 name[1] = KS_EXTRA;
24642 name[2] = (int)KE_SNR;
24643 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24644 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024645 goto theend;
24646 }
24647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024648 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024649 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024650 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024651 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24652 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24653 {
24654 /* When there was "s:" already or the name expanded to get a
24655 * leading "s:" then remove it. */
24656 lv.ll_name += 2;
24657 len -= 2;
24658 lead = 2;
24659 }
24660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024661 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024662 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024663 /* skip over "s:" and "g:" */
24664 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024665 lv.ll_name += 2;
24666 len = (int)(end - lv.ll_name);
24667 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024668
24669 /*
24670 * Copy the function name to allocated memory.
24671 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24672 * Accept <SNR>123_name() outside a script.
24673 */
24674 if (skip)
24675 lead = 0; /* do nothing */
24676 else if (lead > 0)
24677 {
24678 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024679 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24680 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024681 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024682 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024683 if (current_SID <= 0)
24684 {
24685 EMSG(_(e_usingsid));
24686 goto theend;
24687 }
24688 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24689 lead += (int)STRLEN(sid_buf);
24690 }
24691 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024692 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024693 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024694 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024695 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024696 goto theend;
24697 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024698 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024699 {
24700 char_u *cp = vim_strchr(lv.ll_name, ':');
24701
24702 if (cp != NULL && cp < end)
24703 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024704 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024705 goto theend;
24706 }
24707 }
24708
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024709 name = alloc((unsigned)(len + lead + 1));
24710 if (name != NULL)
24711 {
24712 if (lead > 0)
24713 {
24714 name[0] = K_SPECIAL;
24715 name[1] = KS_EXTRA;
24716 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024717 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024718 STRCPY(name + 3, sid_buf);
24719 }
24720 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024721 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024722 }
24723 *pp = end;
24724
24725theend:
24726 clear_lval(&lv);
24727 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728}
24729
24730/*
24731 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24732 * Return 2 if "p" starts with "s:".
24733 * Return 0 otherwise.
24734 */
24735 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024736eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024738 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24739 * the standard library function. */
24740 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24741 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 return 5;
24743 if (p[0] == 's' && p[1] == ':')
24744 return 2;
24745 return 0;
24746}
24747
24748/*
24749 * Return TRUE if "p" starts with "<SID>" or "s:".
24750 * Only works if eval_fname_script() returned non-zero for "p"!
24751 */
24752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024753eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754{
24755 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24756}
24757
24758/*
24759 * List the head of the function: "name(arg1, arg2)".
24760 */
24761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024762list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763{
24764 int j;
24765
24766 msg_start();
24767 if (indent)
24768 MSG_PUTS(" ");
24769 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 {
24772 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024773 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 }
24775 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024778 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 {
24780 if (j)
24781 MSG_PUTS(", ");
24782 msg_puts(FUNCARG(fp, j));
24783 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024784 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 {
24786 if (j)
24787 MSG_PUTS(", ");
24788 MSG_PUTS("...");
24789 }
24790 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024791 if (fp->uf_flags & FC_ABORT)
24792 MSG_PUTS(" abort");
24793 if (fp->uf_flags & FC_RANGE)
24794 MSG_PUTS(" range");
24795 if (fp->uf_flags & FC_DICT)
24796 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024797 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024798 if (p_verbose > 0)
24799 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800}
24801
24802/*
24803 * Find a function by name, return pointer to it in ufuncs.
24804 * Return NULL for unknown function.
24805 */
24806 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024807find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024808{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024809 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024811 hi = hash_find(&func_hashtab, name);
24812 if (!HASHITEM_EMPTY(hi))
24813 return HI2UF(hi);
24814 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815}
24816
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024817#if defined(EXITFREE) || defined(PROTO)
24818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024819free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024820{
24821 hashitem_T *hi;
24822
24823 /* Need to start all over every time, because func_free() may change the
24824 * hash table. */
24825 while (func_hashtab.ht_used > 0)
24826 for (hi = func_hashtab.ht_array; ; ++hi)
24827 if (!HASHITEM_EMPTY(hi))
24828 {
24829 func_free(HI2UF(hi));
24830 break;
24831 }
24832}
24833#endif
24834
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024836translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024837{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024838 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024839 return find_internal_func(name) >= 0;
24840 return find_func(name) != NULL;
24841}
24842
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024843/*
24844 * Return TRUE if a function "name" exists.
24845 */
24846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024847function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024848{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024849 char_u *nm = name;
24850 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024851 int n = FALSE;
24852
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024853 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024854 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024855 nm = skipwhite(nm);
24856
24857 /* Only accept "funcname", "funcname ", "funcname (..." and
24858 * "funcname(...", not "funcname!...". */
24859 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024860 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024861 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024862 return n;
24863}
24864
Bram Moolenaara1544c02013-05-30 12:35:52 +020024865 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024866get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024867{
24868 char_u *nm = name;
24869 char_u *p;
24870
Bram Moolenaar65639032016-03-16 21:40:30 +010024871 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024872
24873 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024874 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024875 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024876
Bram Moolenaara1544c02013-05-30 12:35:52 +020024877 vim_free(p);
24878 return NULL;
24879}
24880
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024881/*
24882 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024883 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24884 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024885 */
24886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024887builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024888{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024889 char_u *p;
24890
24891 if (!ASCII_ISLOWER(name[0]))
24892 return FALSE;
24893 p = vim_strchr(name, AUTOLOAD_CHAR);
24894 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024895}
24896
Bram Moolenaar05159a02005-02-26 23:04:13 +000024897#if defined(FEAT_PROFILE) || defined(PROTO)
24898/*
24899 * Start profiling function "fp".
24900 */
24901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024902func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024903{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024904 int len = fp->uf_lines.ga_len;
24905
24906 if (len == 0)
24907 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024908 fp->uf_tm_count = 0;
24909 profile_zero(&fp->uf_tm_self);
24910 profile_zero(&fp->uf_tm_total);
24911 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024912 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024913 if (fp->uf_tml_total == NULL)
24914 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024915 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024916 if (fp->uf_tml_self == NULL)
24917 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024918 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024919 fp->uf_tml_idx = -1;
24920 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24921 || fp->uf_tml_self == NULL)
24922 return; /* out of memory */
24923
24924 fp->uf_profiling = TRUE;
24925}
24926
24927/*
24928 * Dump the profiling results for all functions in file "fd".
24929 */
24930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024931func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024932{
24933 hashitem_T *hi;
24934 int todo;
24935 ufunc_T *fp;
24936 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024937 ufunc_T **sorttab;
24938 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024939
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024940 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024941 if (todo == 0)
24942 return; /* nothing to dump */
24943
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024944 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024945
Bram Moolenaar05159a02005-02-26 23:04:13 +000024946 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24947 {
24948 if (!HASHITEM_EMPTY(hi))
24949 {
24950 --todo;
24951 fp = HI2UF(hi);
24952 if (fp->uf_profiling)
24953 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024954 if (sorttab != NULL)
24955 sorttab[st_len++] = fp;
24956
Bram Moolenaar05159a02005-02-26 23:04:13 +000024957 if (fp->uf_name[0] == K_SPECIAL)
24958 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24959 else
24960 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24961 if (fp->uf_tm_count == 1)
24962 fprintf(fd, "Called 1 time\n");
24963 else
24964 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24965 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24966 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24967 fprintf(fd, "\n");
24968 fprintf(fd, "count total (s) self (s)\n");
24969
24970 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24971 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024972 if (FUNCLINE(fp, i) == NULL)
24973 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024974 prof_func_line(fd, fp->uf_tml_count[i],
24975 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24977 }
24978 fprintf(fd, "\n");
24979 }
24980 }
24981 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024982
24983 if (sorttab != NULL && st_len > 0)
24984 {
24985 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24986 prof_total_cmp);
24987 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24988 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24989 prof_self_cmp);
24990 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24991 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024992
24993 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024994}
Bram Moolenaar73830342005-02-28 22:48:19 +000024995
24996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024997prof_sort_list(
24998 FILE *fd,
24999 ufunc_T **sorttab,
25000 int st_len,
25001 char *title,
25002 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025003{
25004 int i;
25005 ufunc_T *fp;
25006
25007 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25008 fprintf(fd, "count total (s) self (s) function\n");
25009 for (i = 0; i < 20 && i < st_len; ++i)
25010 {
25011 fp = sorttab[i];
25012 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25013 prefer_self);
25014 if (fp->uf_name[0] == K_SPECIAL)
25015 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25016 else
25017 fprintf(fd, " %s()\n", fp->uf_name);
25018 }
25019 fprintf(fd, "\n");
25020}
25021
25022/*
25023 * Print the count and times for one function or function line.
25024 */
25025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025026prof_func_line(
25027 FILE *fd,
25028 int count,
25029 proftime_T *total,
25030 proftime_T *self,
25031 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025032{
25033 if (count > 0)
25034 {
25035 fprintf(fd, "%5d ", count);
25036 if (prefer_self && profile_equal(total, self))
25037 fprintf(fd, " ");
25038 else
25039 fprintf(fd, "%s ", profile_msg(total));
25040 if (!prefer_self && profile_equal(total, self))
25041 fprintf(fd, " ");
25042 else
25043 fprintf(fd, "%s ", profile_msg(self));
25044 }
25045 else
25046 fprintf(fd, " ");
25047}
25048
25049/*
25050 * Compare function for total time sorting.
25051 */
25052 static int
25053#ifdef __BORLANDC__
25054_RTLENTRYF
25055#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025056prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025057{
25058 ufunc_T *p1, *p2;
25059
25060 p1 = *(ufunc_T **)s1;
25061 p2 = *(ufunc_T **)s2;
25062 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25063}
25064
25065/*
25066 * Compare function for self time sorting.
25067 */
25068 static int
25069#ifdef __BORLANDC__
25070_RTLENTRYF
25071#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025072prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025073{
25074 ufunc_T *p1, *p2;
25075
25076 p1 = *(ufunc_T **)s1;
25077 p2 = *(ufunc_T **)s2;
25078 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25079}
25080
Bram Moolenaar05159a02005-02-26 23:04:13 +000025081#endif
25082
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025083/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025084 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025085 * Return TRUE if a package was loaded.
25086 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025088script_autoload(
25089 char_u *name,
25090 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025091{
25092 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025093 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025094 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025095 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025096
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025097 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025098 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025099 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025100 return FALSE;
25101
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025102 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025103
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025104 /* Find the name in the list of previously loaded package names. Skip
25105 * "autoload/", it's always the same. */
25106 for (i = 0; i < ga_loaded.ga_len; ++i)
25107 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25108 break;
25109 if (!reload && i < ga_loaded.ga_len)
25110 ret = FALSE; /* was loaded already */
25111 else
25112 {
25113 /* Remember the name if it wasn't loaded already. */
25114 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25115 {
25116 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25117 tofree = NULL;
25118 }
25119
25120 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025121 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025122 ret = TRUE;
25123 }
25124
25125 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025126 return ret;
25127}
25128
25129/*
25130 * Return the autoload script name for a function or variable name.
25131 * Returns NULL when out of memory.
25132 */
25133 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025134autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025135{
25136 char_u *p;
25137 char_u *scriptname;
25138
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025139 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025140 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25141 if (scriptname == NULL)
25142 return FALSE;
25143 STRCPY(scriptname, "autoload/");
25144 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025145 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025146 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025147 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025148 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025149 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025150}
25151
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25153
25154/*
25155 * Function given to ExpandGeneric() to obtain the list of user defined
25156 * function names.
25157 */
25158 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025159get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025161 static long_u done;
25162 static hashitem_T *hi;
25163 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164
25165 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025167 done = 0;
25168 hi = func_hashtab.ht_array;
25169 }
25170 if (done < func_hashtab.ht_used)
25171 {
25172 if (done++ > 0)
25173 ++hi;
25174 while (HASHITEM_EMPTY(hi))
25175 ++hi;
25176 fp = HI2UF(hi);
25177
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025178 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025179 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025180
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025181 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25182 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183
25184 cat_func_name(IObuff, fp);
25185 if (xp->xp_context != EXPAND_USER_FUNC)
25186 {
25187 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025188 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 STRCAT(IObuff, ")");
25190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191 return IObuff;
25192 }
25193 return NULL;
25194}
25195
25196#endif /* FEAT_CMDL_COMPL */
25197
25198/*
25199 * Copy the function name of "fp" to buffer "buf".
25200 * "buf" must be able to hold the function name plus three bytes.
25201 * Takes care of script-local function names.
25202 */
25203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025204cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025206 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 {
25208 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025209 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 }
25211 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025212 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213}
25214
25215/*
25216 * ":delfunction {name}"
25217 */
25218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025219ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025221 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222 char_u *p;
25223 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025224 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225
25226 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025227 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025228 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025230 {
25231 if (fudi.fd_dict != NULL && !eap->skip)
25232 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 if (!ends_excmd(*skipwhite(p)))
25236 {
25237 vim_free(name);
25238 EMSG(_(e_trailing));
25239 return;
25240 }
25241 eap->nextcmd = check_nextcmd(p);
25242 if (eap->nextcmd != NULL)
25243 *p = NUL;
25244
25245 if (!eap->skip)
25246 fp = find_func(name);
25247 vim_free(name);
25248
25249 if (!eap->skip)
25250 {
25251 if (fp == NULL)
25252 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025253 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254 return;
25255 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025256 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 {
25258 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25259 return;
25260 }
25261
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025262 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025264 /* Delete the dict item that refers to the function, it will
25265 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025266 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025268 else
25269 func_free(fp);
25270 }
25271}
25272
25273/*
25274 * Free a function and remove it from the list of functions.
25275 */
25276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025277func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025278{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025279 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025280
25281 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025282 ga_clear_strings(&(fp->uf_args));
25283 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025284#ifdef FEAT_PROFILE
25285 vim_free(fp->uf_tml_count);
25286 vim_free(fp->uf_tml_total);
25287 vim_free(fp->uf_tml_self);
25288#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025289
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025290 /* remove the function from the function hashtable */
25291 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25292 if (HASHITEM_EMPTY(hi))
25293 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025294 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025295 hash_remove(&func_hashtab, hi);
25296
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025297 vim_free(fp);
25298}
25299
25300/*
25301 * Unreference a Function: decrement the reference count and free it when it
25302 * becomes zero. Only for numbered functions.
25303 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025305func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025306{
25307 ufunc_T *fp;
25308
25309 if (name != NULL && isdigit(*name))
25310 {
25311 fp = find_func(name);
25312 if (fp == NULL)
25313 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025314 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025315 {
25316 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025317 * when "uf_calls" becomes zero. */
25318 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025319 func_free(fp);
25320 }
25321 }
25322}
25323
25324/*
25325 * Count a reference to a Function.
25326 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025328func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025329{
25330 ufunc_T *fp;
25331
25332 if (name != NULL && isdigit(*name))
25333 {
25334 fp = find_func(name);
25335 if (fp == NULL)
25336 EMSG2(_(e_intern2), "func_ref()");
25337 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025338 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 }
25340}
25341
25342/*
25343 * Call a user function.
25344 */
25345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025346call_user_func(
25347 ufunc_T *fp, /* pointer to function */
25348 int argcount, /* nr of args */
25349 typval_T *argvars, /* arguments */
25350 typval_T *rettv, /* return value */
25351 linenr_T firstline, /* first line of range */
25352 linenr_T lastline, /* last line of range */
25353 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354{
Bram Moolenaar33570922005-01-25 22:26:29 +000025355 char_u *save_sourcing_name;
25356 linenr_T save_sourcing_lnum;
25357 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025358 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025359 int save_did_emsg;
25360 static int depth = 0;
25361 dictitem_T *v;
25362 int fixvar_idx = 0; /* index in fixvar[] */
25363 int i;
25364 int ai;
25365 char_u numbuf[NUMBUFLEN];
25366 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025367 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025368#ifdef FEAT_PROFILE
25369 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025370 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372
25373 /* If depth of calling is getting too high, don't execute the function */
25374 if (depth >= p_mfd)
25375 {
25376 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025377 rettv->v_type = VAR_NUMBER;
25378 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 return;
25380 }
25381 ++depth;
25382
25383 line_breakcheck(); /* check for CTRL-C hit */
25384
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025385 fc = (funccall_T *)alloc(sizeof(funccall_T));
25386 fc->caller = current_funccal;
25387 current_funccal = fc;
25388 fc->func = fp;
25389 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025390 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025391 fc->linenr = 0;
25392 fc->returned = FALSE;
25393 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025395 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25396 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397
Bram Moolenaar33570922005-01-25 22:26:29 +000025398 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025399 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025400 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25401 * each argument variable and saves a lot of time.
25402 */
25403 /*
25404 * Init l: variables.
25405 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025406 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025407 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025408 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025409 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25410 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025411 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025412 name = v->di_key;
25413 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025414 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025415 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025416 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025417 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025418 v->di_tv.vval.v_dict = selfdict;
25419 ++selfdict->dv_refcount;
25420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025421
Bram Moolenaar33570922005-01-25 22:26:29 +000025422 /*
25423 * Init a: variables.
25424 * Set a:0 to "argcount".
25425 * Set a:000 to a list with room for the "..." arguments.
25426 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025427 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025428 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025429 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025430 /* Use "name" to avoid a warning from some compiler that checks the
25431 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025432 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025433 name = v->di_key;
25434 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025435 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025436 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025437 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025438 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025439 v->di_tv.vval.v_list = &fc->l_varlist;
25440 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25441 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25442 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025443
25444 /*
25445 * Set a:firstline to "firstline" and a:lastline to "lastline".
25446 * Set a:name to named arguments.
25447 * Set a:N to the "..." arguments.
25448 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025449 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025450 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025451 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025452 (varnumber_T)lastline);
25453 for (i = 0; i < argcount; ++i)
25454 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025455 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 if (ai < 0)
25457 /* named argument a:name */
25458 name = FUNCARG(fp, i);
25459 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025460 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025461 /* "..." argument a:1, a:2, etc. */
25462 sprintf((char *)numbuf, "%d", ai + 1);
25463 name = numbuf;
25464 }
25465 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25466 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025467 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025468 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25469 }
25470 else
25471 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025472 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25473 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025474 if (v == NULL)
25475 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025476 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025477 }
25478 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025479 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025480
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025481 /* Note: the values are copied directly to avoid alloc/free.
25482 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025483 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025484 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025485
25486 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25487 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025488 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25489 fc->l_listitems[ai].li_tv = argvars[i];
25490 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025491 }
25492 }
25493
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 /* Don't redraw while executing the function. */
25495 ++RedrawingDisabled;
25496 save_sourcing_name = sourcing_name;
25497 save_sourcing_lnum = sourcing_lnum;
25498 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025499 /* need space for function name + ("function " + 3) or "[number]" */
25500 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25501 + STRLEN(fp->uf_name) + 20;
25502 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 if (sourcing_name != NULL)
25504 {
25505 if (save_sourcing_name != NULL
25506 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025507 sprintf((char *)sourcing_name, "%s[%d]..",
25508 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 else
25510 STRCPY(sourcing_name, "function ");
25511 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25512
25513 if (p_verbose >= 12)
25514 {
25515 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025516 verbose_enter_scroll();
25517
Bram Moolenaar555b2802005-05-19 21:08:39 +000025518 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519 if (p_verbose >= 14)
25520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025522 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025523 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025524 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525
25526 msg_puts((char_u *)"(");
25527 for (i = 0; i < argcount; ++i)
25528 {
25529 if (i > 0)
25530 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025531 if (argvars[i].v_type == VAR_NUMBER)
25532 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 else
25534 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025535 /* Do not want errors such as E724 here. */
25536 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025537 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025538 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025539 if (s != NULL)
25540 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025541 if (vim_strsize(s) > MSG_BUF_CLEN)
25542 {
25543 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25544 s = buf;
25545 }
25546 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025547 vim_free(tofree);
25548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 }
25550 }
25551 msg_puts((char_u *)")");
25552 }
25553 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025554
25555 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 --no_wait_return;
25557 }
25558 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025559#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025560 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025561 {
25562 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25563 func_do_profile(fp);
25564 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025565 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025566 {
25567 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025568 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025569 profile_zero(&fp->uf_tm_children);
25570 }
25571 script_prof_save(&wait_start);
25572 }
25573#endif
25574
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025576 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 save_did_emsg = did_emsg;
25578 did_emsg = FALSE;
25579
25580 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025581 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25583
25584 --RedrawingDisabled;
25585
25586 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025587 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025589 clear_tv(rettv);
25590 rettv->v_type = VAR_NUMBER;
25591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 }
25593
Bram Moolenaar05159a02005-02-26 23:04:13 +000025594#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025595 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025597 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025598 profile_end(&call_start);
25599 profile_sub_wait(&wait_start, &call_start);
25600 profile_add(&fp->uf_tm_total, &call_start);
25601 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025602 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025603 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025604 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25605 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025606 }
25607 }
25608#endif
25609
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610 /* when being verbose, mention the return value */
25611 if (p_verbose >= 12)
25612 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025613 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025614 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025617 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025618 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025619 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025620 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025622 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025623 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025624 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025625 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025626 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025627
Bram Moolenaar555b2802005-05-19 21:08:39 +000025628 /* The value may be very long. Skip the middle part, so that we
25629 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025630 * truncate it at the end. Don't want errors such as E724 here. */
25631 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025632 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025633 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025634 if (s != NULL)
25635 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025636 if (vim_strsize(s) > MSG_BUF_CLEN)
25637 {
25638 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25639 s = buf;
25640 }
25641 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025642 vim_free(tofree);
25643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644 }
25645 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025646
25647 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 --no_wait_return;
25649 }
25650
25651 vim_free(sourcing_name);
25652 sourcing_name = save_sourcing_name;
25653 sourcing_lnum = save_sourcing_lnum;
25654 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025655#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025656 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025657 script_prof_restore(&wait_start);
25658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025659
25660 if (p_verbose >= 12 && sourcing_name != NULL)
25661 {
25662 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025663 verbose_enter_scroll();
25664
Bram Moolenaar555b2802005-05-19 21:08:39 +000025665 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025667
25668 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 --no_wait_return;
25670 }
25671
25672 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025673 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025675
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025676 /* If the a:000 list and the l: and a: dicts are not referenced we can
25677 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025678 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25679 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25680 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25681 {
25682 free_funccal(fc, FALSE);
25683 }
25684 else
25685 {
25686 hashitem_T *hi;
25687 listitem_T *li;
25688 int todo;
25689
25690 /* "fc" is still in use. This can happen when returning "a:000" or
25691 * assigning "l:" to a global variable.
25692 * Link "fc" in the list for garbage collection later. */
25693 fc->caller = previous_funccal;
25694 previous_funccal = fc;
25695
25696 /* Make a copy of the a: variables, since we didn't do that above. */
25697 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25698 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25699 {
25700 if (!HASHITEM_EMPTY(hi))
25701 {
25702 --todo;
25703 v = HI2DI(hi);
25704 copy_tv(&v->di_tv, &v->di_tv);
25705 }
25706 }
25707
25708 /* Make a copy of the a:000 items, since we didn't do that above. */
25709 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25710 copy_tv(&li->li_tv, &li->li_tv);
25711 }
25712}
25713
25714/*
25715 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025716 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025717 */
25718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025719can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025720{
25721 return (fc->l_varlist.lv_copyID != copyID
25722 && fc->l_vars.dv_copyID != copyID
25723 && fc->l_avars.dv_copyID != copyID);
25724}
25725
25726/*
25727 * Free "fc" and what it contains.
25728 */
25729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025730free_funccal(
25731 funccall_T *fc,
25732 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025733{
25734 listitem_T *li;
25735
25736 /* The a: variables typevals may not have been allocated, only free the
25737 * allocated variables. */
25738 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25739
25740 /* free all l: variables */
25741 vars_clear(&fc->l_vars.dv_hashtab);
25742
25743 /* Free the a:000 variables if they were allocated. */
25744 if (free_val)
25745 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25746 clear_tv(&li->li_tv);
25747
25748 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749}
25750
25751/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025752 * Add a number variable "name" to dict "dp" with value "nr".
25753 */
25754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025755add_nr_var(
25756 dict_T *dp,
25757 dictitem_T *v,
25758 char *name,
25759 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025760{
25761 STRCPY(v->di_key, name);
25762 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25763 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25764 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025765 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025766 v->di_tv.vval.v_number = nr;
25767}
25768
25769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770 * ":return [expr]"
25771 */
25772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025773ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774{
25775 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025776 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 int returning = FALSE;
25778
25779 if (current_funccal == NULL)
25780 {
25781 EMSG(_("E133: :return not inside a function"));
25782 return;
25783 }
25784
25785 if (eap->skip)
25786 ++emsg_skip;
25787
25788 eap->nextcmd = NULL;
25789 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025790 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791 {
25792 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025793 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025795 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 }
25797 /* It's safer to return also on error. */
25798 else if (!eap->skip)
25799 {
25800 /*
25801 * Return unless the expression evaluation has been cancelled due to an
25802 * aborting error, an interrupt, or an exception.
25803 */
25804 if (!aborting())
25805 returning = do_return(eap, FALSE, TRUE, NULL);
25806 }
25807
25808 /* When skipping or the return gets pending, advance to the next command
25809 * in this line (!returning). Otherwise, ignore the rest of the line.
25810 * Following lines will be ignored by get_func_line(). */
25811 if (returning)
25812 eap->nextcmd = NULL;
25813 else if (eap->nextcmd == NULL) /* no argument */
25814 eap->nextcmd = check_nextcmd(arg);
25815
25816 if (eap->skip)
25817 --emsg_skip;
25818}
25819
25820/*
25821 * Return from a function. Possibly makes the return pending. Also called
25822 * for a pending return at the ":endtry" or after returning from an extra
25823 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025824 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025825 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 * FALSE when the return gets pending.
25827 */
25828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025829do_return(
25830 exarg_T *eap,
25831 int reanimate,
25832 int is_cmd,
25833 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834{
25835 int idx;
25836 struct condstack *cstack = eap->cstack;
25837
25838 if (reanimate)
25839 /* Undo the return. */
25840 current_funccal->returned = FALSE;
25841
25842 /*
25843 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25844 * not in its finally clause (which then is to be executed next) is found.
25845 * In this case, make the ":return" pending for execution at the ":endtry".
25846 * Otherwise, return normally.
25847 */
25848 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25849 if (idx >= 0)
25850 {
25851 cstack->cs_pending[idx] = CSTP_RETURN;
25852
25853 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025854 /* A pending return again gets pending. "rettv" points to an
25855 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025857 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 else
25859 {
25860 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025861 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025863 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025865 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 {
25867 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025868 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025869 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 else
25871 EMSG(_(e_outofmem));
25872 }
25873 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025874 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875
25876 if (reanimate)
25877 {
25878 /* The pending return value could be overwritten by a ":return"
25879 * without argument in a finally clause; reset the default
25880 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025881 current_funccal->rettv->v_type = VAR_NUMBER;
25882 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025883 }
25884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025885 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 }
25887 else
25888 {
25889 current_funccal->returned = TRUE;
25890
25891 /* If the return is carried out now, store the return value. For
25892 * a return immediately after reanimation, the value is already
25893 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025894 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025896 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025897 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025899 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 }
25901 }
25902
25903 return idx < 0;
25904}
25905
25906/*
25907 * Free the variable with a pending return value.
25908 */
25909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025910discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911{
Bram Moolenaar33570922005-01-25 22:26:29 +000025912 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913}
25914
25915/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025916 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917 * is an allocated string. Used by report_pending() for verbose messages.
25918 */
25919 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025920get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025922 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025923 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025924 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025926 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025927 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025928 if (s == NULL)
25929 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025930
25931 STRCPY(IObuff, ":return ");
25932 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25933 if (STRLEN(s) + 8 >= IOSIZE)
25934 STRCPY(IObuff + IOSIZE - 4, "...");
25935 vim_free(tofree);
25936 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937}
25938
25939/*
25940 * Get next function line.
25941 * Called by do_cmdline() to get the next line.
25942 * Returns allocated string, or NULL for end of function.
25943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025945get_func_line(
25946 int c UNUSED,
25947 void *cookie,
25948 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949{
Bram Moolenaar33570922005-01-25 22:26:29 +000025950 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025951 ufunc_T *fp = fcp->func;
25952 char_u *retval;
25953 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954
25955 /* If breakpoints have been added/deleted need to check for it. */
25956 if (fcp->dbg_tick != debug_tick)
25957 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025958 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 sourcing_lnum);
25960 fcp->dbg_tick = debug_tick;
25961 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025962#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025963 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025964 func_line_end(cookie);
25965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966
Bram Moolenaar05159a02005-02-26 23:04:13 +000025967 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025968 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25969 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970 retval = NULL;
25971 else
25972 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025973 /* Skip NULL lines (continuation lines). */
25974 while (fcp->linenr < gap->ga_len
25975 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25976 ++fcp->linenr;
25977 if (fcp->linenr >= gap->ga_len)
25978 retval = NULL;
25979 else
25980 {
25981 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25982 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025983#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025984 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025985 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025986#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988 }
25989
25990 /* Did we encounter a breakpoint? */
25991 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25992 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025993 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025995 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996 sourcing_lnum);
25997 fcp->dbg_tick = debug_tick;
25998 }
25999
26000 return retval;
26001}
26002
Bram Moolenaar05159a02005-02-26 23:04:13 +000026003#if defined(FEAT_PROFILE) || defined(PROTO)
26004/*
26005 * Called when starting to read a function line.
26006 * "sourcing_lnum" must be correct!
26007 * When skipping lines it may not actually be executed, but we won't find out
26008 * until later and we need to store the time now.
26009 */
26010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026011func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026012{
26013 funccall_T *fcp = (funccall_T *)cookie;
26014 ufunc_T *fp = fcp->func;
26015
26016 if (fp->uf_profiling && sourcing_lnum >= 1
26017 && sourcing_lnum <= fp->uf_lines.ga_len)
26018 {
26019 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026020 /* Skip continuation lines. */
26021 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26022 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026023 fp->uf_tml_execed = FALSE;
26024 profile_start(&fp->uf_tml_start);
26025 profile_zero(&fp->uf_tml_children);
26026 profile_get_wait(&fp->uf_tml_wait);
26027 }
26028}
26029
26030/*
26031 * Called when actually executing a function line.
26032 */
26033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026034func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026035{
26036 funccall_T *fcp = (funccall_T *)cookie;
26037 ufunc_T *fp = fcp->func;
26038
26039 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26040 fp->uf_tml_execed = TRUE;
26041}
26042
26043/*
26044 * Called when done with a function line.
26045 */
26046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026047func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026048{
26049 funccall_T *fcp = (funccall_T *)cookie;
26050 ufunc_T *fp = fcp->func;
26051
26052 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26053 {
26054 if (fp->uf_tml_execed)
26055 {
26056 ++fp->uf_tml_count[fp->uf_tml_idx];
26057 profile_end(&fp->uf_tml_start);
26058 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026059 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026060 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26061 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026062 }
26063 fp->uf_tml_idx = -1;
26064 }
26065}
26066#endif
26067
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068/*
26069 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026070 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071 */
26072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026073func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074{
Bram Moolenaar33570922005-01-25 22:26:29 +000026075 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076
26077 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26078 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026079 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080 || fcp->returned);
26081}
26082
26083/*
26084 * return TRUE if cookie indicates a function which "abort"s on errors.
26085 */
26086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026087func_has_abort(
26088 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026089{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026090 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091}
26092
26093#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26094typedef enum
26095{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026096 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26097 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26098 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099} var_flavour_T;
26100
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026101static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102
26103 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026104var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105{
26106 char_u *p = varname;
26107
26108 if (ASCII_ISUPPER(*p))
26109 {
26110 while (*(++p))
26111 if (ASCII_ISLOWER(*p))
26112 return VAR_FLAVOUR_SESSION;
26113 return VAR_FLAVOUR_VIMINFO;
26114 }
26115 else
26116 return VAR_FLAVOUR_DEFAULT;
26117}
26118#endif
26119
26120#if defined(FEAT_VIMINFO) || defined(PROTO)
26121/*
26122 * Restore global vars that start with a capital from the viminfo file
26123 */
26124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026125read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026126{
26127 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026128 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026129 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026130 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131
26132 if (!writing && (find_viminfo_parameter('!') != NULL))
26133 {
26134 tab = vim_strchr(virp->vir_line + 1, '\t');
26135 if (tab != NULL)
26136 {
26137 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026138 switch (*tab)
26139 {
26140 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026141#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026142 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026143#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026144 case 'D': type = VAR_DICT; break;
26145 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026146 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148
26149 tab = vim_strchr(tab, '\t');
26150 if (tab != NULL)
26151 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026152 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026153 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026154 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026156#ifdef FEAT_FLOAT
26157 else if (type == VAR_FLOAT)
26158 (void)string2float(tab + 1, &tv.vval.v_float);
26159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026161 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026162 if (type == VAR_DICT || type == VAR_LIST)
26163 {
26164 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26165
26166 if (etv == NULL)
26167 /* Failed to parse back the dict or list, use it as a
26168 * string. */
26169 tv.v_type = VAR_STRING;
26170 else
26171 {
26172 vim_free(tv.vval.v_string);
26173 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026174 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026175 }
26176 }
26177
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026178 /* when in a function use global variables */
26179 save_funccal = current_funccal;
26180 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026181 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026182 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026183
26184 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026185 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026186 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26187 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188 }
26189 }
26190 }
26191
26192 return viminfo_readline(virp);
26193}
26194
26195/*
26196 * Write global vars that start with a capital to the viminfo file
26197 */
26198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026199write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200{
Bram Moolenaar33570922005-01-25 22:26:29 +000026201 hashitem_T *hi;
26202 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026203 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026204 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026205 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026206 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026207 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208
26209 if (find_viminfo_parameter('!') == NULL)
26210 return;
26211
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026212 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026213
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026214 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026215 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026216 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026217 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026218 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026219 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026220 this_var = HI2DI(hi);
26221 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026222 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026223 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026224 {
26225 case VAR_STRING: s = "STR"; break;
26226 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026227 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026228 case VAR_DICT: s = "DIC"; break;
26229 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026230 case VAR_SPECIAL: s = "XPL"; break;
26231
26232 case VAR_UNKNOWN:
26233 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026234 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026235 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026236 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026237 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026238 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026239 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026240 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026241 if (p != NULL)
26242 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026243 vim_free(tofree);
26244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245 }
26246 }
26247}
26248#endif
26249
26250#if defined(FEAT_SESSION) || defined(PROTO)
26251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026252store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026253{
Bram Moolenaar33570922005-01-25 22:26:29 +000026254 hashitem_T *hi;
26255 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026256 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257 char_u *p, *t;
26258
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026259 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026260 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026262 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026263 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026264 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026265 this_var = HI2DI(hi);
26266 if ((this_var->di_tv.v_type == VAR_NUMBER
26267 || this_var->di_tv.v_type == VAR_STRING)
26268 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026269 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026270 /* Escape special characters with a backslash. Turn a LF and
26271 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026272 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026273 (char_u *)"\\\"\n\r");
26274 if (p == NULL) /* out of memory */
26275 break;
26276 for (t = p; *t != NUL; ++t)
26277 if (*t == '\n')
26278 *t = 'n';
26279 else if (*t == '\r')
26280 *t = 'r';
26281 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026282 this_var->di_key,
26283 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26284 : ' ',
26285 p,
26286 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26287 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026288 || put_eol(fd) == FAIL)
26289 {
26290 vim_free(p);
26291 return FAIL;
26292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 vim_free(p);
26294 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026295#ifdef FEAT_FLOAT
26296 else if (this_var->di_tv.v_type == VAR_FLOAT
26297 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26298 {
26299 float_T f = this_var->di_tv.vval.v_float;
26300 int sign = ' ';
26301
26302 if (f < 0)
26303 {
26304 f = -f;
26305 sign = '-';
26306 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026307 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026308 this_var->di_key, sign, f) < 0)
26309 || put_eol(fd) == FAIL)
26310 return FAIL;
26311 }
26312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026313 }
26314 }
26315 return OK;
26316}
26317#endif
26318
Bram Moolenaar661b1822005-07-28 22:36:45 +000026319/*
26320 * Display script name where an item was last set.
26321 * Should only be invoked when 'verbose' is non-zero.
26322 */
26323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026324last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026325{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026326 char_u *p;
26327
Bram Moolenaar661b1822005-07-28 22:36:45 +000026328 if (scriptID != 0)
26329 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026330 p = home_replace_save(NULL, get_scriptname(scriptID));
26331 if (p != NULL)
26332 {
26333 verbose_enter();
26334 MSG_PUTS(_("\n\tLast set from "));
26335 MSG_PUTS(p);
26336 vim_free(p);
26337 verbose_leave();
26338 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026339 }
26340}
26341
Bram Moolenaard812df62008-11-09 12:46:09 +000026342/*
26343 * List v:oldfiles in a nice way.
26344 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026346ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026347{
26348 list_T *l = vimvars[VV_OLDFILES].vv_list;
26349 listitem_T *li;
26350 int nr = 0;
26351
26352 if (l == NULL)
26353 msg((char_u *)_("No old files"));
26354 else
26355 {
26356 msg_start();
26357 msg_scroll = TRUE;
26358 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26359 {
26360 msg_outnum((long)++nr);
26361 MSG_PUTS(": ");
26362 msg_outtrans(get_tv_string(&li->li_tv));
26363 msg_putchar('\n');
26364 out_flush(); /* output one line at a time */
26365 ui_breakcheck();
26366 }
26367 /* Assume "got_int" was set to truncate the listing. */
26368 got_int = FALSE;
26369
26370#ifdef FEAT_BROWSE_CMD
26371 if (cmdmod.browse)
26372 {
26373 quit_more = FALSE;
26374 nr = prompt_for_number(FALSE);
26375 msg_starthere();
26376 if (nr > 0)
26377 {
26378 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26379 (long)nr);
26380
26381 if (p != NULL)
26382 {
26383 p = expand_env_save(p);
26384 eap->arg = p;
26385 eap->cmdidx = CMD_edit;
26386 cmdmod.browse = FALSE;
26387 do_exedit(eap, NULL);
26388 vim_free(p);
26389 }
26390 }
26391 }
26392#endif
26393 }
26394}
26395
Bram Moolenaar53744302015-07-17 17:38:22 +020026396/* reset v:option_new, v:option_old and v:option_type */
26397 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026398reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026399{
26400 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26401 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26402 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26403}
26404
26405
Bram Moolenaar071d4272004-06-13 20:20:40 +000026406#endif /* FEAT_EVAL */
26407
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026409#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410
26411#ifdef WIN3264
26412/*
26413 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26414 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026415static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26416static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26417static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418
26419/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026420 * Get the short path (8.3) for the filename in "fnamep".
26421 * Only works for a valid file name.
26422 * When the path gets longer "fnamep" is changed and the allocated buffer
26423 * is put in "bufp".
26424 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26425 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 */
26427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026428get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026430 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026431 char_u *newbuf;
26432
26433 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026434 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026435 if (l > len - 1)
26436 {
26437 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026438 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439 newbuf = vim_strnsave(*fnamep, l);
26440 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026441 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026442
26443 vim_free(*bufp);
26444 *fnamep = *bufp = newbuf;
26445
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026446 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026447 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448 }
26449
26450 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026451 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452}
26453
26454/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026455 * Get the short path (8.3) for the filename in "fname". The converted
26456 * path is returned in "bufp".
26457 *
26458 * Some of the directories specified in "fname" may not exist. This function
26459 * will shorten the existing directories at the beginning of the path and then
26460 * append the remaining non-existing path.
26461 *
26462 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026463 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026464 * bufp - Pointer to an allocated buffer for the filename.
26465 * fnamelen - Length of the filename pointed to by fname
26466 *
26467 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026468 */
26469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026470shortpath_for_invalid_fname(
26471 char_u **fname,
26472 char_u **bufp,
26473 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026474{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026475 char_u *short_fname, *save_fname, *pbuf_unused;
26476 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026478 int old_len, len;
26479 int new_len, sfx_len;
26480 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026481
26482 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026483 old_len = *fnamelen;
26484 save_fname = vim_strnsave(*fname, old_len);
26485 pbuf_unused = NULL;
26486 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026488 endp = save_fname + old_len - 1; /* Find the end of the copy */
26489 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026491 /*
26492 * Try shortening the supplied path till it succeeds by removing one
26493 * directory at a time from the tail of the path.
26494 */
26495 len = 0;
26496 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026497 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026498 /* go back one path-separator */
26499 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26500 --endp;
26501 if (endp <= save_fname)
26502 break; /* processed the complete path */
26503
26504 /*
26505 * Replace the path separator with a NUL and try to shorten the
26506 * resulting path.
26507 */
26508 ch = *endp;
26509 *endp = 0;
26510 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026511 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026512 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26513 {
26514 retval = FAIL;
26515 goto theend;
26516 }
26517 *endp = ch; /* preserve the string */
26518
26519 if (len > 0)
26520 break; /* successfully shortened the path */
26521
26522 /* failed to shorten the path. Skip the path separator */
26523 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524 }
26525
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026526 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026527 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026528 /*
26529 * Succeeded in shortening the path. Now concatenate the shortened
26530 * path with the remaining path at the tail.
26531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026533 /* Compute the length of the new path. */
26534 sfx_len = (int)(save_endp - endp) + 1;
26535 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026537 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026539 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026541 /* There is not enough space in the currently allocated string,
26542 * copy it to a buffer big enough. */
26543 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026545 {
26546 retval = FAIL;
26547 goto theend;
26548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026549 }
26550 else
26551 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026552 /* Transfer short_fname to the main buffer (it's big enough),
26553 * unless get_short_pathname() did its work in-place. */
26554 *fname = *bufp = save_fname;
26555 if (short_fname != save_fname)
26556 vim_strncpy(save_fname, short_fname, len);
26557 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026558 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026559
26560 /* concat the not-shortened part of the path */
26561 vim_strncpy(*fname + len, endp, sfx_len);
26562 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026564
26565theend:
26566 vim_free(pbuf_unused);
26567 vim_free(save_fname);
26568
26569 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026570}
26571
26572/*
26573 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026574 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575 */
26576 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026577shortpath_for_partial(
26578 char_u **fnamep,
26579 char_u **bufp,
26580 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026581{
26582 int sepcount, len, tflen;
26583 char_u *p;
26584 char_u *pbuf, *tfname;
26585 int hasTilde;
26586
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026587 /* Count up the path separators from the RHS.. so we know which part
26588 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026589 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026590 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026591 if (vim_ispathsep(*p))
26592 ++sepcount;
26593
26594 /* Need full path first (use expand_env() to remove a "~/") */
26595 hasTilde = (**fnamep == '~');
26596 if (hasTilde)
26597 pbuf = tfname = expand_env_save(*fnamep);
26598 else
26599 pbuf = tfname = FullName_save(*fnamep, FALSE);
26600
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026601 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026602
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026603 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026605
26606 if (len == 0)
26607 {
26608 /* Don't have a valid filename, so shorten the rest of the
26609 * path if we can. This CAN give us invalid 8.3 filenames, but
26610 * there's not a lot of point in guessing what it might be.
26611 */
26612 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026613 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26614 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026615 }
26616
26617 /* Count the paths backward to find the beginning of the desired string. */
26618 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026619 {
26620#ifdef FEAT_MBYTE
26621 if (has_mbyte)
26622 p -= mb_head_off(tfname, p);
26623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026624 if (vim_ispathsep(*p))
26625 {
26626 if (sepcount == 0 || (hasTilde && sepcount == 1))
26627 break;
26628 else
26629 sepcount --;
26630 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026632 if (hasTilde)
26633 {
26634 --p;
26635 if (p >= tfname)
26636 *p = '~';
26637 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026638 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026639 }
26640 else
26641 ++p;
26642
26643 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26644 vim_free(*bufp);
26645 *fnamelen = (int)STRLEN(p);
26646 *bufp = pbuf;
26647 *fnamep = p;
26648
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026649 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026650}
26651#endif /* WIN3264 */
26652
26653/*
26654 * Adjust a filename, according to a string of modifiers.
26655 * *fnamep must be NUL terminated when called. When returning, the length is
26656 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026657 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026658 * When there is an error, *fnamep is set to NULL.
26659 */
26660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026661modify_fname(
26662 char_u *src, /* string with modifiers */
26663 int *usedlen, /* characters after src that are used */
26664 char_u **fnamep, /* file name so far */
26665 char_u **bufp, /* buffer for allocated file name or NULL */
26666 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026667{
26668 int valid = 0;
26669 char_u *tail;
26670 char_u *s, *p, *pbuf;
26671 char_u dirname[MAXPATHL];
26672 int c;
26673 int has_fullname = 0;
26674#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026675 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026676 int has_shortname = 0;
26677#endif
26678
26679repeat:
26680 /* ":p" - full path/file_name */
26681 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26682 {
26683 has_fullname = 1;
26684
26685 valid |= VALID_PATH;
26686 *usedlen += 2;
26687
26688 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26689 if ((*fnamep)[0] == '~'
26690#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26691 && ((*fnamep)[1] == '/'
26692# ifdef BACKSLASH_IN_FILENAME
26693 || (*fnamep)[1] == '\\'
26694# endif
26695 || (*fnamep)[1] == NUL)
26696
26697#endif
26698 )
26699 {
26700 *fnamep = expand_env_save(*fnamep);
26701 vim_free(*bufp); /* free any allocated file name */
26702 *bufp = *fnamep;
26703 if (*fnamep == NULL)
26704 return -1;
26705 }
26706
26707 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026708 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026709 {
26710 if (vim_ispathsep(*p)
26711 && p[1] == '.'
26712 && (p[2] == NUL
26713 || vim_ispathsep(p[2])
26714 || (p[2] == '.'
26715 && (p[3] == NUL || vim_ispathsep(p[3])))))
26716 break;
26717 }
26718
26719 /* FullName_save() is slow, don't use it when not needed. */
26720 if (*p != NUL || !vim_isAbsName(*fnamep))
26721 {
26722 *fnamep = FullName_save(*fnamep, *p != NUL);
26723 vim_free(*bufp); /* free any allocated file name */
26724 *bufp = *fnamep;
26725 if (*fnamep == NULL)
26726 return -1;
26727 }
26728
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026729#ifdef WIN3264
26730# if _WIN32_WINNT >= 0x0500
26731 if (vim_strchr(*fnamep, '~') != NULL)
26732 {
26733 /* Expand 8.3 filename to full path. Needed to make sure the same
26734 * file does not have two different names.
26735 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26736 p = alloc(_MAX_PATH + 1);
26737 if (p != NULL)
26738 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026739 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026740 {
26741 vim_free(*bufp);
26742 *bufp = *fnamep = p;
26743 }
26744 else
26745 vim_free(p);
26746 }
26747 }
26748# endif
26749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026750 /* Append a path separator to a directory. */
26751 if (mch_isdir(*fnamep))
26752 {
26753 /* Make room for one or two extra characters. */
26754 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26755 vim_free(*bufp); /* free any allocated file name */
26756 *bufp = *fnamep;
26757 if (*fnamep == NULL)
26758 return -1;
26759 add_pathsep(*fnamep);
26760 }
26761 }
26762
26763 /* ":." - path relative to the current directory */
26764 /* ":~" - path relative to the home directory */
26765 /* ":8" - shortname path - postponed till after */
26766 while (src[*usedlen] == ':'
26767 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26768 {
26769 *usedlen += 2;
26770 if (c == '8')
26771 {
26772#ifdef WIN3264
26773 has_shortname = 1; /* Postpone this. */
26774#endif
26775 continue;
26776 }
26777 pbuf = NULL;
26778 /* Need full path first (use expand_env() to remove a "~/") */
26779 if (!has_fullname)
26780 {
26781 if (c == '.' && **fnamep == '~')
26782 p = pbuf = expand_env_save(*fnamep);
26783 else
26784 p = pbuf = FullName_save(*fnamep, FALSE);
26785 }
26786 else
26787 p = *fnamep;
26788
26789 has_fullname = 0;
26790
26791 if (p != NULL)
26792 {
26793 if (c == '.')
26794 {
26795 mch_dirname(dirname, MAXPATHL);
26796 s = shorten_fname(p, dirname);
26797 if (s != NULL)
26798 {
26799 *fnamep = s;
26800 if (pbuf != NULL)
26801 {
26802 vim_free(*bufp); /* free any allocated file name */
26803 *bufp = pbuf;
26804 pbuf = NULL;
26805 }
26806 }
26807 }
26808 else
26809 {
26810 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26811 /* Only replace it when it starts with '~' */
26812 if (*dirname == '~')
26813 {
26814 s = vim_strsave(dirname);
26815 if (s != NULL)
26816 {
26817 *fnamep = s;
26818 vim_free(*bufp);
26819 *bufp = s;
26820 }
26821 }
26822 }
26823 vim_free(pbuf);
26824 }
26825 }
26826
26827 tail = gettail(*fnamep);
26828 *fnamelen = (int)STRLEN(*fnamep);
26829
26830 /* ":h" - head, remove "/file_name", can be repeated */
26831 /* Don't remove the first "/" or "c:\" */
26832 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26833 {
26834 valid |= VALID_HEAD;
26835 *usedlen += 2;
26836 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026837 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026838 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026839 *fnamelen = (int)(tail - *fnamep);
26840#ifdef VMS
26841 if (*fnamelen > 0)
26842 *fnamelen += 1; /* the path separator is part of the path */
26843#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026844 if (*fnamelen == 0)
26845 {
26846 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26847 p = vim_strsave((char_u *)".");
26848 if (p == NULL)
26849 return -1;
26850 vim_free(*bufp);
26851 *bufp = *fnamep = tail = p;
26852 *fnamelen = 1;
26853 }
26854 else
26855 {
26856 while (tail > s && !after_pathsep(s, tail))
26857 mb_ptr_back(*fnamep, tail);
26858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026859 }
26860
26861 /* ":8" - shortname */
26862 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26863 {
26864 *usedlen += 2;
26865#ifdef WIN3264
26866 has_shortname = 1;
26867#endif
26868 }
26869
26870#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026871 /*
26872 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026873 */
26874 if (has_shortname)
26875 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026876 /* Copy the string if it is shortened by :h and when it wasn't copied
26877 * yet, because we are going to change it in place. Avoids changing
26878 * the buffer name for "%:8". */
26879 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026880 {
26881 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026882 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026883 return -1;
26884 vim_free(*bufp);
26885 *bufp = *fnamep = p;
26886 }
26887
26888 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026889 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026890 if (!has_fullname && !vim_isAbsName(*fnamep))
26891 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026892 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026893 return -1;
26894 }
26895 else
26896 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026897 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026898
Bram Moolenaardc935552011-08-17 15:23:23 +020026899 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026900 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026901 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026902 return -1;
26903
26904 if (l == 0)
26905 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026906 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026907 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026908 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026909 return -1;
26910 }
26911 *fnamelen = l;
26912 }
26913 }
26914#endif /* WIN3264 */
26915
26916 /* ":t" - tail, just the basename */
26917 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26918 {
26919 *usedlen += 2;
26920 *fnamelen -= (int)(tail - *fnamep);
26921 *fnamep = tail;
26922 }
26923
26924 /* ":e" - extension, can be repeated */
26925 /* ":r" - root, without extension, can be repeated */
26926 while (src[*usedlen] == ':'
26927 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26928 {
26929 /* find a '.' in the tail:
26930 * - for second :e: before the current fname
26931 * - otherwise: The last '.'
26932 */
26933 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26934 s = *fnamep - 2;
26935 else
26936 s = *fnamep + *fnamelen - 1;
26937 for ( ; s > tail; --s)
26938 if (s[0] == '.')
26939 break;
26940 if (src[*usedlen + 1] == 'e') /* :e */
26941 {
26942 if (s > tail)
26943 {
26944 *fnamelen += (int)(*fnamep - (s + 1));
26945 *fnamep = s + 1;
26946#ifdef VMS
26947 /* cut version from the extension */
26948 s = *fnamep + *fnamelen - 1;
26949 for ( ; s > *fnamep; --s)
26950 if (s[0] == ';')
26951 break;
26952 if (s > *fnamep)
26953 *fnamelen = s - *fnamep;
26954#endif
26955 }
26956 else if (*fnamep <= tail)
26957 *fnamelen = 0;
26958 }
26959 else /* :r */
26960 {
26961 if (s > tail) /* remove one extension */
26962 *fnamelen = (int)(s - *fnamep);
26963 }
26964 *usedlen += 2;
26965 }
26966
26967 /* ":s?pat?foo?" - substitute */
26968 /* ":gs?pat?foo?" - global substitute */
26969 if (src[*usedlen] == ':'
26970 && (src[*usedlen + 1] == 's'
26971 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26972 {
26973 char_u *str;
26974 char_u *pat;
26975 char_u *sub;
26976 int sep;
26977 char_u *flags;
26978 int didit = FALSE;
26979
26980 flags = (char_u *)"";
26981 s = src + *usedlen + 2;
26982 if (src[*usedlen + 1] == 'g')
26983 {
26984 flags = (char_u *)"g";
26985 ++s;
26986 }
26987
26988 sep = *s++;
26989 if (sep)
26990 {
26991 /* find end of pattern */
26992 p = vim_strchr(s, sep);
26993 if (p != NULL)
26994 {
26995 pat = vim_strnsave(s, (int)(p - s));
26996 if (pat != NULL)
26997 {
26998 s = p + 1;
26999 /* find end of substitution */
27000 p = vim_strchr(s, sep);
27001 if (p != NULL)
27002 {
27003 sub = vim_strnsave(s, (int)(p - s));
27004 str = vim_strnsave(*fnamep, *fnamelen);
27005 if (sub != NULL && str != NULL)
27006 {
27007 *usedlen = (int)(p + 1 - src);
27008 s = do_string_sub(str, pat, sub, flags);
27009 if (s != NULL)
27010 {
27011 *fnamep = s;
27012 *fnamelen = (int)STRLEN(s);
27013 vim_free(*bufp);
27014 *bufp = s;
27015 didit = TRUE;
27016 }
27017 }
27018 vim_free(sub);
27019 vim_free(str);
27020 }
27021 vim_free(pat);
27022 }
27023 }
27024 /* after using ":s", repeat all the modifiers */
27025 if (didit)
27026 goto repeat;
27027 }
27028 }
27029
Bram Moolenaar26df0922014-02-23 23:39:13 +010027030 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27031 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027032 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027033 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027034 if (c != NUL)
27035 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027036 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027037 if (c != NUL)
27038 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027039 if (p == NULL)
27040 return -1;
27041 vim_free(*bufp);
27042 *bufp = *fnamep = p;
27043 *fnamelen = (int)STRLEN(p);
27044 *usedlen += 2;
27045 }
27046
Bram Moolenaar071d4272004-06-13 20:20:40 +000027047 return valid;
27048}
27049
27050/*
27051 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27052 * "flags" can be "g" to do a global substitute.
27053 * Returns an allocated string, NULL for error.
27054 */
27055 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027056do_string_sub(
27057 char_u *str,
27058 char_u *pat,
27059 char_u *sub,
27060 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027061{
27062 int sublen;
27063 regmatch_T regmatch;
27064 int i;
27065 int do_all;
27066 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027067 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027068 garray_T ga;
27069 char_u *ret;
27070 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027071 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027072
27073 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27074 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027075 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027076
27077 ga_init2(&ga, 1, 200);
27078
27079 do_all = (flags[0] == 'g');
27080
27081 regmatch.rm_ic = p_ic;
27082 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27083 if (regmatch.regprog != NULL)
27084 {
27085 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027086 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027087 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27088 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027089 /* Skip empty match except for first match. */
27090 if (regmatch.startp[0] == regmatch.endp[0])
27091 {
27092 if (zero_width == regmatch.startp[0])
27093 {
27094 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027095 i = MB_PTR2LEN(tail);
27096 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27097 (size_t)i);
27098 ga.ga_len += i;
27099 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027100 continue;
27101 }
27102 zero_width = regmatch.startp[0];
27103 }
27104
Bram Moolenaar071d4272004-06-13 20:20:40 +000027105 /*
27106 * Get some space for a temporary buffer to do the substitution
27107 * into. It will contain:
27108 * - The text up to where the match is.
27109 * - The substituted text.
27110 * - The text after the match.
27111 */
27112 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027113 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027114 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27115 {
27116 ga_clear(&ga);
27117 break;
27118 }
27119
27120 /* copy the text up to where the match is */
27121 i = (int)(regmatch.startp[0] - tail);
27122 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27123 /* add the substituted text */
27124 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27125 + ga.ga_len + i, TRUE, TRUE, FALSE);
27126 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027127 tail = regmatch.endp[0];
27128 if (*tail == NUL)
27129 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027130 if (!do_all)
27131 break;
27132 }
27133
27134 if (ga.ga_data != NULL)
27135 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27136
Bram Moolenaar473de612013-06-08 18:19:48 +020027137 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027138 }
27139
27140 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27141 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027142 if (p_cpo == empty_option)
27143 p_cpo = save_cpo;
27144 else
27145 /* Darn, evaluating {sub} expression changed the value. */
27146 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027147
27148 return ret;
27149}
27150
27151#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */