blob: 99b948c7d80f438bd9c14f3f60943cf608aa6bbe [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
475static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200486static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200487static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
488static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_asin(typval_T *argvars, typval_T *rettv);
492static void f_atan(typval_T *argvars, typval_T *rettv);
493static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100495static void f_browse(typval_T *argvars, typval_T *rettv);
496static void f_browsedir(typval_T *argvars, typval_T *rettv);
497static void f_bufexists(typval_T *argvars, typval_T *rettv);
498static void f_buflisted(typval_T *argvars, typval_T *rettv);
499static void f_bufloaded(typval_T *argvars, typval_T *rettv);
500static void f_bufname(typval_T *argvars, typval_T *rettv);
501static void f_bufnr(typval_T *argvars, typval_T *rettv);
502static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
503static void f_byte2line(typval_T *argvars, typval_T *rettv);
504static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
505static void f_byteidx(typval_T *argvars, typval_T *rettv);
506static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
507static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100515static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100516static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100517static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
520static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100521static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100523static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
524static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100525static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100526static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_changenr(typval_T *argvars, typval_T *rettv);
529static void f_char2nr(typval_T *argvars, typval_T *rettv);
530static void f_cindent(typval_T *argvars, typval_T *rettv);
531static void f_clearmatches(typval_T *argvars, typval_T *rettv);
532static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000533#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_complete(typval_T *argvars, typval_T *rettv);
535static void f_complete_add(typval_T *argvars, typval_T *rettv);
536static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_confirm(typval_T *argvars, typval_T *rettv);
539static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_cos(typval_T *argvars, typval_T *rettv);
542static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_count(typval_T *argvars, typval_T *rettv);
545static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
546static void f_cursor(typval_T *argsvars, typval_T *rettv);
547static void f_deepcopy(typval_T *argvars, typval_T *rettv);
548static void f_delete(typval_T *argvars, typval_T *rettv);
549static void f_did_filetype(typval_T *argvars, typval_T *rettv);
550static void f_diff_filler(typval_T *argvars, typval_T *rettv);
551static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100552static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_empty(typval_T *argvars, typval_T *rettv);
554static void f_escape(typval_T *argvars, typval_T *rettv);
555static void f_eval(typval_T *argvars, typval_T *rettv);
556static void f_eventhandler(typval_T *argvars, typval_T *rettv);
557static void f_executable(typval_T *argvars, typval_T *rettv);
558static void f_exepath(typval_T *argvars, typval_T *rettv);
559static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_expand(typval_T *argvars, typval_T *rettv);
564static void f_extend(typval_T *argvars, typval_T *rettv);
565static void f_feedkeys(typval_T *argvars, typval_T *rettv);
566static void f_filereadable(typval_T *argvars, typval_T *rettv);
567static void f_filewritable(typval_T *argvars, typval_T *rettv);
568static void f_filter(typval_T *argvars, typval_T *rettv);
569static void f_finddir(typval_T *argvars, typval_T *rettv);
570static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000571#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100572static void f_float2nr(typval_T *argvars, typval_T *rettv);
573static void f_floor(typval_T *argvars, typval_T *rettv);
574static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000575#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100576static void f_fnameescape(typval_T *argvars, typval_T *rettv);
577static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
578static void f_foldclosed(typval_T *argvars, typval_T *rettv);
579static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
580static void f_foldlevel(typval_T *argvars, typval_T *rettv);
581static void f_foldtext(typval_T *argvars, typval_T *rettv);
582static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
583static void f_foreground(typval_T *argvars, typval_T *rettv);
584static void f_function(typval_T *argvars, typval_T *rettv);
585static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
586static void f_get(typval_T *argvars, typval_T *rettv);
587static void f_getbufline(typval_T *argvars, typval_T *rettv);
588static void f_getbufvar(typval_T *argvars, typval_T *rettv);
589static void f_getchar(typval_T *argvars, typval_T *rettv);
590static void f_getcharmod(typval_T *argvars, typval_T *rettv);
591static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
592static void f_getcmdline(typval_T *argvars, typval_T *rettv);
593static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
594static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
595static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
596static void f_getcwd(typval_T *argvars, typval_T *rettv);
597static void f_getfontname(typval_T *argvars, typval_T *rettv);
598static void f_getfperm(typval_T *argvars, typval_T *rettv);
599static void f_getfsize(typval_T *argvars, typval_T *rettv);
600static void f_getftime(typval_T *argvars, typval_T *rettv);
601static void f_getftype(typval_T *argvars, typval_T *rettv);
602static void f_getline(typval_T *argvars, typval_T *rettv);
603static void f_getmatches(typval_T *argvars, typval_T *rettv);
604static void f_getpid(typval_T *argvars, typval_T *rettv);
605static void f_getcurpos(typval_T *argvars, typval_T *rettv);
606static void f_getpos(typval_T *argvars, typval_T *rettv);
607static void f_getqflist(typval_T *argvars, typval_T *rettv);
608static void f_getreg(typval_T *argvars, typval_T *rettv);
609static void f_getregtype(typval_T *argvars, typval_T *rettv);
610static void f_gettabvar(typval_T *argvars, typval_T *rettv);
611static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
612static void f_getwinposx(typval_T *argvars, typval_T *rettv);
613static void f_getwinposy(typval_T *argvars, typval_T *rettv);
614static void f_getwinvar(typval_T *argvars, typval_T *rettv);
615static void f_glob(typval_T *argvars, typval_T *rettv);
616static void f_globpath(typval_T *argvars, typval_T *rettv);
617static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
618static void f_has(typval_T *argvars, typval_T *rettv);
619static void f_has_key(typval_T *argvars, typval_T *rettv);
620static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
621static void f_hasmapto(typval_T *argvars, typval_T *rettv);
622static void f_histadd(typval_T *argvars, typval_T *rettv);
623static void f_histdel(typval_T *argvars, typval_T *rettv);
624static void f_histget(typval_T *argvars, typval_T *rettv);
625static void f_histnr(typval_T *argvars, typval_T *rettv);
626static void f_hlID(typval_T *argvars, typval_T *rettv);
627static void f_hlexists(typval_T *argvars, typval_T *rettv);
628static void f_hostname(typval_T *argvars, typval_T *rettv);
629static void f_iconv(typval_T *argvars, typval_T *rettv);
630static void f_indent(typval_T *argvars, typval_T *rettv);
631static void f_index(typval_T *argvars, typval_T *rettv);
632static void f_input(typval_T *argvars, typval_T *rettv);
633static void f_inputdialog(typval_T *argvars, typval_T *rettv);
634static void f_inputlist(typval_T *argvars, typval_T *rettv);
635static void f_inputrestore(typval_T *argvars, typval_T *rettv);
636static void f_inputsave(typval_T *argvars, typval_T *rettv);
637static void f_inputsecret(typval_T *argvars, typval_T *rettv);
638static void f_insert(typval_T *argvars, typval_T *rettv);
639static void f_invert(typval_T *argvars, typval_T *rettv);
640static void f_isdirectory(typval_T *argvars, typval_T *rettv);
641static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100642#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
643static void f_isnan(typval_T *argvars, typval_T *rettv);
644#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100647static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100648static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100649static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100650static void f_job_start(typval_T *argvars, typval_T *rettv);
651static void f_job_stop(typval_T *argvars, typval_T *rettv);
652static void f_job_status(typval_T *argvars, typval_T *rettv);
653#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100655static void f_js_decode(typval_T *argvars, typval_T *rettv);
656static void f_js_encode(typval_T *argvars, typval_T *rettv);
657static void f_json_decode(typval_T *argvars, typval_T *rettv);
658static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_keys(typval_T *argvars, typval_T *rettv);
660static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
661static void f_len(typval_T *argvars, typval_T *rettv);
662static void f_libcall(typval_T *argvars, typval_T *rettv);
663static void f_libcallnr(typval_T *argvars, typval_T *rettv);
664static void f_line(typval_T *argvars, typval_T *rettv);
665static void f_line2byte(typval_T *argvars, typval_T *rettv);
666static void f_lispindent(typval_T *argvars, typval_T *rettv);
667static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_log(typval_T *argvars, typval_T *rettv);
670static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200674#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_map(typval_T *argvars, typval_T *rettv);
676static void f_maparg(typval_T *argvars, typval_T *rettv);
677static void f_mapcheck(typval_T *argvars, typval_T *rettv);
678static void f_match(typval_T *argvars, typval_T *rettv);
679static void f_matchadd(typval_T *argvars, typval_T *rettv);
680static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
681static void f_matcharg(typval_T *argvars, typval_T *rettv);
682static void f_matchdelete(typval_T *argvars, typval_T *rettv);
683static void f_matchend(typval_T *argvars, typval_T *rettv);
684static void f_matchlist(typval_T *argvars, typval_T *rettv);
685static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200686static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_max(typval_T *argvars, typval_T *rettv);
688static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100693#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
697static void f_nr2char(typval_T *argvars, typval_T *rettv);
698static void f_or(typval_T *argvars, typval_T *rettv);
699static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100700#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100702#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
707static void f_printf(typval_T *argvars, typval_T *rettv);
708static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#endif
712#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200714#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_range(typval_T *argvars, typval_T *rettv);
716static void f_readfile(typval_T *argvars, typval_T *rettv);
717static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100718#ifdef FEAT_FLOAT
719static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_reltimestr(typval_T *argvars, typval_T *rettv);
722static void f_remote_expr(typval_T *argvars, typval_T *rettv);
723static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
724static void f_remote_peek(typval_T *argvars, typval_T *rettv);
725static void f_remote_read(typval_T *argvars, typval_T *rettv);
726static void f_remote_send(typval_T *argvars, typval_T *rettv);
727static void f_remove(typval_T *argvars, typval_T *rettv);
728static void f_rename(typval_T *argvars, typval_T *rettv);
729static void f_repeat(typval_T *argvars, typval_T *rettv);
730static void f_resolve(typval_T *argvars, typval_T *rettv);
731static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_screenattr(typval_T *argvars, typval_T *rettv);
736static void f_screenchar(typval_T *argvars, typval_T *rettv);
737static void f_screencol(typval_T *argvars, typval_T *rettv);
738static void f_screenrow(typval_T *argvars, typval_T *rettv);
739static void f_search(typval_T *argvars, typval_T *rettv);
740static void f_searchdecl(typval_T *argvars, typval_T *rettv);
741static void f_searchpair(typval_T *argvars, typval_T *rettv);
742static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
743static void f_searchpos(typval_T *argvars, typval_T *rettv);
744static void f_server2client(typval_T *argvars, typval_T *rettv);
745static void f_serverlist(typval_T *argvars, typval_T *rettv);
746static void f_setbufvar(typval_T *argvars, typval_T *rettv);
747static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
748static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100749static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_setline(typval_T *argvars, typval_T *rettv);
751static void f_setloclist(typval_T *argvars, typval_T *rettv);
752static void f_setmatches(typval_T *argvars, typval_T *rettv);
753static void f_setpos(typval_T *argvars, typval_T *rettv);
754static void f_setqflist(typval_T *argvars, typval_T *rettv);
755static void f_setreg(typval_T *argvars, typval_T *rettv);
756static void f_settabvar(typval_T *argvars, typval_T *rettv);
757static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
758static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100759#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100761#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_shellescape(typval_T *argvars, typval_T *rettv);
763static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
764static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sin(typval_T *argvars, typval_T *rettv);
767static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sort(typval_T *argvars, typval_T *rettv);
770static void f_soundfold(typval_T *argvars, typval_T *rettv);
771static void f_spellbadword(typval_T *argvars, typval_T *rettv);
772static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
773static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sqrt(typval_T *argvars, typval_T *rettv);
776static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_str2nr(typval_T *argvars, typval_T *rettv);
779static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000782#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200783static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_stridx(typval_T *argvars, typval_T *rettv);
785static void f_string(typval_T *argvars, typval_T *rettv);
786static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200787static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100788static void f_strpart(typval_T *argvars, typval_T *rettv);
789static void f_strridx(typval_T *argvars, typval_T *rettv);
790static void f_strtrans(typval_T *argvars, typval_T *rettv);
791static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
792static void f_strwidth(typval_T *argvars, typval_T *rettv);
793static void f_submatch(typval_T *argvars, typval_T *rettv);
794static void f_substitute(typval_T *argvars, typval_T *rettv);
795static void f_synID(typval_T *argvars, typval_T *rettv);
796static void f_synIDattr(typval_T *argvars, typval_T *rettv);
797static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
798static void f_synstack(typval_T *argvars, typval_T *rettv);
799static void f_synconcealed(typval_T *argvars, typval_T *rettv);
800static void f_system(typval_T *argvars, typval_T *rettv);
801static void f_systemlist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
804static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
805static void f_taglist(typval_T *argvars, typval_T *rettv);
806static void f_tagfiles(typval_T *argvars, typval_T *rettv);
807static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200808static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
809#ifdef FEAT_JOB_CHANNEL
810static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
811#endif
812static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_job(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_list(typval_T *argvars, typval_T *rettv);
817static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
818static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200819#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_tan(typval_T *argvars, typval_T *rettv);
821static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200822#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100823#ifdef FEAT_TIMERS
824static void f_timer_start(typval_T *argvars, typval_T *rettv);
825static void f_timer_stop(typval_T *argvars, typval_T *rettv);
826#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static void f_tolower(typval_T *argvars, typval_T *rettv);
828static void f_toupper(typval_T *argvars, typval_T *rettv);
829static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000830#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_type(typval_T *argvars, typval_T *rettv);
834static void f_undofile(typval_T *argvars, typval_T *rettv);
835static void f_undotree(typval_T *argvars, typval_T *rettv);
836static void f_uniq(typval_T *argvars, typval_T *rettv);
837static void f_values(typval_T *argvars, typval_T *rettv);
838static void f_virtcol(typval_T *argvars, typval_T *rettv);
839static void f_visualmode(typval_T *argvars, typval_T *rettv);
840static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100841static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100842static void f_win_getid(typval_T *argvars, typval_T *rettv);
843static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
844static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
845static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static void f_winbufnr(typval_T *argvars, typval_T *rettv);
847static void f_wincol(typval_T *argvars, typval_T *rettv);
848static void f_winheight(typval_T *argvars, typval_T *rettv);
849static void f_winline(typval_T *argvars, typval_T *rettv);
850static void f_winnr(typval_T *argvars, typval_T *rettv);
851static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
852static void f_winrestview(typval_T *argvars, typval_T *rettv);
853static void f_winsaveview(typval_T *argvars, typval_T *rettv);
854static void f_winwidth(typval_T *argvars, typval_T *rettv);
855static void f_writefile(typval_T *argvars, typval_T *rettv);
856static void f_wordcount(typval_T *argvars, typval_T *rettv);
857static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
860static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
861static int get_env_len(char_u **arg);
862static int get_id_len(char_u **arg);
863static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
864static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000865#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
866#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
867 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
869static int eval_isnamec(int c);
870static int eval_isnamec1(int c);
871static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
872static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static typval_T *alloc_string_tv(char_u *string);
874static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100875#ifdef FEAT_FLOAT
876static float_T get_tv_float(typval_T *varp);
877#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static linenr_T get_tv_lnum(typval_T *argvars);
879static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
881static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
882static hashtab_T *find_var_ht(char_u *name, char_u **varname);
883static funccall_T *get_funccal(void);
884static void vars_clear_ext(hashtab_T *ht, int free_val);
885static void delete_var(hashtab_T *ht, hashitem_T *hi);
886static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
887static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
888static void set_var(char_u *name, typval_T *varp, int copy);
889static int var_check_ro(int flags, char_u *name, int use_gettext);
890static int var_check_fixed(int flags, char_u *name, int use_gettext);
891static int var_check_func_name(char_u *name, int new_var);
892static int valid_varname(char_u *varname);
893static int tv_check_lock(int lock, char_u *name, int use_gettext);
894static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
895static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100896static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100897static int eval_fname_script(char_u *p);
898static int eval_fname_sid(char_u *p);
899static void list_func_head(ufunc_T *fp, int indent);
900static ufunc_T *find_func(char_u *name);
901static int function_exists(char_u *name);
902static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static void func_do_profile(ufunc_T *fp);
905static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
906static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000907static int
908# ifdef __BORLANDC__
909 _RTLENTRYF
910# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100918static int script_autoload(char_u *name, int reload);
919static char_u *autoload_name(char_u *name);
920static void cat_func_name(char_u *buf, ufunc_T *fp);
921static void func_free(ufunc_T *fp);
922static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
923static int can_free_funccal(funccall_T *fc, int copyID) ;
924static void free_funccal(funccall_T *fc, int free_val);
925static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
926static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
927static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
928static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
929static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
930static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
931static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
932static int write_list(FILE *fd, list_T *list, int binary);
933static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200935
936#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100937static int compare_func_name(const void *s1, const void *s2);
938static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939#endif
940
Bram Moolenaar33570922005-01-25 22:26:29 +0000941/*
942 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000943 */
944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 int i;
948 struct vimvar *p;
949
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
951 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200952 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000953 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955
956 for (i = 0; i < VV_LEN; ++i)
957 {
958 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200959 if (STRLEN(p->vv_name) > 16)
960 {
961 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
962 getout(1);
963 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 STRCPY(p->vv_di.di_key, p->vv_name);
965 if (p->vv_flags & VV_RO)
966 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
967 else if (p->vv_flags & VV_RO_SBX)
968 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
969 else
970 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000971
972 /* add to v: scope dict, unless the value is not always available */
973 if (p->vv_type != VAR_UNKNOWN)
974 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000976 /* add to compat scope dict */
977 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100979 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
980
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000981 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100982 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200983 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100984 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100985
986 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
987 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
988 set_vim_var_nr(VV_NONE, VVAL_NONE);
989 set_vim_var_nr(VV_NULL, VVAL_NULL);
990
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200991 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200992
993#ifdef EBCDIC
994 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100995 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200996 */
997 sortFunctions();
998#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000999}
1000
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001#if defined(EXITFREE) || defined(PROTO)
1002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001003eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004{
1005 int i;
1006 struct vimvar *p;
1007
1008 for (i = 0; i < VV_LEN; ++i)
1009 {
1010 p = &vimvars[i];
1011 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001012 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001013 vim_free(p->vv_str);
1014 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001015 }
1016 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1017 {
1018 list_unref(p->vv_list);
1019 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001020 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021 }
1022 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001023 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024 hash_clear(&compat_hashtab);
1025
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001027# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001028 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030
1031 /* global variables */
1032 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001033
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001034 /* autoloaded script names */
1035 ga_clear_strings(&ga_loaded);
1036
Bram Moolenaarcca74132013-09-25 21:00:28 +02001037 /* Script-local variables. First clear all the variables and in a second
1038 * loop free the scriptvar_T, because a variable in one script might hold
1039 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001040 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 ga_clear(&ga_scripts);
1045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001046 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001047 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001048
1049 /* functions */
1050 free_all_functions();
1051 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001052}
1053#endif
1054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 * Return the name of the executed function.
1057 */
1058 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the next breakpoint line for a funccall cookie.
1066 */
1067 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the address holding the debug tick for a funccall cookie.
1075 */
1076 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Return the nesting level for a funccall cookie.
1084 */
1085 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001092funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001094/* pointer to list of previously used funccal, still around because some
1095 * item in it is still being used. */
1096funccall_T *previous_funccal = NULL;
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
1099 * Return TRUE when a function was ended by a ":return" command.
1100 */
1101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 return current_funccal->returned;
1105}
1106
1107
1108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * Set an internal variable to a string value. Creates the variable if it does
1110 * not already exist.
1111 */
1112 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001113set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 val = vim_strsave(value);
1119 if (val != NULL)
1120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001122 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 }
1128}
1129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132static char_u *redir_endp = NULL;
1133static char_u *redir_varname = NULL;
1134
1135/*
1136 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001137 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 * Returns OK if successfully completed the setup. FAIL otherwise.
1139 */
1140 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001141var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
1143 int save_emsg;
1144 int err;
1145 typval_T tv;
1146
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001148 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 {
1150 EMSG(_(e_invarg));
1151 return FAIL;
1152 }
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 redir_varname = vim_strsave(name);
1156 if (redir_varname == NULL)
1157 return FAIL;
1158
1159 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1160 if (redir_lval == NULL)
1161 {
1162 var_redir_stop();
1163 return FAIL;
1164 }
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 /* The output is stored in growarray "redir_ga" until redirection ends. */
1167 ga_init2(&redir_ga, (int)sizeof(char), 500);
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001170 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001171 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1173 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001174 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 if (redir_endp != NULL && *redir_endp != NUL)
1176 /* Trailing characters are present after the variable name */
1177 EMSG(_(e_trailing));
1178 else
1179 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
1184
1185 /* check if we can write to the variable: set it to or append an empty
1186 * string */
1187 save_emsg = did_emsg;
1188 did_emsg = FALSE;
1189 tv.v_type = VAR_STRING;
1190 tv.vval.v_string = (char_u *)"";
1191 if (append)
1192 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1193 else
1194 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001195 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001197 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 if (err)
1199 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001200 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
1202 return FAIL;
1203 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 * Append "value[value_len]" to the variable set by var_redir_start().
1210 * The actual appending is postponed until redirection ends, because the value
1211 * appended may in fact be the string we write to, changing it may cause freed
1212 * memory to be used:
1213 * :redir => foo
1214 * :let foo
1215 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 */
1217 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001218var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001220 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221
1222 if (redir_lval == NULL)
1223 return;
1224
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001226 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001228 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231 {
1232 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 }
1235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237}
1238
1239/*
1240 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 */
1243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001244var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001246 typval_T tv;
1247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248 if (redir_lval != NULL)
1249 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 /* If there was no error: assign the text to the variable. */
1251 if (redir_endp != NULL)
1252 {
1253 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1254 tv.v_type = VAR_STRING;
1255 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001256 /* Call get_lval() again, if it's inside a Dict or List it may
1257 * have changed. */
1258 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001259 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001260 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1261 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1262 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001264
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001265 /* free the collected output */
1266 vim_free(redir_ga.ga_data);
1267 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001269 vim_free(redir_lval);
1270 redir_lval = NULL;
1271 }
1272 vim_free(redir_varname);
1273 redir_varname = NULL;
1274}
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# if defined(FEAT_MBYTE) || defined(PROTO)
1277 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001278eval_charconvert(
1279 char_u *enc_from,
1280 char_u *enc_to,
1281 char_u *fname_from,
1282 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int err = FALSE;
1285
1286 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1287 set_vim_var_string(VV_CC_TO, enc_to, -1);
1288 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1289 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1290 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1291 err = TRUE;
1292 set_vim_var_string(VV_CC_FROM, NULL, -1);
1293 set_vim_var_string(VV_CC_TO, NULL, -1);
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296
1297 if (err)
1298 return FAIL;
1299 return OK;
1300}
1301# endif
1302
1303# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err = FALSE;
1308
1309 set_vim_var_string(VV_FNAME_IN, fname, -1);
1310 set_vim_var_string(VV_CMDARG, args, -1);
1311 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1312 err = TRUE;
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_CMDARG, NULL, -1);
1315
1316 if (err)
1317 {
1318 mch_remove(fname);
1319 return FAIL;
1320 }
1321 return OK;
1322}
1323# endif
1324
1325# if defined(FEAT_DIFF) || defined(PROTO)
1326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_diff(
1328 char_u *origfile,
1329 char_u *newfile,
1330 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int err = FALSE;
1333
1334 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1335 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1336 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1337 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1338 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1339 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1340 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1341}
1342
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_patch(
1345 char_u *origfile,
1346 char_u *difffile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359# endif
1360
1361/*
1362 * Top level evaluation function, returning a boolean.
1363 * Sets "error" to TRUE if there was an error.
1364 * Return TRUE or FALSE.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_bool(
1368 char_u *arg,
1369 int *error,
1370 char_u **nextcmd,
1371 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 int retval = FALSE;
1375
1376 if (skip)
1377 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
1381 {
1382 *error = FALSE;
1383 if (!skip)
1384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001385 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
1396 * Top level evaluation function, returning a string. If "skip" is TRUE,
1397 * only parsing to "nextcmd" is done, without reporting errors. Return
1398 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1399 */
1400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401eval_to_string_skip(
1402 char_u *arg,
1403 char_u **nextcmd,
1404 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 char_u *retval;
1408
1409 if (skip)
1410 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = NULL;
1413 else
1414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 retval = vim_strsave(get_tv_string(&tv));
1416 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 if (skip)
1419 --emsg_skip;
1420
1421 return retval;
1422}
1423
1424/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001425 * Skip over an expression at "*pp".
1426 * Return FAIL for an error, OK otherwise.
1427 */
1428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001429skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430{
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001432
1433 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435}
1436
1437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001439 * When "convert" is TRUE convert a List into a sequence of lines and convert
1440 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * Return pointer to allocated memory, or NULL for failure.
1442 */
1443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444eval_to_string(
1445 char_u *arg,
1446 char_u **nextcmd,
1447 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001451 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001452#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = NULL;
1458 else
1459 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001460 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001461 {
1462 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001463 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001464 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001466 if (tv.vval.v_list->lv_len > 0)
1467 ga_append(&ga, NL);
1468 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 ga_append(&ga, NUL);
1470 retval = (char_u *)ga.ga_data;
1471 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001472#ifdef FEAT_FLOAT
1473 else if (convert && tv.v_type == VAR_FLOAT)
1474 {
1475 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1476 retval = vim_strsave(numbuf);
1477 }
1478#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001479 else
1480 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483
1484 return retval;
1485}
1486
1487/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 * Call eval_to_string() without using current local variables and using
1489 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
1491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492eval_to_string_safe(
1493 char_u *arg,
1494 char_u **nextcmd,
1495 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 char_u *retval;
1498 void *save_funccalp;
1499
1500 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 if (use_sandbox)
1502 ++sandbox;
1503 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001504 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 if (use_sandbox)
1506 --sandbox;
1507 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 restore_funccal(save_funccalp);
1509 return retval;
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
1513 * Top level evaluation function, returning a number.
1514 * Evaluates "expr" silently.
1515 * Returns -1 for an error.
1516 */
1517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001522 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 ++emsg_off;
1525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 retval = -1;
1528 else
1529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001530 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 --emsg_off;
1534
1535 return retval;
1536}
1537
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538/*
1539 * Prepare v: variable "idx" to be used.
1540 * Save the current typeval in "save_tv".
1541 * When not used yet add the variable to the v: hashtable.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545{
1546 *save_tv = vimvars[idx].vv_tv;
1547 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1548 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1549}
1550
1551/*
1552 * Restore v: variable "idx" to typeval "save_tv".
1553 * When no longer defined, remove the variable from the v: hashtable.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001557{
1558 hashitem_T *hi;
1559
Bram Moolenaara40058a2005-07-11 22:42:07 +00001560 vimvars[idx].vv_tv = *save_tv;
1561 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1562 {
1563 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1564 if (HASHITEM_EMPTY(hi))
1565 EMSG2(_(e_intern2), "restore_vimvar()");
1566 else
1567 hash_remove(&vimvarht, hi);
1568 }
1569}
1570
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001571#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001572/*
1573 * Evaluate an expression to a list with suggestions.
1574 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001575 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576 */
1577 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 typval_T save_val;
1581 typval_T rettv;
1582 list_T *list = NULL;
1583 char_u *p = skipwhite(expr);
1584
1585 /* Set "v:val" to the bad word. */
1586 prepare_vimvar(VV_VAL, &save_val);
1587 vimvars[VV_VAL].vv_type = VAR_STRING;
1588 vimvars[VV_VAL].vv_str = badword;
1589 if (p_verbose == 0)
1590 ++emsg_off;
1591
1592 if (eval1(&p, &rettv, TRUE) == OK)
1593 {
1594 if (rettv.v_type != VAR_LIST)
1595 clear_tv(&rettv);
1596 else
1597 list = rettv.vval.v_list;
1598 }
1599
1600 if (p_verbose == 0)
1601 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001602 restore_vimvar(VV_VAL, &save_val);
1603
1604 return list;
1605}
1606
1607/*
1608 * "list" is supposed to contain two items: a word and a number. Return the
1609 * word in "pp" and the number as the return value.
1610 * Return -1 if anything isn't right.
1611 * Used to get the good word and score from the eval_spell_expr() result.
1612 */
1613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615{
1616 listitem_T *li;
1617
1618 li = list->lv_first;
1619 if (li == NULL)
1620 return -1;
1621 *pp = get_tv_string(&li->li_tv);
1622
1623 li = li->li_next;
1624 if (li == NULL)
1625 return -1;
1626 return get_tv_number(&li->li_tv);
1627}
1628#endif
1629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001631 * Top level evaluation function.
1632 * Returns an allocated typval_T with the result.
1633 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634 */
1635 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001637{
1638 typval_T *tv;
1639
1640 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001641 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642 {
1643 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 }
1646
1647 return tv;
1648}
1649
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653 * Uses argv[argc] for the function arguments. Only Number and String
1654 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658call_vim_function(
1659 char_u *func,
1660 int argc,
1661 char_u **argv,
1662 int safe, /* use the sandbox */
1663 int str_arg_only, /* all arguments are strings */
1664 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 long n;
1668 int len;
1669 int i;
1670 int doesrange;
1671 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001674 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 for (i = 0; i < argc; i++)
1679 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001680 /* Pass a NULL or empty argument as an empty string */
1681 if (argv[i] == NULL || *argv[i] == NUL)
1682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001683 argvars[i].v_type = VAR_STRING;
1684 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 continue;
1686 }
1687
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001688 if (str_arg_only)
1689 len = 0;
1690 else
1691 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001692 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (len != 0 && len == (int)STRLEN(argv[i]))
1694 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001695 argvars[i].v_type = VAR_NUMBER;
1696 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 else
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 }
1704
1705 if (safe)
1706 {
1707 save_funccalp = save_funccal();
1708 ++sandbox;
1709 }
1710
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1712 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001714 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (safe)
1716 {
1717 --sandbox;
1718 restore_funccal(save_funccalp);
1719 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 vim_free(argvars);
1721
1722 if (ret == FAIL)
1723 clear_tv(rettv);
1724
1725 return ret;
1726}
1727
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001728/*
1729 * Call vimL function "func" and return the result as a number.
1730 * Returns -1 when calling the function fails.
1731 * Uses argv[argc] for the function arguments.
1732 */
1733 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734call_func_retnr(
1735 char_u *func,
1736 int argc,
1737 char_u **argv,
1738 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739{
1740 typval_T rettv;
1741 long retval;
1742
1743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1745 return -1;
1746
1747 retval = get_tv_number_chk(&rettv, NULL);
1748 clear_tv(&rettv);
1749 return retval;
1750}
1751
1752#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1753 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1754
Bram Moolenaar4f688582007-07-24 12:34:30 +00001755# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001757 * Call vimL function "func" and return the result as a string.
1758 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 * Uses argv[argc] for the function arguments.
1760 */
1761 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762call_func_retstr(
1763 char_u *func,
1764 int argc,
1765 char_u **argv,
1766 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767{
1768 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001769 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001771 /* All arguments are passed as strings, no conversion to number. */
1772 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773 return NULL;
1774
1775 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 return retval;
1778}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001779# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001781/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001782 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001784 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 */
1786 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787call_func_retlist(
1788 char_u *func,
1789 int argc,
1790 char_u **argv,
1791 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792{
1793 typval_T rettv;
1794
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001795 /* All arguments are passed as strings, no conversion to number. */
1796 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797 return NULL;
1798
1799 if (rettv.v_type != VAR_LIST)
1800 {
1801 clear_tv(&rettv);
1802 return NULL;
1803 }
1804
1805 return rettv.vval.v_list;
1806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
1809/*
1810 * Save the current function call pointer, and set it to NULL.
1811 * Used when executing autocommands and for ":source".
1812 */
1813 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 current_funccal = NULL;
1819 return (void *)fc;
1820}
1821
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825 funccall_T *fc = (funccall_T *)vfc;
1826
1827 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830#if defined(FEAT_PROFILE) || defined(PROTO)
1831/*
1832 * Prepare profiling for entering a child or something else that is not
1833 * counted for the script/function itself.
1834 * Should always be called in pair with prof_child_exit().
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837prof_child_enter(
1838 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001839{
1840 funccall_T *fc = current_funccal;
1841
1842 if (fc != NULL && fc->func->uf_profiling)
1843 profile_start(&fc->prof_child);
1844 script_prof_save(tm);
1845}
1846
1847/*
1848 * Take care of time spent in a child.
1849 * Should always be called after prof_child_enter().
1850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852prof_child_exit(
1853 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001854{
1855 funccall_T *fc = current_funccal;
1856
1857 if (fc != NULL && fc->func->uf_profiling)
1858 {
1859 profile_end(&fc->prof_child);
1860 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1861 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1862 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1863 }
1864 script_prof_restore(tm);
1865}
1866#endif
1867
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_FOLDING
1870/*
1871 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1872 * it in "*cp". Doesn't give error messages.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int retval;
1879 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001880 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1881 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001884 if (use_sandbox)
1885 ++sandbox;
1886 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 retval = 0;
1890 else
1891 {
1892 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (tv.v_type == VAR_NUMBER)
1894 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001895 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 retval = 0;
1897 else
1898 {
1899 /* If the result is a string, check if there is a non-digit before
1900 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (!VIM_ISDIGIT(*s) && *s != '-')
1903 *cp = *s++;
1904 retval = atol((char *)s);
1905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001909 if (use_sandbox)
1910 --sandbox;
1911 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 return retval;
1914}
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 * ":let" list all variable values
1919 * ":let var1 var2" list variable values
1920 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * ":let var += expr" assignment command.
1922 * ":let var -= expr" assignment command.
1923 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 int var_count = 0;
1934 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001936 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 argend = skip_var_list(arg, &var_count, &semicolon);
1940 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001942 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1943 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 expr = skipwhite(argend);
1945 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1946 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001948 /*
1949 * ":let" without "=": list variables
1950 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 if (*arg == '[')
1952 EMSG(_(e_invarg));
1953 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001959 list_glob_vars(&first);
1960 list_buf_vars(&first);
1961 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001962#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_script_vars(&first);
1966 list_func_vars(&first);
1967 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 eap->nextcmd = check_nextcmd(arg);
1970 }
1971 else
1972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 op[0] = '=';
1974 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001975 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001977 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1978 op[0] = *expr; /* +=, -= or .= */
1979 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001981 else
1982 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (eap->skip)
1985 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (eap->skip)
1988 {
1989 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --emsg_skip;
1992 }
1993 else if (i != FAIL)
1994 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
2000}
2001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002/*
2003 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2004 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 * When "nextchars" is not NULL it points to a string with characters that
2006 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2007 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 * Returns OK or FAIL;
2009 */
2010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011ex_let_vars(
2012 char_u *arg_start,
2013 typval_T *tv,
2014 int copy, /* copy values from "tv", don't move */
2015 int semicolon, /* from skip_var_list() */
2016 int var_count, /* from skip_var_list() */
2017 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018{
2019 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 listitem_T *item;
2023 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024
2025 if (*arg != '[')
2026 {
2027 /*
2028 * ":let var = expr" or ":for var in list"
2029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return FAIL;
2032 return OK;
2033 }
2034
2035 /*
2036 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2037 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002038 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 {
2040 EMSG(_(e_listreq));
2041 return FAIL;
2042 }
2043
2044 i = list_len(l);
2045 if (semicolon == 0 && var_count < i)
2046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002047 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 }
2050 if (var_count - semicolon > i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055
2056 item = l->lv_first;
2057 while (*arg != ']')
2058 {
2059 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 item = item->li_next;
2062 if (arg == NULL)
2063 return FAIL;
2064
2065 arg = skipwhite(arg);
2066 if (*arg == ';')
2067 {
2068 /* Put the rest of the list (may be empty) in the var after ';'.
2069 * Create a new list for this. */
2070 l = list_alloc();
2071 if (l == NULL)
2072 return FAIL;
2073 while (item != NULL)
2074 {
2075 list_append_tv(l, &item->li_tv);
2076 item = item->li_next;
2077 }
2078
2079 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002080 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 ltv.vval.v_list = l;
2082 l->lv_refcount = 1;
2083
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2085 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 clear_tv(&ltv);
2087 if (arg == NULL)
2088 return FAIL;
2089 break;
2090 }
2091 else if (*arg != ',' && *arg != ']')
2092 {
2093 EMSG2(_(e_intern2), "ex_let_vars()");
2094 return FAIL;
2095 }
2096 }
2097
2098 return OK;
2099}
2100
2101/*
2102 * Skip over assignable variable "var" or list of variables "[var, var]".
2103 * Used for ":let varvar = expr" and ":for varvar in expr".
2104 * For "[var, var]" increment "*var_count" for each variable.
2105 * for "[var, var; var]" set "semicolon".
2106 * Return NULL for an error.
2107 */
2108 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109skip_var_list(
2110 char_u *arg,
2111 int *var_count,
2112 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113{
2114 char_u *p, *s;
2115
2116 if (*arg == '[')
2117 {
2118 /* "[var, var]": find the matching ']'. */
2119 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002120 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 {
2122 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2123 s = skip_var_one(p);
2124 if (s == p)
2125 {
2126 EMSG2(_(e_invarg2), p);
2127 return NULL;
2128 }
2129 ++*var_count;
2130
2131 p = skipwhite(s);
2132 if (*p == ']')
2133 break;
2134 else if (*p == ';')
2135 {
2136 if (*semicolon == 1)
2137 {
2138 EMSG(_("Double ; in list of variables"));
2139 return NULL;
2140 }
2141 *semicolon = 1;
2142 }
2143 else if (*p != ',')
2144 {
2145 EMSG2(_(e_invarg2), p);
2146 return NULL;
2147 }
2148 }
2149 return p + 1;
2150 }
2151 else
2152 return skip_var_one(arg);
2153}
2154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002156 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002157 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 if (*arg == '@' && arg[1] != NUL)
2163 return arg + 2;
2164 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2165 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166}
2167
Bram Moolenaara7043832005-01-21 11:56:39 +00002168/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 * List variables for hashtab "ht" with prefix "prefix".
2170 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_hashtable_vars(
2174 hashtab_T *ht,
2175 char_u *prefix,
2176 int empty,
2177 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 hashitem_T *hi;
2180 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 int todo;
2182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002183 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2185 {
2186 if (!HASHITEM_EMPTY(hi))
2187 {
2188 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 di = HI2DI(hi);
2190 if (empty || di->di_tv.v_type != VAR_STRING
2191 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193 }
2194 }
2195}
2196
2197/*
2198 * List global variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002204}
2205
2206/*
2207 * List buffer variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002211{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u numbuf[NUMBUFLEN];
2213
Bram Moolenaar429fa852013-04-15 12:27:36 +02002214 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216
2217 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2219 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002220}
2221
2222/*
2223 * List window variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002227{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002228 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002230}
2231
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232#ifdef FEAT_WINDOWS
2233/*
2234 * List tab page variables.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002239 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241}
2242#endif
2243
Bram Moolenaara7043832005-01-21 11:56:39 +00002244/*
2245 * List Vim variables.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251}
2252
2253/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254 * List script-local variables, if there is a script.
2255 */
2256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258{
2259 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2261 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262}
2263
2264/*
2265 * List function variables, if there is a function.
2266 */
2267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002269{
2270 if (current_funccal != NULL)
2271 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002273}
2274
2275/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 * List variables in "arg".
2277 */
2278 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002279list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280{
2281 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 char_u *name_start;
2285 char_u *arg_subsc;
2286 char_u *tofree;
2287 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288
2289 while (!ends_excmd(*arg) && !got_int)
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2295 {
2296 emsg_severe = TRUE;
2297 EMSG(_(e_trailing));
2298 break;
2299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 /* get_name_len() takes care of expanding curly braces */
2304 name_start = name = arg;
2305 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2306 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* This is mainly to keep test 49 working: when expanding
2309 * curly braces fails overrule the exception error message. */
2310 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 emsg_severe = TRUE;
2313 EMSG2(_(e_invarg2), arg);
2314 break;
2315 }
2316 error = TRUE;
2317 }
2318 else
2319 {
2320 if (tofree != NULL)
2321 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002322 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 else
2325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 /* handle d.key, l[idx], f(expr) */
2327 arg_subsc = arg;
2328 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002336 case 'g': list_glob_vars(first); break;
2337 case 'b': list_buf_vars(first); break;
2338 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002339#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002340 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002341#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 case 'v': list_vim_vars(first); break;
2343 case 's': list_script_vars(first); break;
2344 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 default:
2346 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002347 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
2349 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 {
2351 char_u numbuf[NUMBUFLEN];
2352 char_u *tf;
2353 int c;
2354 char_u *s;
2355
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002356 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002357 c = *arg;
2358 *arg = NUL;
2359 list_one_var_a((char_u *)"",
2360 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002361 tv.v_type,
2362 s == NULL ? (char_u *)"" : s,
2363 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002364 *arg = c;
2365 vim_free(tf);
2366 }
2367 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002371
2372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374
2375 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
2377
2378 return arg;
2379}
2380
2381/*
2382 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2383 * Returns a pointer to the char just after the var name.
2384 * Returns NULL if there is an error.
2385 */
2386 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002387ex_let_one(
2388 char_u *arg, /* points to variable name */
2389 typval_T *tv, /* value to assign to variable */
2390 int copy, /* copy value from "tv" */
2391 char_u *endchars, /* valid chars after variable name or NULL */
2392 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393{
2394 int c1;
2395 char_u *name;
2396 char_u *p;
2397 char_u *arg_end = NULL;
2398 int len;
2399 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
2402 /*
2403 * ":let $VAR = expr": Set environment variable.
2404 */
2405 if (*arg == '$')
2406 {
2407 /* Find the end of the name. */
2408 ++arg;
2409 name = arg;
2410 len = get_env_len(&arg);
2411 if (len == 0)
2412 EMSG2(_(e_invarg2), name - 1);
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002420 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 {
2422 c1 = name[len];
2423 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 p = get_tv_string_chk(tv);
2425 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
2427 int mustfree = FALSE;
2428 char_u *s = vim_getenv(name, &mustfree);
2429
2430 if (s != NULL)
2431 {
2432 p = tofree = concat_str(s, p);
2433 if (mustfree)
2434 vim_free(s);
2435 }
2436 }
2437 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 if (STRICMP(name, "HOME") == 0)
2441 init_homedir();
2442 else if (didset_vim && STRICMP(name, "VIM") == 0)
2443 didset_vim = FALSE;
2444 else if (didset_vimruntime
2445 && STRICMP(name, "VIMRUNTIME") == 0)
2446 didset_vimruntime = FALSE;
2447 arg_end = arg;
2448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 }
2452 }
2453 }
2454
2455 /*
2456 * ":let &option = expr": Set option value.
2457 * ":let &l:option = expr": Set local option value.
2458 * ":let &g:option = expr": Set global option value.
2459 */
2460 else if (*arg == '&')
2461 {
2462 /* Find the end of the name. */
2463 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 if (p == NULL || (endchars != NULL
2465 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 EMSG(_(e_letunexp));
2467 else
2468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 long n;
2470 int opt_type;
2471 long numval;
2472 char_u *stringval = NULL;
2473 char_u *s;
2474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 c1 = *p;
2476 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477
2478 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 s = get_tv_string_chk(tv); /* != NULL if number or string */
2480 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 {
2482 opt_type = get_option_value(arg, &numval,
2483 &stringval, opt_flags);
2484 if ((opt_type == 1 && *op == '.')
2485 || (opt_type == 0 && *op != '.'))
2486 EMSG2(_(e_letwrong), op);
2487 else
2488 {
2489 if (opt_type == 1) /* number */
2490 {
2491 if (*op == '+')
2492 n = numval + n;
2493 else
2494 n = numval - n;
2495 }
2496 else if (opt_type == 0 && stringval != NULL) /* string */
2497 {
2498 s = concat_str(stringval, s);
2499 vim_free(stringval);
2500 stringval = s;
2501 }
2502 }
2503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (s != NULL)
2505 {
2506 set_option_value(arg, n, s, opt_flags);
2507 arg_end = p;
2508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let @r = expr": Set register contents.
2516 */
2517 else if (*arg == '@')
2518 {
2519 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 if (op != NULL && (*op == '+' || *op == '-'))
2521 EMSG2(_(e_letwrong), op);
2522 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 char_u *s;
2529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 p = get_tv_string_chk(tv);
2531 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002533 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 if (s != NULL)
2535 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 vim_free(s);
2538 }
2539 }
2540 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 arg_end = arg + 1;
2544 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
2547 }
2548
2549 /*
2550 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2561 EMSG(_(e_letunexp));
2562 else
2563 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 arg_end = p;
2566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 }
2570
2571 else
2572 EMSG2(_(e_invarg2), arg);
2573
2574 return arg_end;
2575}
2576
2577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2579 */
2580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582{
2583 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2584 {
2585 EMSG2(_(e_readonlyvar), arg);
2586 return TRUE;
2587 }
2588 return FALSE;
2589}
2590
2591/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Get an lval: variable, Dict item or List item that can be assigned a value
2593 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2594 * "name.key", "name.key[expr]" etc.
2595 * Indexing only works if "name" is an existing List or Dictionary.
2596 * "name" points to the start of the name.
2597 * If "rettv" is not NULL it points to the value to be assigned.
2598 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2599 * wrong; must end in space or cmd separator.
2600 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 * flags:
2602 * GLV_QUIET: do not give error messages
2603 * GLV_NO_AUTOLOAD: do not use script autoloading
2604 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002606 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 */
2609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610get_lval(
2611 char_u *name,
2612 typval_T *rettv,
2613 lval_T *lp,
2614 int unlet,
2615 int skip,
2616 int flags, /* GLV_ values */
2617 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 char_u *expr_start, *expr_end;
2621 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 dictitem_T *v;
2623 typval_T var1;
2624 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634
2635 if (skip)
2636 {
2637 /* When skipping just find the end of the name. */
2638 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002639 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 }
2641
2642 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002643 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (expr_start != NULL)
2645 {
2646 /* Don't expand the name when we already know there is an error. */
2647 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2648 && *p != '[' && *p != '.')
2649 {
2650 EMSG(_(e_trailing));
2651 return NULL;
2652 }
2653
2654 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2655 if (lp->ll_exp_name == NULL)
2656 {
2657 /* Report an invalid expression in braces, unless the
2658 * expression evaluation has been cancelled due to an
2659 * aborting error, an interrupt, or an exception. */
2660 if (!aborting() && !quiet)
2661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 EMSG2(_(e_invarg2), name);
2664 return NULL;
2665 }
2666 }
2667 lp->ll_name = lp->ll_exp_name;
2668 }
2669 else
2670 lp->ll_name = name;
2671
2672 /* Without [idx] or .key we are done. */
2673 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2674 return p;
2675
2676 cc = *p;
2677 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002678 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (v == NULL && !quiet)
2680 EMSG2(_(e_undefvar), lp->ll_name);
2681 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 if (v == NULL)
2683 return NULL;
2684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 /*
2686 * Loop until no more [idx] or .key is following.
2687 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002688 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2692 && !(lp->ll_tv->v_type == VAR_DICT
2693 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E689: Can only index a List or Dictionary"));
2697 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E708: [:] must come last"));
2703 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 len = -1;
2707 if (*p == '.')
2708 {
2709 key = p + 1;
2710 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2711 ;
2712 if (len == 0)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
2716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 p = key + len;
2719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 else
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p == ':')
2725 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 else
2727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 empty1 = FALSE;
2729 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var1) == NULL)
2732 {
2733 /* not a number or string */
2734 clear_tv(&var1);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
2738
2739 /* Optionally get the second index [ :expr]. */
2740 if (*p == ':')
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (rettv != NULL && (rettv->v_type != VAR_LIST
2751 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759 p = skipwhite(p + 1);
2760 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (!empty1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 if (get_tv_string_chk(&var2) == NULL)
2772 {
2773 /* not a number or string */
2774 if (!empty1)
2775 clear_tv(&var1);
2776 clear_tv(&var2);
2777 return NULL;
2778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
2788 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (!empty1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795
2796 /* Skip to past ']'. */
2797 ++p;
2798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
2802 if (len == -1)
2803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002805 key = get_tv_string_chk(&var1); /* is number or string */
2806 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 lp->ll_list = NULL;
2813 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 /* When assigning to a scope dictionary check that a function and
2817 * variable name is valid (only variable name unless it is l: or
2818 * g: dictionary). Disallow overwriting a builtin function. */
2819 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 int prevval;
2822 int wrong;
2823
2824 if (len != -1)
2825 {
2826 prevval = key[len];
2827 key[len] = NUL;
2828 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002829 else
2830 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002831 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2832 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 || !valid_varname(key);
2835 if (len != -1)
2836 key[len] = prevval;
2837 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* Can't add "v:" variable. */
2844 if (lp->ll_dict == &vimvardict)
2845 {
2846 EMSG2(_(e_illvar), name);
2847 return NULL;
2848 }
2849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002850 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 if (len == -1)
2856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 }
2859 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (len == -1)
2864 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 p = NULL;
2867 break;
2868 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002869 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002870 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002871 return NULL;
2872
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 if (len == -1)
2874 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877 else
2878 {
2879 /*
2880 * Get the number and item for the only or first index of the List.
2881 */
2882 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 else
2885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 clear_tv(&var1);
2888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_list = lp->ll_tv->vval.v_list;
2891 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2892 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 if (lp->ll_n1 < 0)
2895 {
2896 lp->ll_n1 = 0;
2897 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2898 }
2899 }
2900 if (lp->ll_li == NULL)
2901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 }
2908
2909 /*
2910 * May need to find the item or absolute index for the second
2911 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 * When no index given: "lp->ll_empty2" is TRUE.
2913 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002917 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002923 {
2924 if (!quiet)
2925 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002927 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2932 if (lp->ll_n1 < 0)
2933 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2934 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002935 {
2936 if (!quiet)
2937 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
2941
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 return p;
2947}
2948
2949/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 */
2952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954{
2955 vim_free(lp->ll_exp_name);
2956 vim_free(lp->ll_newkey);
2957}
2958
2959/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 */
2964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965set_var_lval(
2966 lval_T *lp,
2967 char_u *endp,
2968 typval_T *rettv,
2969 int copy,
2970 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971{
2972 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002973 listitem_T *ri;
2974 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975
2976 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 cc = *endp;
2981 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002986 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002987 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002988 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002989 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 if ((di == NULL
2992 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2993 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2994 FALSE)))
2995 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 set_var(lp->ll_name, &tv, FALSE);
2997 clear_tv(&tv);
2998 }
2999 }
3000 else
3001 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 else if (tv_check_lock(lp->ll_newkey == NULL
3006 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003007 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003008 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 else if (lp->ll_range)
3010 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 listitem_T *ll_li = lp->ll_li;
3012 int ll_n1 = lp->ll_n1;
3013
3014 /*
3015 * Check whether any of the list items is locked
3016 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003017 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003019 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020 return;
3021 ri = ri->li_next;
3022 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3023 break;
3024 ll_li = ll_li->li_next;
3025 ++ll_n1;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /*
3029 * Assign the List values to the list items.
3030 */
3031 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3035 else
3036 {
3037 clear_tv(&lp->ll_li->li_tv);
3038 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3042 break;
3043 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003046 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 ri = NULL;
3049 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_li = lp->ll_li->li_next;
3053 ++lp->ll_n1;
3054 }
3055 if (ri != NULL)
3056 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (lp->ll_empty2
3058 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 : lp->ll_n1 != lp->ll_n2)
3060 EMSG(_("E711: List value has not enough items"));
3061 }
3062 else
3063 {
3064 /*
3065 * Assign to a List or Dictionary item.
3066 */
3067 if (lp->ll_newkey != NULL)
3068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 if (op != NULL && *op != '=')
3070 {
3071 EMSG2(_(e_letwrong), op);
3072 return;
3073 }
3074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003076 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 if (di == NULL)
3078 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003079 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3080 {
3081 vim_free(di);
3082 return;
3083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 else if (op != NULL && *op != '=')
3087 {
3088 tv_op(lp->ll_tv, rettv, op);
3089 return;
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /*
3095 * Assign the value to the variable or list item.
3096 */
3097 if (copy)
3098 copy_tv(rettv, lp->ll_tv);
3099 else
3100 {
3101 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003102 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 }
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106}
3107
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3110 * Returns OK or FAIL.
3111 */
3112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114{
3115 long n;
3116 char_u numbuf[NUMBUFLEN];
3117 char_u *s;
3118
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003119 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3120 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3121 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 {
3123 switch (tv1->v_type)
3124 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003125 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 case VAR_DICT:
3127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003128 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003129 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003131 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 break;
3133
3134 case VAR_LIST:
3135 if (*op != '+' || tv2->v_type != VAR_LIST)
3136 break;
3137 /* List += List */
3138 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3139 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3140 return OK;
3141
3142 case VAR_NUMBER:
3143 case VAR_STRING:
3144 if (tv2->v_type == VAR_LIST)
3145 break;
3146 if (*op == '+' || *op == '-')
3147 {
3148 /* nr += nr or nr -= nr*/
3149 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150#ifdef FEAT_FLOAT
3151 if (tv2->v_type == VAR_FLOAT)
3152 {
3153 float_T f = n;
3154
3155 if (*op == '+')
3156 f += tv2->vval.v_float;
3157 else
3158 f -= tv2->vval.v_float;
3159 clear_tv(tv1);
3160 tv1->v_type = VAR_FLOAT;
3161 tv1->vval.v_float = f;
3162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164#endif
3165 {
3166 if (*op == '+')
3167 n += get_tv_number(tv2);
3168 else
3169 n -= get_tv_number(tv2);
3170 clear_tv(tv1);
3171 tv1->v_type = VAR_NUMBER;
3172 tv1->vval.v_number = n;
3173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 else
3176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 if (tv2->v_type == VAR_FLOAT)
3178 break;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* str .= str */
3181 s = get_tv_string(tv1);
3182 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3183 clear_tv(tv1);
3184 tv1->v_type = VAR_STRING;
3185 tv1->vval.v_string = s;
3186 }
3187 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003190#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 {
3192 float_T f;
3193
3194 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3195 && tv2->v_type != VAR_NUMBER
3196 && tv2->v_type != VAR_STRING))
3197 break;
3198 if (tv2->v_type == VAR_FLOAT)
3199 f = tv2->vval.v_float;
3200 else
3201 f = get_tv_number(tv2);
3202 if (*op == '+')
3203 tv1->vval.v_float += f;
3204 else
3205 tv1->vval.v_float -= f;
3206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003208 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003209 }
3210 }
3211
3212 EMSG2(_(e_letwrong), op);
3213 return FAIL;
3214}
3215
3216/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 * Add a watcher to a list.
3218 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
3222 lw->lw_next = l->lv_watch;
3223 l->lv_watch = lw;
3224}
3225
3226/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003227 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 * No warning when it isn't found...
3229 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 lwp = &l->lv_watch;
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 {
3238 if (lw == lwrem)
3239 {
3240 *lwp = lw->lw_next;
3241 break;
3242 }
3243 lwp = &lw->lw_next;
3244 }
3245}
3246
3247/*
3248 * Just before removing an item from a list: advance watchers to the next
3249 * item.
3250 */
3251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3257 if (lw->lw_item == item)
3258 lw->lw_item = item->li_next;
3259}
3260
3261/*
3262 * Evaluate the expression used in a ":for var in expr" command.
3263 * "arg" points to "var".
3264 * Set "*errp" to TRUE for an error, FALSE otherwise;
3265 * Return a pointer that holds the info. Null when there is an error.
3266 */
3267 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval_for_line(
3269 char_u *arg,
3270 int *errp,
3271 char_u **nextcmdp,
3272 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T tv;
3277 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 *errp = TRUE; /* default: there is an error */
3280
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 if (fi == NULL)
3283 return NULL;
3284
3285 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3286 if (expr == NULL)
3287 return fi;
3288
3289 expr = skipwhite(expr);
3290 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3291 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003292 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 return fi;
3294 }
3295
3296 if (skip)
3297 ++emsg_skip;
3298 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3299 {
3300 *errp = FALSE;
3301 if (!skip)
3302 {
3303 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003304 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 clear_tv(&tv);
3308 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003309 else if (l == NULL)
3310 {
3311 /* a null list is like an empty list: do nothing */
3312 clear_tv(&tv);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 else
3315 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003316 /* No need to increment the refcount, it's already set for the
3317 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 fi->fi_list = l;
3319 list_add_watch(l, &fi->fi_lw);
3320 fi->fi_lw.lw_item = l->lv_first;
3321 }
3322 }
3323 }
3324 if (skip)
3325 --emsg_skip;
3326
3327 return fi;
3328}
3329
3330/*
3331 * Use the first item in a ":for" list. Advance to the next.
3332 * Assign the values to the variable (list). "arg" points to the first one.
3333 * Return TRUE when a valid item was found, FALSE when at end of list or
3334 * something wrong.
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342
3343 item = fi->fi_lw.lw_item;
3344 if (item == NULL)
3345 result = FALSE;
3346 else
3347 {
3348 fi->fi_lw.lw_item = item->li_next;
3349 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3350 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3351 }
3352 return result;
3353}
3354
3355/*
3356 * Free the structure used to store info used by ":for".
3357 */
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360{
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003363 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003364 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003366 list_unref(fi->fi_list);
3367 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 vim_free(fi);
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3372
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374set_context_for_expression(
3375 expand_T *xp,
3376 char_u *arg,
3377 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int got_eq = FALSE;
3380 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 if (cmdidx == CMD_let)
3384 {
3385 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003386 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 {
3388 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003389 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 {
3391 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003393 if (vim_iswhite(*p))
3394 break;
3395 }
3396 return;
3397 }
3398 }
3399 else
3400 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3401 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while ((xp->xp_pattern = vim_strpbrk(arg,
3403 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3404 {
3405 c = *xp->xp_pattern;
3406 if (c == '&')
3407 {
3408 c = xp->xp_pattern[1];
3409 if (c == '&')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = cmdidx != CMD_let || got_eq
3413 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3414 }
3415 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003418 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3419 xp->xp_pattern += 2;
3420
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else if (c == '$')
3424 {
3425 /* environment variable */
3426 xp->xp_context = EXPAND_ENV_VARS;
3427 }
3428 else if (c == '=')
3429 {
3430 got_eq = TRUE;
3431 xp->xp_context = EXPAND_EXPRESSION;
3432 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003433 else if (c == '#'
3434 && xp->xp_context == EXPAND_EXPRESSION)
3435 {
3436 /* Autoload function/variable contains '#'. */
3437 break;
3438 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003439 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && xp->xp_context == EXPAND_FUNCTIONS
3441 && vim_strchr(xp->xp_pattern, '(') == NULL)
3442 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003443 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445 }
3446 else if (cmdidx != CMD_let || got_eq)
3447 {
3448 if (c == '"') /* string */
3449 {
3450 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3451 if (c == '\\' && xp->xp_pattern[1] != NUL)
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_NOTHING;
3454 }
3455 else if (c == '\'') /* literal string */
3456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003457 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3459 /* skip */ ;
3460 xp->xp_context = EXPAND_NOTHING;
3461 }
3462 else if (c == '|')
3463 {
3464 if (xp->xp_pattern[1] == '|')
3465 {
3466 ++xp->xp_pattern;
3467 xp->xp_context = EXPAND_EXPRESSION;
3468 }
3469 else
3470 xp->xp_context = EXPAND_COMMANDS;
3471 }
3472 else
3473 xp->xp_context = EXPAND_EXPRESSION;
3474 }
3475 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003476 /* Doesn't look like something valid, expand as an expression
3477 * anyway. */
3478 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 arg = xp->xp_pattern;
3480 if (*arg != NUL)
3481 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3482 /* skip */ ;
3483 }
3484 xp->xp_pattern = arg;
3485}
3486
3487#endif /* FEAT_CMDL_COMPL */
3488
3489/*
3490 * ":1,25call func(arg1, arg2)" function call.
3491 */
3492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 char_u *arg = eap->arg;
3496 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 linenr_T lnum;
3502 int doesrange;
3503 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003505 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 if (eap->skip)
3508 {
3509 /* trans_function_name() doesn't work well when skipping, use eval0()
3510 * instead to skip to any following command, e.g. for:
3511 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003512 ++emsg_skip;
3513 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3514 clear_tv(&rettv);
3515 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003516 return;
3517 }
3518
Bram Moolenaar65639032016-03-16 21:40:30 +01003519 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003520 if (fudi.fd_newkey != NULL)
3521 {
3522 /* Still need to give an error message for missing key. */
3523 EMSG2(_(e_dictkey), fudi.fd_newkey);
3524 vim_free(fudi.fd_newkey);
3525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003526 if (tofree == NULL)
3527 return;
3528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 /* Increase refcount on dictionary, it could get deleted when evaluating
3530 * the arguments. */
3531 if (fudi.fd_dict != NULL)
3532 ++fudi.fd_dict->dv_refcount;
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3535 * contents. For VAR_PARTIAL get its partial, unless we already have one
3536 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 name = deref_func_name(tofree, &len,
3539 partial != NULL ? NULL : &partial, FALSE);
3540
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 /* Skip white space to allow ":call func ()". Not good, but required for
3542 * backward compatibility. */
3543 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 if (*startarg != '(')
3547 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003548 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 goto end;
3550 }
3551
3552 /*
3553 * When skipping, evaluate the function once, to find the end of the
3554 * arguments.
3555 * When the function takes a range, this is discovered after the first
3556 * call, and the loop is broken.
3557 */
3558 if (eap->skip)
3559 {
3560 ++emsg_skip;
3561 lnum = eap->line2; /* do it once, also with an invalid range */
3562 }
3563 else
3564 lnum = eap->line1;
3565 for ( ; lnum <= eap->line2; ++lnum)
3566 {
3567 if (!eap->skip && eap->addr_count > 0)
3568 {
3569 curwin->w_cursor.lnum = lnum;
3570 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003571#ifdef FEAT_VIRTUALEDIT
3572 curwin->w_cursor.coladd = 0;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003578 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 failed = TRUE;
3581 break;
3582 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003583
3584 /* Handle a function returning a Funcref, Dictionary or List. */
3585 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3586 {
3587 failed = TRUE;
3588 break;
3589 }
3590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (doesrange || eap->skip)
3593 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (aborting())
3600 break;
3601 }
3602 if (eap->skip)
3603 --emsg_skip;
3604
3605 if (!failed)
3606 {
3607 /* Check for trailing illegal characters and a following command. */
3608 if (!ends_excmd(*arg))
3609 {
3610 emsg_severe = TRUE;
3611 EMSG(_(e_trailing));
3612 }
3613 else
3614 eap->nextcmd = check_nextcmd(arg);
3615 }
3616
3617end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003618 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * ":unlet[!] var1 ... " command.
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 ex_unletlock(eap, eap->arg, 0);
3629}
3630
3631/*
3632 * ":lockvar" and ":unlockvar" commands
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 int deep = 2;
3639
3640 if (eap->forceit)
3641 deep = -1;
3642 else if (vim_isdigit(*arg))
3643 {
3644 deep = getdigits(&arg);
3645 arg = skipwhite(arg);
3646 }
3647
3648 ex_unletlock(eap, arg, deep);
3649}
3650
3651/*
3652 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3653 */
3654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ex_unletlock(
3656 exarg_T *eap,
3657 char_u *argstart,
3658 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659{
3660 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 do
3666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003668 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003669 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 if (lv.ll_name == NULL)
3671 error = TRUE; /* error but continue parsing */
3672 if (name_end == NULL || (!vim_iswhite(*name_end)
3673 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (name_end != NULL)
3676 {
3677 emsg_severe = TRUE;
3678 EMSG(_(e_trailing));
3679 }
3680 if (!(eap->skip || error))
3681 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 break;
3683 }
3684
3685 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 {
3687 if (eap->cmdidx == CMD_unlet)
3688 {
3689 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3690 error = TRUE;
3691 }
3692 else
3693 {
3694 if (do_lock_var(&lv, name_end, deep,
3695 eap->cmdidx == CMD_lockvar) == FAIL)
3696 error = TRUE;
3697 }
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 if (!eap->skip)
3701 clear_lval(&lv);
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 arg = skipwhite(name_end);
3704 } while (!ends_excmd(*arg));
3705
3706 eap->nextcmd = check_nextcmd(arg);
3707}
3708
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710do_unlet_var(
3711 lval_T *lp,
3712 char_u *name_end,
3713 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 int ret = OK;
3716 int cc;
3717
3718 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003731 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 else if (lp->ll_range)
3736 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003738 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003739 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003740
3741 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3742 {
3743 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003744 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745 return FAIL;
3746 ll_li = li;
3747 ++ll_n1;
3748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749
3750 /* Delete a range of List items. */
3751 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3752 {
3753 li = lp->ll_li->li_next;
3754 listitem_remove(lp->ll_list, lp->ll_li);
3755 lp->ll_li = li;
3756 ++lp->ll_n1;
3757 }
3758 }
3759 else
3760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003761 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 /* unlet a List item. */
3763 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003766 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 }
3768
3769 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770}
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hashtab_T *ht;
3780 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003782 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 ht = find_var_ht(name, &varname);
3786 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003788 if (ht == &globvarht)
3789 d = &globvardict;
3790 else if (current_funccal != NULL
3791 && ht == &current_funccal->l_vars.dv_hashtab)
3792 d = &current_funccal->l_vars;
3793 else if (ht == &compat_hashtab)
3794 d = &vimvardict;
3795 else
3796 {
3797 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3798 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3799 }
3800 if (d == NULL)
3801 {
3802 EMSG2(_(e_intern2), "do_unlet()");
3803 return FAIL;
3804 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hi = hash_find(ht, varname);
3806 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003808 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003809 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003810 || var_check_ro(di->di_flags, name, FALSE)
3811 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003812 return FAIL;
3813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003814 delete_var(ht, hi);
3815 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (forceit)
3819 return OK;
3820 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822}
3823
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003824/*
3825 * Lock or unlock variable indicated by "lp".
3826 * "deep" is the levels to go (-1 for unlimited);
3827 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3828 */
3829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830do_lock_var(
3831 lval_T *lp,
3832 char_u *name_end,
3833 int deep,
3834 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835{
3836 int ret = OK;
3837 int cc;
3838 dictitem_T *di;
3839
3840 if (deep == 0) /* nothing to do */
3841 return OK;
3842
3843 if (lp->ll_tv == NULL)
3844 {
3845 cc = *name_end;
3846 *name_end = NUL;
3847
3848 /* Normal name or expanded name. */
3849 if (check_changedtick(lp->ll_name))
3850 ret = FAIL;
3851 else
3852 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003853 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854 if (di == NULL)
3855 ret = FAIL;
3856 else
3857 {
3858 if (lock)
3859 di->di_flags |= DI_FLAGS_LOCK;
3860 else
3861 di->di_flags &= ~DI_FLAGS_LOCK;
3862 item_lock(&di->di_tv, deep, lock);
3863 }
3864 }
3865 *name_end = cc;
3866 }
3867 else if (lp->ll_range)
3868 {
3869 listitem_T *li = lp->ll_li;
3870
3871 /* (un)lock a range of List items. */
3872 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3873 {
3874 item_lock(&li->li_tv, deep, lock);
3875 li = li->li_next;
3876 ++lp->ll_n1;
3877 }
3878 }
3879 else if (lp->ll_list != NULL)
3880 /* (un)lock a List item. */
3881 item_lock(&lp->ll_li->li_tv, deep, lock);
3882 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003883 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 item_lock(&lp->ll_di->di_tv, deep, lock);
3885
3886 return ret;
3887}
3888
3889/*
3890 * Lock or unlock an item. "deep" is nr of levels to go.
3891 */
3892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003893item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003894{
3895 static int recurse = 0;
3896 list_T *l;
3897 listitem_T *li;
3898 dict_T *d;
3899 hashitem_T *hi;
3900 int todo;
3901
3902 if (recurse >= DICT_MAXNEST)
3903 {
3904 EMSG(_("E743: variable nested too deep for (un)lock"));
3905 return;
3906 }
3907 if (deep == 0)
3908 return;
3909 ++recurse;
3910
3911 /* lock/unlock the item itself */
3912 if (lock)
3913 tv->v_lock |= VAR_LOCKED;
3914 else
3915 tv->v_lock &= ~VAR_LOCKED;
3916
3917 switch (tv->v_type)
3918 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 case VAR_UNKNOWN:
3920 case VAR_NUMBER:
3921 case VAR_STRING:
3922 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003923 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003924 case VAR_FLOAT:
3925 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003927 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 break;
3929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003930 case VAR_LIST:
3931 if ((l = tv->vval.v_list) != NULL)
3932 {
3933 if (lock)
3934 l->lv_lock |= VAR_LOCKED;
3935 else
3936 l->lv_lock &= ~VAR_LOCKED;
3937 if (deep < 0 || deep > 1)
3938 /* recursive: lock/unlock the items the List contains */
3939 for (li = l->lv_first; li != NULL; li = li->li_next)
3940 item_lock(&li->li_tv, deep - 1, lock);
3941 }
3942 break;
3943 case VAR_DICT:
3944 if ((d = tv->vval.v_dict) != NULL)
3945 {
3946 if (lock)
3947 d->dv_lock |= VAR_LOCKED;
3948 else
3949 d->dv_lock &= ~VAR_LOCKED;
3950 if (deep < 0 || deep > 1)
3951 {
3952 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003954 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3955 {
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 --todo;
3959 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3960 }
3961 }
3962 }
3963 }
3964 }
3965 --recurse;
3966}
3967
Bram Moolenaara40058a2005-07-11 22:42:07 +00003968/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003969 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3970 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003971 */
3972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003974{
3975 return (tv->v_lock & VAR_LOCKED)
3976 || (tv->v_type == VAR_LIST
3977 && tv->vval.v_list != NULL
3978 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3979 || (tv->v_type == VAR_DICT
3980 && tv->vval.v_dict != NULL
3981 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3982}
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3985/*
3986 * Delete all "menutrans_" variables.
3987 */
3988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003989del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003995 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 {
3998 if (!HASHITEM_EMPTY(hi))
3999 {
4000 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4002 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 }
4004 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006}
4007#endif
4008
4009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4010
4011/*
4012 * Local string buffer for the next two functions to store a variable name
4013 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4014 * get_user_var_name().
4015 */
4016
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004017static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019static char_u *varnamebuf = NULL;
4020static int varnamebuflen = 0;
4021
4022/*
4023 * Function to concatenate a prefix and a variable name.
4024 */
4025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 int len;
4029
4030 len = (int)STRLEN(name) + 3;
4031 if (len > varnamebuflen)
4032 {
4033 vim_free(varnamebuf);
4034 len += 10; /* some additional space */
4035 varnamebuf = alloc(len);
4036 if (varnamebuf == NULL)
4037 {
4038 varnamebuflen = 0;
4039 return NULL;
4040 }
4041 varnamebuflen = len;
4042 }
4043 *varnamebuf = prefix;
4044 varnamebuf[1] = ':';
4045 STRCPY(varnamebuf + 2, name);
4046 return varnamebuf;
4047}
4048
4049/*
4050 * Function given to ExpandGeneric() to obtain the list of user defined
4051 * (global/buffer/window/built-in) variable names.
4052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004054get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 static long_u gdone;
4057 static long_u bdone;
4058 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 static long_u tdone;
4061#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 static int vidx;
4063 static hashitem_T *hi;
4064 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 tdone = 0;
4071#endif
4072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* Global variables */
4075 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004079 else
4080 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004081 while (HASHITEM_EMPTY(hi))
4082 ++hi;
4083 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4084 return cat_prefix_varname('g', hi->hi_key);
4085 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
4088 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004089 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004102 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return (char_u *)"b:changedtick";
4104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105
4106 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004110 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004112 else
4113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004118
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119#ifdef FEAT_WINDOWS
4120 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004121 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 if (tdone < ht->ht_used)
4123 {
4124 if (tdone++ == 0)
4125 hi = ht->ht_array;
4126 else
4127 ++hi;
4128 while (HASHITEM_EMPTY(hi))
4129 ++hi;
4130 return cat_prefix_varname('t', hi->hi_key);
4131 }
4132#endif
4133
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 /* v: variables */
4135 if (vidx < VV_LEN)
4136 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 vim_free(varnamebuf);
4139 varnamebuf = NULL;
4140 varnamebuflen = 0;
4141 return NULL;
4142}
4143
4144#endif /* FEAT_CMDL_COMPL */
4145
4146/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004147 * Return TRUE if "pat" matches "text".
4148 * Does not use 'cpo' and always uses 'magic'.
4149 */
4150 static int
4151pattern_match(char_u *pat, char_u *text, int ic)
4152{
4153 int matches = FALSE;
4154 char_u *save_cpo;
4155 regmatch_T regmatch;
4156
4157 /* avoid 'l' flag in 'cpoptions' */
4158 save_cpo = p_cpo;
4159 p_cpo = (char_u *)"";
4160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4161 if (regmatch.regprog != NULL)
4162 {
4163 regmatch.rm_ic = ic;
4164 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4165 vim_regfree(regmatch.regprog);
4166 }
4167 p_cpo = save_cpo;
4168 return matches;
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * types for expressions.
4173 */
4174typedef enum
4175{
4176 TYPE_UNKNOWN = 0
4177 , TYPE_EQUAL /* == */
4178 , TYPE_NEQUAL /* != */
4179 , TYPE_GREATER /* > */
4180 , TYPE_GEQUAL /* >= */
4181 , TYPE_SMALLER /* < */
4182 , TYPE_SEQUAL /* <= */
4183 , TYPE_MATCH /* =~ */
4184 , TYPE_NOMATCH /* !~ */
4185} exptype_T;
4186
4187/*
4188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4191 */
4192
4193/*
4194 * Handle zero level expression.
4195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval0(
4202 char_u *arg,
4203 typval_T *rettv,
4204 char_u **nextcmd,
4205 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 int ret;
4208 char_u *p;
4209
4210 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (ret == FAIL || !ends_excmd(*p))
4213 {
4214 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /*
4217 * Report the invalid expression unless the expression evaluation has
4218 * been cancelled due to an aborting error, an interrupt, or an
4219 * exception.
4220 */
4221 if (!aborting())
4222 EMSG2(_(e_invexpr2), arg);
4223 ret = FAIL;
4224 }
4225 if (nextcmd != NULL)
4226 *nextcmd = check_nextcmd(p);
4227
4228 return ret;
4229}
4230
4231/*
4232 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004233 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004238 * Note: "rettv.v_lock" is not set.
4239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Return OK or FAIL.
4241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 if ((*arg)[0] == '?')
4255 {
4256 result = FALSE;
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
4260
4261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return FAIL;
4274
4275 /*
4276 * Check for the ":".
4277 */
4278 if ((*arg)[0] != ':')
4279 {
4280 EMSG(_("E109: Missing ':' after '?'"));
4281 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284 }
4285
4286 /*
4287 * Get the third variable.
4288 */
4289 *arg = skipwhite(*arg + 1);
4290 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4291 {
4292 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return FAIL;
4295 }
4296 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle first level expression:
4305 * expr2 || expr2 || expr2 logical OR
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 long result;
4317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Get the first variable.
4322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return FAIL;
4325
4326 /*
4327 * Repeat until there is no following "||".
4328 */
4329 first = TRUE;
4330 result = FALSE;
4331 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4332 {
4333 if (evaluate && first)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 first = FALSE;
4341 }
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(*arg + 2);
4347 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4348 return FAIL;
4349
4350 /*
4351 * Compute the result.
4352 */
4353 if (evaluate && !result)
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle second level expression:
4373 * expr3 && expr3 && expr3 logical AND
4374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 long result;
4385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 /*
4389 * Get the first variable.
4390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393
4394 /*
4395 * Repeat until there is no following "&&".
4396 */
4397 first = TRUE;
4398 result = TRUE;
4399 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4400 {
4401 if (evaluate && first)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
4407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 first = FALSE;
4409 }
4410
4411 /*
4412 * Get the second variable.
4413 */
4414 *arg = skipwhite(*arg + 2);
4415 if (eval4(arg, &var2, evaluate && result) == FAIL)
4416 return FAIL;
4417
4418 /*
4419 * Compute the result.
4420 */
4421 if (evaluate && result)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 if (error)
4427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 if (evaluate)
4430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 rettv->v_type = VAR_NUMBER;
4432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 }
4435
4436 return OK;
4437}
4438
4439/*
4440 * Handle third level expression:
4441 * var1 == var2
4442 * var1 =~ var2
4443 * var1 != var2
4444 * var1 !~ var2
4445 * var1 > var2
4446 * var1 >= var2
4447 * var1 < var2
4448 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 * var1 is var2
4450 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 *
4452 * "arg" must point to the first non-white of the expression.
4453 * "arg" is advanced to the next non-white after the recognized expression.
4454 *
4455 * Return OK or FAIL.
4456 */
4457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *p;
4462 int i;
4463 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int len = 2;
4466 long n1, n2;
4467 char_u *s1, *s2;
4468 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
4472 * Get the first variable.
4473 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476
4477 p = *arg;
4478 switch (p[0])
4479 {
4480 case '=': if (p[1] == '=')
4481 type = TYPE_EQUAL;
4482 else if (p[1] == '~')
4483 type = TYPE_MATCH;
4484 break;
4485 case '!': if (p[1] == '=')
4486 type = TYPE_NEQUAL;
4487 else if (p[1] == '~')
4488 type = TYPE_NOMATCH;
4489 break;
4490 case '>': if (p[1] != '=')
4491 {
4492 type = TYPE_GREATER;
4493 len = 1;
4494 }
4495 else
4496 type = TYPE_GEQUAL;
4497 break;
4498 case '<': if (p[1] != '=')
4499 {
4500 type = TYPE_SMALLER;
4501 len = 1;
4502 }
4503 else
4504 type = TYPE_SEQUAL;
4505 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 case 'i': if (p[1] == 's')
4507 {
4508 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4509 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004510 i = p[len];
4511 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 {
4513 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4514 type_is = TRUE;
4515 }
4516 }
4517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
4520 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004521 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 if (type != TYPE_UNKNOWN)
4524 {
4525 /* extra question mark appended: ignore case */
4526 if (p[len] == '?')
4527 {
4528 ic = TRUE;
4529 ++len;
4530 }
4531 /* extra '#' appended: match case */
4532 else if (p[len] == '#')
4533 {
4534 ic = FALSE;
4535 ++len;
4536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 ic = p_ic;
4540
4541 /*
4542 * Get the second variable.
4543 */
4544 *arg = skipwhite(p + len);
4545 if (eval5(arg, &var2, evaluate) == FAIL)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return FAIL;
4549 }
4550
4551 if (evaluate)
4552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 if (type_is && rettv->v_type != var2.v_type)
4554 {
4555 /* For "is" a different type always means FALSE, for "notis"
4556 * it means TRUE. */
4557 n1 = (type == TYPE_NEQUAL);
4558 }
4559 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4560 {
4561 if (type_is)
4562 {
4563 n1 = (rettv->v_type == var2.v_type
4564 && rettv->vval.v_list == var2.vval.v_list);
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 else if (rettv->v_type != var2.v_type
4569 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4570 {
4571 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004572 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004574 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
4579 else
4580 {
4581 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004582 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4583 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 }
4588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4590 {
4591 if (type_is)
4592 {
4593 n1 = (rettv->v_type == var2.v_type
4594 && rettv->vval.v_dict == var2.vval.v_dict);
4595 if (type == TYPE_NEQUAL)
4596 n1 = !n1;
4597 }
4598 else if (rettv->v_type != var2.v_type
4599 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4600 {
4601 if (rettv->v_type != var2.v_type)
4602 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4603 else
4604 EMSG(_("E736: Invalid operation for Dictionary"));
4605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 else
4610 {
4611 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004612 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4613 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 }
4618
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004619 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4620 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004621 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004622 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004624 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 clear_tv(rettv);
4626 clear_tv(&var2);
4627 return FAIL;
4628 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004630 if (type == TYPE_NEQUAL)
4631 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 }
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634#ifdef FEAT_FLOAT
4635 /*
4636 * If one of the two variables is a float, compare as a float.
4637 * When using "=~" or "!~", always compare as string.
4638 */
4639 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4640 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4641 {
4642 float_T f1, f2;
4643
4644 if (rettv->v_type == VAR_FLOAT)
4645 f1 = rettv->vval.v_float;
4646 else
4647 f1 = get_tv_number(rettv);
4648 if (var2.v_type == VAR_FLOAT)
4649 f2 = var2.vval.v_float;
4650 else
4651 f2 = get_tv_number(&var2);
4652 n1 = FALSE;
4653 switch (type)
4654 {
4655 case TYPE_EQUAL: n1 = (f1 == f2); break;
4656 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4657 case TYPE_GREATER: n1 = (f1 > f2); break;
4658 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4659 case TYPE_SMALLER: n1 = (f1 < f2); break;
4660 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4661 case TYPE_UNKNOWN:
4662 case TYPE_MATCH:
4663 case TYPE_NOMATCH: break; /* avoid gcc warning */
4664 }
4665 }
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * If one of the two variables is a number, compare as a number.
4670 * When using "=~" or "!~", always compare as string.
4671 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004672 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 n1 = get_tv_number(rettv);
4676 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 switch (type)
4678 {
4679 case TYPE_EQUAL: n1 = (n1 == n2); break;
4680 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4681 case TYPE_GREATER: n1 = (n1 > n2); break;
4682 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4683 case TYPE_SMALLER: n1 = (n1 < n2); break;
4684 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4685 case TYPE_UNKNOWN:
4686 case TYPE_MATCH:
4687 case TYPE_NOMATCH: break; /* avoid gcc warning */
4688 }
4689 }
4690 else
4691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 s1 = get_tv_string_buf(rettv, buf1);
4693 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4695 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4696 else
4697 i = 0;
4698 n1 = FALSE;
4699 switch (type)
4700 {
4701 case TYPE_EQUAL: n1 = (i == 0); break;
4702 case TYPE_NEQUAL: n1 = (i != 0); break;
4703 case TYPE_GREATER: n1 = (i > 0); break;
4704 case TYPE_GEQUAL: n1 = (i >= 0); break;
4705 case TYPE_SMALLER: n1 = (i < 0); break;
4706 case TYPE_SEQUAL: n1 = (i <= 0); break;
4707
4708 case TYPE_MATCH:
4709 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004710 n1 = pattern_match(s2, s1, ic);
4711 if (type == TYPE_NOMATCH)
4712 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714
4715 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4716 }
4717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 rettv->v_type = VAR_NUMBER;
4721 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723 }
4724
4725 return OK;
4726}
4727
4728/*
4729 * Handle fourth level expression:
4730 * + number addition
4731 * - number subtraction
4732 * . string concatenation
4733 *
4734 * "arg" must point to the first non-white of the expression.
4735 * "arg" is advanced to the next non-white after the recognized expression.
4736 *
4737 * Return OK or FAIL.
4738 */
4739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004740eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar33570922005-01-25 22:26:29 +00004742 typval_T var2;
4743 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 int op;
4745 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 float_T f1 = 0, f2 = 0;
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *s1, *s2;
4750 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4751 char_u *p;
4752
4753 /*
4754 * Get the first variable.
4755 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 /*
4760 * Repeat computing, until no '+', '-' or '.' is following.
4761 */
4762 for (;;)
4763 {
4764 op = **arg;
4765 if (op != '+' && op != '-' && op != '.')
4766 break;
4767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 if ((op != '+' || rettv->v_type != VAR_LIST)
4769#ifdef FEAT_FLOAT
4770 && (op == '.' || rettv->v_type != VAR_FLOAT)
4771#endif
4772 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
4774 /* For "list + ...", an illegal use of the first operand as
4775 * a number cannot be determined before evaluating the 2nd
4776 * operand: if this is also a list, all is ok.
4777 * For "something . ...", "something - ..." or "non-list + ...",
4778 * we know that the first operand needs to be a string or number
4779 * without evaluating the 2nd operand. So check before to avoid
4780 * side effects after an error. */
4781 if (evaluate && get_tv_string_chk(rettv) == NULL)
4782 {
4783 clear_tv(rettv);
4784 return FAIL;
4785 }
4786 }
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 /*
4789 * Get the second variable.
4790 */
4791 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
4797
4798 if (evaluate)
4799 {
4800 /*
4801 * Compute the result.
4802 */
4803 if (op == '.')
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4806 s2 = get_tv_string_buf_chk(&var2, buf2);
4807 if (s2 == NULL) /* type error ? */
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
4815 rettv->v_type = VAR_STRING;
4816 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004818 else if (op == '+' && rettv->v_type == VAR_LIST
4819 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004820 {
4821 /* concatenate Lists */
4822 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4823 &var3) == FAIL)
4824 {
4825 clear_tv(rettv);
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 clear_tv(rettv);
4830 *rettv = var3;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else
4833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 int error = FALSE;
4835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 f1 = rettv->vval.v_float;
4840 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 else
4843#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 n1 = get_tv_number_chk(rettv, &error);
4846 if (error)
4847 {
4848 /* This can only happen for "list + non-list". For
4849 * "non-list + ..." or "something - ...", we returned
4850 * before evaluating the 2nd operand. */
4851 clear_tv(rettv);
4852 return FAIL;
4853 }
4854#ifdef FEAT_FLOAT
4855 if (var2.v_type == VAR_FLOAT)
4856 f1 = n1;
4857#endif
4858 }
4859#ifdef FEAT_FLOAT
4860 if (var2.v_type == VAR_FLOAT)
4861 {
4862 f2 = var2.vval.v_float;
4863 n2 = 0;
4864 }
4865 else
4866#endif
4867 {
4868 n2 = get_tv_number_chk(&var2, &error);
4869 if (error)
4870 {
4871 clear_tv(rettv);
4872 clear_tv(&var2);
4873 return FAIL;
4874 }
4875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 f2 = n2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881
4882#ifdef FEAT_FLOAT
4883 /* If there is a float on either side the result is a float. */
4884 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4885 {
4886 if (op == '+')
4887 f1 = f1 + f2;
4888 else
4889 f1 = f1 - f2;
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
4895 {
4896 if (op == '+')
4897 n1 = n1 + n2;
4898 else
4899 n1 = n1 - n2;
4900 rettv->v_type = VAR_NUMBER;
4901 rettv->vval.v_number = n1;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907 return OK;
4908}
4909
4910/*
4911 * Handle fifth level expression:
4912 * * number multiplication
4913 * / number division
4914 * % number modulo
4915 *
4916 * "arg" must point to the first non-white of the expression.
4917 * "arg" is advanced to the next non-white after the recognized expression.
4918 *
4919 * Return OK or FAIL.
4920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922eval6(
4923 char_u **arg,
4924 typval_T *rettv,
4925 int evaluate,
4926 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int op;
4930 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 int use_float = FALSE;
4933 float_T f1 = 0, f2;
4934#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 /*
4938 * Get the first variable.
4939 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004940 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942
4943 /*
4944 * Repeat computing, until no '*', '/' or '%' is following.
4945 */
4946 for (;;)
4947 {
4948 op = **arg;
4949 if (op != '*' && op != '/' && op != '%')
4950 break;
4951
4952 if (evaluate)
4953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954#ifdef FEAT_FLOAT
4955 if (rettv->v_type == VAR_FLOAT)
4956 {
4957 f1 = rettv->vval.v_float;
4958 use_float = TRUE;
4959 n1 = 0;
4960 }
4961 else
4962#endif
4963 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 if (error)
4966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
4969 n1 = 0;
4970
4971 /*
4972 * Get the second variable.
4973 */
4974 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004975 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return FAIL;
4977
4978 if (evaluate)
4979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (var2.v_type == VAR_FLOAT)
4982 {
4983 if (!use_float)
4984 {
4985 f1 = n1;
4986 use_float = TRUE;
4987 }
4988 f2 = var2.vval.v_float;
4989 n2 = 0;
4990 }
4991 else
4992#endif
4993 {
4994 n2 = get_tv_number_chk(&var2, &error);
4995 clear_tv(&var2);
4996 if (error)
4997 return FAIL;
4998#ifdef FEAT_FLOAT
4999 if (use_float)
5000 f2 = n2;
5001#endif
5002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008#ifdef FEAT_FLOAT
5009 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 f1 = f1 * f2;
5013 else if (op == '/')
5014 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005015# ifdef VMS
5016 /* VMS crashes on divide by zero, work around it */
5017 if (f2 == 0.0)
5018 {
5019 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005020 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005021 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005022 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005024 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005025 }
5026 else
5027 f1 = f1 / f2;
5028# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 /* We rely on the floating point library to handle divide
5030 * by zero to result in "inf" and not a crash. */
5031 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005032# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005036 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 return FAIL;
5038 }
5039 rettv->v_type = VAR_FLOAT;
5040 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 n1 = n1 * n2;
5047 else if (op == '/')
5048 {
5049 if (n2 == 0) /* give an error message? */
5050 {
5051 if (n1 == 0)
5052 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5053 else if (n1 < 0)
5054 n1 = -0x7fffffffL;
5055 else
5056 n1 = 0x7fffffffL;
5057 }
5058 else
5059 n1 = n1 / n2;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 {
5063 if (n2 == 0) /* give an error message? */
5064 n1 = 0;
5065 else
5066 n1 = n1 % n2;
5067 }
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 return OK;
5075}
5076
5077/*
5078 * Handle sixth level expression:
5079 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005080 * "string" string constant
5081 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * &option-name option value
5083 * @r register contents
5084 * identifier variable value
5085 * function() function call
5086 * $VAR environment variable
5087 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005088 * [expr, expr] List
5089 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *
5091 * Also handle:
5092 * ! in front logical NOT
5093 * - in front unary minus
5094 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 * trailing [] subscript in String or List
5096 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *
5098 * "arg" must point to the first non-white of the expression.
5099 * "arg" is advanced to the next non-white after the recognized expression.
5100 *
5101 * Return OK or FAIL.
5102 */
5103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104eval7(
5105 char_u **arg,
5106 typval_T *rettv,
5107 int evaluate,
5108 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 long n;
5111 int len;
5112 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 char_u *start_leader, *end_leader;
5114 int ret = OK;
5115 char_u *alias;
5116
5117 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 /*
5124 * Skip '!' and '-' characters. They are handled later.
5125 */
5126 start_leader = *arg;
5127 while (**arg == '!' || **arg == '-' || **arg == '+')
5128 *arg = skipwhite(*arg + 1);
5129 end_leader = *arg;
5130
5131 switch (**arg)
5132 {
5133 /*
5134 * Number constant.
5135 */
5136 case '0':
5137 case '1':
5138 case '2':
5139 case '3':
5140 case '4':
5141 case '5':
5142 case '6':
5143 case '7':
5144 case '8':
5145 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 {
5147#ifdef FEAT_FLOAT
5148 char_u *p = skipdigits(*arg + 1);
5149 int get_float = FALSE;
5150
5151 /* We accept a float when the format matches
5152 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005153 * strict to avoid backwards compatibility problems.
5154 * Don't look for a float after the "." operator, so that
5155 * ":let vers = 1.2.3" doesn't fail. */
5156 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 get_float = TRUE;
5159 p = skipdigits(p + 2);
5160 if (*p == 'e' || *p == 'E')
5161 {
5162 ++p;
5163 if (*p == '-' || *p == '+')
5164 ++p;
5165 if (!vim_isdigit(*p))
5166 get_float = FALSE;
5167 else
5168 p = skipdigits(p + 1);
5169 }
5170 if (ASCII_ISALPHA(*p) || *p == '.')
5171 get_float = FALSE;
5172 }
5173 if (get_float)
5174 {
5175 float_T f;
5176
5177 *arg += string2float(*arg, &f);
5178 if (evaluate)
5179 {
5180 rettv->v_type = VAR_FLOAT;
5181 rettv->vval.v_float = f;
5182 }
5183 }
5184 else
5185#endif
5186 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005187 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 *arg += len;
5189 if (evaluate)
5190 {
5191 rettv->v_type = VAR_NUMBER;
5192 rettv->vval.v_number = n;
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 /*
5199 * String constant: "string".
5200 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 break;
5209
5210 /*
5211 * List: [expr, expr]
5212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215
5216 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 * Dictionary: {key: val, key: val}
5218 */
5219 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5220 break;
5221
5222 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005223 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227
5228 /*
5229 * Environment variable: $VAR.
5230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233
5234 /*
5235 * Register contents: @r.
5236 */
5237 case '@': ++*arg;
5238 if (evaluate)
5239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005241 rettv->vval.v_string = get_reg_contents(**arg,
5242 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 if (**arg != NUL)
5245 ++*arg;
5246 break;
5247
5248 /*
5249 * nested expression: (expression).
5250 */
5251 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (**arg == ')')
5254 ++*arg;
5255 else if (ret == OK)
5256 {
5257 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ret = FAIL;
5260 }
5261 break;
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
5265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266
5267 if (ret == NOTDONE)
5268 {
5269 /*
5270 * Must be a variable or function name.
5271 * Can also be a curly-braces kind of name: {expr}.
5272 */
5273 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 if (alias != NULL)
5276 s = alias;
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 ret = FAIL;
5280 else
5281 {
5282 if (**arg == '(') /* recursive! */
5283 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 partial_T *partial;
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /* If "s" is the name of a variable of type VAR_FUNC
5287 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005288 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289
5290 /* Invoke the function. */
5291 ret = get_func_tv(s, len, rettv, arg,
5292 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005293 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005294
5295 /* If evaluate is FALSE rettv->v_type was not set in
5296 * get_func_tv, but it's needed in handle_subscript() to parse
5297 * what follows. So set it here. */
5298 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5299 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005300 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005301 rettv->v_type = VAR_FUNC;
5302 }
5303
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /* Stop the expression evaluation when immediately
5305 * aborting on error, or when an interrupt occurred or
5306 * an exception was thrown but not caught. */
5307 if (aborting())
5308 {
5309 if (ret == OK)
5310 clear_tv(rettv);
5311 ret = FAIL;
5312 }
5313 }
5314 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005315 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 else
5317 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005319 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 }
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 *arg = skipwhite(*arg);
5323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5325 * expr(expr). */
5326 if (ret == OK)
5327 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5331 */
5332 if (ret == OK && evaluate && end_leader > start_leader)
5333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005335 int val = 0;
5336#ifdef FEAT_FLOAT
5337 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 if (rettv->v_type == VAR_FLOAT)
5340 f = rettv->vval.v_float;
5341 else
5342#endif
5343 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 clear_tv(rettv);
5347 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else
5350 {
5351 while (end_leader > start_leader)
5352 {
5353 --end_leader;
5354 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 {
5356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 f = !f;
5359 else
5360#endif
5361 val = !val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364 {
5365#ifdef FEAT_FLOAT
5366 if (rettv->v_type == VAR_FLOAT)
5367 f = -f;
5368 else
5369#endif
5370 val = -val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005373#ifdef FEAT_FLOAT
5374 if (rettv->v_type == VAR_FLOAT)
5375 {
5376 clear_tv(rettv);
5377 rettv->vval.v_float = f;
5378 }
5379 else
5380#endif
5381 {
5382 clear_tv(rettv);
5383 rettv->v_type = VAR_NUMBER;
5384 rettv->vval.v_number = val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
5389 return ret;
5390}
5391
5392/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005393 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5394 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5396 */
5397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398eval_index(
5399 char_u **arg,
5400 typval_T *rettv,
5401 int evaluate,
5402 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403{
5404 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 long len = -1;
5408 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005415 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 if (verbose)
5417 EMSG(_("E695: Cannot index a Funcref"));
5418 return FAIL;
5419 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005420#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 if (verbose)
5422 EMSG(_(e_float_as_string));
5423 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005424#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005425 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005427 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 if (verbose)
5429 EMSG(_("E909: Cannot index a special variable"));
5430 return FAIL;
5431 case VAR_UNKNOWN:
5432 if (evaluate)
5433 return FAIL;
5434 /* FALLTHROUGH */
5435
5436 case VAR_STRING:
5437 case VAR_NUMBER:
5438 case VAR_LIST:
5439 case VAR_DICT:
5440 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005443 init_tv(&var1);
5444 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 /*
5448 * dict.name
5449 */
5450 key = *arg + 1;
5451 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5452 ;
5453 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 /*
5460 * something[idx]
5461 *
5462 * Get the (first) variable from inside the [].
5463 */
5464 *arg = skipwhite(*arg + 1);
5465 if (**arg == ':')
5466 empty1 = TRUE;
5467 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5468 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005469 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5470 {
5471 /* not a number or string */
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475
5476 /*
5477 * Get the second variable from inside the [:].
5478 */
5479 if (**arg == ':')
5480 {
5481 range = TRUE;
5482 *arg = skipwhite(*arg + 1);
5483 if (**arg == ']')
5484 empty2 = TRUE;
5485 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005487 if (!empty1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5492 {
5493 /* not a number or string */
5494 if (!empty1)
5495 clear_tv(&var1);
5496 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 return FAIL;
5498 }
5499 }
5500
5501 /* Check for the ']'. */
5502 if (**arg != ']')
5503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 if (verbose)
5505 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 clear_tv(&var1);
5507 if (range)
5508 clear_tv(&var2);
5509 return FAIL;
5510 }
5511 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513
5514 if (evaluate)
5515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 n1 = 0;
5517 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n1 = get_tv_number(&var1);
5520 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 if (range)
5523 {
5524 if (empty2)
5525 n2 = -1;
5526 else
5527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 n2 = get_tv_number(&var2);
5529 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005537 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 case VAR_SPECIAL:
5540 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005541 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005542 break; /* not evaluating, skipping over subscript */
5543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 case VAR_NUMBER:
5545 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 len = (long)STRLEN(s);
5548 if (range)
5549 {
5550 /* The resulting variable is a substring. If the indexes
5551 * are out of range the result is empty. */
5552 if (n1 < 0)
5553 {
5554 n1 = len + n1;
5555 if (n1 < 0)
5556 n1 = 0;
5557 }
5558 if (n2 < 0)
5559 n2 = len + n2;
5560 else if (n2 >= len)
5561 n2 = len;
5562 if (n1 >= len || n2 < 0 || n1 > n2)
5563 s = NULL;
5564 else
5565 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5566 }
5567 else
5568 {
5569 /* The resulting variable is a string of a single
5570 * character. If the index is too big or negative the
5571 * result is empty. */
5572 if (n1 >= len || n1 < 0)
5573 s = NULL;
5574 else
5575 s = vim_strnsave(s + n1, 1);
5576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 break;
5581
5582 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 if (n1 < 0)
5585 n1 = len + n1;
5586 if (!empty1 && (n1 < 0 || n1 >= len))
5587 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 /* For a range we allow invalid values and return an empty
5589 * list. A list index out of range is an error. */
5590 if (!range)
5591 {
5592 if (verbose)
5593 EMSGN(_(e_listidx), n1);
5594 return FAIL;
5595 }
5596 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 }
5598 if (range)
5599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602
5603 if (n2 < 0)
5604 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005605 else if (n2 >= len)
5606 n2 = len - 1;
5607 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005608 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 l = list_alloc();
5610 if (l == NULL)
5611 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 n1 <= n2; ++n1)
5614 {
5615 if (list_append_tv(l, &item->li_tv) == FAIL)
5616 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 return FAIL;
5619 }
5620 item = item->li_next;
5621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 clear_tv(rettv);
5623 rettv->v_type = VAR_LIST;
5624 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005625 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 else
5628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005629 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 clear_tv(rettv);
5631 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634
5635 case VAR_DICT:
5636 if (range)
5637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005638 if (verbose)
5639 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (len == -1)
5641 clear_tv(&var1);
5642 return FAIL;
5643 }
5644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646
5647 if (len == -1)
5648 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005649 key = get_tv_string_chk(&var1);
5650 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 clear_tv(&var1);
5653 return FAIL;
5654 }
5655 }
5656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005660 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (len == -1)
5662 clear_tv(&var1);
5663 if (item == NULL)
5664 return FAIL;
5665
5666 copy_tv(&item->di_tv, &var1);
5667 clear_tv(rettv);
5668 *rettv = var1;
5669 }
5670 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 }
5672 }
5673
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Get an option value.
5679 * "arg" points to the '&' or '+' before the option name.
5680 * "arg" is advanced to character after the option name.
5681 * Return OK or FAIL.
5682 */
5683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684get_option_tv(
5685 char_u **arg,
5686 typval_T *rettv, /* when NULL, only check if option exists */
5687 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 char_u *option_end;
5690 long numval;
5691 char_u *stringval;
5692 int opt_type;
5693 int c;
5694 int working = (**arg == '+'); /* has("+option") */
5695 int ret = OK;
5696 int opt_flags;
5697
5698 /*
5699 * Isolate the option name and find its value.
5700 */
5701 option_end = find_option_end(arg, &opt_flags);
5702 if (option_end == NULL)
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 EMSG2(_("E112: Option name missing: %s"), *arg);
5706 return FAIL;
5707 }
5708
5709 if (!evaluate)
5710 {
5711 *arg = option_end;
5712 return OK;
5713 }
5714
5715 c = *option_end;
5716 *option_end = NUL;
5717 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (opt_type == -3) /* invalid name */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 EMSG2(_("E113: Unknown option: %s"), *arg);
5724 ret = FAIL;
5725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (opt_type == -2) /* hidden string option */
5729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730 rettv->v_type = VAR_STRING;
5731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else if (opt_type == -1) /* hidden number option */
5734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 rettv->v_type = VAR_NUMBER;
5736 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else if (opt_type == 1) /* number option */
5739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 rettv->v_type = VAR_NUMBER;
5741 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 else /* string option */
5744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 }
5749 else if (working && (opt_type == -2 || opt_type == -1))
5750 ret = FAIL;
5751
5752 *option_end = c; /* put back for error messages */
5753 *arg = option_end;
5754
5755 return ret;
5756}
5757
5758/*
5759 * Allocate a variable for a string constant.
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 char_u *p;
5766 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int extra = 0;
5768
5769 /*
5770 * Find the end of the string, skipping backslashed characters.
5771 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (*p == '\\' && p[1] != NUL)
5775 {
5776 ++p;
5777 /* A "\<x>" form occupies at least 4 characters, and produces up
5778 * to 6 characters: reserve space for 2 extra */
5779 if (*p == '<')
5780 extra += 2;
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783
5784 if (*p != '"')
5785 {
5786 EMSG2(_("E114: Missing quote: %s"), *arg);
5787 return FAIL;
5788 }
5789
5790 /* If only parsing, set *arg and return here */
5791 if (!evaluate)
5792 {
5793 *arg = p + 1;
5794 return OK;
5795 }
5796
5797 /*
5798 * Copy the string into allocated memory, handling backslashed
5799 * characters.
5800 */
5801 name = alloc((unsigned)(p - *arg + extra));
5802 if (name == NULL)
5803 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 rettv->v_type = VAR_STRING;
5805 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (*p == '\\')
5810 {
5811 switch (*++p)
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case 'b': *name++ = BS; ++p; break;
5814 case 'e': *name++ = ESC; ++p; break;
5815 case 'f': *name++ = FF; ++p; break;
5816 case 'n': *name++ = NL; ++p; break;
5817 case 'r': *name++ = CAR; ++p; break;
5818 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 case 'X': /* hex: "\x1", "\x12" */
5821 case 'x':
5822 case 'u': /* Unicode: "\u0023" */
5823 case 'U':
5824 if (vim_isxdigit(p[1]))
5825 {
5826 int n, nr;
5827 int c = toupper(*p);
5828
5829 if (c == 'X')
5830 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005831 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005833 else
5834 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 nr = 0;
5836 while (--n >= 0 && vim_isxdigit(p[1]))
5837 {
5838 ++p;
5839 nr = (nr << 4) + hex2nr(*p);
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef FEAT_MBYTE
5843 /* For "\u" store the number according to
5844 * 'encoding'. */
5845 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852
5853 /* octal: "\1", "\12", "\123" */
5854 case '0':
5855 case '1':
5856 case '2':
5857 case '3':
5858 case '4':
5859 case '5':
5860 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 case '7': *name = *p++ - '0';
5862 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *name = (*name << 3) + *p++ - '0';
5865 if (*p >= '0' && *p <= '7')
5866 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 break;
5870
5871 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (extra != 0)
5874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 }
5878 /* FALLTHROUGH */
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 }
5883 }
5884 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 *arg = p + 1;
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
5894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Return OK or FAIL.
5897 */
5898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005899get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 int reduce = 0;
5904
5905 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 */
5908 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5909 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 break;
5914 ++reduce;
5915 ++p;
5916 }
5917 }
5918
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 return FAIL;
5923 }
5924
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 if (!evaluate)
5927 {
5928 *arg = p + 1;
5929 return OK;
5930 }
5931
5932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 */
5935 str = alloc((unsigned)((p - *arg) - reduce));
5936 if (str == NULL)
5937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 rettv->v_type = VAR_STRING;
5939 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 break;
5947 ++p;
5948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 *arg = p + 1;
5953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 return OK;
5955}
5956
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005959{
5960 int i;
5961
5962 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005964 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966 func_unref(pt->pt_name);
5967 vim_free(pt->pt_name);
5968 vim_free(pt);
5969}
5970
5971/*
5972 * Unreference a closure: decrement the reference count and free it when it
5973 * becomes zero.
5974 */
5975 void
5976partial_unref(partial_T *pt)
5977{
5978 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980}
5981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 * Allocate a variable for a List and fill it from "*arg".
5984 * Return OK or FAIL.
5985 */
5986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l = NULL;
5990 typval_T tv;
5991 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992
5993 if (evaluate)
5994 {
5995 l = list_alloc();
5996 if (l == NULL)
5997 return FAIL;
5998 }
5999
6000 *arg = skipwhite(*arg + 1);
6001 while (**arg != ']' && **arg != NUL)
6002 {
6003 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6004 goto failret;
6005 if (evaluate)
6006 {
6007 item = listitem_alloc();
6008 if (item != NULL)
6009 {
6010 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006011 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 list_append(l, item);
6013 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 else
6015 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
6017
6018 if (**arg == ']')
6019 break;
6020 if (**arg != ',')
6021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 goto failret;
6024 }
6025 *arg = skipwhite(*arg + 1);
6026 }
6027
6028 if (**arg != ']')
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031failret:
6032 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006033 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 return FAIL;
6035 }
6036
6037 *arg = skipwhite(*arg + 1);
6038 if (evaluate)
6039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 rettv->v_type = VAR_LIST;
6041 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 ++l->lv_refcount;
6043 }
6044
6045 return OK;
6046}
6047
6048/*
6049 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006050 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 list_T *l;
6056
6057 l = (list_T *)alloc_clear(sizeof(list_T));
6058 if (l != NULL)
6059 {
6060 /* Prepend the list to the list of lists for garbage collection. */
6061 if (first_list != NULL)
6062 first_list->lv_used_prev = l;
6063 l->lv_used_prev = NULL;
6064 l->lv_used_next = first_list;
6065 first_list = l;
6066 }
6067 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068}
6069
6070/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006071 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006072 * Returns OK or FAIL.
6073 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076{
6077 list_T *l = list_alloc();
6078
6079 if (l == NULL)
6080 return FAIL;
6081
6082 rettv->vval.v_list = l;
6083 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006084 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006085 ++l->lv_refcount;
6086 return OK;
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Unreference a list: decrement the reference count and free it when it
6091 * becomes zero.
6092 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006096 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006101 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 * Ignores the reference count.
6103 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 static void
6105list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006109 for (item = l->lv_first; item != NULL; item = l->lv_first)
6110 {
6111 /* Remove the item before deleting it. */
6112 l->lv_first = item->li_next;
6113 clear_tv(&item->li_tv);
6114 vim_free(item);
6115 }
6116}
6117
6118 static void
6119list_free_list(list_T *l)
6120{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Remove the list from the list of lists for garbage collection. */
6122 if (l->lv_used_prev == NULL)
6123 first_list = l->lv_used_next;
6124 else
6125 l->lv_used_prev->lv_used_next = l->lv_used_next;
6126 if (l->lv_used_next != NULL)
6127 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 vim_free(l);
6130}
6131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 void
6133list_free(list_T *l)
6134{
6135 if (!in_free_unref_items)
6136 {
6137 list_free_contents(l);
6138 list_free_list(l);
6139 }
6140}
6141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142/*
6143 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006144 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006146 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006147listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150}
6151
6152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006153 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 vim_free(item);
6160}
6161
6162/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006163 * Remove a list item from a List and free it. Also clears the value.
6164 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006167{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006168 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006169 listitem_free(item);
6170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Get the number of items in a list.
6174 */
6175 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 if (l == NULL)
6179 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181}
6182
6183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * Return TRUE when two lists have exactly the same values.
6185 */
6186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187list_equal(
6188 list_T *l1,
6189 list_T *l2,
6190 int ic, /* ignore case for strings */
6191 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006195 if (l1 == NULL || l2 == NULL)
6196 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 if (l1 == l2)
6198 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006199 if (list_len(l1) != list_len(l2))
6200 return FALSE;
6201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 for (item1 = l1->lv_first, item2 = l2->lv_first;
6203 item1 != NULL && item2 != NULL;
6204 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 return FALSE;
6207 return item1 == NULL && item2 == NULL;
6208}
6209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210/*
6211 * Return the dictitem that an entry in a hashtable points to.
6212 */
6213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215{
6216 return HI2DI(hi);
6217}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 * Return TRUE when two dictionaries have exactly the same key/values.
6221 */
6222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223dict_equal(
6224 dict_T *d1,
6225 dict_T *d2,
6226 int ic, /* ignore case for strings */
6227 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228{
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hashitem_T *hi;
6230 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006232
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006233 if (d1 == NULL || d2 == NULL)
6234 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006235 if (d1 == d2)
6236 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006237 if (dict_len(d1) != dict_len(d2))
6238 return FALSE;
6239
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006240 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006243 if (!HASHITEM_EMPTY(hi))
6244 {
6245 item2 = dict_find(d2, hi->hi_key, -1);
6246 if (item2 == NULL)
6247 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 return FALSE;
6250 --todo;
6251 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252 }
6253 return TRUE;
6254}
6255
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006256static int tv_equal_recurse_limit;
6257
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006258/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006260 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006261 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 */
6263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006264tv_equal(
6265 typval_T *tv1,
6266 typval_T *tv2,
6267 int ic, /* ignore case */
6268 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269{
6270 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006271 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006272 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006273 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006275 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6276 if ((tv1->v_type == VAR_FUNC
6277 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6278 && (tv2->v_type == VAR_FUNC
6279 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6280 {
6281 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6282 : tv1->vval.v_partial->pt_name;
6283 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6284 : tv2->vval.v_partial->pt_name;
6285 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6286 }
6287
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006288 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006289 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006290
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006291 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292 * recursiveness to a limit. We guess they are equal then.
6293 * A fixed limit has the problem of still taking an awful long time.
6294 * Reduce the limit every time running into it. That should work fine for
6295 * deeply linked structures that are not recursively linked and catch
6296 * recursiveness quickly. */
6297 if (!recursive)
6298 tv_equal_recurse_limit = 1000;
6299 if (recursive_cnt >= tv_equal_recurse_limit)
6300 {
6301 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006302 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006303 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006304
6305 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006307 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006308 ++recursive_cnt;
6309 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6310 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006311 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006312
6313 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006314 ++recursive_cnt;
6315 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6316 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006317 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006318
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006319 case VAR_NUMBER:
6320 return tv1->vval.v_number == tv2->vval.v_number;
6321
6322 case VAR_STRING:
6323 s1 = get_tv_string_buf(tv1, buf1);
6324 s2 = get_tv_string_buf(tv2, buf2);
6325 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006326
6327 case VAR_SPECIAL:
6328 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006329
6330 case VAR_FLOAT:
6331#ifdef FEAT_FLOAT
6332 return tv1->vval.v_float == tv2->vval.v_float;
6333#endif
6334 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006336 return tv1->vval.v_job == tv2->vval.v_job;
6337#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006338 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006340 return tv1->vval.v_channel == tv2->vval.v_channel;
6341#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006342 case VAR_FUNC:
6343 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006344 case VAR_UNKNOWN:
6345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006347
Bram Moolenaara03f2332016-02-06 18:09:59 +01006348 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6349 * does not equal anything, not even itself. */
6350 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351}
6352
6353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 * Locate item with index "n" in list "l" and return it.
6355 * A negative index is counted from the end; -1 is the last item.
6356 * Returns NULL when "n" is out of range.
6357 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006358 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 long idx;
6363
6364 if (l == NULL)
6365 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006366
6367 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 n = l->lv_len + n;
6370
6371 /* Check for index out of range. */
6372 if (n < 0 || n >= l->lv_len)
6373 return NULL;
6374
6375 /* When there is a cached index may start search from there. */
6376 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006378 if (n < l->lv_idx / 2)
6379 {
6380 /* closest to the start of the list */
6381 item = l->lv_first;
6382 idx = 0;
6383 }
6384 else if (n > (l->lv_idx + l->lv_len) / 2)
6385 {
6386 /* closest to the end of the list */
6387 item = l->lv_last;
6388 idx = l->lv_len - 1;
6389 }
6390 else
6391 {
6392 /* closest to the cached index */
6393 item = l->lv_idx_item;
6394 idx = l->lv_idx;
6395 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 }
6397 else
6398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 if (n < l->lv_len / 2)
6400 {
6401 /* closest to the start of the list */
6402 item = l->lv_first;
6403 idx = 0;
6404 }
6405 else
6406 {
6407 /* closest to the end of the list */
6408 item = l->lv_last;
6409 idx = l->lv_len - 1;
6410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006412
6413 while (n > idx)
6414 {
6415 /* search forward */
6416 item = item->li_next;
6417 ++idx;
6418 }
6419 while (n < idx)
6420 {
6421 /* search backward */
6422 item = item->li_prev;
6423 --idx;
6424 }
6425
6426 /* cache the used index */
6427 l->lv_idx = idx;
6428 l->lv_idx_item = item;
6429
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 return item;
6431}
6432
6433/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006434 * Get list item "l[idx]" as a number.
6435 */
6436 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_find_nr(
6438 list_T *l,
6439 long idx,
6440 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006441{
6442 listitem_T *li;
6443
6444 li = list_find(l, idx);
6445 if (li == NULL)
6446 {
6447 if (errorp != NULL)
6448 *errorp = TRUE;
6449 return -1L;
6450 }
6451 return get_tv_number_chk(&li->li_tv, errorp);
6452}
6453
6454/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6456 */
6457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006459{
6460 listitem_T *li;
6461
6462 li = list_find(l, idx - 1);
6463 if (li == NULL)
6464 {
6465 EMSGN(_(e_listidx), idx);
6466 return NULL;
6467 }
6468 return get_tv_string(&li->li_tv);
6469}
6470
6471/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006472 * Locate "item" list "l" and return its index.
6473 * Returns -1 when "item" is not in the list.
6474 */
6475 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006477{
6478 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480
6481 if (l == NULL)
6482 return -1;
6483 idx = 0;
6484 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6485 ++idx;
6486 if (li == NULL)
6487 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006488 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006489}
6490
6491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Append item "item" to the end of list "l".
6493 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496{
6497 if (l->lv_last == NULL)
6498 {
6499 /* empty list */
6500 l->lv_first = item;
6501 l->lv_last = item;
6502 item->li_prev = NULL;
6503 }
6504 else
6505 {
6506 l->lv_last->li_next = item;
6507 item->li_prev = l->lv_last;
6508 l->lv_last = item;
6509 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 item->li_next = NULL;
6512}
6513
6514/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006521 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006525 copy_tv(tv, &li->li_tv);
6526 list_append(l, li);
6527 return OK;
6528}
6529
6530/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006531 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006532 * Return FAIL when out of memory.
6533 */
6534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006535list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006536{
6537 listitem_T *li = listitem_alloc();
6538
6539 if (li == NULL)
6540 return FAIL;
6541 li->li_tv.v_type = VAR_DICT;
6542 li->li_tv.v_lock = 0;
6543 li->li_tv.vval.v_dict = dict;
6544 list_append(list, li);
6545 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 return OK;
6547}
6548
6549/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006550 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006551 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006552 * Returns FAIL when out of memory.
6553 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006556{
6557 listitem_T *li = listitem_alloc();
6558
6559 if (li == NULL)
6560 return FAIL;
6561 list_append(l, li);
6562 li->li_tv.v_type = VAR_STRING;
6563 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006564 if (str == NULL)
6565 li->li_tv.vval.v_string = NULL;
6566 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006567 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006568 return FAIL;
6569 return OK;
6570}
6571
6572/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006573 * Append "n" to list "l".
6574 * Returns FAIL when out of memory.
6575 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006578{
6579 listitem_T *li;
6580
6581 li = listitem_alloc();
6582 if (li == NULL)
6583 return FAIL;
6584 li->li_tv.v_type = VAR_NUMBER;
6585 li->li_tv.v_lock = 0;
6586 li->li_tv.vval.v_number = n;
6587 list_append(l, li);
6588 return OK;
6589}
6590
6591/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 * If "item" is NULL append at the end.
6594 * Return FAIL when out of memory.
6595 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600
6601 if (ni == NULL)
6602 return FAIL;
6603 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006604 list_insert(l, ni, item);
6605 return OK;
6606}
6607
6608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006610{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 if (item == NULL)
6612 /* Append new item at end of list. */
6613 list_append(l, ni);
6614 else
6615 {
6616 /* Insert new item before existing item. */
6617 ni->li_prev = item->li_prev;
6618 ni->li_next = item;
6619 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006621 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006622 ++l->lv_idx;
6623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006625 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006627 l->lv_idx_item = NULL;
6628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632}
6633
6634/*
6635 * Extend "l1" with "l2".
6636 * If "bef" is NULL append at the end, otherwise insert before this item.
6637 * Returns FAIL when out of memory.
6638 */
6639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006643 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006645 /* We also quit the loop when we have inserted the original item count of
6646 * the list, avoid a hang when we extend a list with itself. */
6647 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6649 return FAIL;
6650 return OK;
6651}
6652
6653/*
6654 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6655 * Return FAIL when out of memory.
6656 */
6657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006658list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659{
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006662 if (l1 == NULL || l2 == NULL)
6663 return FAIL;
6664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006666 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 if (l == NULL)
6668 return FAIL;
6669 tv->v_type = VAR_LIST;
6670 tv->vval.v_list = l;
6671
6672 /* append all items from the second list */
6673 return list_extend(l, l2, NULL);
6674}
6675
6676/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006677 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 * Returns NULL when out of memory.
6681 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684{
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 list_T *copy;
6686 listitem_T *item;
6687 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688
6689 if (orig == NULL)
6690 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691
6692 copy = list_alloc();
6693 if (copy != NULL)
6694 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 if (copyID != 0)
6696 {
6697 /* Do this before adding the items, because one of the items may
6698 * refer back to this list. */
6699 orig->lv_copyID = copyID;
6700 orig->lv_copylist = copy;
6701 }
6702 for (item = orig->lv_first; item != NULL && !got_int;
6703 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 {
6705 ni = listitem_alloc();
6706 if (ni == NULL)
6707 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006708 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006709 {
6710 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6711 {
6712 vim_free(ni);
6713 break;
6714 }
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006717 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718 list_append(copy, ni);
6719 }
6720 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006721 if (item != NULL)
6722 {
6723 list_unref(copy);
6724 copy = NULL;
6725 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006726 }
6727
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 return copy;
6729}
6730
6731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006733 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006734 * This used to be called list_remove, but that conflicts with a Sun header
6735 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006739{
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 /* notify watchers */
6743 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006745 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006746 list_fix_watch(l, ip);
6747 if (ip == item2)
6748 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006750
6751 if (item2->li_next == NULL)
6752 l->lv_last = item->li_prev;
6753 else
6754 item2->li_next->li_prev = item->li_prev;
6755 if (item->li_prev == NULL)
6756 l->lv_first = item2->li_next;
6757 else
6758 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006759 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760}
6761
6762/*
6763 * Return an allocated string with the string representation of a list.
6764 * May return NULL.
6765 */
6766 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006768{
6769 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770
6771 if (tv->vval.v_list == NULL)
6772 return NULL;
6773 ga_init2(&ga, (int)sizeof(char), 80);
6774 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006776 {
6777 vim_free(ga.ga_data);
6778 return NULL;
6779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 ga_append(&ga, ']');
6781 ga_append(&ga, NUL);
6782 return (char_u *)ga.ga_data;
6783}
6784
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785typedef struct join_S {
6786 char_u *s;
6787 char_u *tofree;
6788} join_T;
6789
6790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006791list_join_inner(
6792 garray_T *gap, /* to store the result in */
6793 list_T *l,
6794 char_u *sep,
6795 int echo_style,
6796 int copyID,
6797 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798{
6799 int i;
6800 join_T *p;
6801 int len;
6802 int sumlen = 0;
6803 int first = TRUE;
6804 char_u *tofree;
6805 char_u numbuf[NUMBUFLEN];
6806 listitem_T *item;
6807 char_u *s;
6808
6809 /* Stringify each item in the list. */
6810 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6811 {
6812 if (echo_style)
6813 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6814 else
6815 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6816 if (s == NULL)
6817 return FAIL;
6818
6819 len = (int)STRLEN(s);
6820 sumlen += len;
6821
Bram Moolenaarcde88542015-08-11 19:14:00 +02006822 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006823 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6824 if (tofree != NULL || s != numbuf)
6825 {
6826 p->s = s;
6827 p->tofree = tofree;
6828 }
6829 else
6830 {
6831 p->s = vim_strnsave(s, len);
6832 p->tofree = p->s;
6833 }
6834
6835 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006836 if (did_echo_string_emsg) /* recursion error, bail out */
6837 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006838 }
6839
6840 /* Allocate result buffer with its total size, avoid re-allocation and
6841 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6842 if (join_gap->ga_len >= 2)
6843 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6844 if (ga_grow(gap, sumlen + 2) == FAIL)
6845 return FAIL;
6846
6847 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6848 {
6849 if (first)
6850 first = FALSE;
6851 else
6852 ga_concat(gap, sep);
6853 p = ((join_T *)join_gap->ga_data) + i;
6854
6855 if (p->s != NULL)
6856 ga_concat(gap, p->s);
6857 line_breakcheck();
6858 }
6859
6860 return OK;
6861}
6862
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006863/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006864 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006865 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006866 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006867 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869list_join(
6870 garray_T *gap,
6871 list_T *l,
6872 char_u *sep,
6873 int echo_style,
6874 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006876 garray_T join_ga;
6877 int retval;
6878 join_T *p;
6879 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006880
Bram Moolenaard39a7512015-04-16 22:51:22 +02006881 if (l->lv_len < 1)
6882 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6884 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6885
6886 /* Dispose each item in join_ga. */
6887 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006888 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006889 p = (join_T *)join_ga.ga_data;
6890 for (i = 0; i < join_ga.ga_len; ++i)
6891 {
6892 vim_free(p->tofree);
6893 ++p;
6894 }
6895 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897
6898 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899}
6900
6901/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006902 * Return the next (unique) copy ID.
6903 * Used for serializing nested structures.
6904 */
6905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006906get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006907{
6908 current_copyID += COPYID_INC;
6909 return current_copyID;
6910}
6911
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006912/* Used by get_func_tv() */
6913static garray_T funcargs = GA_EMPTY;
6914
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006915/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 * Garbage collection for lists and dictionaries.
6917 *
6918 * We use reference counts to be able to free most items right away when they
6919 * are no longer used. But for composite items it's possible that it becomes
6920 * unused while the reference count is > 0: When there is a recursive
6921 * reference. Example:
6922 * :let l = [1, 2, 3]
6923 * :let d = {9: l}
6924 * :let l[1] = d
6925 *
6926 * Since this is quite unusual we handle this with garbage collection: every
6927 * once in a while find out which lists and dicts are not referenced from any
6928 * variable.
6929 *
6930 * Here is a good reference text about garbage collection (refers to Python
6931 * but it applies to all reference-counting mechanisms):
6932 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006934
6935/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006937 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006941garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 buf_T *buf;
6946 win_T *wp;
6947 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006948 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006949 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006951#ifdef FEAT_WINDOWS
6952 tabpage_T *tp;
6953#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006955 if (!testing)
6956 {
6957 /* Only do this once. */
6958 want_garbage_collect = FALSE;
6959 may_garbage_collect = FALSE;
6960 garbage_collect_at_exit = FALSE;
6961 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006962
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 /* We advance by two because we add one for items referenced through
6964 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006965 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /*
6968 * 1. Go through all accessible variables and mark all lists and dicts
6969 * with copyID.
6970 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971
6972 /* Don't free variables in the previous_funccal list unless they are only
6973 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006974 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6976 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6978 NULL);
6979 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6980 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 }
6982
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 /* script-local variables */
6984 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986
6987 /* buffer-local variables */
6988 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6995 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006996#ifdef FEAT_AUTOCMD
6997 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6999 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007000#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007002#ifdef FEAT_WINDOWS
7003 /* tabpage-local variables */
7004 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7006 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007#endif
7008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011
7012 /* function-local variables */
7013 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7014 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7016 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
7018
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007019 /* function call arguments, if v:testing is set. */
7020 for (i = 0; i < funcargs.ga_len; ++i)
7021 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7022 copyID, NULL, NULL);
7023
Bram Moolenaard812df62008-11-09 12:46:09 +00007024 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007026
Bram Moolenaar1dced572012-04-05 16:54:08 +02007027#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007029#endif
7030
Bram Moolenaardb913952012-06-29 12:54:53 +02007031#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007033#endif
7034
7035#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007037#endif
7038
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007039#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007040 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007041 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007042#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007043#ifdef FEAT_NETBEANS_INTG
7044 abort = abort || set_ref_in_nb_channel(copyID);
7045#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007048 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 /*
7050 * 2. Free lists and dictionaries that are not referenced.
7051 */
7052 did_free = free_unref_items(copyID);
7053
7054 /*
7055 * 3. Check if any funccal can be freed now.
7056 */
7057 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007058 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 if (can_free_funccal(*pfc, copyID))
7060 {
7061 fc = *pfc;
7062 *pfc = fc->caller;
7063 free_funccal(fc, TRUE);
7064 did_free = TRUE;
7065 did_free_funccal = TRUE;
7066 }
7067 else
7068 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 if (did_free_funccal)
7071 /* When a funccal was freed some more items might be garbage
7072 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007073 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 else if (p_verbose > 0)
7076 {
7077 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7078 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007079
7080 return did_free;
7081}
7082
7083/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007084 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007085 */
7086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007087free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007088{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007089 dict_T *dd, *dd_next;
7090 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007091 int did_free = FALSE;
7092
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007093 /* Let all "free" functions know that we are here. This means no
7094 * dictionaries, lists, channels or jobs are to be freed, because we will
7095 * do that here. */
7096 in_free_unref_items = TRUE;
7097
7098 /*
7099 * PASS 1: free the contents of the items. We don't free the items
7100 * themselves yet, so that it is possible to decrement refcount counters
7101 */
7102
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007104 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007106 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007107 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007109 /* Free the Dictionary and ordinary items it contains, but don't
7110 * recurse into Lists and Dictionaries, they will be in the list
7111 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007112 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115
7116 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007117 * Go through the list of lists and free items without the copyID.
7118 * But don't free a list that has a watcher (used in a for loop), these
7119 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007121 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7122 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7123 && ll->lv_watch == NULL)
7124 {
7125 /* Free the List and ordinary items it contains, but don't recurse
7126 * into Lists and Dictionaries, they will be in the list of dicts
7127 * or list of lists. */
7128 list_free_contents(ll);
7129 did_free = TRUE;
7130 }
7131
7132#ifdef FEAT_JOB_CHANNEL
7133 /* Go through the list of jobs and free items without the copyID. This
7134 * must happen before doing channels, because jobs refer to channels, but
7135 * the reference from the channel to the job isn't tracked. */
7136 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7137
7138 /* Go through the list of channels and free items without the copyID. */
7139 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7140#endif
7141
7142 /*
7143 * PASS 2: free the items themselves.
7144 */
7145 for (dd = first_dict; dd != NULL; dd = dd_next)
7146 {
7147 dd_next = dd->dv_used_next;
7148 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7149 dict_free_dict(dd);
7150 }
7151
7152 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007153 {
7154 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7156 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007157 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007158 /* Free the List and ordinary items it contains, but don't recurse
7159 * into Lists and Dictionaries, they will be in the list of dicts
7160 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007161 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007162 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007163 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007164
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007165#ifdef FEAT_JOB_CHANNEL
7166 /* Go through the list of jobs and free items without the copyID. This
7167 * must happen before doing channels, because jobs refer to channels, but
7168 * the reference from the channel to the job isn't tracked. */
7169 free_unused_jobs(copyID, COPYID_MASK);
7170
7171 /* Go through the list of channels and free items without the copyID. */
7172 free_unused_channels(copyID, COPYID_MASK);
7173#endif
7174
7175 in_free_unref_items = FALSE;
7176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 return did_free;
7178}
7179
7180/*
7181 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 * "list_stack" is used to add lists to be marked. Can be NULL.
7183 *
7184 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188{
7189 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 hashtab_T *cur_ht;
7193 ht_stack_T *ht_stack = NULL;
7194 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 cur_ht = ht;
7197 for (;;)
7198 {
7199 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007201 /* Mark each item in the hashtab. If the item contains a hashtab
7202 * it is added to ht_stack, if it contains a list it is added to
7203 * list_stack. */
7204 todo = (int)cur_ht->ht_used;
7205 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7206 if (!HASHITEM_EMPTY(hi))
7207 {
7208 --todo;
7209 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7210 &ht_stack, list_stack);
7211 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007213
7214 if (ht_stack == NULL)
7215 break;
7216
7217 /* take an item from the stack */
7218 cur_ht = ht_stack->ht;
7219 tempitem = ht_stack;
7220 ht_stack = ht_stack->prev;
7221 free(tempitem);
7222 }
7223
7224 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007225}
7226
7227/*
7228 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007229 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7230 *
7231 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007235{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007236 listitem_T *li;
7237 int abort = FALSE;
7238 list_T *cur_l;
7239 list_stack_T *list_stack = NULL;
7240 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007242 cur_l = l;
7243 for (;;)
7244 {
7245 if (!abort)
7246 /* Mark each item in the list. If the item contains a hashtab
7247 * it is added to ht_stack, if it contains a list it is added to
7248 * list_stack. */
7249 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7250 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7251 ht_stack, &list_stack);
7252 if (list_stack == NULL)
7253 break;
7254
7255 /* take an item from the stack */
7256 cur_l = list_stack->list;
7257 tempitem = list_stack;
7258 list_stack = list_stack->prev;
7259 free(tempitem);
7260 }
7261
7262 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263}
7264
7265/*
7266 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007267 * "list_stack" is used to add lists to be marked. Can be NULL.
7268 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7269 *
7270 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007271 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273set_ref_in_item(
7274 typval_T *tv,
7275 int copyID,
7276 ht_stack_T **ht_stack,
7277 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007278{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007279 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007280
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007281 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007283 dict_T *dd = tv->vval.v_dict;
7284
Bram Moolenaara03f2332016-02-06 18:09:59 +01007285 if (dd != NULL && dd->dv_copyID != copyID)
7286 {
7287 /* Didn't see this dict yet. */
7288 dd->dv_copyID = copyID;
7289 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007290 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7292 }
7293 else
7294 {
7295 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7296 if (newitem == NULL)
7297 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007298 else
7299 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007300 newitem->ht = &dd->dv_hashtab;
7301 newitem->prev = *ht_stack;
7302 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007303 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007304 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007305 }
7306 }
7307 else if (tv->v_type == VAR_LIST)
7308 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007309 list_T *ll = tv->vval.v_list;
7310
Bram Moolenaara03f2332016-02-06 18:09:59 +01007311 if (ll != NULL && ll->lv_copyID != copyID)
7312 {
7313 /* Didn't see this list yet. */
7314 ll->lv_copyID = copyID;
7315 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007316 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007317 abort = set_ref_in_list(ll, copyID, ht_stack);
7318 }
7319 else
7320 {
7321 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007322 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007323 if (newitem == NULL)
7324 abort = TRUE;
7325 else
7326 {
7327 newitem->list = ll;
7328 newitem->prev = *list_stack;
7329 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007330 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007331 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007332 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007333 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007334 else if (tv->v_type == VAR_PARTIAL)
7335 {
7336 partial_T *pt = tv->vval.v_partial;
7337 int i;
7338
7339 /* A partial does not have a copyID, because it cannot contain itself.
7340 */
7341 if (pt != NULL)
7342 {
7343 if (pt->pt_dict != NULL)
7344 {
7345 typval_T dtv;
7346
7347 dtv.v_type = VAR_DICT;
7348 dtv.vval.v_dict = pt->pt_dict;
7349 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7350 }
7351
7352 for (i = 0; i < pt->pt_argc; ++i)
7353 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7354 ht_stack, list_stack);
7355 }
7356 }
7357#ifdef FEAT_JOB_CHANNEL
7358 else if (tv->v_type == VAR_JOB)
7359 {
7360 job_T *job = tv->vval.v_job;
7361 typval_T dtv;
7362
7363 if (job != NULL && job->jv_copyID != copyID)
7364 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007365 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007366 if (job->jv_channel != NULL)
7367 {
7368 dtv.v_type = VAR_CHANNEL;
7369 dtv.vval.v_channel = job->jv_channel;
7370 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7371 }
7372 if (job->jv_exit_partial != NULL)
7373 {
7374 dtv.v_type = VAR_PARTIAL;
7375 dtv.vval.v_partial = job->jv_exit_partial;
7376 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7377 }
7378 }
7379 }
7380 else if (tv->v_type == VAR_CHANNEL)
7381 {
7382 channel_T *ch =tv->vval.v_channel;
7383 int part;
7384 typval_T dtv;
7385 jsonq_T *jq;
7386 cbq_T *cq;
7387
7388 if (ch != NULL && ch->ch_copyID != copyID)
7389 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007390 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007391 for (part = PART_SOCK; part <= PART_IN; ++part)
7392 {
7393 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7394 jq = jq->jq_next)
7395 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7396 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7397 cq = cq->cq_next)
7398 if (cq->cq_partial != NULL)
7399 {
7400 dtv.v_type = VAR_PARTIAL;
7401 dtv.vval.v_partial = cq->cq_partial;
7402 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7403 }
7404 if (ch->ch_part[part].ch_partial != NULL)
7405 {
7406 dtv.v_type = VAR_PARTIAL;
7407 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7409 }
7410 }
7411 if (ch->ch_partial != NULL)
7412 {
7413 dtv.v_type = VAR_PARTIAL;
7414 dtv.vval.v_partial = ch->ch_partial;
7415 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7416 }
7417 if (ch->ch_close_partial != NULL)
7418 {
7419 dtv.v_type = VAR_PARTIAL;
7420 dtv.vval.v_partial = ch->ch_close_partial;
7421 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7422 }
7423 }
7424 }
7425#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007426 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007427}
7428
7429/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 * Allocate an empty header for a dictionary.
7431 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007432 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434{
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007439 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007440 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007441 if (first_dict != NULL)
7442 first_dict->dv_used_prev = d;
7443 d->dv_used_next = first_dict;
7444 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007445 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007446
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007448 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007449 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007450 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007452 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007453 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454}
7455
7456/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007457 * Allocate an empty dict for a return value.
7458 * Returns OK or FAIL.
7459 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007462{
7463 dict_T *d = dict_alloc();
7464
7465 if (d == NULL)
7466 return FAIL;
7467
7468 rettv->vval.v_dict = d;
7469 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007470 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007471 ++d->dv_refcount;
7472 return OK;
7473}
7474
7475
7476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 * Unreference a Dictionary: decrement the reference count and free it when it
7478 * becomes zero.
7479 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007483 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007484 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485}
7486
7487/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007488 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 * Ignores the reference count.
7490 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007491 static void
7492dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007496 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007498 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007499 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007500 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 if (!HASHITEM_EMPTY(hi))
7504 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007505 /* Remove the item before deleting it, just in case there is
7506 * something recursive causing trouble. */
7507 di = HI2DI(hi);
7508 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007509 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007510 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 --todo;
7512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007515}
7516
7517 static void
7518dict_free_dict(dict_T *d)
7519{
7520 /* Remove the dict from the list of dicts for garbage collection. */
7521 if (d->dv_used_prev == NULL)
7522 first_dict = d->dv_used_next;
7523 else
7524 d->dv_used_prev->dv_used_next = d->dv_used_next;
7525 if (d->dv_used_next != NULL)
7526 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 vim_free(d);
7528}
7529
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007530 void
7531dict_free(dict_T *d)
7532{
7533 if (!in_free_unref_items)
7534 {
7535 dict_free_contents(d);
7536 dict_free_dict(d);
7537 }
7538}
7539
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540/*
7541 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 * The "key" is copied to the new item.
7543 * Note that the value of the item "di_tv" still needs to be initialized!
7544 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007546 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548{
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007551 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007555 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558}
7559
7560/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 * Make a copy of a Dictionary item.
7562 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565{
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007568 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7569 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 if (di != NULL)
7571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007573 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 copy_tv(&org->di_tv, &di->di_tv);
7575 }
7576 return di;
7577}
7578
7579/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 * Remove item "item" from Dictionary "dict" and free it.
7581 */
7582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007583dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584{
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 if (HASHITEM_EMPTY(hi))
7589 EMSG2(_(e_intern2), "dictitem_remove()");
7590 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 dictitem_free(item);
7593}
7594
7595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 * Free a dict item. Also clears the value.
7597 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007602 if (item->di_flags & DI_FLAGS_ALLOC)
7603 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604}
7605
7606/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7608 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 * Returns NULL when out of memory.
7611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007613dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614{
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 dict_T *copy;
7616 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619
7620 if (orig == NULL)
7621 return NULL;
7622
7623 copy = dict_alloc();
7624 if (copy != NULL)
7625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 if (copyID != 0)
7627 {
7628 orig->dv_copyID = copyID;
7629 orig->dv_copydict = copy;
7630 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007631 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 --todo;
7637
7638 di = dictitem_alloc(hi->hi_key);
7639 if (di == NULL)
7640 break;
7641 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007642 {
7643 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7644 copyID) == FAIL)
7645 {
7646 vim_free(di);
7647 break;
7648 }
7649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 else
7651 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7652 if (dict_add(copy, di) == FAIL)
7653 {
7654 dictitem_free(di);
7655 break;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659
Bram Moolenaare9a41262005-01-15 22:18:47 +00007660 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007661 if (todo > 0)
7662 {
7663 dict_unref(copy);
7664 copy = NULL;
7665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 }
7667
7668 return copy;
7669}
7670
7671/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007673 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007676dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677{
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679}
7680
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007682 * Add a number or string entry to dictionary "d".
7683 * When "str" is NULL use number "nr", otherwise use "str".
7684 * Returns FAIL when out of memory and when key already exists.
7685 */
7686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007687dict_add_nr_str(
7688 dict_T *d,
7689 char *key,
7690 long nr,
7691 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007692{
7693 dictitem_T *item;
7694
7695 item = dictitem_alloc((char_u *)key);
7696 if (item == NULL)
7697 return FAIL;
7698 item->di_tv.v_lock = 0;
7699 if (str == NULL)
7700 {
7701 item->di_tv.v_type = VAR_NUMBER;
7702 item->di_tv.vval.v_number = nr;
7703 }
7704 else
7705 {
7706 item->di_tv.v_type = VAR_STRING;
7707 item->di_tv.vval.v_string = vim_strsave(str);
7708 }
7709 if (dict_add(d, item) == FAIL)
7710 {
7711 dictitem_free(item);
7712 return FAIL;
7713 }
7714 return OK;
7715}
7716
7717/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007718 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007719 * Returns FAIL when out of memory and when key already exists.
7720 */
7721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007723{
7724 dictitem_T *item;
7725
7726 item = dictitem_alloc((char_u *)key);
7727 if (item == NULL)
7728 return FAIL;
7729 item->di_tv.v_lock = 0;
7730 item->di_tv.v_type = VAR_LIST;
7731 item->di_tv.vval.v_list = list;
7732 if (dict_add(d, item) == FAIL)
7733 {
7734 dictitem_free(item);
7735 return FAIL;
7736 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007737 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007738 return OK;
7739}
7740
7741/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 * Get the number of items in a Dictionary.
7743 */
7744 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007745dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007746{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007747 if (d == NULL)
7748 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007749 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007750}
7751
7752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 * Find item "key[len]" in Dictionary "d".
7754 * If "len" is negative use strlen(key).
7755 * Returns NULL when not found.
7756 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007757 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007758dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760#define AKEYLEN 200
7761 char_u buf[AKEYLEN];
7762 char_u *akey;
7763 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766 if (len < 0)
7767 akey = key;
7768 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 tofree = akey = vim_strnsave(key, len);
7771 if (akey == NULL)
7772 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007773 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 else
7775 {
7776 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007777 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 akey = buf;
7779 }
7780
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 vim_free(tofree);
7783 if (HASHITEM_EMPTY(hi))
7784 return NULL;
7785 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786}
7787
7788/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007789 * Get a string item from a dictionary.
7790 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007791 * Returns NULL if the entry doesn't exist or out of memory.
7792 */
7793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007795{
7796 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007797 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007798
7799 di = dict_find(d, key, -1);
7800 if (di == NULL)
7801 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007802 s = get_tv_string(&di->di_tv);
7803 if (save && s != NULL)
7804 s = vim_strsave(s);
7805 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007806}
7807
7808/*
7809 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007810 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007811 */
7812 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007814{
7815 dictitem_T *di;
7816
7817 di = dict_find(d, key, -1);
7818 if (di == NULL)
7819 return 0;
7820 return get_tv_number(&di->di_tv);
7821}
7822
7823/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 * Return an allocated string with the string representation of a Dictionary.
7825 * May return NULL.
7826 */
7827 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829{
7830 garray_T ga;
7831 int first = TRUE;
7832 char_u *tofree;
7833 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007837 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007838
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007839 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840 return NULL;
7841 ga_init2(&ga, (int)sizeof(char), 80);
7842 ga_append(&ga, '{');
7843
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007844 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007845 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007847 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007849 --todo;
7850
7851 if (first)
7852 first = FALSE;
7853 else
7854 ga_concat(&ga, (char_u *)", ");
7855
7856 tofree = string_quote(hi->hi_key, FALSE);
7857 if (tofree != NULL)
7858 {
7859 ga_concat(&ga, tofree);
7860 vim_free(tofree);
7861 }
7862 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007864 if (s != NULL)
7865 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007867 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007868 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007869 line_breakcheck();
7870
Bram Moolenaar8c711452005-01-14 21:53:12 +00007871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007873 if (todo > 0)
7874 {
7875 vim_free(ga.ga_data);
7876 return NULL;
7877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878
7879 ga_append(&ga, '}');
7880 ga_append(&ga, NUL);
7881 return (char_u *)ga.ga_data;
7882}
7883
7884/*
7885 * Allocate a variable for a Dictionary and fill it from "*arg".
7886 * Return OK or FAIL. Returns NOTDONE for {expr}.
7887 */
7888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890{
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 dict_T *d = NULL;
7892 typval_T tvkey;
7893 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007894 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007897 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898
7899 /*
7900 * First check if it's not a curly-braces thing: {expr}.
7901 * Must do this without evaluating, otherwise a function may be called
7902 * twice. Unfortunately this means we need to call eval1() twice for the
7903 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 if (*start != '}')
7907 {
7908 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7909 return FAIL;
7910 if (*start == '}')
7911 return NOTDONE;
7912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913
7914 if (evaluate)
7915 {
7916 d = dict_alloc();
7917 if (d == NULL)
7918 return FAIL;
7919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007920 tvkey.v_type = VAR_UNKNOWN;
7921 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922
7923 *arg = skipwhite(*arg + 1);
7924 while (**arg != '}' && **arg != NUL)
7925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007926 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 goto failret;
7928 if (**arg != ':')
7929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007930 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007931 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 goto failret;
7933 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007934 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007936 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007937 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007938 {
7939 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007940 clear_tv(&tvkey);
7941 goto failret;
7942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944
7945 *arg = skipwhite(*arg + 1);
7946 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7947 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007948 if (evaluate)
7949 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 goto failret;
7951 }
7952 if (evaluate)
7953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007954 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 if (item != NULL)
7956 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007957 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 clear_tv(&tv);
7960 goto failret;
7961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 item = dictitem_alloc(key);
7963 clear_tv(&tvkey);
7964 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007967 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007968 if (dict_add(d, item) == FAIL)
7969 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 }
7971 }
7972
7973 if (**arg == '}')
7974 break;
7975 if (**arg != ',')
7976 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007977 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 goto failret;
7979 }
7980 *arg = skipwhite(*arg + 1);
7981 }
7982
7983 if (**arg != '}')
7984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007985 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986failret:
7987 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007988 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 return FAIL;
7990 }
7991
7992 *arg = skipwhite(*arg + 1);
7993 if (evaluate)
7994 {
7995 rettv->v_type = VAR_DICT;
7996 rettv->vval.v_dict = d;
7997 ++d->dv_refcount;
7998 }
7999
8000 return OK;
8001}
8002
Bram Moolenaar17a13432016-01-24 14:22:10 +01008003 static char *
8004get_var_special_name(int nr)
8005{
8006 switch (nr)
8007 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008008 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008009 case VVAL_TRUE: return "v:true";
8010 case VVAL_NONE: return "v:none";
8011 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008012 }
8013 EMSG2(_(e_intern2), "get_var_special_name()");
8014 return "42";
8015}
8016
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 * Return a string with the string representation of a variable.
8019 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008020 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008022 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008023 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 */
8025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008026echo_string(
8027 typval_T *tv,
8028 char_u **tofree,
8029 char_u *numbuf,
8030 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 static int recurse = 0;
8033 char_u *r = NULL;
8034
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008037 if (!did_echo_string_emsg)
8038 {
8039 /* Only give this message once for a recursive call to avoid
8040 * flooding the user with errors. And stop iterating over lists
8041 * and dicts. */
8042 did_echo_string_emsg = TRUE;
8043 EMSG(_("E724: variable nested too deep for displaying"));
8044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008046 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 }
8048 ++recurse;
8049
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050 switch (tv->v_type)
8051 {
8052 case VAR_FUNC:
8053 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054 r = tv->vval.v_string;
8055 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008056
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008057 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008058 {
8059 partial_T *pt = tv->vval.v_partial;
8060 char_u *fname = string_quote(pt == NULL ? NULL
8061 : pt->pt_name, FALSE);
8062 garray_T ga;
8063 int i;
8064 char_u *tf;
8065
8066 ga_init2(&ga, 1, 100);
8067 ga_concat(&ga, (char_u *)"function(");
8068 if (fname != NULL)
8069 {
8070 ga_concat(&ga, fname);
8071 vim_free(fname);
8072 }
8073 if (pt != NULL && pt->pt_argc > 0)
8074 {
8075 ga_concat(&ga, (char_u *)", [");
8076 for (i = 0; i < pt->pt_argc; ++i)
8077 {
8078 if (i > 0)
8079 ga_concat(&ga, (char_u *)", ");
8080 ga_concat(&ga,
8081 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8082 vim_free(tf);
8083 }
8084 ga_concat(&ga, (char_u *)"]");
8085 }
8086 if (pt != NULL && pt->pt_dict != NULL)
8087 {
8088 typval_T dtv;
8089
8090 ga_concat(&ga, (char_u *)", ");
8091 dtv.v_type = VAR_DICT;
8092 dtv.vval.v_dict = pt->pt_dict;
8093 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8094 vim_free(tf);
8095 }
8096 ga_concat(&ga, (char_u *)")");
8097
8098 *tofree = ga.ga_data;
8099 r = *tofree;
8100 break;
8101 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008102
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008103 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008104 if (tv->vval.v_list == NULL)
8105 {
8106 *tofree = NULL;
8107 r = NULL;
8108 }
8109 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8110 {
8111 *tofree = NULL;
8112 r = (char_u *)"[...]";
8113 }
8114 else
8115 {
8116 tv->vval.v_list->lv_copyID = copyID;
8117 *tofree = list2string(tv, copyID);
8118 r = *tofree;
8119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008120 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008121
Bram Moolenaar8c711452005-01-14 21:53:12 +00008122 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008123 if (tv->vval.v_dict == NULL)
8124 {
8125 *tofree = NULL;
8126 r = NULL;
8127 }
8128 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8129 {
8130 *tofree = NULL;
8131 r = (char_u *)"{...}";
8132 }
8133 else
8134 {
8135 tv->vval.v_dict->dv_copyID = copyID;
8136 *tofree = dict2string(tv, copyID);
8137 r = *tofree;
8138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 case VAR_STRING:
8142 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008143 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008144 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008145 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008146 *tofree = NULL;
8147 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008149
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008151#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152 *tofree = NULL;
8153 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8154 r = numbuf;
8155 break;
8156#endif
8157
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008158 case VAR_SPECIAL:
8159 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008160 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008161 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163
Bram Moolenaar8502c702014-06-17 12:51:16 +02008164 if (--recurse == 0)
8165 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008167}
8168
8169/*
8170 * Return a string with the string representation of a variable.
8171 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8172 * "numbuf" is used for a number.
8173 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008174 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008176 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008177tv2string(
8178 typval_T *tv,
8179 char_u **tofree,
8180 char_u *numbuf,
8181 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008182{
8183 switch (tv->v_type)
8184 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185 case VAR_FUNC:
8186 *tofree = string_quote(tv->vval.v_string, TRUE);
8187 return *tofree;
8188 case VAR_STRING:
8189 *tofree = string_quote(tv->vval.v_string, FALSE);
8190 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008191 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008192#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 *tofree = NULL;
8194 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8195 return numbuf;
8196#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008199 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008200 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008201 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008202 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008203 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008204 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008205 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008207 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208}
8209
8210/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 * Return string "str" in ' quotes, doubling ' characters.
8212 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008213 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008214 */
8215 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008216string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217{
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008219 char_u *p, *r, *s;
8220
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 len = (function ? 13 : 3);
8222 if (str != NULL)
8223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008224 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 for (p = str; *p != NUL; mb_ptr_adv(p))
8226 if (*p == '\'')
8227 ++len;
8228 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 s = r = alloc(len);
8230 if (r != NULL)
8231 {
8232 if (function)
8233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008234 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 r += 10;
8236 }
8237 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 if (str != NULL)
8240 for (p = str; *p != NUL; )
8241 {
8242 if (*p == '\'')
8243 *r++ = '\'';
8244 MB_COPY_CHAR(p, r);
8245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008247 if (function)
8248 *r++ = ')';
8249 *r++ = NUL;
8250 }
8251 return s;
8252}
8253
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008254#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255/*
8256 * Convert the string "text" to a floating point number.
8257 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8258 * this always uses a decimal point.
8259 * Returns the length of the text that was consumed.
8260 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008262string2float(
8263 char_u *text,
8264 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265{
8266 char *s = (char *)text;
8267 float_T f;
8268
8269 f = strtod(s, &s);
8270 *value = f;
8271 return (int)((char_u *)s - text);
8272}
8273#endif
8274
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * Get the value of an environment variable.
8277 * "arg" is pointing to the '$'. It is advanced to after the name.
8278 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008279 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 */
8281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008282get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 char_u *string = NULL;
8285 int len;
8286 int cc;
8287 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008288 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 ++*arg;
8291 name = *arg;
8292 len = get_env_len(arg);
8293 if (evaluate)
8294 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008295 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008296 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008297
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008298 cc = name[len];
8299 name[len] = NUL;
8300 /* first try vim_getenv(), fast for normal environment vars */
8301 string = vim_getenv(name, &mustfree);
8302 if (string != NULL && *string != NUL)
8303 {
8304 if (!mustfree)
8305 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008307 else
8308 {
8309 if (mustfree)
8310 vim_free(string);
8311
8312 /* next try expanding things like $VIM and ${HOME} */
8313 string = expand_env_save(name - 1);
8314 if (string != NULL && *string == '$')
8315 {
8316 vim_free(string);
8317 string = NULL;
8318 }
8319 }
8320 name[len] = cc;
8321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->v_type = VAR_STRING;
8323 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325
8326 return OK;
8327}
8328
8329/*
8330 * Array with names and number of arguments of all internal functions
8331 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8332 */
8333static struct fst
8334{
8335 char *f_name; /* function name */
8336 char f_min_argc; /* minimal number of arguments */
8337 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008338 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008339 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340} functions[] =
8341{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008342#ifdef FEAT_FLOAT
8343 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008344 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008345#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008346 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008347 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008348 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"append", 2, 2, f_append},
8350 {"argc", 0, 0, f_argc},
8351 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008352 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008353 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008354#ifdef FEAT_FLOAT
8355 {"asin", 1, 1, f_asin}, /* WJMc */
8356#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008357 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008358 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008359 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008360 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008361 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008362 {"assert_notequal", 2, 3, f_assert_notequal},
8363 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008364 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008365#ifdef FEAT_FLOAT
8366 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008367 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008370 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"bufexists", 1, 1, f_bufexists},
8372 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8373 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8374 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8375 {"buflisted", 1, 1, f_buflisted},
8376 {"bufloaded", 1, 1, f_bufloaded},
8377 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008378 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"bufwinnr", 1, 1, f_bufwinnr},
8380 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008381 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008382 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008384#ifdef FEAT_FLOAT
8385 {"ceil", 1, 1, f_ceil},
8386#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008387#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008388 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008389 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8390 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008391 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008392 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008393 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008394 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008395 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008396 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008397 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008398 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008399 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8400 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008401 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008402 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008403#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008404 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008405 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008407 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008409#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008410 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008411 {"complete_add", 1, 1, f_complete_add},
8412 {"complete_check", 0, 0, f_complete_check},
8413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008415 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008416#ifdef FEAT_FLOAT
8417 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008418 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008420 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008422 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008423 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008424 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008426 {"diff_filler", 1, 1, f_diff_filler},
8427 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008428 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008429 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008431 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"eventhandler", 0, 0, f_eventhandler},
8433 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008434 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008436#ifdef FEAT_FLOAT
8437 {"exp", 1, 1, f_exp},
8438#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008439 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008440 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008441 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8443 {"filereadable", 1, 1, f_filereadable},
8444 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008445 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008446 {"finddir", 1, 3, f_finddir},
8447 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008448#ifdef FEAT_FLOAT
8449 {"float2nr", 1, 1, f_float2nr},
8450 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008451 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008453 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {"fnamemodify", 2, 2, f_fnamemodify},
8455 {"foldclosed", 1, 1, f_foldclosed},
8456 {"foldclosedend", 1, 1, f_foldclosedend},
8457 {"foldlevel", 1, 1, f_foldlevel},
8458 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008459 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008461 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008462 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008463 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008464 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008465 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {"getchar", 0, 1, f_getchar},
8467 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008468 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"getcmdline", 0, 0, f_getcmdline},
8470 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008471 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008472 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008473 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008474 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008475 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008476 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {"getfsize", 1, 1, f_getfsize},
8478 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008479 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008480 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008481 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008482 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008483 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008484 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008485 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008486 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008488 {"gettabvar", 2, 3, f_gettabvar},
8489 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"getwinposx", 0, 0, f_getwinposx},
8491 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008492 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008493 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008494 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008495 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008498 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008499 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8501 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8502 {"histadd", 2, 2, f_histadd},
8503 {"histdel", 1, 2, f_histdel},
8504 {"histget", 1, 2, f_histget},
8505 {"histnr", 1, 1, f_histnr},
8506 {"hlID", 1, 1, f_hlID},
8507 {"hlexists", 1, 1, f_hlexists},
8508 {"hostname", 0, 0, f_hostname},
8509 {"iconv", 3, 3, f_iconv},
8510 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008511 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008512 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008514 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"inputrestore", 0, 0, f_inputrestore},
8516 {"inputsave", 0, 0, f_inputsave},
8517 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008519 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008521 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008522#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8523 {"isnan", 1, 1, f_isnan},
8524#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008525 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008526#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008527 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008528 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008529 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008530 {"job_start", 1, 2, f_job_start},
8531 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008532 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008533#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008534 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008535 {"js_decode", 1, 1, f_js_decode},
8536 {"js_encode", 1, 1, f_js_encode},
8537 {"json_decode", 1, 1, f_json_decode},
8538 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008539 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008541 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"libcall", 3, 3, f_libcall},
8543 {"libcallnr", 3, 3, f_libcallnr},
8544 {"line", 1, 1, f_line},
8545 {"line2byte", 1, 1, f_line2byte},
8546 {"lispindent", 1, 1, f_lispindent},
8547 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008548#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008549 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008550 {"log10", 1, 1, f_log10},
8551#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008552#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008553 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008554#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008555 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008556 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008557 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008558 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008559 {"matchadd", 2, 5, f_matchadd},
8560 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008561 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008562 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008563 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008564 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008565 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008566 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008567 {"max", 1, 1, f_max},
8568 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008569#ifdef vim_mkdir
8570 {"mkdir", 1, 3, f_mkdir},
8571#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008572 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008573#ifdef FEAT_MZSCHEME
8574 {"mzeval", 1, 1, f_mzeval},
8575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008577 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008578 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008579 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008580#ifdef FEAT_PERL
8581 {"perleval", 1, 1, f_perleval},
8582#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008583#ifdef FEAT_FLOAT
8584 {"pow", 2, 2, f_pow},
8585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008587 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008588 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008589#ifdef FEAT_PYTHON3
8590 {"py3eval", 1, 1, f_py3eval},
8591#endif
8592#ifdef FEAT_PYTHON
8593 {"pyeval", 1, 1, f_pyeval},
8594#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008595 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008596 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008597 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008598#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008599 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008600#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008601 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"remote_expr", 2, 3, f_remote_expr},
8603 {"remote_foreground", 1, 1, f_remote_foreground},
8604 {"remote_peek", 1, 2, f_remote_peek},
8605 {"remote_read", 1, 1, f_remote_read},
8606 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008607 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008609 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008611 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612#ifdef FEAT_FLOAT
8613 {"round", 1, 1, f_round},
8614#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008615 {"screenattr", 2, 2, f_screenattr},
8616 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008617 {"screencol", 0, 0, f_screencol},
8618 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008619 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008620 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008621 {"searchpair", 3, 7, f_searchpair},
8622 {"searchpairpos", 3, 7, f_searchpairpos},
8623 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {"server2client", 2, 2, f_server2client},
8625 {"serverlist", 0, 0, f_serverlist},
8626 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008627 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008629 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008631 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008632 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008633 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008634 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008636 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008637 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008639#ifdef FEAT_CRYPT
8640 {"sha256", 1, 1, f_sha256},
8641#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008642 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008643 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008645#ifdef FEAT_FLOAT
8646 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008647 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008648#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008649 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008650 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008651 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008652 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008653 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008654#ifdef FEAT_FLOAT
8655 {"sqrt", 1, 1, f_sqrt},
8656 {"str2float", 1, 1, f_str2float},
8657#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008658 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008659 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008660 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008661 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662#ifdef HAVE_STRFTIME
8663 {"strftime", 1, 2, f_strftime},
8664#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008665 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {"strlen", 1, 1, f_strlen},
8669 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008670 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008672 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008673 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {"substitute", 4, 4, f_substitute},
8675 {"synID", 3, 3, f_synID},
8676 {"synIDattr", 2, 3, f_synIDattr},
8677 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008678 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008679 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008680 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008681 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008682 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008683 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008684 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008685 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008686 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008687#ifdef FEAT_FLOAT
8688 {"tan", 1, 1, f_tan},
8689 {"tanh", 1, 1, f_tanh},
8690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {"tempname", 0, 0, f_tempname},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008692 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8693#ifdef FEAT_JOB_CHANNEL
8694 {"test_null_channel", 0, 0, f_test_null_channel},
8695#endif
8696 {"test_null_dict", 0, 0, f_test_null_dict},
8697#ifdef FEAT_JOB_CHANNEL
8698 {"test_null_job", 0, 0, f_test_null_job},
8699#endif
8700 {"test_null_list", 0, 0, f_test_null_list},
8701 {"test_null_partial", 0, 0, f_test_null_partial},
8702 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008703#ifdef FEAT_TIMERS
8704 {"timer_start", 2, 3, f_timer_start},
8705 {"timer_stop", 1, 1, f_timer_stop},
8706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {"tolower", 1, 1, f_tolower},
8708 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008709 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710#ifdef FEAT_FLOAT
8711 {"trunc", 1, 1, f_trunc},
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008714 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008715 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008716 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008717 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {"virtcol", 1, 1, f_virtcol},
8719 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008720 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008721 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008722 {"win_getid", 0, 2, f_win_getid},
8723 {"win_gotoid", 1, 1, f_win_gotoid},
8724 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8725 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {"winbufnr", 1, 1, f_winbufnr},
8727 {"wincol", 0, 0, f_wincol},
8728 {"winheight", 1, 1, f_winheight},
8729 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008730 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008732 {"winrestview", 1, 1, f_winrestview},
8733 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008735 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008736 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008737 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738};
8739
8740#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8741
8742/*
8743 * Function given to ExpandGeneric() to obtain the list of internal
8744 * or user defined function names.
8745 */
8746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008747get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 static int intidx = -1;
8750 char_u *name;
8751
8752 if (idx == 0)
8753 intidx = -1;
8754 if (intidx < 0)
8755 {
8756 name = get_user_func_name(xp, idx);
8757 if (name != NULL)
8758 return name;
8759 }
8760 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8761 {
8762 STRCPY(IObuff, functions[intidx].f_name);
8763 STRCAT(IObuff, "(");
8764 if (functions[intidx].f_max_argc == 0)
8765 STRCAT(IObuff, ")");
8766 return IObuff;
8767 }
8768
8769 return NULL;
8770}
8771
8772/*
8773 * Function given to ExpandGeneric() to obtain the list of internal or
8774 * user defined variable or function names.
8775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 static int intidx = -1;
8780 char_u *name;
8781
8782 if (idx == 0)
8783 intidx = -1;
8784 if (intidx < 0)
8785 {
8786 name = get_function_name(xp, idx);
8787 if (name != NULL)
8788 return name;
8789 }
8790 return get_user_var_name(xp, ++intidx);
8791}
8792
8793#endif /* FEAT_CMDL_COMPL */
8794
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008795#if defined(EBCDIC) || defined(PROTO)
8796/*
8797 * Compare struct fst by function name.
8798 */
8799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008800compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008801{
8802 struct fst *p1 = (struct fst *)s1;
8803 struct fst *p2 = (struct fst *)s2;
8804
8805 return STRCMP(p1->f_name, p2->f_name);
8806}
8807
8808/*
8809 * Sort the function table by function name.
8810 * The sorting of the table above is ASCII dependant.
8811 * On machines using EBCDIC we have to sort it.
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008815{
8816 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8817
8818 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8819}
8820#endif
8821
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*
8824 * Find internal function in table above.
8825 * Return index, or -1 if not found
8826 */
8827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008828find_internal_func(
8829 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 int first = 0;
8832 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8833 int cmp;
8834 int x;
8835
8836 /*
8837 * Find the function name in the table. Binary search.
8838 */
8839 while (first <= last)
8840 {
8841 x = first + ((unsigned)(last - first) >> 1);
8842 cmp = STRCMP(name, functions[x].f_name);
8843 if (cmp < 0)
8844 last = x - 1;
8845 else if (cmp > 0)
8846 first = x + 1;
8847 else
8848 return x;
8849 }
8850 return -1;
8851}
8852
8853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8855 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008856 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8857 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008858 */
8859 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008860deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008861{
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008863 int cc;
8864
Bram Moolenaar65639032016-03-16 21:40:30 +01008865 if (partialp != NULL)
8866 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 cc = name[*lenp];
8869 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008870 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008873 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008875 {
8876 *lenp = 0;
8877 return (char_u *)""; /* just in case */
8878 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008879 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008881 }
8882
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008883 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8884 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008885 partial_T *pt = v->di_tv.vval.v_partial;
8886
8887 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008888 {
8889 *lenp = 0;
8890 return (char_u *)""; /* just in case */
8891 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008892 if (partialp != NULL)
8893 *partialp = pt;
8894 *lenp = (int)STRLEN(pt->pt_name);
8895 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008896 }
8897
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008898 return name;
8899}
8900
8901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 * Allocate a variable for the result of a function.
8903 * Return OK or FAIL.
8904 */
8905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008906get_func_tv(
8907 char_u *name, /* name of the function */
8908 int len, /* length of "name" */
8909 typval_T *rettv,
8910 char_u **arg, /* argument, pointing to the '(' */
8911 linenr_T firstline, /* first line of range */
8912 linenr_T lastline, /* last line of range */
8913 int *doesrange, /* return: function handled range */
8914 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008915 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 char_u *argp;
8919 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008920 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 int argcount = 0; /* number of arguments found */
8922
8923 /*
8924 * Get the arguments.
8925 */
8926 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008927 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
8929 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8930 if (*argp == ')' || *argp == ',' || *argp == NUL)
8931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8933 {
8934 ret = FAIL;
8935 break;
8936 }
8937 ++argcount;
8938 if (*argp != ',')
8939 break;
8940 }
8941 if (*argp == ')')
8942 ++argp;
8943 else
8944 ret = FAIL;
8945
8946 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008947 {
8948 int i = 0;
8949
8950 if (get_vim_var_nr(VV_TESTING))
8951 {
8952 /* Prepare for calling garbagecollect_for_testing(), need to know
8953 * what variables are used on the call stack. */
8954 if (funcargs.ga_itemsize == 0)
8955 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8956 for (i = 0; i < argcount; ++i)
8957 if (ga_grow(&funcargs, 1) == OK)
8958 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8959 &argvars[i];
8960 }
8961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008963 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008964
8965 funcargs.ga_len -= i;
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 {
8969 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008970 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008972 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978 *arg = skipwhite(argp);
8979 return ret;
8980}
8981
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008982#define ERROR_UNKNOWN 0
8983#define ERROR_TOOMANY 1
8984#define ERROR_TOOFEW 2
8985#define ERROR_SCRIPT 3
8986#define ERROR_DICT 4
8987#define ERROR_NONE 5
8988#define ERROR_OTHER 6
8989#define FLEN_FIXED 40
8990
8991/*
8992 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8993 * Change <SNR>123_name() to K_SNR 123_name().
8994 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8995 * (slow).
8996 */
8997 static char_u *
8998fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8999{
9000 int llen;
9001 char_u *fname;
9002 int i;
9003
9004 llen = eval_fname_script(name);
9005 if (llen > 0)
9006 {
9007 fname_buf[0] = K_SPECIAL;
9008 fname_buf[1] = KS_EXTRA;
9009 fname_buf[2] = (int)KE_SNR;
9010 i = 3;
9011 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9012 {
9013 if (current_SID <= 0)
9014 *error = ERROR_SCRIPT;
9015 else
9016 {
9017 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9018 i = (int)STRLEN(fname_buf);
9019 }
9020 }
9021 if (i + STRLEN(name + llen) < FLEN_FIXED)
9022 {
9023 STRCPY(fname_buf + i, name + llen);
9024 fname = fname_buf;
9025 }
9026 else
9027 {
9028 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9029 if (fname == NULL)
9030 *error = ERROR_OTHER;
9031 else
9032 {
9033 *tofree = fname;
9034 mch_memmove(fname, fname_buf, (size_t)i);
9035 STRCPY(fname + i, name + llen);
9036 }
9037 }
9038 }
9039 else
9040 fname = name;
9041 return fname;
9042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044/*
9045 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009046 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009047 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050call_func(
9051 char_u *funcname, /* name of the function */
9052 int len, /* length of "name" */
9053 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 int argcount_in, /* number of "argvars" */
9055 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009056 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009057 linenr_T firstline, /* first line of range */
9058 linenr_T lastline, /* last line of range */
9059 int *doesrange, /* return: function handled range */
9060 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009061 partial_T *partial, /* optional, can be NULL */
9062 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 int error = ERROR_NONE;
9066 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009069 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009071 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009072 int argcount = argcount_in;
9073 typval_T *argvars = argvars_in;
9074 dict_T *selfdict = selfdict_in;
9075 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9076 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009077
9078 /* Make a copy of the name, if it comes from a funcref variable it could
9079 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009080 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009081 if (name == NULL)
9082 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009084 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086 *doesrange = FALSE;
9087
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009088 if (partial != NULL)
9089 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009090 /* When the function has a partial with a dict and there is a dict
9091 * argument, use the dict argument. That is backwards compatible.
9092 * When the dict was bound explicitly use the one from the partial. */
9093 if (partial->pt_dict != NULL
9094 && (selfdict_in == NULL || !partial->pt_auto))
9095 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009096 if (error == ERROR_NONE && partial->pt_argc > 0)
9097 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009098 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9099 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9100 for (i = 0; i < argcount_in; ++i)
9101 argv[i + argv_clear] = argvars_in[i];
9102 argvars = argv;
9103 argcount = partial->pt_argc + argcount_in;
9104 }
9105 }
9106
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107
9108 /* execute the function if no errors detected and executing */
9109 if (evaluate && error == ERROR_NONE)
9110 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009111 char_u *rfname = fname;
9112
9113 /* Ignore "g:" before a function name. */
9114 if (fname[0] == 'g' && fname[1] == ':')
9115 rfname = fname + 2;
9116
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009117 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9118 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 error = ERROR_UNKNOWN;
9120
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009121 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
9123 /*
9124 * User defined function.
9125 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009126 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009127
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009129 /* Trigger FuncUndefined event, may load the function. */
9130 if (fp == NULL
9131 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009132 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009133 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009135 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009136 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 }
9138#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009139 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009140 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009141 {
9142 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009143 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009144 }
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 if (fp != NULL)
9147 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009148 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009150 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009152 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009154 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009155 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 else
9157 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009158 int did_save_redo = FALSE;
9159
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 /*
9161 * Call the user function.
9162 * Save and restore search patterns, script variables and
9163 * redo buffer.
9164 */
9165 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009166#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009167 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009168#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009169 {
9170 saveRedobuff();
9171 did_save_redo = TRUE;
9172 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009173 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009175 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009176 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9177 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9178 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009179 /* Function was unreferenced while being used, free it
9180 * now. */
9181 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009182 if (did_save_redo)
9183 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 restore_search_patterns();
9185 error = ERROR_NONE;
9186 }
9187 }
9188 }
9189 else
9190 {
9191 /*
9192 * Find the function name in the table, call its implementation.
9193 */
9194 i = find_internal_func(fname);
9195 if (i >= 0)
9196 {
9197 if (argcount < functions[i].f_min_argc)
9198 error = ERROR_TOOFEW;
9199 else if (argcount > functions[i].f_max_argc)
9200 error = ERROR_TOOMANY;
9201 else
9202 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009203 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 error = ERROR_NONE;
9206 }
9207 }
9208 }
9209 /*
9210 * The function call (or "FuncUndefined" autocommand sequence) might
9211 * have been aborted by an error, an interrupt, or an explicitly thrown
9212 * exception that has not been caught so far. This situation can be
9213 * tested for by calling aborting(). For an error in an internal
9214 * function or for the "E132" error in call_user_func(), however, the
9215 * throw point at which the "force_abort" flag (temporarily reset by
9216 * emsg()) is normally updated has not been reached yet. We need to
9217 * update that flag first to make aborting() reliable.
9218 */
9219 update_force_abort();
9220 }
9221 if (error == ERROR_NONE)
9222 ret = OK;
9223
9224 /*
9225 * Report an error unless the argument evaluation or function call has been
9226 * cancelled due to an aborting error, an interrupt, or an exception.
9227 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009228 if (!aborting())
9229 {
9230 switch (error)
9231 {
9232 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009233 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009234 break;
9235 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009237 break;
9238 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009239 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009240 name);
9241 break;
9242 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009243 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009244 name);
9245 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009246 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009247 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009248 name);
9249 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009250 }
9251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009253 while (argv_clear > 0)
9254 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009255 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009256 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
9258 return ret;
9259}
9260
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009261/*
9262 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009263 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009264 */
9265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009266emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009267{
9268 char_u *p;
9269
9270 if (*name == K_SPECIAL)
9271 p = concat_str((char_u *)"<SNR>", name + 3);
9272 else
9273 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009274 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009275 if (p != name)
9276 vim_free(p);
9277}
9278
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009279/*
9280 * Return TRUE for a non-zero Number and a non-empty String.
9281 */
9282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009283non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009284{
9285 return ((argvars[0].v_type == VAR_NUMBER
9286 && argvars[0].vval.v_number != 0)
9287 || (argvars[0].v_type == VAR_STRING
9288 && argvars[0].vval.v_string != NULL
9289 && *argvars[0].vval.v_string != NUL));
9290}
9291
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292/*********************************************
9293 * Implementation of the built-in functions
9294 */
9295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009297static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009298
9299/*
9300 * Get the float value of "argvars[0]" into "f".
9301 * Returns FAIL when the argument is not a Number or Float.
9302 */
9303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009305{
9306 if (argvars[0].v_type == VAR_FLOAT)
9307 {
9308 *f = argvars[0].vval.v_float;
9309 return OK;
9310 }
9311 if (argvars[0].v_type == VAR_NUMBER)
9312 {
9313 *f = (float_T)argvars[0].vval.v_number;
9314 return OK;
9315 }
9316 EMSG(_("E808: Number or Float required"));
9317 return FAIL;
9318}
9319
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009320/*
9321 * "abs(expr)" function
9322 */
9323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009324f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009325{
9326 if (argvars[0].v_type == VAR_FLOAT)
9327 {
9328 rettv->v_type = VAR_FLOAT;
9329 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9330 }
9331 else
9332 {
9333 varnumber_T n;
9334 int error = FALSE;
9335
9336 n = get_tv_number_chk(&argvars[0], &error);
9337 if (error)
9338 rettv->vval.v_number = -1;
9339 else if (n > 0)
9340 rettv->vval.v_number = n;
9341 else
9342 rettv->vval.v_number = -n;
9343 }
9344}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009345
9346/*
9347 * "acos()" function
9348 */
9349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009350f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009351{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009352 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353
9354 rettv->v_type = VAR_FLOAT;
9355 if (get_float_arg(argvars, &f) == OK)
9356 rettv->vval.v_float = acos(f);
9357 else
9358 rettv->vval.v_float = 0.0;
9359}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#endif
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
Bram Moolenaar33570922005-01-25 22:26:29 +00009368 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009373 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009374 && !tv_check_lock(l->lv_lock,
9375 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009376 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009378 }
9379 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009380 EMSG(_(e_listreq));
9381}
9382
9383/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009384 * "alloc_fail(id, countdown, repeat)" function
9385 */
9386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009387f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009388{
9389 if (argvars[0].v_type != VAR_NUMBER
9390 || argvars[0].vval.v_number <= 0
9391 || argvars[1].v_type != VAR_NUMBER
9392 || argvars[1].vval.v_number < 0
9393 || argvars[2].v_type != VAR_NUMBER)
9394 EMSG(_(e_invarg));
9395 else
9396 {
9397 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009398 if (alloc_fail_id >= aid_last)
9399 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009400 alloc_fail_countdown = argvars[1].vval.v_number;
9401 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009402 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009403 }
9404}
9405
9406/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009407 * "and(expr, expr)" function
9408 */
9409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009410f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009411{
9412 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9413 & get_tv_number_chk(&argvars[1], NULL);
9414}
9415
9416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009417 * "append(lnum, string/list)" function
9418 */
9419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009420f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009421{
9422 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 list_T *l = NULL;
9425 listitem_T *li = NULL;
9426 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 long added = 0;
9428
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009429 /* When coming here from Insert mode, sync undo, so that this can be
9430 * undone separately from what was previously inserted. */
9431 if (u_sync_once == 2)
9432 {
9433 u_sync_once = 1; /* notify that u_sync() was called */
9434 u_sync(TRUE);
9435 }
9436
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 lnum = get_tv_lnum(argvars);
9438 if (lnum >= 0
9439 && lnum <= curbuf->b_ml.ml_line_count
9440 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009441 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009442 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009443 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009444 l = argvars[1].vval.v_list;
9445 if (l == NULL)
9446 return;
9447 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009448 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009449 for (;;)
9450 {
9451 if (l == NULL)
9452 tv = &argvars[1]; /* append a string */
9453 else if (li == NULL)
9454 break; /* end of list */
9455 else
9456 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009457 line = get_tv_string_chk(tv);
9458 if (line == NULL) /* type error */
9459 {
9460 rettv->vval.v_number = 1; /* Failed */
9461 break;
9462 }
9463 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464 ++added;
9465 if (l == NULL)
9466 break;
9467 li = li->li_next;
9468 }
9469
9470 appended_lines_mark(lnum, added);
9471 if (curwin->w_cursor.lnum > lnum)
9472 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 else
9475 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
9479 * "argc()" function
9480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486
9487/*
9488 * "argidx()" function
9489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009497 * "arglistid()" function
9498 */
9499 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009500f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009501{
9502 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009503
9504 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009505 wp = find_tabwin(&argvars[0], &argvars[1]);
9506 if (wp != NULL)
9507 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009508}
9509
9510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 * "argv(nr)" function
9512 */
9513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 int idx;
9517
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009518 if (argvars[0].v_type != VAR_UNKNOWN)
9519 {
9520 idx = get_tv_number_chk(&argvars[0], NULL);
9521 if (idx >= 0 && idx < ARGCOUNT)
9522 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9523 else
9524 rettv->vval.v_string = NULL;
9525 rettv->v_type = VAR_STRING;
9526 }
9527 else if (rettv_list_alloc(rettv) == OK)
9528 for (idx = 0; idx < ARGCOUNT; ++idx)
9529 list_append_string(rettv->vval.v_list,
9530 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009533typedef enum
9534{
9535 ASSERT_EQUAL,
9536 ASSERT_NOTEQUAL,
9537 ASSERT_MATCH,
9538 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009539 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009540} assert_type_T;
9541
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009542static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009543static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009544static void assert_error(garray_T *gap);
9545static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009546
9547/*
9548 * Prepare "gap" for an assert error and add the sourcing position.
9549 */
9550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009551prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009552{
9553 char buf[NUMBUFLEN];
9554
9555 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009556 if (sourcing_name != NULL)
9557 {
9558 ga_concat(gap, sourcing_name);
9559 if (sourcing_lnum > 0)
9560 ga_concat(gap, (char_u *)" ");
9561 }
9562 if (sourcing_lnum > 0)
9563 {
9564 sprintf(buf, "line %ld", (long)sourcing_lnum);
9565 ga_concat(gap, (char_u *)buf);
9566 }
9567 if (sourcing_name != NULL || sourcing_lnum > 0)
9568 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009569}
9570
9571/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009572 * Append "str" to "gap", escaping unprintable characters.
9573 * Changes NL to \n, CR to \r, etc.
9574 */
9575 static void
9576ga_concat_esc(garray_T *gap, char_u *str)
9577{
9578 char_u *p;
9579 char_u buf[NUMBUFLEN];
9580
Bram Moolenaarf1551962016-03-15 12:55:58 +01009581 if (str == NULL)
9582 {
9583 ga_concat(gap, (char_u *)"NULL");
9584 return;
9585 }
9586
Bram Moolenaar23689172016-02-15 22:37:37 +01009587 for (p = str; *p != NUL; ++p)
9588 switch (*p)
9589 {
9590 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9591 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9592 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9593 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9594 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9595 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9596 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9597 default:
9598 if (*p < ' ')
9599 {
9600 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9601 ga_concat(gap, buf);
9602 }
9603 else
9604 ga_append(gap, *p);
9605 break;
9606 }
9607}
9608
9609/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009610 * Fill "gap" with information about an assert error.
9611 */
9612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613fill_assert_error(
9614 garray_T *gap,
9615 typval_T *opt_msg_tv,
9616 char_u *exp_str,
9617 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009618 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009619 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009620{
9621 char_u numbuf[NUMBUFLEN];
9622 char_u *tofree;
9623
9624 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9625 {
9626 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9627 vim_free(tofree);
9628 }
9629 else
9630 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009631 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009632 ga_concat(gap, (char_u *)"Pattern ");
9633 else
9634 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009635 if (exp_str == NULL)
9636 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009637 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009638 vim_free(tofree);
9639 }
9640 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009641 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009642 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009643 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009644 else if (atype == ASSERT_NOTMATCH)
9645 ga_concat(gap, (char_u *)" does match ");
9646 else if (atype == ASSERT_NOTEQUAL)
9647 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009648 else
9649 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009650 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009651 vim_free(tofree);
9652 }
9653}
Bram Moolenaar43345542015-11-29 17:35:35 +01009654
9655/*
9656 * Add an assert error to v:errors.
9657 */
9658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009660{
9661 struct vimvar *vp = &vimvars[VV_ERRORS];
9662
9663 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9664 /* Make sure v:errors is a list. */
9665 set_vim_var_list(VV_ERRORS, list_alloc());
9666 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9667}
9668
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669 static void
9670assert_equal_common(typval_T *argvars, assert_type_T atype)
9671{
9672 garray_T ga;
9673
9674 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9675 != (atype == ASSERT_EQUAL))
9676 {
9677 prepare_assert_error(&ga);
9678 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9679 atype);
9680 assert_error(&ga);
9681 ga_clear(&ga);
9682 }
9683}
9684
Bram Moolenaar43345542015-11-29 17:35:35 +01009685/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009686 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009687 */
9688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009689f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009690{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009691 assert_equal_common(argvars, ASSERT_EQUAL);
9692}
Bram Moolenaar43345542015-11-29 17:35:35 +01009693
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009694/*
9695 * "assert_notequal(expected, actual[, msg])" function
9696 */
9697 static void
9698f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9699{
9700 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009701}
9702
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009703/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009704 * "assert_exception(string[, msg])" function
9705 */
9706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009707f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009708{
9709 garray_T ga;
9710 char *error;
9711
9712 error = (char *)get_tv_string_chk(&argvars[0]);
9713 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9714 {
9715 prepare_assert_error(&ga);
9716 ga_concat(&ga, (char_u *)"v:exception is not set");
9717 assert_error(&ga);
9718 ga_clear(&ga);
9719 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009720 else if (error != NULL
9721 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009722 {
9723 prepare_assert_error(&ga);
9724 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009725 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009726 assert_error(&ga);
9727 ga_clear(&ga);
9728 }
9729}
9730
9731/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009732 * "assert_fails(cmd [, error])" function
9733 */
9734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009735f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009736{
9737 char_u *cmd = get_tv_string_chk(&argvars[0]);
9738 garray_T ga;
9739
9740 called_emsg = FALSE;
9741 suppress_errthrow = TRUE;
9742 emsg_silent = TRUE;
9743 do_cmdline_cmd(cmd);
9744 if (!called_emsg)
9745 {
9746 prepare_assert_error(&ga);
9747 ga_concat(&ga, (char_u *)"command did not fail: ");
9748 ga_concat(&ga, cmd);
9749 assert_error(&ga);
9750 ga_clear(&ga);
9751 }
9752 else if (argvars[1].v_type != VAR_UNKNOWN)
9753 {
9754 char_u buf[NUMBUFLEN];
9755 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9756
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009757 if (error == NULL
9758 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009759 {
9760 prepare_assert_error(&ga);
9761 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009762 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009763 assert_error(&ga);
9764 ga_clear(&ga);
9765 }
9766 }
9767
9768 called_emsg = FALSE;
9769 suppress_errthrow = FALSE;
9770 emsg_silent = FALSE;
9771 emsg_on_display = FALSE;
9772 set_vim_var_string(VV_ERRMSG, NULL, 0);
9773}
9774
9775/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 * Common for assert_true() and assert_false().
9777 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009780{
9781 int error = FALSE;
9782 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009783
Bram Moolenaar37127922016-02-06 20:29:28 +01009784 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009785 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009786 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009787 if (argvars[0].v_type != VAR_NUMBER
9788 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9789 || error)
9790 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009791 prepare_assert_error(&ga);
9792 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009793 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009794 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009795 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009796 ga_clear(&ga);
9797 }
9798}
9799
9800/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009801 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009802 */
9803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009805{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009806 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009807}
9808
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009809 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009810assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009811{
9812 garray_T ga;
9813 char_u buf1[NUMBUFLEN];
9814 char_u buf2[NUMBUFLEN];
9815 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9816 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9817
Bram Moolenaar72188e92016-03-28 22:48:29 +02009818 if (pat == NULL || text == NULL)
9819 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009820 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009821 {
9822 prepare_assert_error(&ga);
9823 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009824 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009825 assert_error(&ga);
9826 ga_clear(&ga);
9827 }
9828}
9829
9830/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009831 * "assert_match(pattern, actual[, msg])" function
9832 */
9833 static void
9834f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9835{
9836 assert_match_common(argvars, ASSERT_MATCH);
9837}
9838
9839/*
9840 * "assert_notmatch(pattern, actual[, msg])" function
9841 */
9842 static void
9843f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9844{
9845 assert_match_common(argvars, ASSERT_NOTMATCH);
9846}
9847
9848/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009849 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009850 */
9851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009853{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009854 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009855}
9856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009857#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009858/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009859 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009860 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009862f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009863{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009864 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009865
9866 rettv->v_type = VAR_FLOAT;
9867 if (get_float_arg(argvars, &f) == OK)
9868 rettv->vval.v_float = asin(f);
9869 else
9870 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009871}
9872
9873/*
9874 * "atan()" function
9875 */
9876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009878{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009879 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009880
9881 rettv->v_type = VAR_FLOAT;
9882 if (get_float_arg(argvars, &f) == OK)
9883 rettv->vval.v_float = atan(f);
9884 else
9885 rettv->vval.v_float = 0.0;
9886}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009887
9888/*
9889 * "atan2()" function
9890 */
9891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009892f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009893{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009894 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009895
9896 rettv->v_type = VAR_FLOAT;
9897 if (get_float_arg(argvars, &fx) == OK
9898 && get_float_arg(&argvars[1], &fy) == OK)
9899 rettv->vval.v_float = atan2(fx, fy);
9900 else
9901 rettv->vval.v_float = 0.0;
9902}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009903#endif
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905/*
9906 * "browse(save, title, initdir, default)" function
9907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911#ifdef FEAT_BROWSE
9912 int save;
9913 char_u *title;
9914 char_u *initdir;
9915 char_u *defname;
9916 char_u buf[NUMBUFLEN];
9917 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 save = get_tv_number_chk(&argvars[0], &error);
9921 title = get_tv_string_chk(&argvars[1]);
9922 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9923 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (error || title == NULL || initdir == NULL || defname == NULL)
9926 rettv->vval.v_string = NULL;
9927 else
9928 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009929 do_browse(save ? BROWSE_SAVE : 0,
9930 title, defname, NULL, initdir, NULL, curbuf);
9931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009935}
9936
9937/*
9938 * "browsedir(title, initdir)" function
9939 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009941f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009942{
9943#ifdef FEAT_BROWSE
9944 char_u *title;
9945 char_u *initdir;
9946 char_u buf[NUMBUFLEN];
9947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 title = get_tv_string_chk(&argvars[0]);
9949 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 if (title == NULL || initdir == NULL)
9952 rettv->vval.v_string = NULL;
9953 else
9954 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009955 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960}
9961
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009962static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009963
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964/*
9965 * Find a buffer by number or exact name.
9966 */
9967 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009968find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009972 if (avar->v_type == VAR_NUMBER)
9973 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009974 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009977 if (buf == NULL)
9978 {
9979 /* No full path name match, try a match with a URL or a "nofile"
9980 * buffer, these don't use the full path. */
9981 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9982 if (buf->b_fname != NULL
9983 && (path_with_url(buf->b_fname)
9984#ifdef FEAT_QUICKFIX
9985 || bt_nofile(buf)
9986#endif
9987 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009988 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009989 break;
9990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992 return buf;
9993}
9994
9995/*
9996 * "bufexists(expr)" function
9997 */
9998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009999f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002}
10003
10004/*
10005 * "buflisted(expr)" function
10006 */
10007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010008f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010 buf_T *buf;
10011
10012 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014}
10015
10016/*
10017 * "bufloaded(expr)" function
10018 */
10019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010020f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 buf_T *buf;
10023
10024 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026}
10027
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010028 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010029buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 int save_magic;
10032 char_u *save_cpo;
10033 buf_T *buf;
10034
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10036 save_magic = p_magic;
10037 p_magic = TRUE;
10038 save_cpo = p_cpo;
10039 p_cpo = (char_u *)"";
10040
10041 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010042 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 p_magic = save_magic;
10045 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010046 return buf;
10047}
10048
10049/*
10050 * Get buffer by number or pattern.
10051 */
10052 static buf_T *
10053get_buf_tv(typval_T *tv, int curtab_only)
10054{
10055 char_u *name = tv->vval.v_string;
10056 buf_T *buf;
10057
10058 if (tv->v_type == VAR_NUMBER)
10059 return buflist_findnr((int)tv->vval.v_number);
10060 if (tv->v_type != VAR_STRING)
10061 return NULL;
10062 if (name == NULL || *name == NUL)
10063 return curbuf;
10064 if (name[0] == '$' && name[1] == NUL)
10065 return lastbuf;
10066
10067 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
10069 /* If not found, try expanding the name, like done for bufexists(). */
10070 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
10073 return buf;
10074}
10075
10076/*
10077 * "bufname(expr)" function
10078 */
10079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010080f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 buf_T *buf;
10083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010086 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 --emsg_off;
10093}
10094
10095/*
10096 * "bufnr(expr)" function
10097 */
10098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010102 int error = FALSE;
10103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010108 --emsg_off;
10109
10110 /* If the buffer isn't found and the second argument is not zero create a
10111 * new buffer. */
10112 if (buf == NULL
10113 && argvars[1].v_type != VAR_UNKNOWN
10114 && get_tv_number_chk(&argvars[1], &error) != 0
10115 && !error
10116 && (name = get_tv_string_chk(&argvars[0])) != NULL
10117 && !error)
10118 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10119
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124}
10125
10126/*
10127 * "bufwinnr(nr)" function
10128 */
10129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010130f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131{
10132#ifdef FEAT_WINDOWS
10133 win_T *wp;
10134 int winnr = 0;
10135#endif
10136 buf_T *buf;
10137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010140 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#ifdef FEAT_WINDOWS
10142 for (wp = firstwin; wp; wp = wp->w_next)
10143 {
10144 ++winnr;
10145 if (wp->w_buffer == buf)
10146 break;
10147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#endif
10152 --emsg_off;
10153}
10154
10155/*
10156 * "byte2line(byte)" function
10157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010159f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160{
10161#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163#else
10164 long boff = 0;
10165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 (linenr_T)0, &boff);
10172#endif
10173}
10174
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010177{
10178#ifdef FEAT_MBYTE
10179 char_u *t;
10180#endif
10181 char_u *str;
10182 long idx;
10183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 str = get_tv_string_chk(&argvars[0]);
10185 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010188 return;
10189
10190#ifdef FEAT_MBYTE
10191 t = str;
10192 for ( ; idx > 0; idx--)
10193 {
10194 if (*t == NUL) /* EOL reached */
10195 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010196 if (enc_utf8 && comp)
10197 t += utf_ptr2len(t);
10198 else
10199 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010200 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010201 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010202#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010203 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010204 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010205#endif
10206}
10207
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010208/*
10209 * "byteidx()" function
10210 */
10211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010213{
10214 byteidx(argvars, rettv, FALSE);
10215}
10216
10217/*
10218 * "byteidxcomp()" function
10219 */
10220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010221f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010222{
10223 byteidx(argvars, rettv, TRUE);
10224}
10225
Bram Moolenaardb913952012-06-29 12:54:53 +020010226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010227func_call(
10228 char_u *name,
10229 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010230 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231 dict_T *selfdict,
10232 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010233{
10234 listitem_T *item;
10235 typval_T argv[MAX_FUNC_ARGS + 1];
10236 int argc = 0;
10237 int dummy;
10238 int r = 0;
10239
10240 for (item = args->vval.v_list->lv_first; item != NULL;
10241 item = item->li_next)
10242 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010243 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010244 {
10245 EMSG(_("E699: Too many arguments"));
10246 break;
10247 }
10248 /* Make a copy of each argument. This is needed to be able to set
10249 * v_lock to VAR_FIXED in the copy without changing the original list.
10250 */
10251 copy_tv(&item->li_tv, &argv[argc++]);
10252 }
10253
10254 if (item == NULL)
10255 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010257 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010258
10259 /* Free the arguments. */
10260 while (argc > 0)
10261 clear_tv(&argv[--argc]);
10262
10263 return r;
10264}
10265
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010266/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010267 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268 */
10269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010270f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010271{
10272 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010273 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010274 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010275
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 if (argvars[1].v_type != VAR_LIST)
10277 {
10278 EMSG(_(e_listreq));
10279 return;
10280 }
10281 if (argvars[1].vval.v_list == NULL)
10282 return;
10283
10284 if (argvars[0].v_type == VAR_FUNC)
10285 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010286 else if (argvars[0].v_type == VAR_PARTIAL)
10287 {
10288 partial = argvars[0].vval.v_partial;
10289 func = partial->pt_name;
10290 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010291 else
10292 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 if (*func == NUL)
10294 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010295
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 if (argvars[2].v_type != VAR_UNKNOWN)
10297 {
10298 if (argvars[2].v_type != VAR_DICT)
10299 {
10300 EMSG(_(e_dictreq));
10301 return;
10302 }
10303 selfdict = argvars[2].vval.v_dict;
10304 }
10305
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010306 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010307}
10308
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010309#ifdef FEAT_FLOAT
10310/*
10311 * "ceil({float})" function
10312 */
10313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010314f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010315{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010316 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010317
10318 rettv->v_type = VAR_FLOAT;
10319 if (get_float_arg(argvars, &f) == OK)
10320 rettv->vval.v_float = ceil(f);
10321 else
10322 rettv->vval.v_float = 0.0;
10323}
10324#endif
10325
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010327/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010328 * "ch_close()" function
10329 */
10330 static void
10331f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10332{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010333 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010334
10335 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010336 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010337 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010338 channel_clear(channel);
10339 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010340}
10341
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010342/*
10343 * "ch_getbufnr()" function
10344 */
10345 static void
10346f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10347{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010348 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010349
10350 rettv->vval.v_number = -1;
10351 if (channel != NULL)
10352 {
10353 char_u *what = get_tv_string(&argvars[1]);
10354 int part;
10355
10356 if (STRCMP(what, "err") == 0)
10357 part = PART_ERR;
10358 else if (STRCMP(what, "out") == 0)
10359 part = PART_OUT;
10360 else if (STRCMP(what, "in") == 0)
10361 part = PART_IN;
10362 else
10363 part = PART_SOCK;
10364 if (channel->ch_part[part].ch_buffer != NULL)
10365 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10366 }
10367}
10368
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010369/*
10370 * "ch_getjob()" function
10371 */
10372 static void
10373f_ch_getjob(typval_T *argvars, typval_T *rettv)
10374{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010375 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010376
10377 if (channel != NULL)
10378 {
10379 rettv->v_type = VAR_JOB;
10380 rettv->vval.v_job = channel->ch_job;
10381 if (channel->ch_job != NULL)
10382 ++channel->ch_job->jv_refcount;
10383 }
10384}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010385
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010386/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010387 * "ch_info()" function
10388 */
10389 static void
10390f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10391{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010392 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010393
10394 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10395 channel_info(channel, rettv->vval.v_dict);
10396}
10397
10398/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010399 * "ch_log()" function
10400 */
10401 static void
10402f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10403{
10404 char_u *msg = get_tv_string(&argvars[0]);
10405 channel_T *channel = NULL;
10406
10407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010408 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010409
10410 ch_log(channel, (char *)msg);
10411}
10412
10413/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010414 * "ch_logfile()" function
10415 */
10416 static void
10417f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10418{
10419 char_u *fname;
10420 char_u *opt = (char_u *)"";
10421 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010422
10423 fname = get_tv_string(&argvars[0]);
10424 if (argvars[1].v_type == VAR_STRING)
10425 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010426 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010427}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010428
10429/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010430 * "ch_open()" function
10431 */
10432 static void
10433f_ch_open(typval_T *argvars, typval_T *rettv)
10434{
Bram Moolenaar77073442016-02-13 23:23:53 +010010435 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010436 if (check_restricted() || check_secure())
10437 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010438 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010439}
10440
10441/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010442 * "ch_read()" function
10443 */
10444 static void
10445f_ch_read(typval_T *argvars, typval_T *rettv)
10446{
10447 common_channel_read(argvars, rettv, FALSE);
10448}
10449
10450/*
10451 * "ch_readraw()" function
10452 */
10453 static void
10454f_ch_readraw(typval_T *argvars, typval_T *rettv)
10455{
10456 common_channel_read(argvars, rettv, TRUE);
10457}
10458
10459/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010460 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010461 */
10462 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010463f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10464{
10465 ch_expr_common(argvars, rettv, TRUE);
10466}
10467
10468/*
10469 * "ch_sendexpr()" function
10470 */
10471 static void
10472f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10473{
10474 ch_expr_common(argvars, rettv, FALSE);
10475}
10476
10477/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010478 * "ch_evalraw()" function
10479 */
10480 static void
10481f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10482{
10483 ch_raw_common(argvars, rettv, TRUE);
10484}
10485
10486/*
10487 * "ch_sendraw()" function
10488 */
10489 static void
10490f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10491{
10492 ch_raw_common(argvars, rettv, FALSE);
10493}
10494
10495/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010496 * "ch_setoptions()" function
10497 */
10498 static void
10499f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10500{
10501 channel_T *channel;
10502 jobopt_T opt;
10503
Bram Moolenaar437905c2016-04-26 19:01:05 +020010504 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010505 if (channel == NULL)
10506 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010507 clear_job_options(&opt);
10508 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010509 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10510 channel_set_options(channel, &opt);
10511 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010512}
10513
10514/*
10515 * "ch_status()" function
10516 */
10517 static void
10518f_ch_status(typval_T *argvars, typval_T *rettv)
10519{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010520 channel_T *channel;
10521
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010522 /* return an empty string by default */
10523 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010524 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010525
Bram Moolenaar437905c2016-04-26 19:01:05 +020010526 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010527 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010528}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010529#endif
10530
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010531/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010532 * "changenr()" function
10533 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010535f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010536{
10537 rettv->vval.v_number = curbuf->b_u_seq_cur;
10538}
10539
10540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 * "char2nr(string)" function
10542 */
10543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010544f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546#ifdef FEAT_MBYTE
10547 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010548 {
10549 int utf8 = 0;
10550
10551 if (argvars[1].v_type != VAR_UNKNOWN)
10552 utf8 = get_tv_number_chk(&argvars[1], NULL);
10553
10554 if (utf8)
10555 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10556 else
10557 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 else
10560#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562}
10563
10564/*
10565 * "cindent(lnum)" function
10566 */
10567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010568f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570#ifdef FEAT_CINDENT
10571 pos_T pos;
10572 linenr_T lnum;
10573
10574 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10577 {
10578 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 curwin->w_cursor = pos;
10581 }
10582 else
10583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010588 * "clearmatches()" function
10589 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010591f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010592{
10593#ifdef FEAT_SEARCH_EXTRA
10594 clear_matches(curwin);
10595#endif
10596}
10597
10598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 * "col(string)" function
10600 */
10601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010602f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 colnr_T col = 0;
10605 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010606 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010608 fp = var2fpos(&argvars[0], FALSE, &fnum);
10609 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 {
10611 if (fp->col == MAXCOL)
10612 {
10613 /* '> can be MAXCOL, get the length of the line then */
10614 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010615 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 else
10617 col = MAXCOL;
10618 }
10619 else
10620 {
10621 col = fp->col + 1;
10622#ifdef FEAT_VIRTUALEDIT
10623 /* col(".") when the cursor is on the NUL at the end of the line
10624 * because of "coladd" can be seen as an extra column. */
10625 if (virtual_active() && fp == &curwin->w_cursor)
10626 {
10627 char_u *p = ml_get_cursor();
10628
10629 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10630 curwin->w_virtcol - curwin->w_cursor.coladd))
10631 {
10632# ifdef FEAT_MBYTE
10633 int l;
10634
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010635 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 col += l;
10637# else
10638 if (*p != NUL && p[1] == NUL)
10639 ++col;
10640# endif
10641 }
10642 }
10643#endif
10644 }
10645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647}
10648
Bram Moolenaar572cb562005-08-05 21:35:02 +000010649#if defined(FEAT_INS_EXPAND)
10650/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010651 * "complete()" function
10652 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010654f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010655{
10656 int startcol;
10657
10658 if ((State & INSERT) == 0)
10659 {
10660 EMSG(_("E785: complete() can only be used in Insert mode"));
10661 return;
10662 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010663
10664 /* Check for undo allowed here, because if something was already inserted
10665 * the line was already saved for undo and this check isn't done. */
10666 if (!undo_allowed())
10667 return;
10668
Bram Moolenaarade00832006-03-10 21:46:58 +000010669 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10670 {
10671 EMSG(_(e_invarg));
10672 return;
10673 }
10674
10675 startcol = get_tv_number_chk(&argvars[0], NULL);
10676 if (startcol <= 0)
10677 return;
10678
10679 set_completion(startcol - 1, argvars[1].vval.v_list);
10680}
10681
10682/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010683 * "complete_add()" function
10684 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010687{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010688 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010689}
10690
10691/*
10692 * "complete_check()" function
10693 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010695f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010696{
10697 int saved = RedrawingDisabled;
10698
10699 RedrawingDisabled = 0;
10700 ins_compl_check_keys(0);
10701 rettv->vval.v_number = compl_interrupted;
10702 RedrawingDisabled = saved;
10703}
10704#endif
10705
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706/*
10707 * "confirm(message, buttons[, default [, type]])" function
10708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10713 char_u *message;
10714 char_u *buttons = NULL;
10715 char_u buf[NUMBUFLEN];
10716 char_u buf2[NUMBUFLEN];
10717 int def = 1;
10718 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 char_u *typestr;
10720 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010722 message = get_tv_string_chk(&argvars[0]);
10723 if (message == NULL)
10724 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010725 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10728 if (buttons == NULL)
10729 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010735 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10736 if (typestr == NULL)
10737 error = TRUE;
10738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740 switch (TOUPPER_ASC(*typestr))
10741 {
10742 case 'E': type = VIM_ERROR; break;
10743 case 'Q': type = VIM_QUESTION; break;
10744 case 'I': type = VIM_INFO; break;
10745 case 'W': type = VIM_WARNING; break;
10746 case 'G': type = VIM_GENERIC; break;
10747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 }
10749 }
10750 }
10751 }
10752
10753 if (buttons == NULL || *buttons == NUL)
10754 buttons = (char_u *)_("&Ok");
10755
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010756 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010757 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010758 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#endif
10760}
10761
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010762/*
10763 * "copy()" function
10764 */
10765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010766f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010767{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010768 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010769}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010771#ifdef FEAT_FLOAT
10772/*
10773 * "cos()" function
10774 */
10775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010777{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010778 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010779
10780 rettv->v_type = VAR_FLOAT;
10781 if (get_float_arg(argvars, &f) == OK)
10782 rettv->vval.v_float = cos(f);
10783 else
10784 rettv->vval.v_float = 0.0;
10785}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010786
10787/*
10788 * "cosh()" function
10789 */
10790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010792{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010793 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010794
10795 rettv->v_type = VAR_FLOAT;
10796 if (get_float_arg(argvars, &f) == OK)
10797 rettv->vval.v_float = cosh(f);
10798 else
10799 rettv->vval.v_float = 0.0;
10800}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010801#endif
10802
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010804 * "count()" function
10805 */
10806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010807f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010808{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010809 long n = 0;
10810 int ic = FALSE;
10811
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 listitem_T *li;
10815 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010817
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 if ((l = argvars[0].vval.v_list) != NULL)
10819 {
10820 li = l->lv_first;
10821 if (argvars[2].v_type != VAR_UNKNOWN)
10822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 int error = FALSE;
10824
10825 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 if (argvars[3].v_type != VAR_UNKNOWN)
10827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 idx = get_tv_number_chk(&argvars[3], &error);
10829 if (!error)
10830 {
10831 li = list_find(l, idx);
10832 if (li == NULL)
10833 EMSGN(_(e_listidx), idx);
10834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 if (error)
10837 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 }
10839
10840 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010841 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 ++n;
10843 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 else if (argvars[0].v_type == VAR_DICT)
10846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 int todo;
10848 dict_T *d;
10849 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010851 if ((d = argvars[0].vval.v_dict) != NULL)
10852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 int error = FALSE;
10854
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 if (argvars[2].v_type != VAR_UNKNOWN)
10856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010857 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 if (argvars[3].v_type != VAR_UNKNOWN)
10859 EMSG(_(e_invarg));
10860 }
10861
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010862 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010863 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010864 {
10865 if (!HASHITEM_EMPTY(hi))
10866 {
10867 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010868 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010869 ++n;
10870 }
10871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 }
10873 }
10874 else
10875 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010876 rettv->vval.v_number = n;
10877}
10878
10879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10881 *
10882 * Checks the existence of a cscope connection.
10883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010885f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
10887#ifdef FEAT_CSCOPE
10888 int num = 0;
10889 char_u *dbpath = NULL;
10890 char_u *prepend = NULL;
10891 char_u buf[NUMBUFLEN];
10892
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010893 if (argvars[0].v_type != VAR_UNKNOWN
10894 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 num = (int)get_tv_number(&argvars[0]);
10897 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010898 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903#endif
10904}
10905
10906/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010907 * "cursor(lnum, col)" function, or
10908 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010910 * Moves the cursor to the specified line and column.
10911 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010914f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915{
10916 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010917#ifdef FEAT_VIRTUALEDIT
10918 long coladd = 0;
10919#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010920 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010922 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010923 if (argvars[1].v_type == VAR_UNKNOWN)
10924 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010925 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010926 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010927
Bram Moolenaar493c1782014-05-28 14:34:46 +020010928 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010929 {
10930 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010931 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010932 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010933 line = pos.lnum;
10934 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010935#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010936 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010937#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010938 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010939 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010940 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010941 set_curswant = FALSE;
10942 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010943 }
10944 else
10945 {
10946 line = get_tv_lnum(argvars);
10947 col = get_tv_number_chk(&argvars[1], NULL);
10948#ifdef FEAT_VIRTUALEDIT
10949 if (argvars[2].v_type != VAR_UNKNOWN)
10950 coladd = get_tv_number_chk(&argvars[2], NULL);
10951#endif
10952 }
10953 if (line < 0 || col < 0
10954#ifdef FEAT_VIRTUALEDIT
10955 || coladd < 0
10956#endif
10957 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (line > 0)
10960 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 if (col > 0)
10962 curwin->w_cursor.col = col - 1;
10963#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010964 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965#endif
10966
10967 /* Make sure the cursor is in a valid position. */
10968 check_cursor();
10969#ifdef FEAT_MBYTE
10970 /* Correct cursor for multi-byte character. */
10971 if (has_mbyte)
10972 mb_adjust_cursor();
10973#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010974
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010975 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010976 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
10979/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010980 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 */
10982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010983f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010985 int noref = 0;
10986
10987 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010989 if (noref < 0 || noref > 1)
10990 EMSG(_(e_invarg));
10991 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010992 {
10993 current_copyID += COPYID_INC;
10994 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996}
10997
10998/*
10999 * "delete()" function
11000 */
11001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011002f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011004 char_u nbuf[NUMBUFLEN];
11005 char_u *name;
11006 char_u *flags;
11007
11008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011010 return;
11011
11012 name = get_tv_string(&argvars[0]);
11013 if (name == NULL || *name == NUL)
11014 {
11015 EMSG(_(e_invarg));
11016 return;
11017 }
11018
11019 if (argvars[1].v_type != VAR_UNKNOWN)
11020 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011022 flags = (char_u *)"";
11023
11024 if (*flags == NUL)
11025 /* delete a file */
11026 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11027 else if (STRCMP(flags, "d") == 0)
11028 /* delete an empty directory */
11029 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11030 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011031 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011032 rettv->vval.v_number = delete_recursive(name);
11033 else
11034 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035}
11036
11037/*
11038 * "did_filetype()" function
11039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011041f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045#endif
11046}
11047
11048/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011049 * "diff_filler()" function
11050 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011052f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011053{
11054#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011056#endif
11057}
11058
11059/*
11060 * "diff_hlID()" function
11061 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011063f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011064{
11065#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011067 static linenr_T prev_lnum = 0;
11068 static int changedtick = 0;
11069 static int fnum = 0;
11070 static int change_start = 0;
11071 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011072 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011073 int filler_lines;
11074 int col;
11075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 if (lnum < 0) /* ignore type error in {lnum} arg */
11077 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011078 if (lnum != prev_lnum
11079 || changedtick != curbuf->b_changedtick
11080 || fnum != curbuf->b_fnum)
11081 {
11082 /* New line, buffer, change: need to get the values. */
11083 filler_lines = diff_check(curwin, lnum);
11084 if (filler_lines < 0)
11085 {
11086 if (filler_lines == -1)
11087 {
11088 change_start = MAXCOL;
11089 change_end = -1;
11090 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11091 hlID = HLF_ADD; /* added line */
11092 else
11093 hlID = HLF_CHD; /* changed line */
11094 }
11095 else
11096 hlID = HLF_ADD; /* added line */
11097 }
11098 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011099 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011100 prev_lnum = lnum;
11101 changedtick = curbuf->b_changedtick;
11102 fnum = curbuf->b_fnum;
11103 }
11104
11105 if (hlID == HLF_CHD || hlID == HLF_TXD)
11106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011108 if (col >= change_start && col <= change_end)
11109 hlID = HLF_TXD; /* changed text */
11110 else
11111 hlID = HLF_CHD; /* changed line */
11112 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011113 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011114#endif
11115}
11116
11117/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011118 * "disable_char_avail_for_testing({expr})" function
11119 */
11120 static void
11121f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11122{
11123 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11124}
11125
11126/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011127 * "empty({expr})" function
11128 */
11129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011130f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011131{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011132 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011133
11134 switch (argvars[0].v_type)
11135 {
11136 case VAR_STRING:
11137 case VAR_FUNC:
11138 n = argvars[0].vval.v_string == NULL
11139 || *argvars[0].vval.v_string == NUL;
11140 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011141 case VAR_PARTIAL:
11142 n = FALSE;
11143 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011144 case VAR_NUMBER:
11145 n = argvars[0].vval.v_number == 0;
11146 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011147 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011148#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011149 n = argvars[0].vval.v_float == 0.0;
11150 break;
11151#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011152 case VAR_LIST:
11153 n = argvars[0].vval.v_list == NULL
11154 || argvars[0].vval.v_list->lv_first == NULL;
11155 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011156 case VAR_DICT:
11157 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011159 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011160 case VAR_SPECIAL:
11161 n = argvars[0].vval.v_number != VVAL_TRUE;
11162 break;
11163
Bram Moolenaar835dc632016-02-07 14:27:38 +010011164 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011165#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011166 n = argvars[0].vval.v_job == NULL
11167 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11168 break;
11169#endif
11170 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011172 n = argvars[0].vval.v_channel == NULL
11173 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011174 break;
11175#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011176 case VAR_UNKNOWN:
11177 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11178 n = TRUE;
11179 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011180 }
11181
11182 rettv->vval.v_number = n;
11183}
11184
11185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 * "escape({string}, {chars})" function
11187 */
11188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011189f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
11191 char_u buf[NUMBUFLEN];
11192
Bram Moolenaar758711c2005-02-02 23:11:38 +000011193 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11194 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196}
11197
11198/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011199 * "eval()" function
11200 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011202f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011203{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011204 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 s = get_tv_string_chk(&argvars[0]);
11207 if (s != NULL)
11208 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011209
Bram Moolenaar615b9972015-01-14 17:15:05 +010011210 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11212 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011213 if (p != NULL && !aborting())
11214 EMSG2(_(e_invexpr2), p);
11215 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011217 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011219 else if (*s != NUL)
11220 EMSG(_(e_trailing));
11221}
11222
11223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 * "eventhandler()" function
11225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011227f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230}
11231
11232/*
11233 * "executable()" function
11234 */
11235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011236f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011238 char_u *name = get_tv_string(&argvars[0]);
11239
11240 /* Check in $PATH and also check directly if there is a directory name. */
11241 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11242 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011243}
11244
11245/*
11246 * "exepath()" function
11247 */
11248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011249f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011250{
11251 char_u *p = NULL;
11252
Bram Moolenaarb5971142015-03-21 17:32:19 +010011253 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011254 rettv->v_type = VAR_STRING;
11255 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256}
11257
11258/*
11259 * "exists()" function
11260 */
11261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011262f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
11264 char_u *p;
11265 char_u *name;
11266 int n = FALSE;
11267 int len = 0;
11268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 if (*p == '$') /* environment variable */
11271 {
11272 /* first try "normal" environment variables (fast) */
11273 if (mch_getenv(p + 1) != NULL)
11274 n = TRUE;
11275 else
11276 {
11277 /* try expanding things like $VIM and ${HOME} */
11278 p = expand_env_save(p);
11279 if (p != NULL && *p != '$')
11280 n = TRUE;
11281 vim_free(p);
11282 }
11283 }
11284 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011287 if (*skipwhite(p) != NUL)
11288 n = FALSE; /* trailing garbage */
11289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 else if (*p == '*') /* internal or user defined function */
11291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011292 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 }
11294 else if (*p == ':')
11295 {
11296 n = cmd_exists(p + 1);
11297 }
11298 else if (*p == '#')
11299 {
11300#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011301 if (p[1] == '#')
11302 n = autocmd_supported(p + 2);
11303 else
11304 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305#endif
11306 }
11307 else /* internal variable */
11308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011309 char_u *tofree;
11310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011312 /* get_name_len() takes care of expanding curly braces */
11313 name = p;
11314 len = get_name_len(&p, &tofree, TRUE, FALSE);
11315 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011317 if (tofree != NULL)
11318 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011319 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011320 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011322 /* handle d.key, l[idx], f(expr) */
11323 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11324 if (n)
11325 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 }
11327 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011328 if (*p != NUL)
11329 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011331 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
11333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335}
11336
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011337#ifdef FEAT_FLOAT
11338/*
11339 * "exp()" function
11340 */
11341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011342f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011343{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011344 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011345
11346 rettv->v_type = VAR_FLOAT;
11347 if (get_float_arg(argvars, &f) == OK)
11348 rettv->vval.v_float = exp(f);
11349 else
11350 rettv->vval.v_float = 0.0;
11351}
11352#endif
11353
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354/*
11355 * "expand()" function
11356 */
11357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011358f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
11360 char_u *s;
11361 int len;
11362 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011363 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011366 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011369 if (argvars[1].v_type != VAR_UNKNOWN
11370 && argvars[2].v_type != VAR_UNKNOWN
11371 && get_tv_number_chk(&argvars[2], &error)
11372 && !error)
11373 {
11374 rettv->v_type = VAR_LIST;
11375 rettv->vval.v_list = NULL;
11376 }
11377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 if (*s == '%' || *s == '#' || *s == '<')
11380 {
11381 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011382 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011384 if (rettv->v_type == VAR_LIST)
11385 {
11386 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11387 list_append_string(rettv->vval.v_list, result, -1);
11388 else
11389 vim_free(result);
11390 }
11391 else
11392 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 }
11394 else
11395 {
11396 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011397 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 if (argvars[1].v_type != VAR_UNKNOWN
11399 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011400 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 if (!error)
11402 {
11403 ExpandInit(&xpc);
11404 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011405 if (p_wic)
11406 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011407 if (rettv->v_type == VAR_STRING)
11408 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11409 options, WILD_ALL);
11410 else if (rettv_list_alloc(rettv) != FAIL)
11411 {
11412 int i;
11413
11414 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11415 for (i = 0; i < xpc.xp_numfiles; i++)
11416 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11417 ExpandCleanup(&xpc);
11418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419 }
11420 else
11421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 }
11423}
11424
11425/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011426 * Go over all entries in "d2" and add them to "d1".
11427 * When "action" is "error" then a duplicate key is an error.
11428 * When "action" is "force" then a duplicate key is overwritten.
11429 * Otherwise duplicate keys are ignored ("action" is "keep").
11430 */
11431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011432dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011433{
11434 dictitem_T *di1;
11435 hashitem_T *hi2;
11436 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011437 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011438
11439 todo = (int)d2->dv_hashtab.ht_used;
11440 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11441 {
11442 if (!HASHITEM_EMPTY(hi2))
11443 {
11444 --todo;
11445 di1 = dict_find(d1, hi2->hi_key, -1);
11446 if (d1->dv_scope != 0)
11447 {
11448 /* Disallow replacing a builtin function in l: and g:.
11449 * Check the key to be valid when adding to any
11450 * scope. */
11451 if (d1->dv_scope == VAR_DEF_SCOPE
11452 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11453 && var_check_func_name(hi2->hi_key,
11454 di1 == NULL))
11455 break;
11456 if (!valid_varname(hi2->hi_key))
11457 break;
11458 }
11459 if (di1 == NULL)
11460 {
11461 di1 = dictitem_copy(HI2DI(hi2));
11462 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11463 dictitem_free(di1);
11464 }
11465 else if (*action == 'e')
11466 {
11467 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11468 break;
11469 }
11470 else if (*action == 'f' && HI2DI(hi2) != di1)
11471 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011472 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11473 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011474 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011475 clear_tv(&di1->di_tv);
11476 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11477 }
11478 }
11479 }
11480}
11481
11482/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011483 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011484 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011485 */
11486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011487f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011488{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011489 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011490
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011492 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 list_T *l1, *l2;
11494 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011497
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498 l1 = argvars[0].vval.v_list;
11499 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011500 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011501 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 {
11503 if (argvars[2].v_type != VAR_UNKNOWN)
11504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011505 before = get_tv_number_chk(&argvars[2], &error);
11506 if (error)
11507 return; /* type error; errmsg already given */
11508
Bram Moolenaar758711c2005-02-02 23:11:38 +000011509 if (before == l1->lv_len)
11510 item = NULL;
11511 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011513 item = list_find(l1, before);
11514 if (item == NULL)
11515 {
11516 EMSGN(_(e_listidx), before);
11517 return;
11518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011519 }
11520 }
11521 else
11522 item = NULL;
11523 list_extend(l1, l2, item);
11524
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 copy_tv(&argvars[0], rettv);
11526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11529 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011530 dict_T *d1, *d2;
11531 char_u *action;
11532 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011533
11534 d1 = argvars[0].vval.v_dict;
11535 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011536 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011537 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538 {
11539 /* Check the third argument. */
11540 if (argvars[2].v_type != VAR_UNKNOWN)
11541 {
11542 static char *(av[]) = {"keep", "force", "error"};
11543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 action = get_tv_string_chk(&argvars[2]);
11545 if (action == NULL)
11546 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011547 for (i = 0; i < 3; ++i)
11548 if (STRCMP(action, av[i]) == 0)
11549 break;
11550 if (i == 3)
11551 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011552 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 return;
11554 }
11555 }
11556 else
11557 action = (char_u *)"force";
11558
Bram Moolenaara9922d62013-05-30 13:01:18 +020011559 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011560
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 copy_tv(&argvars[0], rettv);
11562 }
11563 }
11564 else
11565 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011566}
11567
11568/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011569 * "feedkeys()" function
11570 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011572f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011573{
11574 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011575 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011576 char_u *keys, *flags;
11577 char_u nbuf[NUMBUFLEN];
11578 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011579 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011580 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011581 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011582
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011583 /* This is not allowed in the sandbox. If the commands would still be
11584 * executed in the sandbox it would be OK, but it probably happens later,
11585 * when "sandbox" is no longer set. */
11586 if (check_secure())
11587 return;
11588
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011589 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011590
11591 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011592 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011593 flags = get_tv_string_buf(&argvars[1], nbuf);
11594 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011595 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011596 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011597 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011598 case 'n': remap = FALSE; break;
11599 case 'm': remap = TRUE; break;
11600 case 't': typed = TRUE; break;
11601 case 'i': insert = TRUE; break;
11602 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011603 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011604 }
11605 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011606 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011607
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011608 if (*keys != NUL || execute)
11609 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011610 /* Need to escape K_SPECIAL and CSI before putting the string in the
11611 * typeahead buffer. */
11612 keys_esc = vim_strsave_escape_csi(keys);
11613 if (keys_esc != NULL)
11614 {
11615 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011616 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011617 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011618 if (vgetc_busy)
11619 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011620 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011621 {
11622 int save_msg_scroll = msg_scroll;
11623
11624 /* Avoid a 1 second delay when the keys start Insert mode. */
11625 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011626
Bram Moolenaar245c4102016-04-20 17:37:41 +020011627 if (!dangerous)
11628 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011629 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011630 if (!dangerous)
11631 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011632 msg_scroll |= save_msg_scroll;
11633 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011634 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011635 }
11636}
11637
11638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 * "filereadable()" function
11640 */
11641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011642f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011644 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 char_u *p;
11646 int n;
11647
Bram Moolenaarc236c162008-07-13 17:41:49 +000011648#ifndef O_NONBLOCK
11649# define O_NONBLOCK 0
11650#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011652 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11653 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 {
11655 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011656 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 }
11658 else
11659 n = FALSE;
11660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662}
11663
11664/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011665 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 * rights to write into.
11667 */
11668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011669f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011671 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672}
11673
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011674 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011675findfilendir(
11676 typval_T *argvars UNUSED,
11677 typval_T *rettv,
11678 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011679{
11680#ifdef FEAT_SEARCHPATH
11681 char_u *fname;
11682 char_u *fresult = NULL;
11683 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11684 char_u *p;
11685 char_u pathbuf[NUMBUFLEN];
11686 int count = 1;
11687 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011688 int error = FALSE;
11689#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011690
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011691 rettv->vval.v_string = NULL;
11692 rettv->v_type = VAR_STRING;
11693
11694#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011696
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011697 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11700 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011701 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702 else
11703 {
11704 if (*p != NUL)
11705 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011708 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710 }
11711
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011712 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11713 error = TRUE;
11714
11715 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 do
11718 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011719 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011720 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 fresult = find_file_in_path_option(first ? fname : NULL,
11722 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011723 0, first, path,
11724 find_what,
11725 curbuf->b_ffname,
11726 find_what == FINDFILE_DIR
11727 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011728 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011729
11730 if (fresult != NULL && rettv->v_type == VAR_LIST)
11731 list_append_string(rettv->vval.v_list, fresult, -1);
11732
11733 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011734 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011735
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011736 if (rettv->v_type == VAR_STRING)
11737 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011738#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011739}
11740
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011741static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11742static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011743
11744/*
11745 * Implementation of map() and filter().
11746 */
11747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011748filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011749{
11750 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011752 listitem_T *li, *nli;
11753 list_T *l = NULL;
11754 dictitem_T *di;
11755 hashtab_T *ht;
11756 hashitem_T *hi;
11757 dict_T *d = NULL;
11758 typval_T save_val;
11759 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011760 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011761 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011762 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011763 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011764 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011765 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011766 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011767
Bram Moolenaare9a41262005-01-15 22:18:47 +000011768 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011769 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011770 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011771 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 return;
11773 }
11774 else if (argvars[0].v_type == VAR_DICT)
11775 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011776 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011777 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 return;
11779 }
11780 else
11781 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011782 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011783 return;
11784 }
11785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 expr = get_tv_string_buf_chk(&argvars[1], buf);
11787 /* On type errors, the preceding call has already displayed an error
11788 * message. Avoid a misleading error message for an empty string that
11789 * was not passed as argument. */
11790 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 prepare_vimvar(VV_VAL, &save_val);
11793 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011794
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011795 /* We reset "did_emsg" to be able to detect whether an error
11796 * occurred during evaluation of the expression. */
11797 save_did_emsg = did_emsg;
11798 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011799
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011800 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 vimvars[VV_KEY].vv_type = VAR_STRING;
11804
11805 ht = &d->dv_hashtab;
11806 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011807 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 if (!HASHITEM_EMPTY(hi))
11811 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011812 int r;
11813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 --todo;
11815 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011816 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011817 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11818 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 break;
11820 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011821 r = filter_map_one(&di->di_tv, expr, map, &rem);
11822 clear_tv(&vimvars[VV_KEY].vv_tv);
11823 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 break;
11825 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011826 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011827 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11828 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011829 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011830 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011831 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 }
11833 }
11834 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011835 }
11836 else
11837 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011838 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 for (li = l->lv_first; li != NULL; li = nli)
11841 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011842 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011843 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011845 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011846 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011847 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011848 break;
11849 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011851 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011852 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011853 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011854
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011855 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011857
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011858 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860
11861 copy_tv(&argvars[0], rettv);
11862}
11863
11864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011865filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866{
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011869 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870
Bram Moolenaar33570922005-01-25 22:26:29 +000011871 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 s = expr;
11873 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011874 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011875 if (*s != NUL) /* check for trailing chars after expr */
11876 {
11877 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011878 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011879 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880 }
11881 if (map)
11882 {
11883 /* map(): replace the list item value */
11884 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011885 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011886 *tv = rettv;
11887 }
11888 else
11889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 int error = FALSE;
11891
Bram Moolenaare9a41262005-01-15 22:18:47 +000011892 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011893 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011894 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011895 /* On type error, nothing has been removed; return FAIL to stop the
11896 * loop. The error message was given by get_tv_number_chk(). */
11897 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011898 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011899 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011900 retval = OK;
11901theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011903 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904}
11905
11906/*
11907 * "filter()" function
11908 */
11909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011910f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011911{
11912 filter_map(argvars, rettv, FALSE);
11913}
11914
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011915/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916 * "finddir({fname}[, {path}[, {count}]])" function
11917 */
11918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011919f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011921 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011922}
11923
11924/*
11925 * "findfile({fname}[, {path}[, {count}]])" function
11926 */
11927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011929{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011930 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011931}
11932
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011933#ifdef FEAT_FLOAT
11934/*
11935 * "float2nr({float})" function
11936 */
11937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011940 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011941
11942 if (get_float_arg(argvars, &f) == OK)
11943 {
11944 if (f < -0x7fffffff)
11945 rettv->vval.v_number = -0x7fffffff;
11946 else if (f > 0x7fffffff)
11947 rettv->vval.v_number = 0x7fffffff;
11948 else
11949 rettv->vval.v_number = (varnumber_T)f;
11950 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011951}
11952
11953/*
11954 * "floor({float})" function
11955 */
11956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011957f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011958{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011959 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011960
11961 rettv->v_type = VAR_FLOAT;
11962 if (get_float_arg(argvars, &f) == OK)
11963 rettv->vval.v_float = floor(f);
11964 else
11965 rettv->vval.v_float = 0.0;
11966}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011967
11968/*
11969 * "fmod()" function
11970 */
11971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011972f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011973{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011974 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011975
11976 rettv->v_type = VAR_FLOAT;
11977 if (get_float_arg(argvars, &fx) == OK
11978 && get_float_arg(&argvars[1], &fy) == OK)
11979 rettv->vval.v_float = fmod(fx, fy);
11980 else
11981 rettv->vval.v_float = 0.0;
11982}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011983#endif
11984
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011986 * "fnameescape({string})" function
11987 */
11988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011989f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011990{
11991 rettv->vval.v_string = vim_strsave_fnameescape(
11992 get_tv_string(&argvars[0]), FALSE);
11993 rettv->v_type = VAR_STRING;
11994}
11995
11996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 * "fnamemodify({fname}, {mods})" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
12002 char_u *fname;
12003 char_u *mods;
12004 int usedlen = 0;
12005 int len;
12006 char_u *fbuf = NULL;
12007 char_u buf[NUMBUFLEN];
12008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 fname = get_tv_string_chk(&argvars[0]);
12010 mods = get_tv_string_buf_chk(&argvars[1], buf);
12011 if (fname == NULL || mods == NULL)
12012 fname = NULL;
12013 else
12014 {
12015 len = (int)STRLEN(fname);
12016 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 vim_free(fbuf);
12025}
12026
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012027static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
12029/*
12030 * "foldclosed()" function
12031 */
12032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012033foldclosed_both(
12034 typval_T *argvars UNUSED,
12035 typval_T *rettv,
12036 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037{
12038#ifdef FEAT_FOLDING
12039 linenr_T lnum;
12040 linenr_T first, last;
12041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12044 {
12045 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12046 {
12047 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 return;
12052 }
12053 }
12054#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056}
12057
12058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059 * "foldclosed()" function
12060 */
12061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012062f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063{
12064 foldclosed_both(argvars, rettv, FALSE);
12065}
12066
12067/*
12068 * "foldclosedend()" function
12069 */
12070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012071f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072{
12073 foldclosed_both(argvars, rettv, TRUE);
12074}
12075
12076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 * "foldlevel()" function
12078 */
12079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012080f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
12082#ifdef FEAT_FOLDING
12083 linenr_T lnum;
12084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089}
12090
12091/*
12092 * "foldtext()" function
12093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012095f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
12097#ifdef FEAT_FOLDING
12098 linenr_T lnum;
12099 char_u *s;
12100 char_u *r;
12101 int len;
12102 char *txt;
12103#endif
12104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->v_type = VAR_STRING;
12106 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12109 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12110 <= curbuf->b_ml.ml_line_count
12111 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 {
12113 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12115 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 {
12117 if (!linewhite(lnum))
12118 break;
12119 ++lnum;
12120 }
12121
12122 /* Find interesting text in this line. */
12123 s = skipwhite(ml_get(lnum));
12124 /* skip C comment-start */
12125 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012128 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012129 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012130 {
12131 s = skipwhite(ml_get(lnum + 1));
12132 if (*s == '*')
12133 s = skipwhite(s + 1);
12134 }
12135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 txt = _("+-%s%3ld lines: ");
12137 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012138 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 + 20 /* for %3ld */
12140 + STRLEN(s))); /* concatenated */
12141 if (r != NULL)
12142 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012143 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12144 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12145 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 len = (int)STRLEN(r);
12147 STRCAT(r, s);
12148 /* remove 'foldmarker' and 'commentstring' */
12149 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 }
12152 }
12153#endif
12154}
12155
12156/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012157 * "foldtextresult(lnum)" function
12158 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012161{
12162#ifdef FEAT_FOLDING
12163 linenr_T lnum;
12164 char_u *text;
12165 char_u buf[51];
12166 foldinfo_T foldinfo;
12167 int fold_count;
12168#endif
12169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->v_type = VAR_STRING;
12171 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012172#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 /* treat illegal types and illegal string values for {lnum} the same */
12175 if (lnum < 0)
12176 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012177 fold_count = foldedCount(curwin, lnum, &foldinfo);
12178 if (fold_count > 0)
12179 {
12180 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12181 &foldinfo, buf);
12182 if (text == buf)
12183 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012185 }
12186#endif
12187}
12188
12189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 * "foreground()" function
12191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012193f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195#ifdef FEAT_GUI
12196 if (gui.in_use)
12197 gui_mch_set_foreground();
12198#else
12199# ifdef WIN32
12200 win32_set_foreground();
12201# endif
12202#endif
12203}
12204
12205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012206 * "function()" function
12207 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012209f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012210{
12211 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012212 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012213 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012214 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012215
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012216 if (argvars[0].v_type == VAR_FUNC)
12217 {
12218 /* function(MyFunc, [arg], dict) */
12219 s = argvars[0].vval.v_string;
12220 }
12221 else if (argvars[0].v_type == VAR_PARTIAL
12222 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012223 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012224 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012225 arg_pt = argvars[0].vval.v_partial;
12226 s = arg_pt->pt_name;
12227 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012228 else
12229 {
12230 /* function('MyFunc', [arg], dict) */
12231 s = get_tv_string(&argvars[0]);
12232 use_string = TRUE;
12233 }
12234
12235 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012236 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012237 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012238 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12239 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012240 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012241 else
12242 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012243 int dict_idx = 0;
12244 int arg_idx = 0;
12245 list_T *list = NULL;
12246
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012247 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012248 {
12249 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012250 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012251
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012252 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12253 * also be called from another script. Using trans_function_name()
12254 * would also work, but some plugins depend on the name being
12255 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012256 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012257 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12258 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012259 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012260 STRCPY(name, sid_buf);
12261 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012262 }
12263 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012264 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012265 name = vim_strsave(s);
12266
12267 if (argvars[1].v_type != VAR_UNKNOWN)
12268 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012269 if (argvars[2].v_type != VAR_UNKNOWN)
12270 {
12271 /* function(name, [args], dict) */
12272 arg_idx = 1;
12273 dict_idx = 2;
12274 }
12275 else if (argvars[1].v_type == VAR_DICT)
12276 /* function(name, dict) */
12277 dict_idx = 1;
12278 else
12279 /* function(name, [args]) */
12280 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012281 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012282 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012283 if (argvars[dict_idx].v_type != VAR_DICT)
12284 {
12285 EMSG(_("E922: expected a dict"));
12286 vim_free(name);
12287 return;
12288 }
12289 if (argvars[dict_idx].vval.v_dict == NULL)
12290 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012292 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012293 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012294 if (argvars[arg_idx].v_type != VAR_LIST)
12295 {
12296 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12297 vim_free(name);
12298 return;
12299 }
12300 list = argvars[arg_idx].vval.v_list;
12301 if (list == NULL || list->lv_len == 0)
12302 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012303 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012304 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012305 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012306 {
12307 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012308
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012309 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012310 if (pt == NULL)
12311 vim_free(name);
12312 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012313 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012314 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012315 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012316 listitem_T *li;
12317 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012318 int arg_len = 0;
12319 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012320
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012321 if (arg_pt != NULL)
12322 arg_len = arg_pt->pt_argc;
12323 if (list != NULL)
12324 lv_len = list->lv_len;
12325 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012327 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012328 if (pt->pt_argv == NULL)
12329 {
12330 vim_free(pt);
12331 vim_free(name);
12332 return;
12333 }
12334 else
12335 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012336 for (i = 0; i < arg_len; i++)
12337 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12338 if (lv_len > 0)
12339 for (li = list->lv_first; li != NULL;
12340 li = li->li_next)
12341 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012342 }
12343 }
12344
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012345 /* For "function(dict.func, [], dict)" and "func" is a partial
12346 * use "dict". That is backwards compatible. */
12347 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012348 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012349 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012350 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12351 ++pt->pt_dict->dv_refcount;
12352 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012353 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012354 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012355 /* If the dict was bound automatically the result is also
12356 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012357 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012358 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012359 if (pt->pt_dict != NULL)
12360 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012361 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012362
12363 pt->pt_refcount = 1;
12364 pt->pt_name = name;
12365 func_ref(pt->pt_name);
12366 }
12367 rettv->v_type = VAR_PARTIAL;
12368 rettv->vval.v_partial = pt;
12369 }
12370 else
12371 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012372 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012373 rettv->v_type = VAR_FUNC;
12374 rettv->vval.v_string = name;
12375 func_ref(name);
12376 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012377 }
12378}
12379
12380/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012381 * "garbagecollect()" function
12382 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012384f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012385{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012386 /* This is postponed until we are back at the toplevel, because we may be
12387 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12388 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012389
12390 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12391 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012392}
12393
12394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012395 * "get()" function
12396 */
12397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012398f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399{
Bram Moolenaar33570922005-01-25 22:26:29 +000012400 listitem_T *li;
12401 list_T *l;
12402 dictitem_T *di;
12403 dict_T *d;
12404 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405
Bram Moolenaare9a41262005-01-15 22:18:47 +000012406 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 int error = FALSE;
12411
12412 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12413 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012417 else if (argvars[0].v_type == VAR_DICT)
12418 {
12419 if ((d = argvars[0].vval.v_dict) != NULL)
12420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012421 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012422 if (di != NULL)
12423 tv = &di->di_tv;
12424 }
12425 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012426 else if (argvars[0].v_type == VAR_PARTIAL)
12427 {
12428 partial_T *pt = argvars[0].vval.v_partial;
12429
12430 if (pt != NULL)
12431 {
12432 char_u *what = get_tv_string(&argvars[1]);
12433
12434 if (STRCMP(what, "func") == 0)
12435 {
12436 rettv->v_type = VAR_STRING;
12437 if (pt->pt_name == NULL)
12438 rettv->vval.v_string = NULL;
12439 else
12440 rettv->vval.v_string = vim_strsave(pt->pt_name);
12441 }
12442 else if (STRCMP(what, "dict") == 0)
12443 {
12444 rettv->v_type = VAR_DICT;
12445 rettv->vval.v_dict = pt->pt_dict;
12446 if (pt->pt_dict != NULL)
12447 ++pt->pt_dict->dv_refcount;
12448 }
12449 else if (STRCMP(what, "args") == 0)
12450 {
12451 rettv->v_type = VAR_LIST;
12452 if (rettv_list_alloc(rettv) == OK)
12453 {
12454 int i;
12455
12456 for (i = 0; i < pt->pt_argc; ++i)
12457 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12458 }
12459 }
12460 else
12461 EMSG2(_(e_invarg2), what);
12462 return;
12463 }
12464 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012465 else
12466 EMSG2(_(e_listdictarg), "get()");
12467
12468 if (tv == NULL)
12469 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012470 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012471 copy_tv(&argvars[2], rettv);
12472 }
12473 else
12474 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012475}
12476
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012477static 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 +000012478
12479/*
12480 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012481 * Return a range (from start to end) of lines in rettv from the specified
12482 * buffer.
12483 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012484 */
12485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486get_buffer_lines(
12487 buf_T *buf,
12488 linenr_T start,
12489 linenr_T end,
12490 int retlist,
12491 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012492{
12493 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012494
Bram Moolenaar959a1432013-12-14 12:17:38 +010012495 rettv->v_type = VAR_STRING;
12496 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012497 if (retlist && rettv_list_alloc(rettv) == FAIL)
12498 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012499
12500 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12501 return;
12502
12503 if (!retlist)
12504 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012505 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12506 p = ml_get_buf(buf, start, FALSE);
12507 else
12508 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012509 rettv->vval.v_string = vim_strsave(p);
12510 }
12511 else
12512 {
12513 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012514 return;
12515
12516 if (start < 1)
12517 start = 1;
12518 if (end > buf->b_ml.ml_line_count)
12519 end = buf->b_ml.ml_line_count;
12520 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012521 if (list_append_string(rettv->vval.v_list,
12522 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012523 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012524 }
12525}
12526
12527/*
12528 * "getbufline()" function
12529 */
12530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012531f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012532{
12533 linenr_T lnum;
12534 linenr_T end;
12535 buf_T *buf;
12536
12537 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12538 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012539 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012540 --emsg_off;
12541
Bram Moolenaar661b1822005-07-28 22:36:45 +000012542 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012543 if (argvars[2].v_type == VAR_UNKNOWN)
12544 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012545 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012546 end = get_tv_lnum_buf(&argvars[2], buf);
12547
Bram Moolenaar342337a2005-07-21 21:11:17 +000012548 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012549}
12550
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551/*
12552 * "getbufvar()" function
12553 */
12554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012555f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012556{
12557 buf_T *buf;
12558 buf_T *save_curbuf;
12559 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012560 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012561 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12564 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012565 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012566 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012567
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012568 rettv->v_type = VAR_STRING;
12569 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570
12571 if (buf != NULL && varname != NULL)
12572 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012573 /* set curbuf to be our buf, temporarily */
12574 save_curbuf = curbuf;
12575 curbuf = buf;
12576
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012578 {
12579 if (get_option_tv(&varname, rettv, TRUE) == OK)
12580 done = TRUE;
12581 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012582 else if (STRCMP(varname, "changedtick") == 0)
12583 {
12584 rettv->v_type = VAR_NUMBER;
12585 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012586 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012587 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012588 else
12589 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012590 /* Look up the variable. */
12591 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12592 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12593 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012595 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012596 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012597 done = TRUE;
12598 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012599 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012600
12601 /* restore previous notion of curbuf */
12602 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012603 }
12604
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012605 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12606 /* use the default value */
12607 copy_tv(&argvars[2], rettv);
12608
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609 --emsg_off;
12610}
12611
12612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613 * "getchar()" function
12614 */
12615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012616f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617{
12618 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012619 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012621 /* Position the cursor. Needed after a message that ends in a space. */
12622 windgoto(msg_row, msg_col);
12623
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624 ++no_mapping;
12625 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012626 for (;;)
12627 {
12628 if (argvars[0].v_type == VAR_UNKNOWN)
12629 /* getchar(): blocking wait. */
12630 n = safe_vgetc();
12631 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12632 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012633 n = vpeekc_any();
12634 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012635 /* illegal argument or getchar(0) and no char avail: return zero */
12636 n = 0;
12637 else
12638 /* getchar(0) and char avail: return char */
12639 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012640
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012641 if (n == K_IGNORE)
12642 continue;
12643 break;
12644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 --no_mapping;
12646 --allow_keys;
12647
Bram Moolenaar219b8702006-11-01 14:32:36 +000012648 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12649 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12650 vimvars[VV_MOUSE_COL].vv_nr = 0;
12651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012652 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 if (IS_SPECIAL(n) || mod_mask != 0)
12654 {
12655 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12656 int i = 0;
12657
12658 /* Turn a special key into three bytes, plus modifier. */
12659 if (mod_mask != 0)
12660 {
12661 temp[i++] = K_SPECIAL;
12662 temp[i++] = KS_MODIFIER;
12663 temp[i++] = mod_mask;
12664 }
12665 if (IS_SPECIAL(n))
12666 {
12667 temp[i++] = K_SPECIAL;
12668 temp[i++] = K_SECOND(n);
12669 temp[i++] = K_THIRD(n);
12670 }
12671#ifdef FEAT_MBYTE
12672 else if (has_mbyte)
12673 i += (*mb_char2bytes)(n, temp + i);
12674#endif
12675 else
12676 temp[i++] = n;
12677 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->v_type = VAR_STRING;
12679 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012680
12681#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012682 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012683 {
12684 int row = mouse_row;
12685 int col = mouse_col;
12686 win_T *win;
12687 linenr_T lnum;
12688# ifdef FEAT_WINDOWS
12689 win_T *wp;
12690# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012691 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012692
12693 if (row >= 0 && col >= 0)
12694 {
12695 /* Find the window at the mouse coordinates and compute the
12696 * text position. */
12697 win = mouse_find_win(&row, &col);
12698 (void)mouse_comp_pos(win, &row, &col, &lnum);
12699# ifdef FEAT_WINDOWS
12700 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012701 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012702# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012703 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012704 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12705 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12706 }
12707 }
12708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 }
12710}
12711
12712/*
12713 * "getcharmod()" function
12714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012716f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719}
12720
12721/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012722 * "getcharsearch()" function
12723 */
12724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012725f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012726{
12727 if (rettv_dict_alloc(rettv) != FAIL)
12728 {
12729 dict_T *dict = rettv->vval.v_dict;
12730
12731 dict_add_nr_str(dict, "char", 0L, last_csearch());
12732 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12733 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12734 }
12735}
12736
12737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 * "getcmdline()" function
12739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012741f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->v_type = VAR_STRING;
12744 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745}
12746
12747/*
12748 * "getcmdpos()" function
12749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012751f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754}
12755
12756/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012757 * "getcmdtype()" function
12758 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012760f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012761{
12762 rettv->v_type = VAR_STRING;
12763 rettv->vval.v_string = alloc(2);
12764 if (rettv->vval.v_string != NULL)
12765 {
12766 rettv->vval.v_string[0] = get_cmdline_type();
12767 rettv->vval.v_string[1] = NUL;
12768 }
12769}
12770
12771/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012772 * "getcmdwintype()" function
12773 */
12774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012775f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012776{
12777 rettv->v_type = VAR_STRING;
12778 rettv->vval.v_string = NULL;
12779#ifdef FEAT_CMDWIN
12780 rettv->vval.v_string = alloc(2);
12781 if (rettv->vval.v_string != NULL)
12782 {
12783 rettv->vval.v_string[0] = cmdwin_type;
12784 rettv->vval.v_string[1] = NUL;
12785 }
12786#endif
12787}
12788
12789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790 * "getcwd()" function
12791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012793f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012795 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012796 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012799 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012800
12801 wp = find_tabwin(&argvars[0], &argvars[1]);
12802 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012804 if (wp->w_localdir != NULL)
12805 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12806 else if(globaldir != NULL)
12807 rettv->vval.v_string = vim_strsave(globaldir);
12808 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012809 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012810 cwd = alloc(MAXPATHL);
12811 if (cwd != NULL)
12812 {
12813 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12814 rettv->vval.v_string = vim_strsave(cwd);
12815 vim_free(cwd);
12816 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012817 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012818#ifdef BACKSLASH_IN_FILENAME
12819 if (rettv->vval.v_string != NULL)
12820 slash_adjust(rettv->vval.v_string);
12821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 }
12823}
12824
12825/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012826 * "getfontname()" function
12827 */
12828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012829f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012830{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 rettv->v_type = VAR_STRING;
12832 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012833#ifdef FEAT_GUI
12834 if (gui.in_use)
12835 {
12836 GuiFont font;
12837 char_u *name = NULL;
12838
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012839 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012840 {
12841 /* Get the "Normal" font. Either the name saved by
12842 * hl_set_font_name() or from the font ID. */
12843 font = gui.norm_font;
12844 name = hl_get_font_name();
12845 }
12846 else
12847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012849 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12850 return;
12851 font = gui_mch_get_font(name, FALSE);
12852 if (font == NOFONT)
12853 return; /* Invalid font name, return empty string. */
12854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012856 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012857 gui_mch_free_font(font);
12858 }
12859#endif
12860}
12861
12862/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012863 * "getfperm({fname})" function
12864 */
12865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012866f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012867{
12868 char_u *fname;
12869 struct stat st;
12870 char_u *perm = NULL;
12871 char_u flags[] = "rwx";
12872 int i;
12873
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012877 if (mch_stat((char *)fname, &st) >= 0)
12878 {
12879 perm = vim_strsave((char_u *)"---------");
12880 if (perm != NULL)
12881 {
12882 for (i = 0; i < 9; i++)
12883 {
12884 if (st.st_mode & (1 << (8 - i)))
12885 perm[i] = flags[i % 3];
12886 }
12887 }
12888 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012890}
12891
12892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 * "getfsize({fname})" function
12894 */
12895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012896f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897{
12898 char_u *fname;
12899 struct stat st;
12900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904
12905 if (mch_stat((char *)fname, &st) >= 0)
12906 {
12907 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012908 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012910 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012912
12913 /* non-perfect check for overflow */
12914 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12915 rettv->vval.v_number = -2;
12916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 }
12918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920}
12921
12922/*
12923 * "getftime({fname})" function
12924 */
12925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012926f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927{
12928 char_u *fname;
12929 struct stat st;
12930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932
12933 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012934 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937}
12938
12939/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012940 * "getftype({fname})" function
12941 */
12942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012943f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012944{
12945 char_u *fname;
12946 struct stat st;
12947 char_u *type = NULL;
12948 char *t;
12949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012952 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012953 if (mch_lstat((char *)fname, &st) >= 0)
12954 {
12955#ifdef S_ISREG
12956 if (S_ISREG(st.st_mode))
12957 t = "file";
12958 else if (S_ISDIR(st.st_mode))
12959 t = "dir";
12960# ifdef S_ISLNK
12961 else if (S_ISLNK(st.st_mode))
12962 t = "link";
12963# endif
12964# ifdef S_ISBLK
12965 else if (S_ISBLK(st.st_mode))
12966 t = "bdev";
12967# endif
12968# ifdef S_ISCHR
12969 else if (S_ISCHR(st.st_mode))
12970 t = "cdev";
12971# endif
12972# ifdef S_ISFIFO
12973 else if (S_ISFIFO(st.st_mode))
12974 t = "fifo";
12975# endif
12976# ifdef S_ISSOCK
12977 else if (S_ISSOCK(st.st_mode))
12978 t = "fifo";
12979# endif
12980 else
12981 t = "other";
12982#else
12983# ifdef S_IFMT
12984 switch (st.st_mode & S_IFMT)
12985 {
12986 case S_IFREG: t = "file"; break;
12987 case S_IFDIR: t = "dir"; break;
12988# ifdef S_IFLNK
12989 case S_IFLNK: t = "link"; break;
12990# endif
12991# ifdef S_IFBLK
12992 case S_IFBLK: t = "bdev"; break;
12993# endif
12994# ifdef S_IFCHR
12995 case S_IFCHR: t = "cdev"; break;
12996# endif
12997# ifdef S_IFIFO
12998 case S_IFIFO: t = "fifo"; break;
12999# endif
13000# ifdef S_IFSOCK
13001 case S_IFSOCK: t = "socket"; break;
13002# endif
13003 default: t = "other";
13004 }
13005# else
13006 if (mch_isdir(fname))
13007 t = "dir";
13008 else
13009 t = "file";
13010# endif
13011#endif
13012 type = vim_strsave((char_u *)t);
13013 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013015}
13016
13017/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013018 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013019 */
13020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013021f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013022{
13023 linenr_T lnum;
13024 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013025 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013026
13027 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013028 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013029 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013030 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013031 retlist = FALSE;
13032 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013033 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013034 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013035 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013036 retlist = TRUE;
13037 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013038
Bram Moolenaar342337a2005-07-21 21:11:17 +000013039 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013040}
13041
13042/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013043 * "getmatches()" function
13044 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013046f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013047{
13048#ifdef FEAT_SEARCH_EXTRA
13049 dict_T *dict;
13050 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013051 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013052
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013053 if (rettv_list_alloc(rettv) == OK)
13054 {
13055 while (cur != NULL)
13056 {
13057 dict = dict_alloc();
13058 if (dict == NULL)
13059 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013060 if (cur->match.regprog == NULL)
13061 {
13062 /* match added with matchaddpos() */
13063 for (i = 0; i < MAXPOSMATCH; ++i)
13064 {
13065 llpos_T *llpos;
13066 char buf[6];
13067 list_T *l;
13068
13069 llpos = &cur->pos.pos[i];
13070 if (llpos->lnum == 0)
13071 break;
13072 l = list_alloc();
13073 if (l == NULL)
13074 break;
13075 list_append_number(l, (varnumber_T)llpos->lnum);
13076 if (llpos->col > 0)
13077 {
13078 list_append_number(l, (varnumber_T)llpos->col);
13079 list_append_number(l, (varnumber_T)llpos->len);
13080 }
13081 sprintf(buf, "pos%d", i + 1);
13082 dict_add_list(dict, buf, l);
13083 }
13084 }
13085 else
13086 {
13087 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13088 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013089 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013090 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13091 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013092# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013093 if (cur->conceal_char)
13094 {
13095 char_u buf[MB_MAXBYTES + 1];
13096
13097 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13098 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13099 }
13100# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013101 list_append_dict(rettv->vval.v_list, dict);
13102 cur = cur->next;
13103 }
13104 }
13105#endif
13106}
13107
13108/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013109 * "getpid()" function
13110 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013112f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013113{
13114 rettv->vval.v_number = mch_get_pid();
13115}
13116
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013118getpos_both(
13119 typval_T *argvars,
13120 typval_T *rettv,
13121 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013122{
Bram Moolenaara5525202006-03-02 22:52:09 +000013123 pos_T *fp;
13124 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013125 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013126
13127 if (rettv_list_alloc(rettv) == OK)
13128 {
13129 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013130 if (getcurpos)
13131 fp = &curwin->w_cursor;
13132 else
13133 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013134 if (fnum != -1)
13135 list_append_number(l, (varnumber_T)fnum);
13136 else
13137 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013138 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13139 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013140 list_append_number(l, (fp != NULL)
13141 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013142 : (varnumber_T)0);
13143 list_append_number(l,
13144#ifdef FEAT_VIRTUALEDIT
13145 (fp != NULL) ? (varnumber_T)fp->coladd :
13146#endif
13147 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013148 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013149 {
13150 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013151 list_append_number(l, curwin->w_curswant == MAXCOL ?
13152 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013153 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013154 }
13155 else
13156 rettv->vval.v_number = FALSE;
13157}
13158
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013159
13160/*
13161 * "getcurpos()" function
13162 */
13163 static void
13164f_getcurpos(typval_T *argvars, typval_T *rettv)
13165{
13166 getpos_both(argvars, rettv, TRUE);
13167}
13168
13169/*
13170 * "getpos(string)" function
13171 */
13172 static void
13173f_getpos(typval_T *argvars, typval_T *rettv)
13174{
13175 getpos_both(argvars, rettv, FALSE);
13176}
13177
Bram Moolenaara5525202006-03-02 22:52:09 +000013178/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013179 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013180 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013182f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013183{
13184#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013185 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013186#endif
13187
Bram Moolenaar2641f772005-03-25 21:58:17 +000013188#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013189 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013190 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013191 wp = NULL;
13192 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13193 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013194 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013195 if (wp == NULL)
13196 return;
13197 }
13198
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013199 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013200 }
13201#endif
13202}
13203
13204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 * "getreg()" function
13206 */
13207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013208f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209{
13210 char_u *strregname;
13211 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013212 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013213 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013214 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013216 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013218 strregname = get_tv_string_chk(&argvars[0]);
13219 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013220 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013222 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013223 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13224 return_list = get_tv_number_chk(&argvars[2], &error);
13225 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013228 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013229
13230 if (error)
13231 return;
13232
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233 regname = (strregname == NULL ? '"' : *strregname);
13234 if (regname == 0)
13235 regname = '"';
13236
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013237 if (return_list)
13238 {
13239 rettv->v_type = VAR_LIST;
13240 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13241 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013242 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013243 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013244 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013245 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013246 }
13247 else
13248 {
13249 rettv->v_type = VAR_STRING;
13250 rettv->vval.v_string = get_reg_contents(regname,
13251 arg2 ? GREG_EXPR_SRC : 0);
13252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253}
13254
13255/*
13256 * "getregtype()" function
13257 */
13258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013259f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260{
13261 char_u *strregname;
13262 int regname;
13263 char_u buf[NUMBUFLEN + 2];
13264 long reglen = 0;
13265
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013266 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 {
13268 strregname = get_tv_string_chk(&argvars[0]);
13269 if (strregname == NULL) /* type error; errmsg already given */
13270 {
13271 rettv->v_type = VAR_STRING;
13272 rettv->vval.v_string = NULL;
13273 return;
13274 }
13275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 else
13277 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013278 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
13280 regname = (strregname == NULL ? '"' : *strregname);
13281 if (regname == 0)
13282 regname = '"';
13283
13284 buf[0] = NUL;
13285 buf[1] = NUL;
13286 switch (get_reg_type(regname, &reglen))
13287 {
13288 case MLINE: buf[0] = 'V'; break;
13289 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 case MBLOCK:
13291 buf[0] = Ctrl_V;
13292 sprintf((char *)buf + 1, "%ld", reglen + 1);
13293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->v_type = VAR_STRING;
13296 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297}
13298
13299/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013300 * "gettabvar()" function
13301 */
13302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013303f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013304{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013305 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013306 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013307 dictitem_T *v;
13308 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013309 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013310
13311 rettv->v_type = VAR_STRING;
13312 rettv->vval.v_string = NULL;
13313
13314 varname = get_tv_string_chk(&argvars[1]);
13315 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13316 if (tp != NULL && varname != NULL)
13317 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013318 /* Set tp to be our tabpage, temporarily. Also set the window to the
13319 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013320 if (switch_win(&oldcurwin, &oldtabpage,
13321 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013322 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013323 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013324 /* look up the variable */
13325 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13326 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13327 if (v != NULL)
13328 {
13329 copy_tv(&v->di_tv, rettv);
13330 done = TRUE;
13331 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013332 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013333
13334 /* restore previous notion of curwin */
13335 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013336 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013337
13338 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013339 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013340}
13341
13342/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013343 * "gettabwinvar()" function
13344 */
13345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013346f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013347{
13348 getwinvar(argvars, rettv, 1);
13349}
13350
13351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 * "getwinposx()" function
13353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013355f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358#ifdef FEAT_GUI
13359 if (gui.in_use)
13360 {
13361 int x, y;
13362
13363 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013364 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365 }
13366#endif
13367}
13368
13369/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013370 * "win_findbuf()" function
13371 */
13372 static void
13373f_win_findbuf(typval_T *argvars, typval_T *rettv)
13374{
13375 if (rettv_list_alloc(rettv) != FAIL)
13376 win_findbuf(argvars, rettv->vval.v_list);
13377}
13378
13379/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013380 * "win_getid()" function
13381 */
13382 static void
13383f_win_getid(typval_T *argvars, typval_T *rettv)
13384{
13385 rettv->vval.v_number = win_getid(argvars);
13386}
13387
13388/*
13389 * "win_gotoid()" function
13390 */
13391 static void
13392f_win_gotoid(typval_T *argvars, typval_T *rettv)
13393{
13394 rettv->vval.v_number = win_gotoid(argvars);
13395}
13396
13397/*
13398 * "win_id2tabwin()" function
13399 */
13400 static void
13401f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13402{
13403 if (rettv_list_alloc(rettv) != FAIL)
13404 win_id2tabwin(argvars, rettv->vval.v_list);
13405}
13406
13407/*
13408 * "win_id2win()" function
13409 */
13410 static void
13411f_win_id2win(typval_T *argvars, typval_T *rettv)
13412{
13413 rettv->vval.v_number = win_id2win(argvars);
13414}
13415
13416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 * "getwinposy()" function
13418 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013420f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_GUI
13424 if (gui.in_use)
13425 {
13426 int x, y;
13427
13428 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013429 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 }
13431#endif
13432}
13433
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013434/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013435 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013436 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013437 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013438find_win_by_nr(
13439 typval_T *vp,
13440 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013441{
13442#ifdef FEAT_WINDOWS
13443 win_T *wp;
13444#endif
13445 int nr;
13446
13447 nr = get_tv_number_chk(vp, NULL);
13448
13449#ifdef FEAT_WINDOWS
13450 if (nr < 0)
13451 return NULL;
13452 if (nr == 0)
13453 return curwin;
13454
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013455 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13456 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013457 if (--nr <= 0)
13458 break;
13459 return wp;
13460#else
13461 if (nr == 0 || nr == 1)
13462 return curwin;
13463 return NULL;
13464#endif
13465}
13466
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013468 * Find window specified by "wvp" in tabpage "tvp".
13469 */
13470 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013471find_tabwin(
13472 typval_T *wvp, /* VAR_UNKNOWN for current window */
13473 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013474{
13475 win_T *wp = NULL;
13476 tabpage_T *tp = NULL;
13477 long n;
13478
13479 if (wvp->v_type != VAR_UNKNOWN)
13480 {
13481 if (tvp->v_type != VAR_UNKNOWN)
13482 {
13483 n = get_tv_number(tvp);
13484 if (n >= 0)
13485 tp = find_tabpage(n);
13486 }
13487 else
13488 tp = curtab;
13489
13490 if (tp != NULL)
13491 wp = find_win_by_nr(wvp, tp);
13492 }
13493 else
13494 wp = curwin;
13495
13496 return wp;
13497}
13498
13499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 * "getwinvar()" function
13501 */
13502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013503f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013505 getwinvar(argvars, rettv, 0);
13506}
13507
13508/*
13509 * getwinvar() and gettabwinvar()
13510 */
13511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013512getwinvar(
13513 typval_T *argvars,
13514 typval_T *rettv,
13515 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013516{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013517 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013519 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013520 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013521 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013522#ifdef FEAT_WINDOWS
13523 win_T *oldcurwin;
13524 tabpage_T *oldtabpage;
13525 int need_switch_win;
13526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013528#ifdef FEAT_WINDOWS
13529 if (off == 1)
13530 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13531 else
13532 tp = curtab;
13533#endif
13534 win = find_win_by_nr(&argvars[off], tp);
13535 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013538 rettv->v_type = VAR_STRING;
13539 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540
13541 if (win != NULL && varname != NULL)
13542 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013543#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013544 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013545 * otherwise the window is not valid. Only do this when needed,
13546 * autocommands get blocked. */
13547 need_switch_win = !(tp == curtab && win == curwin);
13548 if (!need_switch_win
13549 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13550#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013551 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013552 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013553 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013554 if (get_option_tv(&varname, rettv, 1) == OK)
13555 done = TRUE;
13556 }
13557 else
13558 {
13559 /* Look up the variable. */
13560 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13561 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13562 varname, FALSE);
13563 if (v != NULL)
13564 {
13565 copy_tv(&v->di_tv, rettv);
13566 done = TRUE;
13567 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013570
Bram Moolenaarba117c22015-09-29 16:53:22 +020013571#ifdef FEAT_WINDOWS
13572 if (need_switch_win)
13573 /* restore previous notion of curwin */
13574 restore_win(oldcurwin, oldtabpage, TRUE);
13575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 }
13577
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013578 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13579 /* use the default return value */
13580 copy_tv(&argvars[off + 2], rettv);
13581
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582 --emsg_off;
13583}
13584
13585/*
13586 * "glob()" function
13587 */
13588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013589f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013591 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013593 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013595 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013596 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013597 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013598 if (argvars[1].v_type != VAR_UNKNOWN)
13599 {
13600 if (get_tv_number_chk(&argvars[1], &error))
13601 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013602 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013603 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013604 if (get_tv_number_chk(&argvars[2], &error))
13605 {
13606 rettv->v_type = VAR_LIST;
13607 rettv->vval.v_list = NULL;
13608 }
13609 if (argvars[3].v_type != VAR_UNKNOWN
13610 && get_tv_number_chk(&argvars[3], &error))
13611 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013612 }
13613 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013614 if (!error)
13615 {
13616 ExpandInit(&xpc);
13617 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013618 if (p_wic)
13619 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013620 if (rettv->v_type == VAR_STRING)
13621 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013622 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013623 else if (rettv_list_alloc(rettv) != FAIL)
13624 {
13625 int i;
13626
13627 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13628 NULL, options, WILD_ALL_KEEP);
13629 for (i = 0; i < xpc.xp_numfiles; i++)
13630 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13631
13632 ExpandCleanup(&xpc);
13633 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013634 }
13635 else
13636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637}
13638
13639/*
13640 * "globpath()" function
13641 */
13642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013643f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013645 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013647 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013648 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013649 garray_T ga;
13650 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013652 /* When the optional second argument is non-zero, don't remove matches
13653 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013655 if (argvars[2].v_type != VAR_UNKNOWN)
13656 {
13657 if (get_tv_number_chk(&argvars[2], &error))
13658 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013659 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013660 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013661 if (get_tv_number_chk(&argvars[3], &error))
13662 {
13663 rettv->v_type = VAR_LIST;
13664 rettv->vval.v_list = NULL;
13665 }
13666 if (argvars[4].v_type != VAR_UNKNOWN
13667 && get_tv_number_chk(&argvars[4], &error))
13668 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013669 }
13670 }
13671 if (file != NULL && !error)
13672 {
13673 ga_init2(&ga, (int)sizeof(char_u *), 10);
13674 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13675 if (rettv->v_type == VAR_STRING)
13676 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13677 else if (rettv_list_alloc(rettv) != FAIL)
13678 for (i = 0; i < ga.ga_len; ++i)
13679 list_append_string(rettv->vval.v_list,
13680 ((char_u **)(ga.ga_data))[i], -1);
13681 ga_clear_strings(&ga);
13682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013683 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013684 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685}
13686
13687/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013688 * "glob2regpat()" function
13689 */
13690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013691f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013692{
13693 char_u *pat = get_tv_string_chk(&argvars[0]);
13694
13695 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013696 rettv->vval.v_string = (pat == NULL)
13697 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013698}
13699
13700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 * "has()" function
13702 */
13703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013704f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705{
13706 int i;
13707 char_u *name;
13708 int n = FALSE;
13709 static char *(has_list[]) =
13710 {
13711#ifdef AMIGA
13712 "amiga",
13713# ifdef FEAT_ARP
13714 "arp",
13715# endif
13716#endif
13717#ifdef __BEOS__
13718 "beos",
13719#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013720#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 "mac",
13722#endif
13723#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013724 "macunix", /* built with 'darwin' enabled */
13725#endif
13726#if defined(__APPLE__) && __APPLE__ == 1
13727 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729#ifdef __QNX__
13730 "qnx",
13731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#ifdef UNIX
13733 "unix",
13734#endif
13735#ifdef VMS
13736 "vms",
13737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738#ifdef WIN32
13739 "win32",
13740#endif
13741#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13742 "win32unix",
13743#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013744#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 "win64",
13746#endif
13747#ifdef EBCDIC
13748 "ebcdic",
13749#endif
13750#ifndef CASE_INSENSITIVE_FILENAME
13751 "fname_case",
13752#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013753#ifdef HAVE_ACL
13754 "acl",
13755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#ifdef FEAT_ARABIC
13757 "arabic",
13758#endif
13759#ifdef FEAT_AUTOCMD
13760 "autocmd",
13761#endif
13762#ifdef FEAT_BEVAL
13763 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013764# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13765 "balloon_multiline",
13766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#endif
13768#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13769 "builtin_terms",
13770# ifdef ALL_BUILTIN_TCAPS
13771 "all_builtin_terms",
13772# endif
13773#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013774#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13775 || defined(FEAT_GUI_W32) \
13776 || defined(FEAT_GUI_MOTIF))
13777 "browsefilter",
13778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#ifdef FEAT_BYTEOFF
13780 "byte_offset",
13781#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013782#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013783 "channel",
13784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785#ifdef FEAT_CINDENT
13786 "cindent",
13787#endif
13788#ifdef FEAT_CLIENTSERVER
13789 "clientserver",
13790#endif
13791#ifdef FEAT_CLIPBOARD
13792 "clipboard",
13793#endif
13794#ifdef FEAT_CMDL_COMPL
13795 "cmdline_compl",
13796#endif
13797#ifdef FEAT_CMDHIST
13798 "cmdline_hist",
13799#endif
13800#ifdef FEAT_COMMENTS
13801 "comments",
13802#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013803#ifdef FEAT_CONCEAL
13804 "conceal",
13805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806#ifdef FEAT_CRYPT
13807 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013808 "crypt-blowfish",
13809 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810#endif
13811#ifdef FEAT_CSCOPE
13812 "cscope",
13813#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013814#ifdef FEAT_CURSORBIND
13815 "cursorbind",
13816#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013817#ifdef CURSOR_SHAPE
13818 "cursorshape",
13819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820#ifdef DEBUG
13821 "debug",
13822#endif
13823#ifdef FEAT_CON_DIALOG
13824 "dialog_con",
13825#endif
13826#ifdef FEAT_GUI_DIALOG
13827 "dialog_gui",
13828#endif
13829#ifdef FEAT_DIFF
13830 "diff",
13831#endif
13832#ifdef FEAT_DIGRAPHS
13833 "digraphs",
13834#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013835#ifdef FEAT_DIRECTX
13836 "directx",
13837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#ifdef FEAT_DND
13839 "dnd",
13840#endif
13841#ifdef FEAT_EMACS_TAGS
13842 "emacs_tags",
13843#endif
13844 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013845 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846#ifdef FEAT_SEARCH_EXTRA
13847 "extra_search",
13848#endif
13849#ifdef FEAT_FKMAP
13850 "farsi",
13851#endif
13852#ifdef FEAT_SEARCHPATH
13853 "file_in_path",
13854#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013855#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013856 "filterpipe",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_FIND_ID
13859 "find_in_path",
13860#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013861#ifdef FEAT_FLOAT
13862 "float",
13863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864#ifdef FEAT_FOLDING
13865 "folding",
13866#endif
13867#ifdef FEAT_FOOTER
13868 "footer",
13869#endif
13870#if !defined(USE_SYSTEM) && defined(UNIX)
13871 "fork",
13872#endif
13873#ifdef FEAT_GETTEXT
13874 "gettext",
13875#endif
13876#ifdef FEAT_GUI
13877 "gui",
13878#endif
13879#ifdef FEAT_GUI_ATHENA
13880# ifdef FEAT_GUI_NEXTAW
13881 "gui_neXtaw",
13882# else
13883 "gui_athena",
13884# endif
13885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886#ifdef FEAT_GUI_GTK
13887 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013888# ifdef USE_GTK3
13889 "gui_gtk3",
13890# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013894#ifdef FEAT_GUI_GNOME
13895 "gui_gnome",
13896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897#ifdef FEAT_GUI_MAC
13898 "gui_mac",
13899#endif
13900#ifdef FEAT_GUI_MOTIF
13901 "gui_motif",
13902#endif
13903#ifdef FEAT_GUI_PHOTON
13904 "gui_photon",
13905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906#ifdef FEAT_GUI_W32
13907 "gui_win32",
13908#endif
13909#ifdef FEAT_HANGULIN
13910 "hangul_input",
13911#endif
13912#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13913 "iconv",
13914#endif
13915#ifdef FEAT_INS_EXPAND
13916 "insert_expand",
13917#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013918#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013919 "job",
13920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921#ifdef FEAT_JUMPLIST
13922 "jumplist",
13923#endif
13924#ifdef FEAT_KEYMAP
13925 "keymap",
13926#endif
13927#ifdef FEAT_LANGMAP
13928 "langmap",
13929#endif
13930#ifdef FEAT_LIBCALL
13931 "libcall",
13932#endif
13933#ifdef FEAT_LINEBREAK
13934 "linebreak",
13935#endif
13936#ifdef FEAT_LISP
13937 "lispindent",
13938#endif
13939#ifdef FEAT_LISTCMDS
13940 "listcmds",
13941#endif
13942#ifdef FEAT_LOCALMAP
13943 "localmap",
13944#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013945#ifdef FEAT_LUA
13946# ifndef DYNAMIC_LUA
13947 "lua",
13948# endif
13949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950#ifdef FEAT_MENU
13951 "menu",
13952#endif
13953#ifdef FEAT_SESSION
13954 "mksession",
13955#endif
13956#ifdef FEAT_MODIFY_FNAME
13957 "modify_fname",
13958#endif
13959#ifdef FEAT_MOUSE
13960 "mouse",
13961#endif
13962#ifdef FEAT_MOUSESHAPE
13963 "mouseshape",
13964#endif
13965#if defined(UNIX) || defined(VMS)
13966# ifdef FEAT_MOUSE_DEC
13967 "mouse_dec",
13968# endif
13969# ifdef FEAT_MOUSE_GPM
13970 "mouse_gpm",
13971# endif
13972# ifdef FEAT_MOUSE_JSB
13973 "mouse_jsbterm",
13974# endif
13975# ifdef FEAT_MOUSE_NET
13976 "mouse_netterm",
13977# endif
13978# ifdef FEAT_MOUSE_PTERM
13979 "mouse_pterm",
13980# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013981# ifdef FEAT_MOUSE_SGR
13982 "mouse_sgr",
13983# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013984# ifdef FEAT_SYSMOUSE
13985 "mouse_sysmouse",
13986# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013987# ifdef FEAT_MOUSE_URXVT
13988 "mouse_urxvt",
13989# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990# ifdef FEAT_MOUSE_XTERM
13991 "mouse_xterm",
13992# endif
13993#endif
13994#ifdef FEAT_MBYTE
13995 "multi_byte",
13996#endif
13997#ifdef FEAT_MBYTE_IME
13998 "multi_byte_ime",
13999#endif
14000#ifdef FEAT_MULTI_LANG
14001 "multi_lang",
14002#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014003#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014004#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014005 "mzscheme",
14006#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008#ifdef FEAT_OLE
14009 "ole",
14010#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014011 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012#ifdef FEAT_PATH_EXTRA
14013 "path_extra",
14014#endif
14015#ifdef FEAT_PERL
14016#ifndef DYNAMIC_PERL
14017 "perl",
14018#endif
14019#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014020#ifdef FEAT_PERSISTENT_UNDO
14021 "persistent_undo",
14022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023#ifdef FEAT_PYTHON
14024#ifndef DYNAMIC_PYTHON
14025 "python",
14026#endif
14027#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014028#ifdef FEAT_PYTHON3
14029#ifndef DYNAMIC_PYTHON3
14030 "python3",
14031#endif
14032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#ifdef FEAT_POSTSCRIPT
14034 "postscript",
14035#endif
14036#ifdef FEAT_PRINTER
14037 "printer",
14038#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014039#ifdef FEAT_PROFILE
14040 "profile",
14041#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014042#ifdef FEAT_RELTIME
14043 "reltime",
14044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045#ifdef FEAT_QUICKFIX
14046 "quickfix",
14047#endif
14048#ifdef FEAT_RIGHTLEFT
14049 "rightleft",
14050#endif
14051#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14052 "ruby",
14053#endif
14054#ifdef FEAT_SCROLLBIND
14055 "scrollbind",
14056#endif
14057#ifdef FEAT_CMDL_INFO
14058 "showcmd",
14059 "cmdline_info",
14060#endif
14061#ifdef FEAT_SIGNS
14062 "signs",
14063#endif
14064#ifdef FEAT_SMARTINDENT
14065 "smartindent",
14066#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014067#ifdef STARTUPTIME
14068 "startuptime",
14069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070#ifdef FEAT_STL_OPT
14071 "statusline",
14072#endif
14073#ifdef FEAT_SUN_WORKSHOP
14074 "sun_workshop",
14075#endif
14076#ifdef FEAT_NETBEANS_INTG
14077 "netbeans_intg",
14078#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014079#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014080 "spell",
14081#endif
14082#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 "syntax",
14084#endif
14085#if defined(USE_SYSTEM) || !defined(UNIX)
14086 "system",
14087#endif
14088#ifdef FEAT_TAG_BINS
14089 "tag_binary",
14090#endif
14091#ifdef FEAT_TAG_OLDSTATIC
14092 "tag_old_static",
14093#endif
14094#ifdef FEAT_TAG_ANYWHITE
14095 "tag_any_white",
14096#endif
14097#ifdef FEAT_TCL
14098# ifndef DYNAMIC_TCL
14099 "tcl",
14100# endif
14101#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014102#ifdef FEAT_TERMGUICOLORS
14103 "termguicolors",
14104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105#ifdef TERMINFO
14106 "terminfo",
14107#endif
14108#ifdef FEAT_TERMRESPONSE
14109 "termresponse",
14110#endif
14111#ifdef FEAT_TEXTOBJ
14112 "textobjects",
14113#endif
14114#ifdef HAVE_TGETENT
14115 "tgetent",
14116#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014117#ifdef FEAT_TIMERS
14118 "timers",
14119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120#ifdef FEAT_TITLE
14121 "title",
14122#endif
14123#ifdef FEAT_TOOLBAR
14124 "toolbar",
14125#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014126#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14127 "unnamedplus",
14128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129#ifdef FEAT_USR_CMDS
14130 "user-commands", /* was accidentally included in 5.4 */
14131 "user_commands",
14132#endif
14133#ifdef FEAT_VIMINFO
14134 "viminfo",
14135#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014136#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 "vertsplit",
14138#endif
14139#ifdef FEAT_VIRTUALEDIT
14140 "virtualedit",
14141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143#ifdef FEAT_VISUALEXTRA
14144 "visualextra",
14145#endif
14146#ifdef FEAT_VREPLACE
14147 "vreplace",
14148#endif
14149#ifdef FEAT_WILDIGN
14150 "wildignore",
14151#endif
14152#ifdef FEAT_WILDMENU
14153 "wildmenu",
14154#endif
14155#ifdef FEAT_WINDOWS
14156 "windows",
14157#endif
14158#ifdef FEAT_WAK
14159 "winaltkeys",
14160#endif
14161#ifdef FEAT_WRITEBACKUP
14162 "writebackup",
14163#endif
14164#ifdef FEAT_XIM
14165 "xim",
14166#endif
14167#ifdef FEAT_XFONTSET
14168 "xfontset",
14169#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014170#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014171 "xpm",
14172 "xpm_w32", /* for backward compatibility */
14173#else
14174# if defined(HAVE_XPM)
14175 "xpm",
14176# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178#ifdef USE_XSMP
14179 "xsmp",
14180#endif
14181#ifdef USE_XSMP_INTERACT
14182 "xsmp_interact",
14183#endif
14184#ifdef FEAT_XCLIPBOARD
14185 "xterm_clipboard",
14186#endif
14187#ifdef FEAT_XTERM_SAVE
14188 "xterm_save",
14189#endif
14190#if defined(UNIX) && defined(FEAT_X11)
14191 "X11",
14192#endif
14193 NULL
14194 };
14195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 for (i = 0; has_list[i] != NULL; ++i)
14198 if (STRICMP(name, has_list[i]) == 0)
14199 {
14200 n = TRUE;
14201 break;
14202 }
14203
14204 if (n == FALSE)
14205 {
14206 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014207 {
14208 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014209 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014210 && vim_isdigit(name[6])
14211 && vim_isdigit(name[8])
14212 && vim_isdigit(name[10]))
14213 {
14214 int major = atoi((char *)name + 6);
14215 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014216
14217 /* Expect "patch-9.9.01234". */
14218 n = (major < VIM_VERSION_MAJOR
14219 || (major == VIM_VERSION_MAJOR
14220 && (minor < VIM_VERSION_MINOR
14221 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014222 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014223 }
14224 else
14225 n = has_patch(atoi((char *)name + 5));
14226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 else if (STRICMP(name, "vim_starting") == 0)
14228 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014229#ifdef FEAT_MBYTE
14230 else if (STRICMP(name, "multi_byte_encoding") == 0)
14231 n = has_mbyte;
14232#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014233#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14234 else if (STRICMP(name, "balloon_multiline") == 0)
14235 n = multiline_balloon_available();
14236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237#ifdef DYNAMIC_TCL
14238 else if (STRICMP(name, "tcl") == 0)
14239 n = tcl_enabled(FALSE);
14240#endif
14241#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14242 else if (STRICMP(name, "iconv") == 0)
14243 n = iconv_enabled(FALSE);
14244#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014245#ifdef DYNAMIC_LUA
14246 else if (STRICMP(name, "lua") == 0)
14247 n = lua_enabled(FALSE);
14248#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014249#ifdef DYNAMIC_MZSCHEME
14250 else if (STRICMP(name, "mzscheme") == 0)
14251 n = mzscheme_enabled(FALSE);
14252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253#ifdef DYNAMIC_RUBY
14254 else if (STRICMP(name, "ruby") == 0)
14255 n = ruby_enabled(FALSE);
14256#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014257#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258#ifdef DYNAMIC_PYTHON
14259 else if (STRICMP(name, "python") == 0)
14260 n = python_enabled(FALSE);
14261#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014262#endif
14263#ifdef FEAT_PYTHON3
14264#ifdef DYNAMIC_PYTHON3
14265 else if (STRICMP(name, "python3") == 0)
14266 n = python3_enabled(FALSE);
14267#endif
14268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269#ifdef DYNAMIC_PERL
14270 else if (STRICMP(name, "perl") == 0)
14271 n = perl_enabled(FALSE);
14272#endif
14273#ifdef FEAT_GUI
14274 else if (STRICMP(name, "gui_running") == 0)
14275 n = (gui.in_use || gui.starting);
14276# ifdef FEAT_GUI_W32
14277 else if (STRICMP(name, "gui_win32s") == 0)
14278 n = gui_is_win32s();
14279# endif
14280# ifdef FEAT_BROWSE
14281 else if (STRICMP(name, "browse") == 0)
14282 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14283# endif
14284#endif
14285#ifdef FEAT_SYN_HL
14286 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014287 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288#endif
14289#if defined(WIN3264)
14290 else if (STRICMP(name, "win95") == 0)
14291 n = mch_windows95();
14292#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014293#ifdef FEAT_NETBEANS_INTG
14294 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014295 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 }
14298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300}
14301
14302/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014303 * "has_key()" function
14304 */
14305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014306f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014307{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014308 if (argvars[0].v_type != VAR_DICT)
14309 {
14310 EMSG(_(e_dictreq));
14311 return;
14312 }
14313 if (argvars[0].vval.v_dict == NULL)
14314 return;
14315
14316 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014317 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014318}
14319
14320/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014321 * "haslocaldir()" function
14322 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014324f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014325{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014326 win_T *wp = NULL;
14327
14328 wp = find_tabwin(&argvars[0], &argvars[1]);
14329 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014330}
14331
14332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 * "hasmapto()" function
14334 */
14335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014336f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
14338 char_u *name;
14339 char_u *mode;
14340 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014341 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014344 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 mode = (char_u *)"nvo";
14346 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014348 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014349 if (argvars[2].v_type != VAR_UNKNOWN)
14350 abbr = get_tv_number(&argvars[2]);
14351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352
Bram Moolenaar2c932302006-03-18 21:42:09 +000014353 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357}
14358
14359/*
14360 * "histadd()" function
14361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014363f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364{
14365#ifdef FEAT_CMDHIST
14366 int histype;
14367 char_u *str;
14368 char_u buf[NUMBUFLEN];
14369#endif
14370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 if (check_restricted() || check_secure())
14373 return;
14374#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14376 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 if (histype >= 0)
14378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014379 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 if (*str != NUL)
14381 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014382 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014384 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 return;
14386 }
14387 }
14388#endif
14389}
14390
14391/*
14392 * "histdel()" function
14393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014395f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
14397#ifdef FEAT_CMDHIST
14398 int n;
14399 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014402 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14403 if (str == NULL)
14404 n = 0;
14405 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014408 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 else
14413 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014415 get_tv_string_buf(&argvars[1], buf));
14416 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417#endif
14418}
14419
14420/*
14421 * "histget()" function
14422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014424f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425{
14426#ifdef FEAT_CMDHIST
14427 int type;
14428 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14432 if (str == NULL)
14433 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 {
14436 type = get_histtype(str);
14437 if (argvars[1].v_type == VAR_UNKNOWN)
14438 idx = get_history_idx(type);
14439 else
14440 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14441 /* -1 on type error */
14442 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448}
14449
14450/*
14451 * "histnr()" function
14452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014454f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455{
14456 int i;
14457
14458#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014459 char_u *history = get_tv_string_chk(&argvars[0]);
14460
14461 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 if (i >= HIST_CMD && i < HIST_COUNT)
14463 i = get_history_idx(i);
14464 else
14465#endif
14466 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468}
14469
14470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 * "highlightID(name)" function
14472 */
14473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014476 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477}
14478
14479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014480 * "highlight_exists()" function
14481 */
14482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014484{
14485 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14486}
14487
14488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 * "hostname()" function
14490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014492f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493{
14494 char_u hostname[256];
14495
14496 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497 rettv->v_type = VAR_STRING;
14498 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499}
14500
14501/*
14502 * iconv() function
14503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014505f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506{
14507#ifdef FEAT_MBYTE
14508 char_u buf1[NUMBUFLEN];
14509 char_u buf2[NUMBUFLEN];
14510 char_u *from, *to, *str;
14511 vimconv_T vimconv;
14512#endif
14513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 rettv->v_type = VAR_STRING;
14515 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516
14517#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 str = get_tv_string(&argvars[0]);
14519 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14520 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 vimconv.vc_type = CONV_NONE;
14522 convert_setup(&vimconv, from, to);
14523
14524 /* If the encodings are equal, no conversion needed. */
14525 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014526 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529
14530 convert_setup(&vimconv, NULL, NULL);
14531 vim_free(from);
14532 vim_free(to);
14533#endif
14534}
14535
14536/*
14537 * "indent()" function
14538 */
14539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014540f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541{
14542 linenr_T lnum;
14543
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014544 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549}
14550
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014551/*
14552 * "index()" function
14553 */
14554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014555f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014556{
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 list_T *l;
14558 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014559 long idx = 0;
14560 int ic = FALSE;
14561
14562 rettv->vval.v_number = -1;
14563 if (argvars[0].v_type != VAR_LIST)
14564 {
14565 EMSG(_(e_listreq));
14566 return;
14567 }
14568 l = argvars[0].vval.v_list;
14569 if (l != NULL)
14570 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014571 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014572 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014574 int error = FALSE;
14575
Bram Moolenaar758711c2005-02-02 23:11:38 +000014576 /* Start at specified item. Use the cached index that list_find()
14577 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014579 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014580 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014581 ic = get_tv_number_chk(&argvars[3], &error);
14582 if (error)
14583 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014584 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014585
Bram Moolenaar758711c2005-02-02 23:11:38 +000014586 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014587 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014588 {
14589 rettv->vval.v_number = idx;
14590 break;
14591 }
14592 }
14593}
14594
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595static int inputsecret_flag = 0;
14596
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014597static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014598
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014600 * This function is used by f_input() and f_inputdialog() functions. The third
14601 * argument to f_input() specifies the type of completion to use at the
14602 * prompt. The third argument to f_inputdialog() specifies the value to return
14603 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 */
14605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014606get_user_input(
14607 typval_T *argvars,
14608 typval_T *rettv,
14609 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 char_u *p = NULL;
14613 int c;
14614 char_u buf[NUMBUFLEN];
14615 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014616 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014617 int xp_type = EXPAND_NOTHING;
14618 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014621 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622
14623#ifdef NO_CONSOLE_INPUT
14624 /* While starting up, there is no place to enter text. */
14625 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627#endif
14628
14629 cmd_silent = FALSE; /* Want to see the prompt. */
14630 if (prompt != NULL)
14631 {
14632 /* Only the part of the message after the last NL is considered as
14633 * prompt for the command line */
14634 p = vim_strrchr(prompt, '\n');
14635 if (p == NULL)
14636 p = prompt;
14637 else
14638 {
14639 ++p;
14640 c = *p;
14641 *p = NUL;
14642 msg_start();
14643 msg_clr_eos();
14644 msg_puts_attr(prompt, echo_attr);
14645 msg_didout = FALSE;
14646 msg_starthere();
14647 *p = c;
14648 }
14649 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 if (argvars[1].v_type != VAR_UNKNOWN)
14652 {
14653 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14654 if (defstr != NULL)
14655 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014657 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014658 {
14659 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014660 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014661 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014662
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014663 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014664 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014665
Bram Moolenaar4463f292005-09-25 22:20:24 +000014666 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14667 if (xp_name == NULL)
14668 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014669
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014670 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014671
Bram Moolenaar4463f292005-09-25 22:20:24 +000014672 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14673 &xp_arg) == FAIL)
14674 return;
14675 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014676 }
14677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014679 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014680 int save_ex_normal_busy = ex_normal_busy;
14681 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014682 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014683 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14684 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014685 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014686 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014687 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014688 && argvars[1].v_type != VAR_UNKNOWN
14689 && argvars[2].v_type != VAR_UNKNOWN)
14690 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14691 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014692
14693 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 /* since the user typed this, no need to wait for return */
14696 need_wait_return = FALSE;
14697 msg_didout = FALSE;
14698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 cmd_silent = cmd_silent_save;
14700}
14701
14702/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014703 * "input()" function
14704 * Also handles inputsecret() when inputsecret is set.
14705 */
14706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014707f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014708{
14709 get_user_input(argvars, rettv, FALSE);
14710}
14711
14712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 * "inputdialog()" function
14714 */
14715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014716f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717{
14718#if defined(FEAT_GUI_TEXTDIALOG)
14719 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14720 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14721 {
14722 char_u *message;
14723 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014726 message = get_tv_string_chk(&argvars[0]);
14727 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014728 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014729 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 else
14731 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 if (message != NULL && defstr != NULL
14733 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014734 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 else
14737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014738 if (message != NULL && defstr != NULL
14739 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014740 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 rettv->vval.v_string = vim_strsave(
14742 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 }
14748 else
14749#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014750 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751}
14752
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014753/*
14754 * "inputlist()" function
14755 */
14756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014757f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014758{
14759 listitem_T *li;
14760 int selected;
14761 int mouse_used;
14762
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014763#ifdef NO_CONSOLE_INPUT
14764 /* While starting up, there is no place to enter text. */
14765 if (no_console_input())
14766 return;
14767#endif
14768 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14769 {
14770 EMSG2(_(e_listarg), "inputlist()");
14771 return;
14772 }
14773
14774 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014775 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014776 lines_left = Rows; /* avoid more prompt */
14777 msg_scroll = TRUE;
14778 msg_clr_eos();
14779
14780 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14781 {
14782 msg_puts(get_tv_string(&li->li_tv));
14783 msg_putchar('\n');
14784 }
14785
14786 /* Ask for choice. */
14787 selected = prompt_for_number(&mouse_used);
14788 if (mouse_used)
14789 selected -= lines_left;
14790
14791 rettv->vval.v_number = selected;
14792}
14793
14794
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14796
14797/*
14798 * "inputrestore()" function
14799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014801f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802{
14803 if (ga_userinput.ga_len > 0)
14804 {
14805 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14807 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014808 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 }
14810 else if (p_verbose > 1)
14811 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014812 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014813 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 }
14815}
14816
14817/*
14818 * "inputsave()" function
14819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014823 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 if (ga_grow(&ga_userinput, 1) == OK)
14825 {
14826 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14827 + ga_userinput.ga_len);
14828 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014829 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 }
14831 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833}
14834
14835/*
14836 * "inputsecret()" function
14837 */
14838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014839f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840{
14841 ++cmdline_star;
14842 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 --cmdline_star;
14845 --inputsecret_flag;
14846}
14847
14848/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014849 * "insert()" function
14850 */
14851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014852f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014853{
14854 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 listitem_T *item;
14856 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014857 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014858
14859 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014860 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014861 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014862 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863 {
14864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014865 before = get_tv_number_chk(&argvars[2], &error);
14866 if (error)
14867 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868
Bram Moolenaar758711c2005-02-02 23:11:38 +000014869 if (before == l->lv_len)
14870 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014871 else
14872 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014873 item = list_find(l, before);
14874 if (item == NULL)
14875 {
14876 EMSGN(_(e_listidx), before);
14877 l = NULL;
14878 }
14879 }
14880 if (l != NULL)
14881 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014882 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014883 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014884 }
14885 }
14886}
14887
14888/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014889 * "invert(expr)" function
14890 */
14891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014892f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014893{
14894 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14895}
14896
14897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898 * "isdirectory()" function
14899 */
14900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014901f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904}
14905
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014906/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014907 * "islocked()" function
14908 */
14909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014910f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014911{
14912 lval_T lv;
14913 char_u *end;
14914 dictitem_T *di;
14915
14916 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014917 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14918 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014919 if (end != NULL && lv.ll_name != NULL)
14920 {
14921 if (*end != NUL)
14922 EMSG(_(e_trailing));
14923 else
14924 {
14925 if (lv.ll_tv == NULL)
14926 {
14927 if (check_changedtick(lv.ll_name))
14928 rettv->vval.v_number = 1; /* always locked */
14929 else
14930 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014931 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014932 if (di != NULL)
14933 {
14934 /* Consider a variable locked when:
14935 * 1. the variable itself is locked
14936 * 2. the value of the variable is locked.
14937 * 3. the List or Dict value is locked.
14938 */
14939 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14940 || tv_islocked(&di->di_tv));
14941 }
14942 }
14943 }
14944 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014945 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014946 else if (lv.ll_newkey != NULL)
14947 EMSG2(_(e_dictkey), lv.ll_newkey);
14948 else if (lv.ll_list != NULL)
14949 /* List item. */
14950 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14951 else
14952 /* Dictionary item. */
14953 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14954 }
14955 }
14956
14957 clear_lval(&lv);
14958}
14959
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014960#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14961/*
14962 * "isnan()" function
14963 */
14964 static void
14965f_isnan(typval_T *argvars, typval_T *rettv)
14966{
14967 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14968 && isnan(argvars[0].vval.v_float);
14969}
14970#endif
14971
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014972static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014973
14974/*
14975 * Turn a dict into a list:
14976 * "what" == 0: list of keys
14977 * "what" == 1: list of values
14978 * "what" == 2: list of items
14979 */
14980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014981dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014982{
Bram Moolenaar33570922005-01-25 22:26:29 +000014983 list_T *l2;
14984 dictitem_T *di;
14985 hashitem_T *hi;
14986 listitem_T *li;
14987 listitem_T *li2;
14988 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014989 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014990
Bram Moolenaar8c711452005-01-14 21:53:12 +000014991 if (argvars[0].v_type != VAR_DICT)
14992 {
14993 EMSG(_(e_dictreq));
14994 return;
14995 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014996 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014997 return;
14998
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014999 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015000 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015001
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015002 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015004 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015005 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015006 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015007 --todo;
15008 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015009
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015010 li = listitem_alloc();
15011 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015012 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015013 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015014
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015015 if (what == 0)
15016 {
15017 /* keys() */
15018 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015019 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015020 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15021 }
15022 else if (what == 1)
15023 {
15024 /* values() */
15025 copy_tv(&di->di_tv, &li->li_tv);
15026 }
15027 else
15028 {
15029 /* items() */
15030 l2 = list_alloc();
15031 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015032 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015033 li->li_tv.vval.v_list = l2;
15034 if (l2 == NULL)
15035 break;
15036 ++l2->lv_refcount;
15037
15038 li2 = listitem_alloc();
15039 if (li2 == NULL)
15040 break;
15041 list_append(l2, li2);
15042 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015043 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015044 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15045
15046 li2 = listitem_alloc();
15047 if (li2 == NULL)
15048 break;
15049 list_append(l2, li2);
15050 copy_tv(&di->di_tv, &li2->li_tv);
15051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015052 }
15053 }
15054}
15055
15056/*
15057 * "items(dict)" function
15058 */
15059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015060f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015061{
15062 dict_list(argvars, rettv, 2);
15063}
15064
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015065#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015066/*
15067 * Get the job from the argument.
15068 * Returns NULL if the job is invalid.
15069 */
15070 static job_T *
15071get_job_arg(typval_T *tv)
15072{
15073 job_T *job;
15074
15075 if (tv->v_type != VAR_JOB)
15076 {
15077 EMSG2(_(e_invarg2), get_tv_string(tv));
15078 return NULL;
15079 }
15080 job = tv->vval.v_job;
15081
15082 if (job == NULL)
15083 EMSG(_("E916: not a valid job"));
15084 return job;
15085}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015086
Bram Moolenaar835dc632016-02-07 14:27:38 +010015087/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015088 * "job_getchannel()" function
15089 */
15090 static void
15091f_job_getchannel(typval_T *argvars, typval_T *rettv)
15092{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015093 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015094
Bram Moolenaar65edff82016-02-21 16:40:11 +010015095 if (job != NULL)
15096 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015097 rettv->v_type = VAR_CHANNEL;
15098 rettv->vval.v_channel = job->jv_channel;
15099 if (job->jv_channel != NULL)
15100 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015101 }
15102}
15103
15104/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015105 * "job_info()" function
15106 */
15107 static void
15108f_job_info(typval_T *argvars, typval_T *rettv)
15109{
15110 job_T *job = get_job_arg(&argvars[0]);
15111
15112 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15113 job_info(job, rettv->vval.v_dict);
15114}
15115
15116/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015117 * "job_setoptions()" function
15118 */
15119 static void
15120f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15121{
15122 job_T *job = get_job_arg(&argvars[0]);
15123 jobopt_T opt;
15124
15125 if (job == NULL)
15126 return;
15127 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015128 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15129 job_set_options(job, &opt);
15130 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015131}
15132
15133/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015134 * "job_start()" function
15135 */
15136 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015137f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015138{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015139 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015140 if (check_restricted() || check_secure())
15141 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015142 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015143}
15144
15145/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015146 * "job_status()" function
15147 */
15148 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015149f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015150{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015151 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015152
Bram Moolenaar65edff82016-02-21 16:40:11 +010015153 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015154 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015155 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015156 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015157 }
15158}
15159
15160/*
15161 * "job_stop()" function
15162 */
15163 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015164f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015165{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015166 job_T *job = get_job_arg(&argvars[0]);
15167
15168 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015169 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015170}
15171#endif
15172
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015174 * "join()" function
15175 */
15176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015177f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015178{
15179 garray_T ga;
15180 char_u *sep;
15181
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015182 if (argvars[0].v_type != VAR_LIST)
15183 {
15184 EMSG(_(e_listreq));
15185 return;
15186 }
15187 if (argvars[0].vval.v_list == NULL)
15188 return;
15189 if (argvars[1].v_type == VAR_UNKNOWN)
15190 sep = (char_u *)" ";
15191 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015192 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015193
15194 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015195
15196 if (sep != NULL)
15197 {
15198 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015199 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015200 ga_append(&ga, NUL);
15201 rettv->vval.v_string = (char_u *)ga.ga_data;
15202 }
15203 else
15204 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015205}
15206
15207/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015208 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015209 */
15210 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015211f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015212{
15213 js_read_T reader;
15214
15215 reader.js_buf = get_tv_string(&argvars[0]);
15216 reader.js_fill = NULL;
15217 reader.js_used = 0;
15218 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15219 EMSG(_(e_invarg));
15220}
15221
15222/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015223 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015224 */
15225 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015226f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015227{
15228 rettv->v_type = VAR_STRING;
15229 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15230}
15231
15232/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015233 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015234 */
15235 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015236f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015237{
15238 js_read_T reader;
15239
15240 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015241 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015242 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015243 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015244 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015245}
15246
15247/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015248 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015249 */
15250 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015251f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015252{
15253 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015254 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015255}
15256
15257/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015258 * "keys()" function
15259 */
15260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015261f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015262{
15263 dict_list(argvars, rettv, 0);
15264}
15265
15266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267 * "last_buffer_nr()" function.
15268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015270f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271{
15272 int n = 0;
15273 buf_T *buf;
15274
15275 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15276 if (n < buf->b_fnum)
15277 n = buf->b_fnum;
15278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015279 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015280}
15281
15282/*
15283 * "len()" function
15284 */
15285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015286f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015287{
15288 switch (argvars[0].v_type)
15289 {
15290 case VAR_STRING:
15291 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 rettv->vval.v_number = (varnumber_T)STRLEN(
15293 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015294 break;
15295 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015296 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015297 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015298 case VAR_DICT:
15299 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15300 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015301 case VAR_UNKNOWN:
15302 case VAR_SPECIAL:
15303 case VAR_FLOAT:
15304 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015305 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015306 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015307 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015308 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309 break;
15310 }
15311}
15312
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015313static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015314
15315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015316libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015317{
15318#ifdef FEAT_LIBCALL
15319 char_u *string_in;
15320 char_u **string_result;
15321 int nr_result;
15322#endif
15323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015325 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015326 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015327
15328 if (check_restricted() || check_secure())
15329 return;
15330
15331#ifdef FEAT_LIBCALL
15332 /* The first two args must be strings, otherwise its meaningless */
15333 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15334 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015335 string_in = NULL;
15336 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337 string_in = argvars[2].vval.v_string;
15338 if (type == VAR_NUMBER)
15339 string_result = NULL;
15340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015341 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015342 if (mch_libcall(argvars[0].vval.v_string,
15343 argvars[1].vval.v_string,
15344 string_in,
15345 argvars[2].vval.v_number,
15346 string_result,
15347 &nr_result) == OK
15348 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015350 }
15351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352}
15353
15354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015355 * "libcall()" function
15356 */
15357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015358f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015359{
15360 libcall_common(argvars, rettv, VAR_STRING);
15361}
15362
15363/*
15364 * "libcallnr()" function
15365 */
15366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015367f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368{
15369 libcall_common(argvars, rettv, VAR_NUMBER);
15370}
15371
15372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 * "line(string)" function
15374 */
15375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015376f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377{
15378 linenr_T lnum = 0;
15379 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015380 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015382 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 if (fp != NULL)
15384 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386}
15387
15388/*
15389 * "line2byte(lnum)" function
15390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015392f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393{
15394#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396#else
15397 linenr_T lnum;
15398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15404 if (rettv->vval.v_number >= 0)
15405 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406#endif
15407}
15408
15409/*
15410 * "lispindent(lnum)" function
15411 */
15412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015413f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414{
15415#ifdef FEAT_LISP
15416 pos_T pos;
15417 linenr_T lnum;
15418
15419 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15422 {
15423 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015424 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015425 curwin->w_cursor = pos;
15426 }
15427 else
15428#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430}
15431
15432/*
15433 * "localtime()" function
15434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015436f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439}
15440
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015441static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442
15443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015444get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445{
15446 char_u *keys;
15447 char_u *which;
15448 char_u buf[NUMBUFLEN];
15449 char_u *keys_buf = NULL;
15450 char_u *rhs;
15451 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015452 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015453 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015454 mapblock_T *mp;
15455 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456
15457 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 rettv->v_type = VAR_STRING;
15459 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462 if (*keys == NUL)
15463 return;
15464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015465 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015467 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015469 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015470 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015471 if (argvars[3].v_type != VAR_UNKNOWN)
15472 get_dict = get_tv_number(&argvars[3]);
15473 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 else
15476 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015477 if (which == NULL)
15478 return;
15479
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480 mode = get_map_mode(&which, 0);
15481
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015482 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015483 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015485
15486 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015488 /* Return a string. */
15489 if (rhs != NULL)
15490 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491
Bram Moolenaarbd743252010-10-20 21:23:33 +020015492 }
15493 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15494 {
15495 /* Return a dictionary. */
15496 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15497 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15498 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499
Bram Moolenaarbd743252010-10-20 21:23:33 +020015500 dict_add_nr_str(dict, "lhs", 0L, lhs);
15501 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15502 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15503 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15504 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15505 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15506 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015507 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015508 dict_add_nr_str(dict, "mode", 0L, mapmode);
15509
15510 vim_free(lhs);
15511 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 }
15513}
15514
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015515#ifdef FEAT_FLOAT
15516/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015517 * "log()" function
15518 */
15519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015520f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015521{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015522 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015523
15524 rettv->v_type = VAR_FLOAT;
15525 if (get_float_arg(argvars, &f) == OK)
15526 rettv->vval.v_float = log(f);
15527 else
15528 rettv->vval.v_float = 0.0;
15529}
15530
15531/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015532 * "log10()" function
15533 */
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015536{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015537 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015538
15539 rettv->v_type = VAR_FLOAT;
15540 if (get_float_arg(argvars, &f) == OK)
15541 rettv->vval.v_float = log10(f);
15542 else
15543 rettv->vval.v_float = 0.0;
15544}
15545#endif
15546
Bram Moolenaar1dced572012-04-05 16:54:08 +020015547#ifdef FEAT_LUA
15548/*
15549 * "luaeval()" function
15550 */
15551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015552f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015553{
15554 char_u *str;
15555 char_u buf[NUMBUFLEN];
15556
15557 str = get_tv_string_buf(&argvars[0], buf);
15558 do_luaeval(str, argvars + 1, rettv);
15559}
15560#endif
15561
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015563 * "map()" function
15564 */
15565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015566f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015567{
15568 filter_map(argvars, rettv, TRUE);
15569}
15570
15571/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015572 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 */
15574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015575f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015577 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578}
15579
15580/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 */
15583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015584f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587}
15588
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015589static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590
15591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015592find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015594 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015595 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015596 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 char_u *pat;
15598 regmatch_T regmatch;
15599 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015600 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 char_u *save_cpo;
15602 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015603 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015604 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015605 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015606 list_T *l = NULL;
15607 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015608 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
15611 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15612 save_cpo = p_cpo;
15613 p_cpo = (char_u *)"";
15614
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015615 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015616 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015617 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015618 /* type 3: return empty list when there are no matches.
15619 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015620 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015621 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015622 if (type == 4
15623 && (list_append_string(rettv->vval.v_list,
15624 (char_u *)"", 0) == FAIL
15625 || list_append_number(rettv->vval.v_list,
15626 (varnumber_T)-1) == FAIL
15627 || list_append_number(rettv->vval.v_list,
15628 (varnumber_T)-1) == FAIL
15629 || list_append_number(rettv->vval.v_list,
15630 (varnumber_T)-1) == FAIL))
15631 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015632 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015633 rettv->vval.v_list = NULL;
15634 goto theend;
15635 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015636 }
15637 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015639 rettv->v_type = VAR_STRING;
15640 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015643 if (argvars[0].v_type == VAR_LIST)
15644 {
15645 if ((l = argvars[0].vval.v_list) == NULL)
15646 goto theend;
15647 li = l->lv_first;
15648 }
15649 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015650 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015651 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015652 len = (long)STRLEN(str);
15653 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015654
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15656 if (pat == NULL)
15657 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015661 int error = FALSE;
15662
15663 start = get_tv_number_chk(&argvars[2], &error);
15664 if (error)
15665 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015666 if (l != NULL)
15667 {
15668 li = list_find(l, start);
15669 if (li == NULL)
15670 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015671 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015672 }
15673 else
15674 {
15675 if (start < 0)
15676 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015677 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015678 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015679 /* When "count" argument is there ignore matches before "start",
15680 * otherwise skip part of the string. Differs when pattern is "^"
15681 * or "\<". */
15682 if (argvars[3].v_type != VAR_UNKNOWN)
15683 startcol = start;
15684 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015685 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015686 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015687 len -= start;
15688 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015689 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015690
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015691 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015692 nth = get_tv_number_chk(&argvars[3], &error);
15693 if (error)
15694 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 }
15696
15697 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15698 if (regmatch.regprog != NULL)
15699 {
15700 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015701
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015702 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015703 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015704 if (l != NULL)
15705 {
15706 if (li == NULL)
15707 {
15708 match = FALSE;
15709 break;
15710 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015711 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015712 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015713 if (str == NULL)
15714 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015715 }
15716
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015717 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015718
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015719 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015720 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 if (l == NULL && !match)
15722 break;
15723
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015724 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015725 if (l != NULL)
15726 {
15727 li = li->li_next;
15728 ++idx;
15729 }
15730 else
15731 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015732#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015733 startcol = (colnr_T)(regmatch.startp[0]
15734 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015735#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015736 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015737#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015738 if (startcol > (colnr_T)len
15739 || str + startcol <= regmatch.startp[0])
15740 {
15741 match = FALSE;
15742 break;
15743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015744 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015745 }
15746
15747 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015749 if (type == 4)
15750 {
15751 listitem_T *li1 = rettv->vval.v_list->lv_first;
15752 listitem_T *li2 = li1->li_next;
15753 listitem_T *li3 = li2->li_next;
15754 listitem_T *li4 = li3->li_next;
15755
15756 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15757 (int)(regmatch.endp[0] - regmatch.startp[0]));
15758 li3->li_tv.vval.v_number =
15759 (varnumber_T)(regmatch.startp[0] - expr);
15760 li4->li_tv.vval.v_number =
15761 (varnumber_T)(regmatch.endp[0] - expr);
15762 if (l != NULL)
15763 li2->li_tv.vval.v_number = (varnumber_T)idx;
15764 }
15765 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015766 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015767 int i;
15768
15769 /* return list with matched string and submatches */
15770 for (i = 0; i < NSUBEXP; ++i)
15771 {
15772 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015773 {
15774 if (list_append_string(rettv->vval.v_list,
15775 (char_u *)"", 0) == FAIL)
15776 break;
15777 }
15778 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015779 regmatch.startp[i],
15780 (int)(regmatch.endp[i] - regmatch.startp[i]))
15781 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015782 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015783 }
15784 }
15785 else if (type == 2)
15786 {
15787 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015788 if (l != NULL)
15789 copy_tv(&li->li_tv, rettv);
15790 else
15791 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015793 }
15794 else if (l != NULL)
15795 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 else
15797 {
15798 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015799 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 (varnumber_T)(regmatch.startp[0] - str);
15801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015804 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 }
15806 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015807 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 }
15809
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015810 if (type == 4 && l == NULL)
15811 /* matchstrpos() without a list: drop the second item. */
15812 listitem_remove(rettv->vval.v_list,
15813 rettv->vval.v_list->lv_first->li_next);
15814
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817 p_cpo = save_cpo;
15818}
15819
15820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015821 * "match()" function
15822 */
15823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015824f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015825{
15826 find_some_match(argvars, rettv, 1);
15827}
15828
15829/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015830 * "matchadd()" function
15831 */
15832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015833f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015834{
15835#ifdef FEAT_SEARCH_EXTRA
15836 char_u buf[NUMBUFLEN];
15837 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15838 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15839 int prio = 10; /* default priority */
15840 int id = -1;
15841 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015842 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015843
15844 rettv->vval.v_number = -1;
15845
15846 if (grp == NULL || pat == NULL)
15847 return;
15848 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015849 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015850 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015851 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015852 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015853 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015854 if (argvars[4].v_type != VAR_UNKNOWN)
15855 {
15856 if (argvars[4].v_type != VAR_DICT)
15857 {
15858 EMSG(_(e_dictreq));
15859 return;
15860 }
15861 if (dict_find(argvars[4].vval.v_dict,
15862 (char_u *)"conceal", -1) != NULL)
15863 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15864 (char_u *)"conceal", FALSE);
15865 }
15866 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015867 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015868 if (error == TRUE)
15869 return;
15870 if (id >= 1 && id <= 3)
15871 {
15872 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15873 return;
15874 }
15875
Bram Moolenaar6561d522015-07-21 15:48:27 +020015876 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15877 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015878#endif
15879}
15880
15881/*
15882 * "matchaddpos()" function
15883 */
15884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015885f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015886{
15887#ifdef FEAT_SEARCH_EXTRA
15888 char_u buf[NUMBUFLEN];
15889 char_u *group;
15890 int prio = 10;
15891 int id = -1;
15892 int error = FALSE;
15893 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015894 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015895
15896 rettv->vval.v_number = -1;
15897
15898 group = get_tv_string_buf_chk(&argvars[0], buf);
15899 if (group == NULL)
15900 return;
15901
15902 if (argvars[1].v_type != VAR_LIST)
15903 {
15904 EMSG2(_(e_listarg), "matchaddpos()");
15905 return;
15906 }
15907 l = argvars[1].vval.v_list;
15908 if (l == NULL)
15909 return;
15910
15911 if (argvars[2].v_type != VAR_UNKNOWN)
15912 {
15913 prio = get_tv_number_chk(&argvars[2], &error);
15914 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015915 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015916 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015917 if (argvars[4].v_type != VAR_UNKNOWN)
15918 {
15919 if (argvars[4].v_type != VAR_DICT)
15920 {
15921 EMSG(_(e_dictreq));
15922 return;
15923 }
15924 if (dict_find(argvars[4].vval.v_dict,
15925 (char_u *)"conceal", -1) != NULL)
15926 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15927 (char_u *)"conceal", FALSE);
15928 }
15929 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015930 }
15931 if (error == TRUE)
15932 return;
15933
15934 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15935 if (id == 1 || id == 2)
15936 {
15937 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15938 return;
15939 }
15940
Bram Moolenaar6561d522015-07-21 15:48:27 +020015941 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15942 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015943#endif
15944}
15945
15946/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015947 * "matcharg()" function
15948 */
15949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015950f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015951{
15952 if (rettv_list_alloc(rettv) == OK)
15953 {
15954#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015955 int id = get_tv_number(&argvars[0]);
15956 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015957
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015958 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015959 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015960 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15961 {
15962 list_append_string(rettv->vval.v_list,
15963 syn_id2name(m->hlg_id), -1);
15964 list_append_string(rettv->vval.v_list, m->pattern, -1);
15965 }
15966 else
15967 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015968 list_append_string(rettv->vval.v_list, NULL, -1);
15969 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015970 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015971 }
15972#endif
15973 }
15974}
15975
15976/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015977 * "matchdelete()" function
15978 */
15979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015980f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015981{
15982#ifdef FEAT_SEARCH_EXTRA
15983 rettv->vval.v_number = match_delete(curwin,
15984 (int)get_tv_number(&argvars[0]), TRUE);
15985#endif
15986}
15987
15988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015989 * "matchend()" function
15990 */
15991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015992f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015993{
15994 find_some_match(argvars, rettv, 0);
15995}
15996
15997/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015998 * "matchlist()" function
15999 */
16000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016001f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016002{
16003 find_some_match(argvars, rettv, 3);
16004}
16005
16006/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016007 * "matchstr()" function
16008 */
16009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016010f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016011{
16012 find_some_match(argvars, rettv, 2);
16013}
16014
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016015/*
16016 * "matchstrpos()" function
16017 */
16018 static void
16019f_matchstrpos(typval_T *argvars, typval_T *rettv)
16020{
16021 find_some_match(argvars, rettv, 4);
16022}
16023
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016024static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016025
16026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016027max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016028{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016029 long n = 0;
16030 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016032
16033 if (argvars[0].v_type == VAR_LIST)
16034 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016035 list_T *l;
16036 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016037
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016038 l = argvars[0].vval.v_list;
16039 if (l != NULL)
16040 {
16041 li = l->lv_first;
16042 if (li != NULL)
16043 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016044 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016045 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016046 {
16047 li = li->li_next;
16048 if (li == NULL)
16049 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016050 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016051 if (domax ? i > n : i < n)
16052 n = i;
16053 }
16054 }
16055 }
16056 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016057 else if (argvars[0].v_type == VAR_DICT)
16058 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016059 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016060 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016061 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016062 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016063
16064 d = argvars[0].vval.v_dict;
16065 if (d != NULL)
16066 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016067 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016068 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016069 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016070 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016072 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016073 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016074 if (first)
16075 {
16076 n = i;
16077 first = FALSE;
16078 }
16079 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016080 n = i;
16081 }
16082 }
16083 }
16084 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016085 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016086 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016087 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016088}
16089
16090/*
16091 * "max()" function
16092 */
16093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016094f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016095{
16096 max_min(argvars, rettv, TRUE);
16097}
16098
16099/*
16100 * "min()" function
16101 */
16102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016103f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016104{
16105 max_min(argvars, rettv, FALSE);
16106}
16107
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016108static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016109
16110/*
16111 * Create the directory in which "dir" is located, and higher levels when
16112 * needed.
16113 */
16114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016115mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016116{
16117 char_u *p;
16118 char_u *updir;
16119 int r = FAIL;
16120
16121 /* Get end of directory name in "dir".
16122 * We're done when it's "/" or "c:/". */
16123 p = gettail_sep(dir);
16124 if (p <= get_past_head(dir))
16125 return OK;
16126
16127 /* If the directory exists we're done. Otherwise: create it.*/
16128 updir = vim_strnsave(dir, (int)(p - dir));
16129 if (updir == NULL)
16130 return FAIL;
16131 if (mch_isdir(updir))
16132 r = OK;
16133 else if (mkdir_recurse(updir, prot) == OK)
16134 r = vim_mkdir_emsg(updir, prot);
16135 vim_free(updir);
16136 return r;
16137}
16138
16139#ifdef vim_mkdir
16140/*
16141 * "mkdir()" function
16142 */
16143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016144f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016145{
16146 char_u *dir;
16147 char_u buf[NUMBUFLEN];
16148 int prot = 0755;
16149
16150 rettv->vval.v_number = FAIL;
16151 if (check_restricted() || check_secure())
16152 return;
16153
16154 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016155 if (*dir == NUL)
16156 rettv->vval.v_number = FAIL;
16157 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016158 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016159 if (*gettail(dir) == NUL)
16160 /* remove trailing slashes */
16161 *gettail_sep(dir) = NUL;
16162
16163 if (argvars[1].v_type != VAR_UNKNOWN)
16164 {
16165 if (argvars[2].v_type != VAR_UNKNOWN)
16166 prot = get_tv_number_chk(&argvars[2], NULL);
16167 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16168 mkdir_recurse(dir, prot);
16169 }
16170 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016171 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016172}
16173#endif
16174
Bram Moolenaar0d660222005-01-07 21:51:51 +000016175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 * "mode()" function
16177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016179f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016181 char_u buf[3];
16182
16183 buf[1] = NUL;
16184 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 if (VIsual_active)
16187 {
16188 if (VIsual_select)
16189 buf[0] = VIsual_mode + 's' - 'v';
16190 else
16191 buf[0] = VIsual_mode;
16192 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016193 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016194 || State == CONFIRM)
16195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016197 if (State == ASKMORE)
16198 buf[1] = 'm';
16199 else if (State == CONFIRM)
16200 buf[1] = '?';
16201 }
16202 else if (State == EXTERNCMD)
16203 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 else if (State & INSERT)
16205 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206#ifdef FEAT_VREPLACE
16207 if (State & VREPLACE_FLAG)
16208 {
16209 buf[0] = 'R';
16210 buf[1] = 'v';
16211 }
16212 else
16213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 if (State & REPLACE_FLAG)
16215 buf[0] = 'R';
16216 else
16217 buf[0] = 'i';
16218 }
16219 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016222 if (exmode_active)
16223 buf[1] = 'v';
16224 }
16225 else if (exmode_active)
16226 {
16227 buf[0] = 'c';
16228 buf[1] = 'e';
16229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016233 if (finish_op)
16234 buf[1] = 'o';
16235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016237 /* Clear out the minor mode when the argument is not a non-zero number or
16238 * non-empty string. */
16239 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016240 buf[1] = NUL;
16241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016242 rettv->vval.v_string = vim_strsave(buf);
16243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244}
16245
Bram Moolenaar429fa852013-04-15 12:27:36 +020016246#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016247/*
16248 * "mzeval()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016252{
16253 char_u *str;
16254 char_u buf[NUMBUFLEN];
16255
16256 str = get_tv_string_buf(&argvars[0], buf);
16257 do_mzeval(str, rettv);
16258}
Bram Moolenaar75676462013-01-30 14:55:42 +010016259
16260 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016261mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016262{
16263 typval_T argvars[3];
16264
16265 argvars[0].v_type = VAR_STRING;
16266 argvars[0].vval.v_string = name;
16267 copy_tv(args, &argvars[1]);
16268 argvars[2].v_type = VAR_UNKNOWN;
16269 f_call(argvars, rettv);
16270 clear_tv(&argvars[1]);
16271}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016272#endif
16273
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 * "nextnonblank()" function
16276 */
16277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016278f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279{
16280 linenr_T lnum;
16281
16282 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016284 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 {
16286 lnum = 0;
16287 break;
16288 }
16289 if (*skipwhite(ml_get(lnum)) != NUL)
16290 break;
16291 }
16292 rettv->vval.v_number = lnum;
16293}
16294
16295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 * "nr2char()" function
16297 */
16298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016299f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300{
16301 char_u buf[NUMBUFLEN];
16302
16303#ifdef FEAT_MBYTE
16304 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016305 {
16306 int utf8 = 0;
16307
16308 if (argvars[1].v_type != VAR_UNKNOWN)
16309 utf8 = get_tv_number_chk(&argvars[1], NULL);
16310 if (utf8)
16311 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16312 else
16313 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315 else
16316#endif
16317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016318 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016319 buf[1] = NUL;
16320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016321 rettv->v_type = VAR_STRING;
16322 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016323}
16324
16325/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016326 * "or(expr, expr)" function
16327 */
16328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016329f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016330{
16331 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16332 | get_tv_number_chk(&argvars[1], NULL);
16333}
16334
16335/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016336 * "pathshorten()" function
16337 */
16338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016340{
16341 char_u *p;
16342
16343 rettv->v_type = VAR_STRING;
16344 p = get_tv_string_chk(&argvars[0]);
16345 if (p == NULL)
16346 rettv->vval.v_string = NULL;
16347 else
16348 {
16349 p = vim_strsave(p);
16350 rettv->vval.v_string = p;
16351 if (p != NULL)
16352 shorten_dir(p);
16353 }
16354}
16355
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016356#ifdef FEAT_PERL
16357/*
16358 * "perleval()" function
16359 */
16360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016361f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016362{
16363 char_u *str;
16364 char_u buf[NUMBUFLEN];
16365
16366 str = get_tv_string_buf(&argvars[0], buf);
16367 do_perleval(str, rettv);
16368}
16369#endif
16370
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016371#ifdef FEAT_FLOAT
16372/*
16373 * "pow()" function
16374 */
16375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016376f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016377{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016378 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016379
16380 rettv->v_type = VAR_FLOAT;
16381 if (get_float_arg(argvars, &fx) == OK
16382 && get_float_arg(&argvars[1], &fy) == OK)
16383 rettv->vval.v_float = pow(fx, fy);
16384 else
16385 rettv->vval.v_float = 0.0;
16386}
16387#endif
16388
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016389/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 * "prevnonblank()" function
16391 */
16392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016393f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394{
16395 linenr_T lnum;
16396
16397 lnum = get_tv_lnum(argvars);
16398 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16399 lnum = 0;
16400 else
16401 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16402 --lnum;
16403 rettv->vval.v_number = lnum;
16404}
16405
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016406/* This dummy va_list is here because:
16407 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16408 * - locally in the function results in a "used before set" warning
16409 * - using va_start() to initialize it gives "function with fixed args" error */
16410static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016411
Bram Moolenaar8c711452005-01-14 21:53:12 +000016412/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016413 * "printf()" function
16414 */
16415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016416f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016417{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016418 char_u buf[NUMBUFLEN];
16419 int len;
16420 char_u *s;
16421 int saved_did_emsg = did_emsg;
16422 char *fmt;
16423
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016424 rettv->v_type = VAR_STRING;
16425 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016426
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016427 /* Get the required length, allocate the buffer and do it for real. */
16428 did_emsg = FALSE;
16429 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16430 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16431 if (!did_emsg)
16432 {
16433 s = alloc(len + 1);
16434 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016435 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016436 rettv->vval.v_string = s;
16437 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016438 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016439 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016440 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016441}
16442
16443/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016444 * "pumvisible()" function
16445 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016448{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016449#ifdef FEAT_INS_EXPAND
16450 if (pum_visible())
16451 rettv->vval.v_number = 1;
16452#endif
16453}
16454
Bram Moolenaardb913952012-06-29 12:54:53 +020016455#ifdef FEAT_PYTHON3
16456/*
16457 * "py3eval()" function
16458 */
16459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016460f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016461{
16462 char_u *str;
16463 char_u buf[NUMBUFLEN];
16464
16465 str = get_tv_string_buf(&argvars[0], buf);
16466 do_py3eval(str, rettv);
16467}
16468#endif
16469
16470#ifdef FEAT_PYTHON
16471/*
16472 * "pyeval()" function
16473 */
16474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016475f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016476{
16477 char_u *str;
16478 char_u buf[NUMBUFLEN];
16479
16480 str = get_tv_string_buf(&argvars[0], buf);
16481 do_pyeval(str, rettv);
16482}
16483#endif
16484
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016486 * "range()" function
16487 */
16488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016489f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016490{
16491 long start;
16492 long end;
16493 long stride = 1;
16494 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016495 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016497 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016498 if (argvars[1].v_type == VAR_UNKNOWN)
16499 {
16500 end = start - 1;
16501 start = 0;
16502 }
16503 else
16504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016505 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016506 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016508 }
16509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016510 if (error)
16511 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016512 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016513 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016514 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016515 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016516 else
16517 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016518 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016519 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016520 if (list_append_number(rettv->vval.v_list,
16521 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016522 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016523 }
16524}
16525
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016526/*
16527 * "readfile()" function
16528 */
16529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016530f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531{
16532 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016533 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016534 char_u *fname;
16535 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016536 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16537 int io_size = sizeof(buf);
16538 int readlen; /* size of last fread() */
16539 char_u *prev = NULL; /* previously read bytes, if any */
16540 long prevlen = 0; /* length of data in prev */
16541 long prevsize = 0; /* size of prev buffer */
16542 long maxline = MAXLNUM;
16543 long cnt = 0;
16544 char_u *p; /* position in buf */
16545 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016546
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016547 if (argvars[1].v_type != VAR_UNKNOWN)
16548 {
16549 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16550 binary = TRUE;
16551 if (argvars[2].v_type != VAR_UNKNOWN)
16552 maxline = get_tv_number(&argvars[2]);
16553 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016555 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016557
16558 /* Always open the file in binary mode, library functions have a mind of
16559 * their own about CR-LF conversion. */
16560 fname = get_tv_string(&argvars[0]);
16561 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16562 {
16563 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16564 return;
16565 }
16566
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016567 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016568 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016569 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016570
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016571 /* This for loop processes what was read, but is also entered at end
16572 * of file so that either:
16573 * - an incomplete line gets written
16574 * - a "binary" file gets an empty line at the end if it ends in a
16575 * newline. */
16576 for (p = buf, start = buf;
16577 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16578 ++p)
16579 {
16580 if (*p == '\n' || readlen <= 0)
16581 {
16582 listitem_T *li;
16583 char_u *s = NULL;
16584 long_u len = p - start;
16585
16586 /* Finished a line. Remove CRs before NL. */
16587 if (readlen > 0 && !binary)
16588 {
16589 while (len > 0 && start[len - 1] == '\r')
16590 --len;
16591 /* removal may cross back to the "prev" string */
16592 if (len == 0)
16593 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16594 --prevlen;
16595 }
16596 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016597 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016598 else
16599 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016600 /* Change "prev" buffer to be the right size. This way
16601 * the bytes are only copied once, and very long lines are
16602 * allocated only once. */
16603 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016604 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016605 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016606 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016607 prev = NULL; /* the list will own the string */
16608 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609 }
16610 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 if (s == NULL)
16612 {
16613 do_outofmem_msg((long_u) prevlen + len + 1);
16614 failed = TRUE;
16615 break;
16616 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016617
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016618 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016619 {
16620 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016621 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016622 break;
16623 }
16624 li->li_tv.v_type = VAR_STRING;
16625 li->li_tv.v_lock = 0;
16626 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016627 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016628
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016629 start = p + 1; /* step over newline */
16630 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016631 break;
16632 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016633 else if (*p == NUL)
16634 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016635#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16637 * when finding the BF and check the previous two bytes. */
16638 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016639 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016640 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16641 * + 1, these may be in the "prev" string. */
16642 char_u back1 = p >= buf + 1 ? p[-1]
16643 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16644 char_u back2 = p >= buf + 2 ? p[-2]
16645 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16646 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016648 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016649 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016650 char_u *dest = p - 2;
16651
16652 /* Usually a BOM is at the beginning of a file, and so at
16653 * the beginning of a line; then we can just step over it.
16654 */
16655 if (start == dest)
16656 start = p + 1;
16657 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016658 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016659 /* have to shuffle buf to close gap */
16660 int adjust_prevlen = 0;
16661
16662 if (dest < buf)
16663 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016664 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016665 dest = buf;
16666 }
16667 if (readlen > p - buf + 1)
16668 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16669 readlen -= 3 - adjust_prevlen;
16670 prevlen -= adjust_prevlen;
16671 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016672 }
16673 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016674 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016675#endif
16676 } /* for */
16677
16678 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16679 break;
16680 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016681 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016682 /* There's part of a line in buf, store it in "prev". */
16683 if (p - start + prevlen >= prevsize)
16684 {
16685 /* need bigger "prev" buffer */
16686 char_u *newprev;
16687
16688 /* A common use case is ordinary text files and "prev" gets a
16689 * fragment of a line, so the first allocation is made
16690 * small, to avoid repeatedly 'allocing' large and
16691 * 'reallocing' small. */
16692 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016693 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016694 else
16695 {
16696 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016697 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016698 prevsize = grow50pc > growmin ? grow50pc : growmin;
16699 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016700 newprev = prev == NULL ? alloc(prevsize)
16701 : vim_realloc(prev, prevsize);
16702 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016703 {
16704 do_outofmem_msg((long_u)prevsize);
16705 failed = TRUE;
16706 break;
16707 }
16708 prev = newprev;
16709 }
16710 /* Add the line part to end of "prev". */
16711 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016712 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016713 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016714 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016715
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016716 /*
16717 * For a negative line count use only the lines at the end of the file,
16718 * free the rest.
16719 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016720 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016721 while (cnt > -maxline)
16722 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016723 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016724 --cnt;
16725 }
16726
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016727 if (failed)
16728 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016729 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016730 /* readfile doc says an empty list is returned on error */
16731 rettv->vval.v_list = list_alloc();
16732 }
16733
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016734 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016735 fclose(fd);
16736}
16737
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016738#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016739static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016740
16741/*
16742 * Convert a List to proftime_T.
16743 * Return FAIL when there is something wrong.
16744 */
16745 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016746list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016747{
16748 long n1, n2;
16749 int error = FALSE;
16750
16751 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16752 || arg->vval.v_list->lv_len != 2)
16753 return FAIL;
16754 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16755 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16756# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016757 tm->HighPart = n1;
16758 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016759# else
16760 tm->tv_sec = n1;
16761 tm->tv_usec = n2;
16762# endif
16763 return error ? FAIL : OK;
16764}
16765#endif /* FEAT_RELTIME */
16766
16767/*
16768 * "reltime()" function
16769 */
16770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016771f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016772{
16773#ifdef FEAT_RELTIME
16774 proftime_T res;
16775 proftime_T start;
16776
16777 if (argvars[0].v_type == VAR_UNKNOWN)
16778 {
16779 /* No arguments: get current time. */
16780 profile_start(&res);
16781 }
16782 else if (argvars[1].v_type == VAR_UNKNOWN)
16783 {
16784 if (list2proftime(&argvars[0], &res) == FAIL)
16785 return;
16786 profile_end(&res);
16787 }
16788 else
16789 {
16790 /* Two arguments: compute the difference. */
16791 if (list2proftime(&argvars[0], &start) == FAIL
16792 || list2proftime(&argvars[1], &res) == FAIL)
16793 return;
16794 profile_sub(&res, &start);
16795 }
16796
16797 if (rettv_list_alloc(rettv) == OK)
16798 {
16799 long n1, n2;
16800
16801# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016802 n1 = res.HighPart;
16803 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016804# else
16805 n1 = res.tv_sec;
16806 n2 = res.tv_usec;
16807# endif
16808 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16809 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16810 }
16811#endif
16812}
16813
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016814#ifdef FEAT_FLOAT
16815/*
16816 * "reltimefloat()" function
16817 */
16818 static void
16819f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16820{
16821# ifdef FEAT_RELTIME
16822 proftime_T tm;
16823# endif
16824
16825 rettv->v_type = VAR_FLOAT;
16826 rettv->vval.v_float = 0;
16827# ifdef FEAT_RELTIME
16828 if (list2proftime(&argvars[0], &tm) == OK)
16829 rettv->vval.v_float = profile_float(&tm);
16830# endif
16831}
16832#endif
16833
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016834/*
16835 * "reltimestr()" function
16836 */
16837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016838f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016839{
16840#ifdef FEAT_RELTIME
16841 proftime_T tm;
16842#endif
16843
16844 rettv->v_type = VAR_STRING;
16845 rettv->vval.v_string = NULL;
16846#ifdef FEAT_RELTIME
16847 if (list2proftime(&argvars[0], &tm) == OK)
16848 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16849#endif
16850}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016851
Bram Moolenaar0d660222005-01-07 21:51:51 +000016852#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016853static void make_connection(void);
16854static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855
16856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016857make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016858{
16859 if (X_DISPLAY == NULL
16860# ifdef FEAT_GUI
16861 && !gui.in_use
16862# endif
16863 )
16864 {
16865 x_force_connect = TRUE;
16866 setup_term_clip();
16867 x_force_connect = FALSE;
16868 }
16869}
16870
16871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016872check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873{
16874 make_connection();
16875 if (X_DISPLAY == NULL)
16876 {
16877 EMSG(_("E240: No connection to Vim server"));
16878 return FAIL;
16879 }
16880 return OK;
16881}
16882#endif
16883
16884#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016886remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887{
16888 char_u *server_name;
16889 char_u *keys;
16890 char_u *r = NULL;
16891 char_u buf[NUMBUFLEN];
16892# ifdef WIN32
16893 HWND w;
16894# else
16895 Window w;
16896# endif
16897
16898 if (check_restricted() || check_secure())
16899 return;
16900
16901# ifdef FEAT_X11
16902 if (check_connection() == FAIL)
16903 return;
16904# endif
16905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 server_name = get_tv_string_chk(&argvars[0]);
16907 if (server_name == NULL)
16908 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909 keys = get_tv_string_buf(&argvars[1], buf);
16910# ifdef WIN32
16911 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16912# else
16913 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16914 < 0)
16915# endif
16916 {
16917 if (r != NULL)
16918 EMSG(r); /* sending worked but evaluation failed */
16919 else
16920 EMSG2(_("E241: Unable to send to %s"), server_name);
16921 return;
16922 }
16923
16924 rettv->vval.v_string = r;
16925
16926 if (argvars[2].v_type != VAR_UNKNOWN)
16927 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016928 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016929 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016932 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016933 v.di_tv.v_type = VAR_STRING;
16934 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935 idvar = get_tv_string_chk(&argvars[2]);
16936 if (idvar != NULL)
16937 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016938 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 }
16940}
16941#endif
16942
16943/*
16944 * "remote_expr()" function
16945 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016947f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948{
16949 rettv->v_type = VAR_STRING;
16950 rettv->vval.v_string = NULL;
16951#ifdef FEAT_CLIENTSERVER
16952 remote_common(argvars, rettv, TRUE);
16953#endif
16954}
16955
16956/*
16957 * "remote_foreground()" function
16958 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016960f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962#ifdef FEAT_CLIENTSERVER
16963# ifdef WIN32
16964 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016965 {
16966 char_u *server_name = get_tv_string_chk(&argvars[0]);
16967
16968 if (server_name != NULL)
16969 serverForeground(server_name);
16970 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971# else
16972 /* Send a foreground() expression to the server. */
16973 argvars[1].v_type = VAR_STRING;
16974 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16975 argvars[2].v_type = VAR_UNKNOWN;
16976 remote_common(argvars, rettv, TRUE);
16977 vim_free(argvars[1].vval.v_string);
16978# endif
16979#endif
16980}
16981
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016983f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984{
16985#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 char_u *s = NULL;
16988# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016989 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016991 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992
16993 if (check_restricted() || check_secure())
16994 {
16995 rettv->vval.v_number = -1;
16996 return;
16997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 serverid = get_tv_string_chk(&argvars[0]);
16999 if (serverid == NULL)
17000 {
17001 rettv->vval.v_number = -1;
17002 return; /* type error; errmsg already given */
17003 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017005 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 if (n == 0)
17007 rettv->vval.v_number = -1;
17008 else
17009 {
17010 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17011 rettv->vval.v_number = (s != NULL);
17012 }
17013# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 if (check_connection() == FAIL)
17015 return;
17016
17017 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019# endif
17020
17021 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017023 char_u *retvar;
17024
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 v.di_tv.v_type = VAR_STRING;
17026 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 retvar = get_tv_string_chk(&argvars[1]);
17028 if (retvar != NULL)
17029 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017030 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 }
17032#else
17033 rettv->vval.v_number = -1;
17034#endif
17035}
17036
Bram Moolenaar0d660222005-01-07 21:51:51 +000017037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017038f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039{
17040 char_u *r = NULL;
17041
17042#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 char_u *serverid = get_tv_string_chk(&argvars[0]);
17044
17045 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 {
17047# ifdef WIN32
17048 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017049 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017051 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 if (n != 0)
17053 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17054 if (r == NULL)
17055# else
17056 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058# endif
17059 EMSG(_("E277: Unable to read a server reply"));
17060 }
17061#endif
17062 rettv->v_type = VAR_STRING;
17063 rettv->vval.v_string = r;
17064}
17065
17066/*
17067 * "remote_send()" function
17068 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017070f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071{
17072 rettv->v_type = VAR_STRING;
17073 rettv->vval.v_string = NULL;
17074#ifdef FEAT_CLIENTSERVER
17075 remote_common(argvars, rettv, FALSE);
17076#endif
17077}
17078
17079/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017080 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081 */
17082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017083f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017084{
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 list_T *l;
17086 listitem_T *item, *item2;
17087 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017088 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017089 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017090 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 dict_T *d;
17092 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017093 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017094
Bram Moolenaar8c711452005-01-14 21:53:12 +000017095 if (argvars[0].v_type == VAR_DICT)
17096 {
17097 if (argvars[2].v_type != VAR_UNKNOWN)
17098 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017099 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017100 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017102 key = get_tv_string_chk(&argvars[1]);
17103 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017105 di = dict_find(d, key, -1);
17106 if (di == NULL)
17107 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017108 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17109 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 {
17111 *rettv = di->di_tv;
17112 init_tv(&di->di_tv);
17113 dictitem_remove(d, di);
17114 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017115 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017116 }
17117 }
17118 else if (argvars[0].v_type != VAR_LIST)
17119 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017120 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017121 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123 int error = FALSE;
17124
17125 idx = get_tv_number_chk(&argvars[1], &error);
17126 if (error)
17127 ; /* type error: do nothing, errmsg already given */
17128 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017129 EMSGN(_(e_listidx), idx);
17130 else
17131 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017132 if (argvars[2].v_type == VAR_UNKNOWN)
17133 {
17134 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017135 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017136 *rettv = item->li_tv;
17137 vim_free(item);
17138 }
17139 else
17140 {
17141 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017142 end = get_tv_number_chk(&argvars[2], &error);
17143 if (error)
17144 ; /* type error: do nothing */
17145 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017146 EMSGN(_(e_listidx), end);
17147 else
17148 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017149 int cnt = 0;
17150
17151 for (li = item; li != NULL; li = li->li_next)
17152 {
17153 ++cnt;
17154 if (li == item2)
17155 break;
17156 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157 if (li == NULL) /* didn't find "item2" after "item" */
17158 EMSG(_(e_invrange));
17159 else
17160 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017161 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017162 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017163 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017164 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017165 l->lv_first = item;
17166 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017167 item->li_prev = NULL;
17168 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017169 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017170 }
17171 }
17172 }
17173 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017174 }
17175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176}
17177
17178/*
17179 * "rename({from}, {to})" function
17180 */
17181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017182f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183{
17184 char_u buf[NUMBUFLEN];
17185
17186 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17190 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191}
17192
17193/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017194 * "repeat()" function
17195 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017197f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017198{
17199 char_u *p;
17200 int n;
17201 int slen;
17202 int len;
17203 char_u *r;
17204 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017205
17206 n = get_tv_number(&argvars[1]);
17207 if (argvars[0].v_type == VAR_LIST)
17208 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017209 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017210 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017211 if (list_extend(rettv->vval.v_list,
17212 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017213 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017214 }
17215 else
17216 {
17217 p = get_tv_string(&argvars[0]);
17218 rettv->v_type = VAR_STRING;
17219 rettv->vval.v_string = NULL;
17220
17221 slen = (int)STRLEN(p);
17222 len = slen * n;
17223 if (len <= 0)
17224 return;
17225
17226 r = alloc(len + 1);
17227 if (r != NULL)
17228 {
17229 for (i = 0; i < n; i++)
17230 mch_memmove(r + i * slen, p, (size_t)slen);
17231 r[len] = NUL;
17232 }
17233
17234 rettv->vval.v_string = r;
17235 }
17236}
17237
17238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 * "resolve()" function
17240 */
17241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017242f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243{
17244 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017245#ifdef HAVE_READLINK
17246 char_u *buf = NULL;
17247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017249 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250#ifdef FEAT_SHORTCUT
17251 {
17252 char_u *v = NULL;
17253
17254 v = mch_resolve_shortcut(p);
17255 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017256 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017257 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017258 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 }
17260#else
17261# ifdef HAVE_READLINK
17262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 char_u *cpy;
17264 int len;
17265 char_u *remain = NULL;
17266 char_u *q;
17267 int is_relative_to_current = FALSE;
17268 int has_trailing_pathsep = FALSE;
17269 int limit = 100;
17270
17271 p = vim_strsave(p);
17272
17273 if (p[0] == '.' && (vim_ispathsep(p[1])
17274 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17275 is_relative_to_current = TRUE;
17276
17277 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017278 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017281 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283
17284 q = getnextcomp(p);
17285 if (*q != NUL)
17286 {
17287 /* Separate the first path component in "p", and keep the
17288 * remainder (beginning with the path separator). */
17289 remain = vim_strsave(q - 1);
17290 q[-1] = NUL;
17291 }
17292
Bram Moolenaard9462e32011-04-11 21:35:11 +020017293 buf = alloc(MAXPATHL + 1);
17294 if (buf == NULL)
17295 goto fail;
17296
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 for (;;)
17298 {
17299 for (;;)
17300 {
17301 len = readlink((char *)p, (char *)buf, MAXPATHL);
17302 if (len <= 0)
17303 break;
17304 buf[len] = NUL;
17305
17306 if (limit-- == 0)
17307 {
17308 vim_free(p);
17309 vim_free(remain);
17310 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017311 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 goto fail;
17313 }
17314
17315 /* Ensure that the result will have a trailing path separator
17316 * if the argument has one. */
17317 if (remain == NULL && has_trailing_pathsep)
17318 add_pathsep(buf);
17319
17320 /* Separate the first path component in the link value and
17321 * concatenate the remainders. */
17322 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17323 if (*q != NUL)
17324 {
17325 if (remain == NULL)
17326 remain = vim_strsave(q - 1);
17327 else
17328 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017329 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 if (cpy != NULL)
17331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 vim_free(remain);
17333 remain = cpy;
17334 }
17335 }
17336 q[-1] = NUL;
17337 }
17338
17339 q = gettail(p);
17340 if (q > p && *q == NUL)
17341 {
17342 /* Ignore trailing path separator. */
17343 q[-1] = NUL;
17344 q = gettail(p);
17345 }
17346 if (q > p && !mch_isFullName(buf))
17347 {
17348 /* symlink is relative to directory of argument */
17349 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17350 if (cpy != NULL)
17351 {
17352 STRCPY(cpy, p);
17353 STRCPY(gettail(cpy), buf);
17354 vim_free(p);
17355 p = cpy;
17356 }
17357 }
17358 else
17359 {
17360 vim_free(p);
17361 p = vim_strsave(buf);
17362 }
17363 }
17364
17365 if (remain == NULL)
17366 break;
17367
17368 /* Append the first path component of "remain" to "p". */
17369 q = getnextcomp(remain + 1);
17370 len = q - remain - (*q != NUL);
17371 cpy = vim_strnsave(p, STRLEN(p) + len);
17372 if (cpy != NULL)
17373 {
17374 STRNCAT(cpy, remain, len);
17375 vim_free(p);
17376 p = cpy;
17377 }
17378 /* Shorten "remain". */
17379 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017380 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 else
17382 {
17383 vim_free(remain);
17384 remain = NULL;
17385 }
17386 }
17387
17388 /* If the result is a relative path name, make it explicitly relative to
17389 * the current directory if and only if the argument had this form. */
17390 if (!vim_ispathsep(*p))
17391 {
17392 if (is_relative_to_current
17393 && *p != NUL
17394 && !(p[0] == '.'
17395 && (p[1] == NUL
17396 || vim_ispathsep(p[1])
17397 || (p[1] == '.'
17398 && (p[2] == NUL
17399 || vim_ispathsep(p[2]))))))
17400 {
17401 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017402 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 if (cpy != NULL)
17404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 vim_free(p);
17406 p = cpy;
17407 }
17408 }
17409 else if (!is_relative_to_current)
17410 {
17411 /* Strip leading "./". */
17412 q = p;
17413 while (q[0] == '.' && vim_ispathsep(q[1]))
17414 q += 2;
17415 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017416 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 }
17418 }
17419
17420 /* Ensure that the result will have no trailing path separator
17421 * if the argument had none. But keep "/" or "//". */
17422 if (!has_trailing_pathsep)
17423 {
17424 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017425 if (after_pathsep(p, q))
17426 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 }
17428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017429 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 }
17431# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017432 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433# endif
17434#endif
17435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437
17438#ifdef HAVE_READLINK
17439fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017440 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017442 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443}
17444
17445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017446 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 */
17448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017449f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450{
Bram Moolenaar33570922005-01-25 22:26:29 +000017451 list_T *l;
17452 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453
Bram Moolenaar0d660222005-01-07 21:51:51 +000017454 if (argvars[0].v_type != VAR_LIST)
17455 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017456 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017457 && !tv_check_lock(l->lv_lock,
17458 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017459 {
17460 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017461 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017462 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017463 while (li != NULL)
17464 {
17465 ni = li->li_prev;
17466 list_append(l, li);
17467 li = ni;
17468 }
17469 rettv->vval.v_list = l;
17470 rettv->v_type = VAR_LIST;
17471 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017472 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474}
17475
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017476#define SP_NOMOVE 0x01 /* don't move cursor */
17477#define SP_REPEAT 0x02 /* repeat to find outer pair */
17478#define SP_RETCOUNT 0x04 /* return matchcount */
17479#define SP_SETPCMARK 0x08 /* set previous context mark */
17480#define SP_START 0x10 /* accept match at start position */
17481#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17482#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017483#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017484
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017485static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017486
17487/*
17488 * Get flags for a search function.
17489 * Possibly sets "p_ws".
17490 * Returns BACKWARD, FORWARD or zero (for an error).
17491 */
17492 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017493get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017494{
17495 int dir = FORWARD;
17496 char_u *flags;
17497 char_u nbuf[NUMBUFLEN];
17498 int mask;
17499
17500 if (varp->v_type != VAR_UNKNOWN)
17501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017502 flags = get_tv_string_buf_chk(varp, nbuf);
17503 if (flags == NULL)
17504 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017505 while (*flags != NUL)
17506 {
17507 switch (*flags)
17508 {
17509 case 'b': dir = BACKWARD; break;
17510 case 'w': p_ws = TRUE; break;
17511 case 'W': p_ws = FALSE; break;
17512 default: mask = 0;
17513 if (flagsp != NULL)
17514 switch (*flags)
17515 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017516 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017517 case 'e': mask = SP_END; break;
17518 case 'm': mask = SP_RETCOUNT; break;
17519 case 'n': mask = SP_NOMOVE; break;
17520 case 'p': mask = SP_SUBPAT; break;
17521 case 'r': mask = SP_REPEAT; break;
17522 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017523 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524 }
17525 if (mask == 0)
17526 {
17527 EMSG2(_(e_invarg2), flags);
17528 dir = 0;
17529 }
17530 else
17531 *flagsp |= mask;
17532 }
17533 if (dir == 0)
17534 break;
17535 ++flags;
17536 }
17537 }
17538 return dir;
17539}
17540
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017542 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017544 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017545search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017547 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 char_u *pat;
17549 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017550 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 int save_p_ws = p_ws;
17552 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017553 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017554 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017555 proftime_T tm;
17556#ifdef FEAT_RELTIME
17557 long time_limit = 0;
17558#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017559 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017560 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017562 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017563 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017564 if (dir == 0)
17565 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017566 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017567 if (flags & SP_START)
17568 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017569 if (flags & SP_END)
17570 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017571 if (flags & SP_COLUMN)
17572 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017573
Bram Moolenaar76929292008-01-06 19:07:36 +000017574 /* Optional arguments: line number to stop searching and timeout. */
17575 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017576 {
17577 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17578 if (lnum_stop < 0)
17579 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017580#ifdef FEAT_RELTIME
17581 if (argvars[3].v_type != VAR_UNKNOWN)
17582 {
17583 time_limit = get_tv_number_chk(&argvars[3], NULL);
17584 if (time_limit < 0)
17585 goto theend;
17586 }
17587#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017588 }
17589
Bram Moolenaar76929292008-01-06 19:07:36 +000017590#ifdef FEAT_RELTIME
17591 /* Set the time limit, if there is one. */
17592 profile_setlimit(time_limit, &tm);
17593#endif
17594
Bram Moolenaar231334e2005-07-25 20:46:57 +000017595 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017596 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017597 * Check to make sure only those flags are set.
17598 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17599 * flags cannot be set. Check for that condition also.
17600 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017601 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017602 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017603 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017604 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017605 goto theend;
17606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017608 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017609 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017610 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017611 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017613 if (flags & SP_SUBPAT)
17614 retval = subpatnum;
17615 else
17616 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017617 if (flags & SP_SETPCMARK)
17618 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017620 if (match_pos != NULL)
17621 {
17622 /* Store the match cursor position */
17623 match_pos->lnum = pos.lnum;
17624 match_pos->col = pos.col + 1;
17625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626 /* "/$" will put the cursor after the end of the line, may need to
17627 * correct that here */
17628 check_cursor();
17629 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017630
17631 /* If 'n' flag is used: restore cursor position. */
17632 if (flags & SP_NOMOVE)
17633 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017634 else
17635 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017636theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017638
17639 return retval;
17640}
17641
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017642#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017643
17644/*
17645 * round() is not in C90, use ceil() or floor() instead.
17646 */
17647 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017648vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017649{
17650 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17651}
17652
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017653/*
17654 * "round({float})" function
17655 */
17656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017657f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017658{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017659 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017660
17661 rettv->v_type = VAR_FLOAT;
17662 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017663 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017664 else
17665 rettv->vval.v_float = 0.0;
17666}
17667#endif
17668
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017669/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017670 * "screenattr()" function
17671 */
17672 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017673f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017674{
17675 int row;
17676 int col;
17677 int c;
17678
17679 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17680 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17681 if (row < 0 || row >= screen_Rows
17682 || col < 0 || col >= screen_Columns)
17683 c = -1;
17684 else
17685 c = ScreenAttrs[LineOffset[row] + col];
17686 rettv->vval.v_number = c;
17687}
17688
17689/*
17690 * "screenchar()" function
17691 */
17692 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017693f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017694{
17695 int row;
17696 int col;
17697 int off;
17698 int c;
17699
17700 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17701 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17702 if (row < 0 || row >= screen_Rows
17703 || col < 0 || col >= screen_Columns)
17704 c = -1;
17705 else
17706 {
17707 off = LineOffset[row] + col;
17708#ifdef FEAT_MBYTE
17709 if (enc_utf8 && ScreenLinesUC[off] != 0)
17710 c = ScreenLinesUC[off];
17711 else
17712#endif
17713 c = ScreenLines[off];
17714 }
17715 rettv->vval.v_number = c;
17716}
17717
17718/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017719 * "screencol()" function
17720 *
17721 * First column is 1 to be consistent with virtcol().
17722 */
17723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017724f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017725{
17726 rettv->vval.v_number = screen_screencol() + 1;
17727}
17728
17729/*
17730 * "screenrow()" function
17731 */
17732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017733f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017734{
17735 rettv->vval.v_number = screen_screenrow() + 1;
17736}
17737
17738/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017739 * "search()" function
17740 */
17741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017742f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017743{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017744 int flags = 0;
17745
17746 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747}
17748
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017750 * "searchdecl()" function
17751 */
17752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017753f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017754{
17755 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017756 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017757 int error = FALSE;
17758 char_u *name;
17759
17760 rettv->vval.v_number = 1; /* default: FAIL */
17761
17762 name = get_tv_string_chk(&argvars[0]);
17763 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017764 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017765 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017766 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17767 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17768 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017769 if (!error && name != NULL)
17770 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017771 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017772}
17773
17774/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017775 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017778searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
17780 char_u *spat, *mpat, *epat;
17781 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 int dir;
17784 int flags = 0;
17785 char_u nbuf1[NUMBUFLEN];
17786 char_u nbuf2[NUMBUFLEN];
17787 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017788 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017789 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017790 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017793 spat = get_tv_string_chk(&argvars[0]);
17794 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17795 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17796 if (spat == NULL || mpat == NULL || epat == NULL)
17797 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 /* Handle the optional fourth argument: flags */
17800 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017801 if (dir == 0)
17802 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017803
17804 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017805 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17806 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017807 if ((flags & (SP_END | SP_SUBPAT)) != 0
17808 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017809 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017810 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017811 goto theend;
17812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017814 /* Using 'r' implies 'W', otherwise it doesn't work. */
17815 if (flags & SP_REPEAT)
17816 p_ws = FALSE;
17817
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017819 if (argvars[3].v_type == VAR_UNKNOWN
17820 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 skip = (char_u *)"";
17822 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017824 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017825 if (argvars[5].v_type != VAR_UNKNOWN)
17826 {
17827 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17828 if (lnum_stop < 0)
17829 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017830#ifdef FEAT_RELTIME
17831 if (argvars[6].v_type != VAR_UNKNOWN)
17832 {
17833 time_limit = get_tv_number_chk(&argvars[6], NULL);
17834 if (time_limit < 0)
17835 goto theend;
17836 }
17837#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017838 }
17839 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017840 if (skip == NULL)
17841 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017843 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017844 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017845
17846theend:
17847 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017848
17849 return retval;
17850}
17851
17852/*
17853 * "searchpair()" function
17854 */
17855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017856f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017857{
17858 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17859}
17860
17861/*
17862 * "searchpairpos()" function
17863 */
17864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017865f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017866{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867 pos_T match_pos;
17868 int lnum = 0;
17869 int col = 0;
17870
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017871 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017872 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017873
17874 if (searchpair_cmn(argvars, &match_pos) > 0)
17875 {
17876 lnum = match_pos.lnum;
17877 col = match_pos.col;
17878 }
17879
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017880 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17881 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017882}
17883
17884/*
17885 * Search for a start/middle/end thing.
17886 * Used by searchpair(), see its documentation for the details.
17887 * Returns 0 or -1 for no match,
17888 */
17889 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017890do_searchpair(
17891 char_u *spat, /* start pattern */
17892 char_u *mpat, /* middle pattern */
17893 char_u *epat, /* end pattern */
17894 int dir, /* BACKWARD or FORWARD */
17895 char_u *skip, /* skip expression */
17896 int flags, /* SP_SETPCMARK and other SP_ values */
17897 pos_T *match_pos,
17898 linenr_T lnum_stop, /* stop at this line if not zero */
17899 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017900{
17901 char_u *save_cpo;
17902 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17903 long retval = 0;
17904 pos_T pos;
17905 pos_T firstpos;
17906 pos_T foundpos;
17907 pos_T save_cursor;
17908 pos_T save_pos;
17909 int n;
17910 int r;
17911 int nest = 1;
17912 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017913 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017914 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017915
17916 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17917 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017918 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017919
Bram Moolenaar76929292008-01-06 19:07:36 +000017920#ifdef FEAT_RELTIME
17921 /* Set the time limit, if there is one. */
17922 profile_setlimit(time_limit, &tm);
17923#endif
17924
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017925 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17926 * start/middle/end (pat3, for the top pair). */
17927 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17928 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17929 if (pat2 == NULL || pat3 == NULL)
17930 goto theend;
17931 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17932 if (*mpat == NUL)
17933 STRCPY(pat3, pat2);
17934 else
17935 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17936 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017937 if (flags & SP_START)
17938 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017939
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 save_cursor = curwin->w_cursor;
17941 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017942 clearpos(&firstpos);
17943 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 pat = pat3;
17945 for (;;)
17946 {
17947 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017948 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17950 /* didn't find it or found the first match again: FAIL */
17951 break;
17952
17953 if (firstpos.lnum == 0)
17954 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017955 if (equalpos(pos, foundpos))
17956 {
17957 /* Found the same position again. Can happen with a pattern that
17958 * has "\zs" at the end and searching backwards. Advance one
17959 * character and try again. */
17960 if (dir == BACKWARD)
17961 decl(&pos);
17962 else
17963 incl(&pos);
17964 }
17965 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017967 /* clear the start flag to avoid getting stuck here */
17968 options &= ~SEARCH_START;
17969
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 /* If the skip pattern matches, ignore this match. */
17971 if (*skip != NUL)
17972 {
17973 save_pos = curwin->w_cursor;
17974 curwin->w_cursor = pos;
17975 r = eval_to_bool(skip, &err, NULL, FALSE);
17976 curwin->w_cursor = save_pos;
17977 if (err)
17978 {
17979 /* Evaluating {skip} caused an error, break here. */
17980 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017981 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 break;
17983 }
17984 if (r)
17985 continue;
17986 }
17987
17988 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17989 {
17990 /* Found end when searching backwards or start when searching
17991 * forward: nested pair. */
17992 ++nest;
17993 pat = pat2; /* nested, don't search for middle */
17994 }
17995 else
17996 {
17997 /* Found end when searching forward or start when searching
17998 * backward: end of (nested) pair; or found middle in outer pair. */
17999 if (--nest == 1)
18000 pat = pat3; /* outer level, search for middle */
18001 }
18002
18003 if (nest == 0)
18004 {
18005 /* Found the match: return matchcount or line number. */
18006 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018007 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018009 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018010 if (flags & SP_SETPCMARK)
18011 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 curwin->w_cursor = pos;
18013 if (!(flags & SP_REPEAT))
18014 break;
18015 nest = 1; /* search for next unmatched */
18016 }
18017 }
18018
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018019 if (match_pos != NULL)
18020 {
18021 /* Store the match cursor position */
18022 match_pos->lnum = curwin->w_cursor.lnum;
18023 match_pos->col = curwin->w_cursor.col + 1;
18024 }
18025
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018027 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 curwin->w_cursor = save_cursor;
18029
18030theend:
18031 vim_free(pat2);
18032 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018033 if (p_cpo == empty_option)
18034 p_cpo = save_cpo;
18035 else
18036 /* Darn, evaluating the {skip} expression changed the value. */
18037 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018038
18039 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040}
18041
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018042/*
18043 * "searchpos()" function
18044 */
18045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018046f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018047{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018048 pos_T match_pos;
18049 int lnum = 0;
18050 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018051 int n;
18052 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018053
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018054 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018055 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018056
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018057 n = search_cmn(argvars, &match_pos, &flags);
18058 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018059 {
18060 lnum = match_pos.lnum;
18061 col = match_pos.col;
18062 }
18063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018064 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18065 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018066 if (flags & SP_SUBPAT)
18067 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018068}
18069
Bram Moolenaar0d660222005-01-07 21:51:51 +000018070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018071f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018073#ifdef FEAT_CLIENTSERVER
18074 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018075 char_u *server = get_tv_string_chk(&argvars[0]);
18076 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077
Bram Moolenaar0d660222005-01-07 21:51:51 +000018078 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018079 if (server == NULL || reply == NULL)
18080 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018081 if (check_restricted() || check_secure())
18082 return;
18083# ifdef FEAT_X11
18084 if (check_connection() == FAIL)
18085 return;
18086# endif
18087
18088 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018090 EMSG(_("E258: Unable to send to client"));
18091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018093 rettv->vval.v_number = 0;
18094#else
18095 rettv->vval.v_number = -1;
18096#endif
18097}
18098
Bram Moolenaar0d660222005-01-07 21:51:51 +000018099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018100f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018101{
18102 char_u *r = NULL;
18103
18104#ifdef FEAT_CLIENTSERVER
18105# ifdef WIN32
18106 r = serverGetVimNames();
18107# else
18108 make_connection();
18109 if (X_DISPLAY != NULL)
18110 r = serverGetVimNames(X_DISPLAY);
18111# endif
18112#endif
18113 rettv->v_type = VAR_STRING;
18114 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115}
18116
18117/*
18118 * "setbufvar()" function
18119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018121f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122{
18123 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018126 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 char_u nbuf[NUMBUFLEN];
18128
18129 if (check_restricted() || check_secure())
18130 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018131 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18132 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018133 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 varp = &argvars[2];
18135
18136 if (buf != NULL && varname != NULL && varp != NULL)
18137 {
18138 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140
18141 if (*varname == '&')
18142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018143 long numval;
18144 char_u *strval;
18145 int error = FALSE;
18146
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018148 numval = get_tv_number_chk(varp, &error);
18149 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018150 if (!error && strval != NULL)
18151 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 }
18153 else
18154 {
18155 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18156 if (bufvarname != NULL)
18157 {
18158 STRCPY(bufvarname, "b:");
18159 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018160 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 vim_free(bufvarname);
18162 }
18163 }
18164
18165 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168}
18169
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018171f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018172{
18173 dict_T *d;
18174 dictitem_T *di;
18175 char_u *csearch;
18176
18177 if (argvars[0].v_type != VAR_DICT)
18178 {
18179 EMSG(_(e_dictreq));
18180 return;
18181 }
18182
18183 if ((d = argvars[0].vval.v_dict) != NULL)
18184 {
18185 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18186 if (csearch != NULL)
18187 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018188#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018189 if (enc_utf8)
18190 {
18191 int pcc[MAX_MCO];
18192 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018193
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018194 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18195 }
18196 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018197#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018198 set_last_csearch(PTR2CHAR(csearch),
18199 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018200 }
18201
18202 di = dict_find(d, (char_u *)"forward", -1);
18203 if (di != NULL)
18204 set_csearch_direction(get_tv_number(&di->di_tv)
18205 ? FORWARD : BACKWARD);
18206
18207 di = dict_find(d, (char_u *)"until", -1);
18208 if (di != NULL)
18209 set_csearch_until(!!get_tv_number(&di->di_tv));
18210 }
18211}
18212
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213/*
18214 * "setcmdpos()" function
18215 */
18216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018217f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018219 int pos = (int)get_tv_number(&argvars[0]) - 1;
18220
18221 if (pos >= 0)
18222 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223}
18224
18225/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018226 * "setfperm({fname}, {mode})" function
18227 */
18228 static void
18229f_setfperm(typval_T *argvars, typval_T *rettv)
18230{
18231 char_u *fname;
18232 char_u modebuf[NUMBUFLEN];
18233 char_u *mode_str;
18234 int i;
18235 int mask;
18236 int mode = 0;
18237
18238 rettv->vval.v_number = 0;
18239 fname = get_tv_string_chk(&argvars[0]);
18240 if (fname == NULL)
18241 return;
18242 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18243 if (mode_str == NULL)
18244 return;
18245 if (STRLEN(mode_str) != 9)
18246 {
18247 EMSG2(_(e_invarg2), mode_str);
18248 return;
18249 }
18250
18251 mask = 1;
18252 for (i = 8; i >= 0; --i)
18253 {
18254 if (mode_str[i] != '-')
18255 mode |= mask;
18256 mask = mask << 1;
18257 }
18258 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18259}
18260
18261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 * "setline()" function
18263 */
18264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018265f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266{
18267 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018268 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018269 list_T *l = NULL;
18270 listitem_T *li = NULL;
18271 long added = 0;
18272 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018274 lnum = get_tv_lnum(&argvars[0]);
18275 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018277 l = argvars[1].vval.v_list;
18278 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018280 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018281 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018282
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018283 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018284 for (;;)
18285 {
18286 if (l != NULL)
18287 {
18288 /* list argument, get next string */
18289 if (li == NULL)
18290 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018291 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018292 li = li->li_next;
18293 }
18294
18295 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018296 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018297 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018298
18299 /* When coming here from Insert mode, sync undo, so that this can be
18300 * undone separately from what was previously inserted. */
18301 if (u_sync_once == 2)
18302 {
18303 u_sync_once = 1; /* notify that u_sync() was called */
18304 u_sync(TRUE);
18305 }
18306
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018307 if (lnum <= curbuf->b_ml.ml_line_count)
18308 {
18309 /* existing line, replace it */
18310 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18311 {
18312 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018313 if (lnum == curwin->w_cursor.lnum)
18314 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018315 rettv->vval.v_number = 0; /* OK */
18316 }
18317 }
18318 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18319 {
18320 /* lnum is one past the last line, append the line */
18321 ++added;
18322 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18323 rettv->vval.v_number = 0; /* OK */
18324 }
18325
18326 if (l == NULL) /* only one string argument */
18327 break;
18328 ++lnum;
18329 }
18330
18331 if (added > 0)
18332 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333}
18334
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018335static 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 +000018336
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018338 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018339 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018341set_qf_ll_list(
18342 win_T *wp UNUSED,
18343 typval_T *list_arg UNUSED,
18344 typval_T *action_arg UNUSED,
18345 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018346{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018347#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018348 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018349 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018350 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018351#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018352
Bram Moolenaar2641f772005-03-25 21:58:17 +000018353 rettv->vval.v_number = -1;
18354
18355#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018356 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018357 EMSG(_(e_listreq));
18358 else
18359 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018360 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018361
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018362 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018363 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018364 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018365 if (act == NULL)
18366 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018367 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018368 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018369 else
18370 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018371 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018372 else if (action_arg->v_type == VAR_UNKNOWN)
18373 action = ' ';
18374 else
18375 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018376
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018377 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018378 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018379 rettv->vval.v_number = 0;
18380 }
18381#endif
18382}
18383
18384/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018385 * "setloclist()" function
18386 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018388f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018389{
18390 win_T *win;
18391
18392 rettv->vval.v_number = -1;
18393
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018394 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018395 if (win != NULL)
18396 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18397}
18398
18399/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018400 * "setmatches()" function
18401 */
18402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018403f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018404{
18405#ifdef FEAT_SEARCH_EXTRA
18406 list_T *l;
18407 listitem_T *li;
18408 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018409 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018410
18411 rettv->vval.v_number = -1;
18412 if (argvars[0].v_type != VAR_LIST)
18413 {
18414 EMSG(_(e_listreq));
18415 return;
18416 }
18417 if ((l = argvars[0].vval.v_list) != NULL)
18418 {
18419
18420 /* To some extent make sure that we are dealing with a list from
18421 * "getmatches()". */
18422 li = l->lv_first;
18423 while (li != NULL)
18424 {
18425 if (li->li_tv.v_type != VAR_DICT
18426 || (d = li->li_tv.vval.v_dict) == NULL)
18427 {
18428 EMSG(_(e_invarg));
18429 return;
18430 }
18431 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018432 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18433 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018434 && dict_find(d, (char_u *)"priority", -1) != NULL
18435 && dict_find(d, (char_u *)"id", -1) != NULL))
18436 {
18437 EMSG(_(e_invarg));
18438 return;
18439 }
18440 li = li->li_next;
18441 }
18442
18443 clear_matches(curwin);
18444 li = l->lv_first;
18445 while (li != NULL)
18446 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018447 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018448 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018449 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018450 char_u *group;
18451 int priority;
18452 int id;
18453 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018454
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018455 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018456 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18457 {
18458 if (s == NULL)
18459 {
18460 s = list_alloc();
18461 if (s == NULL)
18462 return;
18463 }
18464
18465 /* match from matchaddpos() */
18466 for (i = 1; i < 9; i++)
18467 {
18468 sprintf((char *)buf, (char *)"pos%d", i);
18469 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18470 {
18471 if (di->di_tv.v_type != VAR_LIST)
18472 return;
18473
18474 list_append_tv(s, &di->di_tv);
18475 s->lv_refcount++;
18476 }
18477 else
18478 break;
18479 }
18480 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018481
18482 group = get_dict_string(d, (char_u *)"group", FALSE);
18483 priority = (int)get_dict_number(d, (char_u *)"priority");
18484 id = (int)get_dict_number(d, (char_u *)"id");
18485 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18486 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18487 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018488 if (i == 0)
18489 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018490 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018491 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018492 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018493 }
18494 else
18495 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018496 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018497 list_unref(s);
18498 s = NULL;
18499 }
18500
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018501 li = li->li_next;
18502 }
18503 rettv->vval.v_number = 0;
18504 }
18505#endif
18506}
18507
18508/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018509 * "setpos()" function
18510 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018512f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018513{
18514 pos_T pos;
18515 int fnum;
18516 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018517 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018518
Bram Moolenaar08250432008-02-13 11:42:46 +000018519 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018520 name = get_tv_string_chk(argvars);
18521 if (name != NULL)
18522 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018523 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018524 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018525 if (--pos.col < 0)
18526 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018527 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018528 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018529 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018530 if (fnum == curbuf->b_fnum)
18531 {
18532 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018533 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018534 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018535 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018536 curwin->w_set_curswant = FALSE;
18537 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018538 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018539 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018540 }
18541 else
18542 EMSG(_(e_invarg));
18543 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018544 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18545 {
18546 /* set mark */
18547 if (setmark_pos(name[1], &pos, fnum) == OK)
18548 rettv->vval.v_number = 0;
18549 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018550 else
18551 EMSG(_(e_invarg));
18552 }
18553 }
18554}
18555
18556/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018557 * "setqflist()" function
18558 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018560f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018561{
18562 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18563}
18564
18565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566 * "setreg()" function
18567 */
18568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018569f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570{
18571 int regname;
18572 char_u *strregname;
18573 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018574 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 int append;
18576 char_u yank_type;
18577 long block_len;
18578
18579 block_len = -1;
18580 yank_type = MAUTO;
18581 append = FALSE;
18582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018583 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018584 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018586 if (strregname == NULL)
18587 return; /* type error; errmsg already given */
18588 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589 if (regname == 0 || regname == '@')
18590 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018594 stropt = get_tv_string_chk(&argvars[2]);
18595 if (stropt == NULL)
18596 return; /* type error */
18597 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018598 switch (*stropt)
18599 {
18600 case 'a': case 'A': /* append */
18601 append = TRUE;
18602 break;
18603 case 'v': case 'c': /* character-wise selection */
18604 yank_type = MCHAR;
18605 break;
18606 case 'V': case 'l': /* line-wise selection */
18607 yank_type = MLINE;
18608 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 case 'b': case Ctrl_V: /* block-wise selection */
18610 yank_type = MBLOCK;
18611 if (VIM_ISDIGIT(stropt[1]))
18612 {
18613 ++stropt;
18614 block_len = getdigits(&stropt) - 1;
18615 --stropt;
18616 }
18617 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 }
18619 }
18620
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018621 if (argvars[1].v_type == VAR_LIST)
18622 {
18623 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018624 char_u **allocval;
18625 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018626 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018627 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018628 int len = argvars[1].vval.v_list->lv_len;
18629 listitem_T *li;
18630
Bram Moolenaar7d647822014-04-05 21:28:56 +020018631 /* First half: use for pointers to result lines; second half: use for
18632 * pointers to allocated copies. */
18633 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018634 if (lstval == NULL)
18635 return;
18636 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018637 allocval = lstval + len + 2;
18638 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018639
18640 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18641 li = li->li_next)
18642 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018643 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018644 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018645 goto free_lstval;
18646 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018647 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018648 /* Need to make a copy, next get_tv_string_buf_chk() will
18649 * overwrite the string. */
18650 strval = vim_strsave(buf);
18651 if (strval == NULL)
18652 goto free_lstval;
18653 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018654 }
18655 *curval++ = strval;
18656 }
18657 *curval++ = NULL;
18658
18659 write_reg_contents_lst(regname, lstval, -1,
18660 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018661free_lstval:
18662 while (curallocval > allocval)
18663 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018664 vim_free(lstval);
18665 }
18666 else
18667 {
18668 strval = get_tv_string_chk(&argvars[1]);
18669 if (strval == NULL)
18670 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018671 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018674 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675}
18676
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018677/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018678 * "settabvar()" function
18679 */
18680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018681f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018682{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018683#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018684 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018685 tabpage_T *tp;
18686#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018687 char_u *varname, *tabvarname;
18688 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018689
18690 rettv->vval.v_number = 0;
18691
18692 if (check_restricted() || check_secure())
18693 return;
18694
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018695#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018696 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018697#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018698 varname = get_tv_string_chk(&argvars[1]);
18699 varp = &argvars[2];
18700
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018701 if (varname != NULL && varp != NULL
18702#ifdef FEAT_WINDOWS
18703 && tp != NULL
18704#endif
18705 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018706 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018707#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018708 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018709 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018710#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018711
18712 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18713 if (tabvarname != NULL)
18714 {
18715 STRCPY(tabvarname, "t:");
18716 STRCPY(tabvarname + 2, varname);
18717 set_var(tabvarname, varp, TRUE);
18718 vim_free(tabvarname);
18719 }
18720
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018721#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018722 /* Restore current tabpage */
18723 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018724 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018725#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018726 }
18727}
18728
18729/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018730 * "settabwinvar()" function
18731 */
18732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018734{
18735 setwinvar(argvars, rettv, 1);
18736}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737
18738/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018739 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018742f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018744 setwinvar(argvars, rettv, 0);
18745}
18746
18747/*
18748 * "setwinvar()" and "settabwinvar()" functions
18749 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018750
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018752setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018753{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 win_T *win;
18755#ifdef FEAT_WINDOWS
18756 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018757 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018758 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759#endif
18760 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018761 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018763 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764
18765 if (check_restricted() || check_secure())
18766 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018767
18768#ifdef FEAT_WINDOWS
18769 if (off == 1)
18770 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18771 else
18772 tp = curtab;
18773#endif
18774 win = find_win_by_nr(&argvars[off], tp);
18775 varname = get_tv_string_chk(&argvars[off + 1]);
18776 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777
18778 if (win != NULL && varname != NULL && varp != NULL)
18779 {
18780#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018781 need_switch_win = !(tp == curtab && win == curwin);
18782 if (!need_switch_win
18783 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018786 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018788 long numval;
18789 char_u *strval;
18790 int error = FALSE;
18791
18792 ++varname;
18793 numval = get_tv_number_chk(varp, &error);
18794 strval = get_tv_string_buf_chk(varp, nbuf);
18795 if (!error && strval != NULL)
18796 set_option_value(varname, numval, strval, OPT_LOCAL);
18797 }
18798 else
18799 {
18800 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18801 if (winvarname != NULL)
18802 {
18803 STRCPY(winvarname, "w:");
18804 STRCPY(winvarname + 2, varname);
18805 set_var(winvarname, varp, TRUE);
18806 vim_free(winvarname);
18807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 }
18809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018811 if (need_switch_win)
18812 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813#endif
18814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018815}
18816
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018817#ifdef FEAT_CRYPT
18818/*
18819 * "sha256({string})" function
18820 */
18821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018822f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018823{
18824 char_u *p;
18825
18826 p = get_tv_string(&argvars[0]);
18827 rettv->vval.v_string = vim_strsave(
18828 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18829 rettv->v_type = VAR_STRING;
18830}
18831#endif /* FEAT_CRYPT */
18832
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018834 * "shellescape({string})" function
18835 */
18836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018837f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018838{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018839 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018840 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018841 rettv->v_type = VAR_STRING;
18842}
18843
18844/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018845 * shiftwidth() function
18846 */
18847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018848f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018849{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018850 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018851}
18852
18853/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018854 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 */
18856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018857f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018859 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860
Bram Moolenaar0d660222005-01-07 21:51:51 +000018861 p = get_tv_string(&argvars[0]);
18862 rettv->vval.v_string = vim_strsave(p);
18863 simplify_filename(rettv->vval.v_string); /* simplify in place */
18864 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865}
18866
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018867#ifdef FEAT_FLOAT
18868/*
18869 * "sin()" function
18870 */
18871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018872f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018873{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018874 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018875
18876 rettv->v_type = VAR_FLOAT;
18877 if (get_float_arg(argvars, &f) == OK)
18878 rettv->vval.v_float = sin(f);
18879 else
18880 rettv->vval.v_float = 0.0;
18881}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018882
18883/*
18884 * "sinh()" function
18885 */
18886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018887f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018888{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018889 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018890
18891 rettv->v_type = VAR_FLOAT;
18892 if (get_float_arg(argvars, &f) == OK)
18893 rettv->vval.v_float = sinh(f);
18894 else
18895 rettv->vval.v_float = 0.0;
18896}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018897#endif
18898
Bram Moolenaar0d660222005-01-07 21:51:51 +000018899static int
18900#ifdef __BORLANDC__
18901 _RTLENTRYF
18902#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018903 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018904static int
18905#ifdef __BORLANDC__
18906 _RTLENTRYF
18907#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018908 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018910/* struct used in the array that's given to qsort() */
18911typedef struct
18912{
18913 listitem_T *item;
18914 int idx;
18915} sortItem_T;
18916
Bram Moolenaar0b962472016-02-22 22:51:33 +010018917/* struct storing information about current sort */
18918typedef struct
18919{
18920 int item_compare_ic;
18921 int item_compare_numeric;
18922 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018923#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018924 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018925#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018926 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018927 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018928 dict_T *item_compare_selfdict;
18929 int item_compare_func_err;
18930 int item_compare_keep_zero;
18931} sortinfo_T;
18932static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018933static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018934#define ITEM_COMPARE_FAIL 999
18935
Bram Moolenaar071d4272004-06-13 20:20:40 +000018936/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018937 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018939 static int
18940#ifdef __BORLANDC__
18941_RTLENTRYF
18942#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018943item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018945 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018946 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018947 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018948 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 int res;
18950 char_u numbuf1[NUMBUFLEN];
18951 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018953 si1 = (sortItem_T *)s1;
18954 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018955 tv1 = &si1->item->li_tv;
18956 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018957
Bram Moolenaar0b962472016-02-22 22:51:33 +010018958 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018959 {
18960 long v1 = get_tv_number(tv1);
18961 long v2 = get_tv_number(tv2);
18962
18963 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18964 }
18965
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018966#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018967 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018968 {
18969 float_T v1 = get_tv_float(tv1);
18970 float_T v2 = get_tv_float(tv2);
18971
18972 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18973 }
18974#endif
18975
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018976 /* tv2string() puts quotes around a string and allocates memory. Don't do
18977 * that for string variables. Use a single quote when comparing with a
18978 * non-string to do what the docs promise. */
18979 if (tv1->v_type == VAR_STRING)
18980 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018981 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018982 p1 = (char_u *)"'";
18983 else
18984 p1 = tv1->vval.v_string;
18985 }
18986 else
18987 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18988 if (tv2->v_type == VAR_STRING)
18989 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018990 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018991 p2 = (char_u *)"'";
18992 else
18993 p2 = tv2->vval.v_string;
18994 }
18995 else
18996 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018997 if (p1 == NULL)
18998 p1 = (char_u *)"";
18999 if (p2 == NULL)
19000 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019001 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019002 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019003 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019004 res = STRICMP(p1, p2);
19005 else
19006 res = STRCMP(p1, p2);
19007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019009 {
19010 double n1, n2;
19011 n1 = strtod((char *)p1, (char **)&p1);
19012 n2 = strtod((char *)p2, (char **)&p2);
19013 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19014 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019015
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019016 /* When the result would be zero, compare the item indexes. Makes the
19017 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019018 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019019 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019020
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021 vim_free(tofree1);
19022 vim_free(tofree2);
19023 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024}
19025
19026 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019027#ifdef __BORLANDC__
19028_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019030item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019032 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019033 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019034 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019035 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019036 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019037 char_u *func_name;
19038 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019040 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019042 return 0;
19043
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019044 si1 = (sortItem_T *)s1;
19045 si2 = (sortItem_T *)s2;
19046
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019047 if (partial == NULL)
19048 func_name = sortinfo->item_compare_func;
19049 else
19050 func_name = partial->pt_name;
19051
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019052 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019053 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019054 copy_tv(&si1->item->li_tv, &argv[0]);
19055 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056
19057 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019058 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019059 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019060 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019061 clear_tv(&argv[0]);
19062 clear_tv(&argv[1]);
19063
19064 if (res == FAIL)
19065 res = ITEM_COMPARE_FAIL;
19066 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019067 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19068 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019069 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019070 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019071
19072 /* When the result would be zero, compare the pointers themselves. Makes
19073 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019074 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019075 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019076
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 return res;
19078}
19079
19080/*
19081 * "sort({list})" function
19082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019084do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085{
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 list_T *l;
19087 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019088 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019089 sortinfo_T *old_sortinfo;
19090 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019091 long len;
19092 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 /* Pointer to current info struct used in compare function. Save and
19095 * restore the current one for nested calls. */
19096 old_sortinfo = sortinfo;
19097 sortinfo = &info;
19098
Bram Moolenaar0d660222005-01-07 21:51:51 +000019099 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019100 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 else
19102 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019103 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019104 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019105 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19106 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019107 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019108 rettv->vval.v_list = l;
19109 rettv->v_type = VAR_LIST;
19110 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 len = list_len(l);
19113 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 info.item_compare_ic = FALSE;
19117 info.item_compare_numeric = FALSE;
19118 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019119#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019121#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019122 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019123 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019125 if (argvars[1].v_type != VAR_UNKNOWN)
19126 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019127 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019129 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019130 else if (argvars[1].v_type == VAR_PARTIAL)
19131 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019132 else
19133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019134 int error = FALSE;
19135
19136 i = get_tv_number_chk(&argvars[1], &error);
19137 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019139 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019141 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019142 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019143 else if (i != 0)
19144 {
19145 EMSG(_(e_invarg));
19146 goto theend;
19147 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019149 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019150 if (*info.item_compare_func == NUL)
19151 {
19152 /* empty string means default sort */
19153 info.item_compare_func = NULL;
19154 }
19155 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019156 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019157 info.item_compare_func = NULL;
19158 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019159 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019160 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019161 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 info.item_compare_func = NULL;
19163 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019164 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019165#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019166 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019167 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019168 info.item_compare_func = NULL;
19169 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019170 }
19171#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019173 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 info.item_compare_func = NULL;
19175 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019176 }
19177 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019179
19180 if (argvars[2].v_type != VAR_UNKNOWN)
19181 {
19182 /* optional third argument: {dict} */
19183 if (argvars[2].v_type != VAR_DICT)
19184 {
19185 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019187 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019188 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019189 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191
Bram Moolenaar0d660222005-01-07 21:51:51 +000019192 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019193 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019195 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196
Bram Moolenaar327aa022014-03-25 18:24:23 +010019197 i = 0;
19198 if (sort)
19199 {
19200 /* sort(): ptrs will be the list to sort */
19201 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019202 {
19203 ptrs[i].item = li;
19204 ptrs[i].idx = i;
19205 ++i;
19206 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019207
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 info.item_compare_func_err = FALSE;
19209 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019210 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019211 if ((info.item_compare_func != NULL
19212 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019213 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019214 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019215 EMSG(_("E702: Sort compare function failed"));
19216 else
19217 {
19218 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019219 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019221 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019222 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019223
Bram Moolenaar0b962472016-02-22 22:51:33 +010019224 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019225 {
19226 /* Clear the List and append the items in sorted order. */
19227 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19228 l->lv_len = 0;
19229 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019230 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019231 }
19232 }
19233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019235 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019236 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019237
19238 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019239 info.item_compare_func_err = FALSE;
19240 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019241 item_compare_func_ptr = info.item_compare_func != NULL
19242 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019243 ? item_compare2 : item_compare;
19244
19245 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19246 li = li->li_next)
19247 {
19248 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19249 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019250 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019251 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019252 {
19253 EMSG(_("E882: Uniq compare function failed"));
19254 break;
19255 }
19256 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019257
Bram Moolenaar0b962472016-02-22 22:51:33 +010019258 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019259 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019260 while (--i >= 0)
19261 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019262 li = ptrs[i].item->li_next;
19263 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019264 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019265 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019266 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019267 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019268 list_fix_watch(l, li);
19269 listitem_free(li);
19270 l->lv_len--;
19271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019272 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019273 }
19274
19275 vim_free(ptrs);
19276 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019277theend:
19278 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279}
19280
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019281/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019282 * "sort({list})" function
19283 */
19284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019285f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019286{
19287 do_sort_uniq(argvars, rettv, TRUE);
19288}
19289
19290/*
19291 * "uniq({list})" function
19292 */
19293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019294f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019295{
19296 do_sort_uniq(argvars, rettv, FALSE);
19297}
19298
19299/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019300 * "soundfold({word})" function
19301 */
19302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019303f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019304{
19305 char_u *s;
19306
19307 rettv->v_type = VAR_STRING;
19308 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019309#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019310 rettv->vval.v_string = eval_soundfold(s);
19311#else
19312 rettv->vval.v_string = vim_strsave(s);
19313#endif
19314}
19315
19316/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019317 * "spellbadword()" function
19318 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019320f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019321{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019322 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019323 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019324 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019326 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019327 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019328
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019329#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019330 if (argvars[0].v_type == VAR_UNKNOWN)
19331 {
19332 /* Find the start and length of the badly spelled word. */
19333 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19334 if (len != 0)
19335 word = ml_get_cursor();
19336 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019337 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019338 {
19339 char_u *str = get_tv_string_chk(&argvars[0]);
19340 int capcol = -1;
19341
19342 if (str != NULL)
19343 {
19344 /* Check the argument for spelling. */
19345 while (*str != NUL)
19346 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019347 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019348 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019349 {
19350 word = str;
19351 break;
19352 }
19353 str += len;
19354 }
19355 }
19356 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019357#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019358
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019359 list_append_string(rettv->vval.v_list, word, len);
19360 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019361 attr == HLF_SPB ? "bad" :
19362 attr == HLF_SPR ? "rare" :
19363 attr == HLF_SPL ? "local" :
19364 attr == HLF_SPC ? "caps" :
19365 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019366}
19367
19368/*
19369 * "spellsuggest()" function
19370 */
19371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019372f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019373{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019374#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019375 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019376 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019377 int maxcount;
19378 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019379 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019380 listitem_T *li;
19381 int need_capital = FALSE;
19382#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019383
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019384 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019385 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019386
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019387#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019388 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019389 {
19390 str = get_tv_string(&argvars[0]);
19391 if (argvars[1].v_type != VAR_UNKNOWN)
19392 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019393 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019394 if (maxcount <= 0)
19395 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019396 if (argvars[2].v_type != VAR_UNKNOWN)
19397 {
19398 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19399 if (typeerr)
19400 return;
19401 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019402 }
19403 else
19404 maxcount = 25;
19405
Bram Moolenaar4770d092006-01-12 23:22:24 +000019406 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019407
19408 for (i = 0; i < ga.ga_len; ++i)
19409 {
19410 str = ((char_u **)ga.ga_data)[i];
19411
19412 li = listitem_alloc();
19413 if (li == NULL)
19414 vim_free(str);
19415 else
19416 {
19417 li->li_tv.v_type = VAR_STRING;
19418 li->li_tv.v_lock = 0;
19419 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019420 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019421 }
19422 }
19423 ga_clear(&ga);
19424 }
19425#endif
19426}
19427
Bram Moolenaar0d660222005-01-07 21:51:51 +000019428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019429f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019430{
19431 char_u *str;
19432 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019433 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434 regmatch_T regmatch;
19435 char_u patbuf[NUMBUFLEN];
19436 char_u *save_cpo;
19437 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019438 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019439 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019440 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019441
19442 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19443 save_cpo = p_cpo;
19444 p_cpo = (char_u *)"";
19445
19446 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019447 if (argvars[1].v_type != VAR_UNKNOWN)
19448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019449 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19450 if (pat == NULL)
19451 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019453 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019454 }
19455 if (pat == NULL || *pat == NUL)
19456 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019457
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019458 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019460 if (typeerr)
19461 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462
Bram Moolenaar0d660222005-01-07 21:51:51 +000019463 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19464 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019466 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019467 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019468 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019469 if (*str == NUL)
19470 match = FALSE; /* empty item at the end */
19471 else
19472 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019473 if (match)
19474 end = regmatch.startp[0];
19475 else
19476 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019477 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19478 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019479 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019480 if (list_append_string(rettv->vval.v_list, str,
19481 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019483 }
19484 if (!match)
19485 break;
19486 /* Advance to just after the match. */
19487 if (regmatch.endp[0] > str)
19488 col = 0;
19489 else
19490 {
19491 /* Don't get stuck at the same match. */
19492#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019493 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019494#else
19495 col = 1;
19496#endif
19497 }
19498 str = regmatch.endp[0];
19499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500
Bram Moolenaar473de612013-06-08 18:19:48 +020019501 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503
Bram Moolenaar0d660222005-01-07 21:51:51 +000019504 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505}
19506
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019507#ifdef FEAT_FLOAT
19508/*
19509 * "sqrt()" function
19510 */
19511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019512f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019513{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019514 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019515
19516 rettv->v_type = VAR_FLOAT;
19517 if (get_float_arg(argvars, &f) == OK)
19518 rettv->vval.v_float = sqrt(f);
19519 else
19520 rettv->vval.v_float = 0.0;
19521}
19522
19523/*
19524 * "str2float()" function
19525 */
19526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019527f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019528{
19529 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19530
19531 if (*p == '+')
19532 p = skipwhite(p + 1);
19533 (void)string2float(p, &rettv->vval.v_float);
19534 rettv->v_type = VAR_FLOAT;
19535}
19536#endif
19537
Bram Moolenaar2c932302006-03-18 21:42:09 +000019538/*
19539 * "str2nr()" function
19540 */
19541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019542f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019543{
19544 int base = 10;
19545 char_u *p;
19546 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019547 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019548
19549 if (argvars[1].v_type != VAR_UNKNOWN)
19550 {
19551 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019552 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019553 {
19554 EMSG(_(e_invarg));
19555 return;
19556 }
19557 }
19558
19559 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019560 if (*p == '+')
19561 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019562 switch (base)
19563 {
19564 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19565 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19566 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19567 default: what = 0;
19568 }
19569 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019570 rettv->vval.v_number = n;
19571}
19572
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573#ifdef HAVE_STRFTIME
19574/*
19575 * "strftime({format}[, {time}])" function
19576 */
19577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019578f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579{
19580 char_u result_buf[256];
19581 struct tm *curtime;
19582 time_t seconds;
19583 char_u *p;
19584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019585 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019587 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019588 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 seconds = time(NULL);
19590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019591 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 curtime = localtime(&seconds);
19593 /* MSVC returns NULL for an invalid value of seconds. */
19594 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 else
19597 {
19598# ifdef FEAT_MBYTE
19599 vimconv_T conv;
19600 char_u *enc;
19601
19602 conv.vc_type = CONV_NONE;
19603 enc = enc_locale();
19604 convert_setup(&conv, p_enc, enc);
19605 if (conv.vc_type != CONV_NONE)
19606 p = string_convert(&conv, p, NULL);
19607# endif
19608 if (p != NULL)
19609 (void)strftime((char *)result_buf, sizeof(result_buf),
19610 (char *)p, curtime);
19611 else
19612 result_buf[0] = NUL;
19613
19614# ifdef FEAT_MBYTE
19615 if (conv.vc_type != CONV_NONE)
19616 vim_free(p);
19617 convert_setup(&conv, enc, p_enc);
19618 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019619 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 else
19621# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019622 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623
19624# ifdef FEAT_MBYTE
19625 /* Release conversion descriptors */
19626 convert_setup(&conv, NULL, NULL);
19627 vim_free(enc);
19628# endif
19629 }
19630}
19631#endif
19632
19633/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019634 * "strgetchar()" function
19635 */
19636 static void
19637f_strgetchar(typval_T *argvars, typval_T *rettv)
19638{
19639 char_u *str;
19640 int len;
19641 int error = FALSE;
19642 int charidx;
19643
19644 rettv->vval.v_number = -1;
19645 str = get_tv_string_chk(&argvars[0]);
19646 if (str == NULL)
19647 return;
19648 len = (int)STRLEN(str);
19649 charidx = get_tv_number_chk(&argvars[1], &error);
19650 if (error)
19651 return;
19652#ifdef FEAT_MBYTE
19653 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019654 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019655
19656 while (charidx >= 0 && byteidx < len)
19657 {
19658 if (charidx == 0)
19659 {
19660 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19661 break;
19662 }
19663 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019664 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019665 }
19666 }
19667#else
19668 if (charidx < len)
19669 rettv->vval.v_number = str[charidx];
19670#endif
19671}
19672
19673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 * "stridx()" function
19675 */
19676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019677f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678{
19679 char_u buf[NUMBUFLEN];
19680 char_u *needle;
19681 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019686 needle = get_tv_string_chk(&argvars[1]);
19687 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019688 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019689 if (needle == NULL || haystack == NULL)
19690 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 if (argvars[2].v_type != VAR_UNKNOWN)
19693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019694 int error = FALSE;
19695
19696 start_idx = get_tv_number_chk(&argvars[2], &error);
19697 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019699 if (start_idx >= 0)
19700 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 }
19702
19703 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19704 if (pos != NULL)
19705 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706}
19707
19708/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709 * "string()" function
19710 */
19711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019712f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019713{
19714 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019715 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019716
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019718 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19719 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019720 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019721 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723}
19724
19725/*
19726 * "strlen()" function
19727 */
19728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019729f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019731 rettv->vval.v_number = (varnumber_T)(STRLEN(
19732 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733}
19734
19735/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019736 * "strchars()" function
19737 */
19738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019739f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019740{
19741 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019742 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019743#ifdef FEAT_MBYTE
19744 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019745 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019746#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019747
19748 if (argvars[1].v_type != VAR_UNKNOWN)
19749 skipcc = get_tv_number_chk(&argvars[1], NULL);
19750 if (skipcc < 0 || skipcc > 1)
19751 EMSG(_(e_invarg));
19752 else
19753 {
19754#ifdef FEAT_MBYTE
19755 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19756 while (*s != NUL)
19757 {
19758 func_mb_ptr2char_adv(&s);
19759 ++len;
19760 }
19761 rettv->vval.v_number = len;
19762#else
19763 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19764#endif
19765 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019766}
19767
19768/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019769 * "strdisplaywidth()" function
19770 */
19771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019772f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019773{
19774 char_u *s = get_tv_string(&argvars[0]);
19775 int col = 0;
19776
19777 if (argvars[1].v_type != VAR_UNKNOWN)
19778 col = get_tv_number(&argvars[1]);
19779
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019780 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019781}
19782
19783/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019784 * "strwidth()" function
19785 */
19786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019787f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019788{
19789 char_u *s = get_tv_string(&argvars[0]);
19790
19791 rettv->vval.v_number = (varnumber_T)(
19792#ifdef FEAT_MBYTE
19793 mb_string2cells(s, -1)
19794#else
19795 STRLEN(s)
19796#endif
19797 );
19798}
19799
19800/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019801 * "strcharpart()" function
19802 */
19803 static void
19804f_strcharpart(typval_T *argvars, typval_T *rettv)
19805{
19806#ifdef FEAT_MBYTE
19807 char_u *p;
19808 int nchar;
19809 int nbyte = 0;
19810 int charlen;
19811 int len = 0;
19812 int slen;
19813 int error = FALSE;
19814
19815 p = get_tv_string(&argvars[0]);
19816 slen = (int)STRLEN(p);
19817
19818 nchar = get_tv_number_chk(&argvars[1], &error);
19819 if (!error)
19820 {
19821 if (nchar > 0)
19822 while (nchar > 0 && nbyte < slen)
19823 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019824 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019825 --nchar;
19826 }
19827 else
19828 nbyte = nchar;
19829 if (argvars[2].v_type != VAR_UNKNOWN)
19830 {
19831 charlen = get_tv_number(&argvars[2]);
19832 while (charlen > 0 && nbyte + len < slen)
19833 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019834 int off = nbyte + len;
19835
19836 if (off < 0)
19837 len += 1;
19838 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019839 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019840 --charlen;
19841 }
19842 }
19843 else
19844 len = slen - nbyte; /* default: all bytes that are available. */
19845 }
19846
19847 /*
19848 * Only return the overlap between the specified part and the actual
19849 * string.
19850 */
19851 if (nbyte < 0)
19852 {
19853 len += nbyte;
19854 nbyte = 0;
19855 }
19856 else if (nbyte > slen)
19857 nbyte = slen;
19858 if (len < 0)
19859 len = 0;
19860 else if (nbyte + len > slen)
19861 len = slen - nbyte;
19862
19863 rettv->v_type = VAR_STRING;
19864 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19865#else
19866 f_strpart(argvars, rettv);
19867#endif
19868}
19869
19870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 * "strpart()" function
19872 */
19873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019874f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875{
19876 char_u *p;
19877 int n;
19878 int len;
19879 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019882 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 slen = (int)STRLEN(p);
19884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019885 n = get_tv_number_chk(&argvars[1], &error);
19886 if (error)
19887 len = 0;
19888 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019889 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 else
19891 len = slen - n; /* default len: all bytes that are available. */
19892
19893 /*
19894 * Only return the overlap between the specified part and the actual
19895 * string.
19896 */
19897 if (n < 0)
19898 {
19899 len += n;
19900 n = 0;
19901 }
19902 else if (n > slen)
19903 n = slen;
19904 if (len < 0)
19905 len = 0;
19906 else if (n + len > slen)
19907 len = slen - n;
19908
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019909 rettv->v_type = VAR_STRING;
19910 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911}
19912
19913/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019914 * "strridx()" function
19915 */
19916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019917f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019918{
19919 char_u buf[NUMBUFLEN];
19920 char_u *needle;
19921 char_u *haystack;
19922 char_u *rest;
19923 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019924 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019926 needle = get_tv_string_chk(&argvars[1]);
19927 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019928
19929 rettv->vval.v_number = -1;
19930 if (needle == NULL || haystack == NULL)
19931 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019932
19933 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019934 if (argvars[2].v_type != VAR_UNKNOWN)
19935 {
19936 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019937 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019938 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019939 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019940 }
19941 else
19942 end_idx = haystack_len;
19943
Bram Moolenaar0d660222005-01-07 21:51:51 +000019944 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019945 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019946 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019947 lastmatch = haystack + end_idx;
19948 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019949 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019950 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019951 for (rest = haystack; *rest != '\0'; ++rest)
19952 {
19953 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019954 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019955 break;
19956 lastmatch = rest;
19957 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019959
19960 if (lastmatch == NULL)
19961 rettv->vval.v_number = -1;
19962 else
19963 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19964}
19965
19966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 * "strtrans()" function
19968 */
19969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019970f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972 rettv->v_type = VAR_STRING;
19973 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974}
19975
19976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019977 * "submatch()" function
19978 */
19979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019980f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019981{
Bram Moolenaar41571762014-04-02 19:00:58 +020019982 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019983 int no;
19984 int retList = 0;
19985
19986 no = (int)get_tv_number_chk(&argvars[0], &error);
19987 if (error)
19988 return;
19989 error = FALSE;
19990 if (argvars[1].v_type != VAR_UNKNOWN)
19991 retList = get_tv_number_chk(&argvars[1], &error);
19992 if (error)
19993 return;
19994
19995 if (retList == 0)
19996 {
19997 rettv->v_type = VAR_STRING;
19998 rettv->vval.v_string = reg_submatch(no);
19999 }
20000 else
20001 {
20002 rettv->v_type = VAR_LIST;
20003 rettv->vval.v_list = reg_submatch_list(no);
20004 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020005}
20006
20007/*
20008 * "substitute()" function
20009 */
20010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020011f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020012{
20013 char_u patbuf[NUMBUFLEN];
20014 char_u subbuf[NUMBUFLEN];
20015 char_u flagsbuf[NUMBUFLEN];
20016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020017 char_u *str = get_tv_string_chk(&argvars[0]);
20018 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20019 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20020 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20021
Bram Moolenaar0d660222005-01-07 21:51:51 +000020022 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020023 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20024 rettv->vval.v_string = NULL;
20025 else
20026 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020027}
20028
20029/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020030 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020033f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034{
20035 int id = 0;
20036#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020037 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038 long col;
20039 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020040 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020042 lnum = get_tv_lnum(argvars); /* -1 on type error */
20043 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20044 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020046 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020047 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020048 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049#endif
20050
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052}
20053
20054/*
20055 * "synIDattr(id, what [, mode])" function
20056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020058f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059{
20060 char_u *p = NULL;
20061#ifdef FEAT_SYN_HL
20062 int id;
20063 char_u *what;
20064 char_u *mode;
20065 char_u modebuf[NUMBUFLEN];
20066 int modec;
20067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 id = get_tv_number(&argvars[0]);
20069 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020070 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020072 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020074 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 modec = 0; /* replace invalid with current */
20076 }
20077 else
20078 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020079#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020080 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 modec = 'g';
20082 else
20083#endif
20084 if (t_colors > 1)
20085 modec = 'c';
20086 else
20087 modec = 't';
20088 }
20089
20090
20091 switch (TOLOWER_ASC(what[0]))
20092 {
20093 case 'b':
20094 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20095 p = highlight_color(id, what, modec);
20096 else /* bold */
20097 p = highlight_has_attr(id, HL_BOLD, modec);
20098 break;
20099
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020100 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 p = highlight_color(id, what, modec);
20102 break;
20103
20104 case 'i':
20105 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20106 p = highlight_has_attr(id, HL_INVERSE, modec);
20107 else /* italic */
20108 p = highlight_has_attr(id, HL_ITALIC, modec);
20109 break;
20110
20111 case 'n': /* name */
20112 p = get_highlight_name(NULL, id - 1);
20113 break;
20114
20115 case 'r': /* reverse */
20116 p = highlight_has_attr(id, HL_INVERSE, modec);
20117 break;
20118
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020119 case 's':
20120 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20121 p = highlight_color(id, what, modec);
20122 else /* standout */
20123 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 break;
20125
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020126 case 'u':
20127 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20128 /* underline */
20129 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20130 else
20131 /* undercurl */
20132 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 break;
20134 }
20135
20136 if (p != NULL)
20137 p = vim_strsave(p);
20138#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139 rettv->v_type = VAR_STRING;
20140 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141}
20142
20143/*
20144 * "synIDtrans(id)" function
20145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020147f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148{
20149 int id;
20150
20151#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020152 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153
20154 if (id > 0)
20155 id = syn_get_final_id(id);
20156 else
20157#endif
20158 id = 0;
20159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020160 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161}
20162
20163/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020164 * "synconcealed(lnum, col)" function
20165 */
20166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020167f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020168{
20169#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20170 long lnum;
20171 long col;
20172 int syntax_flags = 0;
20173 int cchar;
20174 int matchid = 0;
20175 char_u str[NUMBUFLEN];
20176#endif
20177
20178 rettv->v_type = VAR_LIST;
20179 rettv->vval.v_list = NULL;
20180
20181#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20182 lnum = get_tv_lnum(argvars); /* -1 on type error */
20183 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20184
20185 vim_memset(str, NUL, sizeof(str));
20186
20187 if (rettv_list_alloc(rettv) != FAIL)
20188 {
20189 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20190 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20191 && curwin->w_p_cole > 0)
20192 {
20193 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20194 syntax_flags = get_syntax_info(&matchid);
20195
20196 /* get the conceal character */
20197 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20198 {
20199 cchar = syn_get_sub_char();
20200 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20201 cchar = lcs_conceal;
20202 if (cchar != NUL)
20203 {
20204# ifdef FEAT_MBYTE
20205 if (has_mbyte)
20206 (*mb_char2bytes)(cchar, str);
20207 else
20208# endif
20209 str[0] = cchar;
20210 }
20211 }
20212 }
20213
20214 list_append_number(rettv->vval.v_list,
20215 (syntax_flags & HL_CONCEAL) != 0);
20216 /* -1 to auto-determine strlen */
20217 list_append_string(rettv->vval.v_list, str, -1);
20218 list_append_number(rettv->vval.v_list, matchid);
20219 }
20220#endif
20221}
20222
20223/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020224 * "synstack(lnum, col)" function
20225 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020227f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020228{
20229#ifdef FEAT_SYN_HL
20230 long lnum;
20231 long col;
20232 int i;
20233 int id;
20234#endif
20235
20236 rettv->v_type = VAR_LIST;
20237 rettv->vval.v_list = NULL;
20238
20239#ifdef FEAT_SYN_HL
20240 lnum = get_tv_lnum(argvars); /* -1 on type error */
20241 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20242
20243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020244 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020245 && rettv_list_alloc(rettv) != FAIL)
20246 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020247 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020248 for (i = 0; ; ++i)
20249 {
20250 id = syn_get_stack_item(i);
20251 if (id < 0)
20252 break;
20253 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20254 break;
20255 }
20256 }
20257#endif
20258}
20259
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020261get_cmd_output_as_rettv(
20262 typval_T *argvars,
20263 typval_T *rettv,
20264 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020266 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020268 char_u *infile = NULL;
20269 char_u buf[NUMBUFLEN];
20270 int err = FALSE;
20271 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020272 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020273 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020275 rettv->v_type = VAR_STRING;
20276 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020277 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020278 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020279
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020280 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020281 {
20282 /*
20283 * Write the string to a temp file, to be used for input of the shell
20284 * command.
20285 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020286 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020287 {
20288 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020289 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020290 }
20291
20292 fd = mch_fopen((char *)infile, WRITEBIN);
20293 if (fd == NULL)
20294 {
20295 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020296 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020297 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020298 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020299 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020300 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20301 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020302 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020303 else
20304 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020305 size_t len;
20306
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020307 p = get_tv_string_buf_chk(&argvars[1], buf);
20308 if (p == NULL)
20309 {
20310 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020311 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020312 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020313 len = STRLEN(p);
20314 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020315 err = TRUE;
20316 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020317 if (fclose(fd) != 0)
20318 err = TRUE;
20319 if (err)
20320 {
20321 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020322 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020323 }
20324 }
20325
Bram Moolenaar52a72462014-08-29 15:53:52 +020020326 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20327 * echoes typeahead, that messes up the display. */
20328 if (!msg_silent)
20329 flags += SHELL_COOKED;
20330
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020331 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020333 int len;
20334 listitem_T *li;
20335 char_u *s = NULL;
20336 char_u *start;
20337 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020338 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339
Bram Moolenaar52a72462014-08-29 15:53:52 +020020340 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020341 if (res == NULL)
20342 goto errret;
20343
20344 list = list_alloc();
20345 if (list == NULL)
20346 goto errret;
20347
20348 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020350 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020351 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020352 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020353 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020354
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020355 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020356 if (s == NULL)
20357 goto errret;
20358
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020359 for (p = s; start < end; ++p, ++start)
20360 *p = *start == NUL ? NL : *start;
20361 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020362
20363 li = listitem_alloc();
20364 if (li == NULL)
20365 {
20366 vim_free(s);
20367 goto errret;
20368 }
20369 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020370 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020371 li->li_tv.vval.v_string = s;
20372 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020374
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020375 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020376 rettv->v_type = VAR_LIST;
20377 rettv->vval.v_list = list;
20378 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020380 else
20381 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020382 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020383#ifdef USE_CR
20384 /* translate <CR> into <NL> */
20385 if (res != NULL)
20386 {
20387 char_u *s;
20388
20389 for (s = res; *s; ++s)
20390 {
20391 if (*s == CAR)
20392 *s = NL;
20393 }
20394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395#else
20396# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020397 /* translate <CR><NL> into <NL> */
20398 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020400 char_u *s, *d;
20401
20402 d = res;
20403 for (s = res; *s; ++s)
20404 {
20405 if (s[0] == CAR && s[1] == NL)
20406 ++s;
20407 *d++ = *s;
20408 }
20409 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411# endif
20412#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020413 rettv->vval.v_string = res;
20414 res = NULL;
20415 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020416
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020417errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020418 if (infile != NULL)
20419 {
20420 mch_remove(infile);
20421 vim_free(infile);
20422 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020423 if (res != NULL)
20424 vim_free(res);
20425 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020426 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020427}
20428
20429/*
20430 * "system()" function
20431 */
20432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020433f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020434{
20435 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20436}
20437
20438/*
20439 * "systemlist()" function
20440 */
20441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020442f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020443{
20444 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445}
20446
20447/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020448 * "tabpagebuflist()" function
20449 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020452{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020453#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020454 tabpage_T *tp;
20455 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020456
20457 if (argvars[0].v_type == VAR_UNKNOWN)
20458 wp = firstwin;
20459 else
20460 {
20461 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20462 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020463 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020464 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020465 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020466 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020467 for (; wp != NULL; wp = wp->w_next)
20468 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020469 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020470 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020471 }
20472#endif
20473}
20474
20475
20476/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020477 * "tabpagenr()" function
20478 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020480f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020481{
20482 int nr = 1;
20483#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020484 char_u *arg;
20485
20486 if (argvars[0].v_type != VAR_UNKNOWN)
20487 {
20488 arg = get_tv_string_chk(&argvars[0]);
20489 nr = 0;
20490 if (arg != NULL)
20491 {
20492 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020493 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020494 else
20495 EMSG2(_(e_invexpr2), arg);
20496 }
20497 }
20498 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020499 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020500#endif
20501 rettv->vval.v_number = nr;
20502}
20503
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020504
20505#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020506static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020507
20508/*
20509 * Common code for tabpagewinnr() and winnr().
20510 */
20511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020512get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020513{
20514 win_T *twin;
20515 int nr = 1;
20516 win_T *wp;
20517 char_u *arg;
20518
20519 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20520 if (argvar->v_type != VAR_UNKNOWN)
20521 {
20522 arg = get_tv_string_chk(argvar);
20523 if (arg == NULL)
20524 nr = 0; /* type error; errmsg already given */
20525 else if (STRCMP(arg, "$") == 0)
20526 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20527 else if (STRCMP(arg, "#") == 0)
20528 {
20529 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20530 if (twin == NULL)
20531 nr = 0;
20532 }
20533 else
20534 {
20535 EMSG2(_(e_invexpr2), arg);
20536 nr = 0;
20537 }
20538 }
20539
20540 if (nr > 0)
20541 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20542 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020543 {
20544 if (wp == NULL)
20545 {
20546 /* didn't find it in this tabpage */
20547 nr = 0;
20548 break;
20549 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020550 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020551 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020552 return nr;
20553}
20554#endif
20555
20556/*
20557 * "tabpagewinnr()" function
20558 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020560f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020561{
20562 int nr = 1;
20563#ifdef FEAT_WINDOWS
20564 tabpage_T *tp;
20565
20566 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20567 if (tp == NULL)
20568 nr = 0;
20569 else
20570 nr = get_winnr(tp, &argvars[1]);
20571#endif
20572 rettv->vval.v_number = nr;
20573}
20574
20575
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020576/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020577 * "tagfiles()" function
20578 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020580f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020581{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020582 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020583 tagname_T tn;
20584 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020585
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020586 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020587 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020588 fname = alloc(MAXPATHL);
20589 if (fname == NULL)
20590 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020591
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020592 for (first = TRUE; ; first = FALSE)
20593 if (get_tagfname(&tn, first, fname) == FAIL
20594 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020595 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020596 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020597 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020598}
20599
20600/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020601 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020602 */
20603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020605{
20606 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020607
20608 tag_pattern = get_tv_string(&argvars[0]);
20609
20610 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020611 if (*tag_pattern == NUL)
20612 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020613
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020614 if (rettv_list_alloc(rettv) == OK)
20615 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020616}
20617
20618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 * "tempname()" function
20620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020622f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623{
20624 static int x = 'A';
20625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020626 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020627 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628
20629 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20630 * names. Skip 'I' and 'O', they are used for shell redirection. */
20631 do
20632 {
20633 if (x == 'Z')
20634 x = '0';
20635 else if (x == '9')
20636 x = 'A';
20637 else
20638 {
20639#ifdef EBCDIC
20640 if (x == 'I')
20641 x = 'J';
20642 else if (x == 'R')
20643 x = 'S';
20644 else
20645#endif
20646 ++x;
20647 }
20648 } while (x == 'I' || x == 'O');
20649}
20650
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020651#ifdef FEAT_FLOAT
20652/*
20653 * "tan()" function
20654 */
20655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020656f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020657{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020658 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020659
20660 rettv->v_type = VAR_FLOAT;
20661 if (get_float_arg(argvars, &f) == OK)
20662 rettv->vval.v_float = tan(f);
20663 else
20664 rettv->vval.v_float = 0.0;
20665}
20666
20667/*
20668 * "tanh()" function
20669 */
20670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020671f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020672{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020673 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020674
20675 rettv->v_type = VAR_FLOAT;
20676 if (get_float_arg(argvars, &f) == OK)
20677 rettv->vval.v_float = tanh(f);
20678 else
20679 rettv->vval.v_float = 0.0;
20680}
20681#endif
20682
Bram Moolenaar574860b2016-05-24 17:33:34 +020020683/*
20684 * "test_garbagecollect_now()" function
20685 */
20686 static void
20687f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20688{
20689 /* This is dangerous, any Lists and Dicts used internally may be freed
20690 * while still in use. */
20691 garbage_collect(TRUE);
20692}
20693
20694#ifdef FEAT_JOB_CHANNEL
20695 static void
20696f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20697{
20698 rettv->v_type = VAR_CHANNEL;
20699 rettv->vval.v_channel = NULL;
20700}
20701#endif
20702
20703 static void
20704f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20705{
20706 rettv->v_type = VAR_DICT;
20707 rettv->vval.v_dict = NULL;
20708}
20709
20710#ifdef FEAT_JOB_CHANNEL
20711 static void
20712f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20713{
20714 rettv->v_type = VAR_JOB;
20715 rettv->vval.v_job = NULL;
20716}
20717#endif
20718
20719 static void
20720f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20721{
20722 rettv->v_type = VAR_LIST;
20723 rettv->vval.v_list = NULL;
20724}
20725
20726 static void
20727f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20728{
20729 rettv->v_type = VAR_PARTIAL;
20730 rettv->vval.v_partial = NULL;
20731}
20732
20733 static void
20734f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20735{
20736 rettv->v_type = VAR_STRING;
20737 rettv->vval.v_string = NULL;
20738}
20739
Bram Moolenaar975b5272016-03-15 23:10:59 +010020740#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20741/*
20742 * Get a callback from "arg". It can be a Funcref or a function name.
20743 * When "arg" is zero return an empty string.
20744 * Return NULL for an invalid argument.
20745 */
20746 char_u *
20747get_callback(typval_T *arg, partial_T **pp)
20748{
20749 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20750 {
20751 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020752 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020753 return (*pp)->pt_name;
20754 }
20755 *pp = NULL;
20756 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20757 return arg->vval.v_string;
20758 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20759 return (char_u *)"";
20760 EMSG(_("E921: Invalid callback argument"));
20761 return NULL;
20762}
20763#endif
20764
20765#ifdef FEAT_TIMERS
20766/*
20767 * "timer_start(time, callback [, options])" function
20768 */
20769 static void
20770f_timer_start(typval_T *argvars, typval_T *rettv)
20771{
20772 long msec = get_tv_number(&argvars[0]);
20773 timer_T *timer;
20774 int repeat = 0;
20775 char_u *callback;
20776 dict_T *dict;
20777
Bram Moolenaar38499922016-04-22 20:46:52 +020020778 if (check_secure())
20779 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020780 if (argvars[2].v_type != VAR_UNKNOWN)
20781 {
20782 if (argvars[2].v_type != VAR_DICT
20783 || (dict = argvars[2].vval.v_dict) == NULL)
20784 {
20785 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20786 return;
20787 }
20788 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20789 repeat = get_dict_number(dict, (char_u *)"repeat");
20790 }
20791
20792 timer = create_timer(msec, repeat);
20793 callback = get_callback(&argvars[1], &timer->tr_partial);
20794 if (callback == NULL)
20795 {
20796 stop_timer(timer);
20797 rettv->vval.v_number = -1;
20798 }
20799 else
20800 {
20801 timer->tr_callback = vim_strsave(callback);
20802 rettv->vval.v_number = timer->tr_id;
20803 }
20804}
20805
20806/*
20807 * "timer_stop(timer)" function
20808 */
20809 static void
20810f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20811{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020812 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020813
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020814 if (argvars[0].v_type != VAR_NUMBER)
20815 {
20816 EMSG(_(e_number_exp));
20817 return;
20818 }
20819 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020820 if (timer != NULL)
20821 stop_timer(timer);
20822}
20823#endif
20824
Bram Moolenaard52d9742005-08-21 22:20:28 +000020825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020826 * "tolower(string)" function
20827 */
20828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020829f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020830{
20831 char_u *p;
20832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020833 p = vim_strsave(get_tv_string(&argvars[0]));
20834 rettv->v_type = VAR_STRING;
20835 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836
20837 if (p != NULL)
20838 while (*p != NUL)
20839 {
20840#ifdef FEAT_MBYTE
20841 int l;
20842
20843 if (enc_utf8)
20844 {
20845 int c, lc;
20846
20847 c = utf_ptr2char(p);
20848 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020849 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 /* TODO: reallocate string when byte count changes. */
20851 if (utf_char2len(lc) == l)
20852 utf_char2bytes(lc, p);
20853 p += l;
20854 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020855 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 p += l; /* skip multi-byte character */
20857 else
20858#endif
20859 {
20860 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20861 ++p;
20862 }
20863 }
20864}
20865
20866/*
20867 * "toupper(string)" function
20868 */
20869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020870f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020872 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020873 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874}
20875
20876/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020877 * "tr(string, fromstr, tostr)" function
20878 */
20879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020880f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020881{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020882 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020883 char_u *fromstr;
20884 char_u *tostr;
20885 char_u *p;
20886#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020887 int inlen;
20888 int fromlen;
20889 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020890 int idx;
20891 char_u *cpstr;
20892 int cplen;
20893 int first = TRUE;
20894#endif
20895 char_u buf[NUMBUFLEN];
20896 char_u buf2[NUMBUFLEN];
20897 garray_T ga;
20898
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020899 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020900 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20901 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020902
20903 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020904 rettv->v_type = VAR_STRING;
20905 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020906 if (fromstr == NULL || tostr == NULL)
20907 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020908 ga_init2(&ga, (int)sizeof(char), 80);
20909
20910#ifdef FEAT_MBYTE
20911 if (!has_mbyte)
20912#endif
20913 /* not multi-byte: fromstr and tostr must be the same length */
20914 if (STRLEN(fromstr) != STRLEN(tostr))
20915 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020916#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020917error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020918#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020919 EMSG2(_(e_invarg2), fromstr);
20920 ga_clear(&ga);
20921 return;
20922 }
20923
20924 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020925 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020926 {
20927#ifdef FEAT_MBYTE
20928 if (has_mbyte)
20929 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020930 inlen = (*mb_ptr2len)(in_str);
20931 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020932 cplen = inlen;
20933 idx = 0;
20934 for (p = fromstr; *p != NUL; p += fromlen)
20935 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020936 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020937 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020938 {
20939 for (p = tostr; *p != NUL; p += tolen)
20940 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020941 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020942 if (idx-- == 0)
20943 {
20944 cplen = tolen;
20945 cpstr = p;
20946 break;
20947 }
20948 }
20949 if (*p == NUL) /* tostr is shorter than fromstr */
20950 goto error;
20951 break;
20952 }
20953 ++idx;
20954 }
20955
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020956 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020957 {
20958 /* Check that fromstr and tostr have the same number of
20959 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020960 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020961 first = FALSE;
20962 for (p = tostr; *p != NUL; p += tolen)
20963 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020964 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020965 --idx;
20966 }
20967 if (idx != 0)
20968 goto error;
20969 }
20970
Bram Moolenaarcde88542015-08-11 19:14:00 +020020971 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020972 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020973 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020974
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020975 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020976 }
20977 else
20978#endif
20979 {
20980 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020981 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020982 if (p != NULL)
20983 ga_append(&ga, tostr[p - fromstr]);
20984 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020985 ga_append(&ga, *in_str);
20986 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020987 }
20988 }
20989
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020990 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020991 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020992 ga_append(&ga, NUL);
20993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020995}
20996
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020997#ifdef FEAT_FLOAT
20998/*
20999 * "trunc({float})" function
21000 */
21001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021002f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021003{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021004 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021005
21006 rettv->v_type = VAR_FLOAT;
21007 if (get_float_arg(argvars, &f) == OK)
21008 /* trunc() is not in C90, use floor() or ceil() instead. */
21009 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21010 else
21011 rettv->vval.v_float = 0.0;
21012}
21013#endif
21014
Bram Moolenaar8299df92004-07-10 09:47:34 +000021015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 * "type(expr)" function
21017 */
21018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021019f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021021 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021022
21023 switch (argvars[0].v_type)
21024 {
21025 case VAR_NUMBER: n = 0; break;
21026 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021027 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021028 case VAR_FUNC: n = 2; break;
21029 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021030 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021031 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021032 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021033 if (argvars[0].vval.v_number == VVAL_FALSE
21034 || argvars[0].vval.v_number == VVAL_TRUE)
21035 n = 6;
21036 else
21037 n = 7;
21038 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021039 case VAR_JOB: n = 8; break;
21040 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021041 case VAR_UNKNOWN:
21042 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21043 n = -1;
21044 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021045 }
21046 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047}
21048
21049/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021050 * "undofile(name)" function
21051 */
21052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021053f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021054{
21055 rettv->v_type = VAR_STRING;
21056#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021057 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021058 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021059
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021060 if (*fname == NUL)
21061 {
21062 /* If there is no file name there will be no undo file. */
21063 rettv->vval.v_string = NULL;
21064 }
21065 else
21066 {
21067 char_u *ffname = FullName_save(fname, FALSE);
21068
21069 if (ffname != NULL)
21070 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21071 vim_free(ffname);
21072 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021073 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021074#else
21075 rettv->vval.v_string = NULL;
21076#endif
21077}
21078
21079/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021080 * "undotree()" function
21081 */
21082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021083f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021084{
21085 if (rettv_dict_alloc(rettv) == OK)
21086 {
21087 dict_T *dict = rettv->vval.v_dict;
21088 list_T *list;
21089
Bram Moolenaar730cde92010-06-27 05:18:54 +020021090 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021091 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021092 dict_add_nr_str(dict, "save_last",
21093 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021094 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21095 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021096 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021097
21098 list = list_alloc();
21099 if (list != NULL)
21100 {
21101 u_eval_tree(curbuf->b_u_oldhead, list);
21102 dict_add_list(dict, "entries", list);
21103 }
21104 }
21105}
21106
21107/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021108 * "values(dict)" function
21109 */
21110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021111f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021112{
21113 dict_list(argvars, rettv, 1);
21114}
21115
21116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117 * "virtcol(string)" function
21118 */
21119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021120f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121{
21122 colnr_T vcol = 0;
21123 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021124 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021126 fp = var2fpos(&argvars[0], FALSE, &fnum);
21127 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21128 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 {
21130 getvvcol(curwin, fp, NULL, NULL, &vcol);
21131 ++vcol;
21132 }
21133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021134 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135}
21136
21137/*
21138 * "visualmode()" function
21139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021141f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 char_u str[2];
21144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021145 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 str[0] = curbuf->b_visual_mode_eval;
21147 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021148 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149
21150 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021151 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153}
21154
21155/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021156 * "wildmenumode()" function
21157 */
21158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021159f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021160{
21161#ifdef FEAT_WILDMENU
21162 if (wild_menu_showing)
21163 rettv->vval.v_number = 1;
21164#endif
21165}
21166
21167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 * "winbufnr(nr)" function
21169 */
21170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021171f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172{
21173 win_T *wp;
21174
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021175 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021179 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180}
21181
21182/*
21183 * "wincol()" function
21184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021186f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187{
21188 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190}
21191
21192/*
21193 * "winheight(nr)" function
21194 */
21195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021196f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197{
21198 win_T *wp;
21199
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021200 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021202 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021204 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205}
21206
21207/*
21208 * "winline()" function
21209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021211f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212{
21213 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215}
21216
21217/*
21218 * "winnr()" function
21219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222{
21223 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021224
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021226 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021228 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229}
21230
21231/*
21232 * "winrestcmd()" function
21233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021235f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236{
21237#ifdef FEAT_WINDOWS
21238 win_T *wp;
21239 int winnr = 1;
21240 garray_T ga;
21241 char_u buf[50];
21242
21243 ga_init2(&ga, (int)sizeof(char), 70);
21244 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21245 {
21246 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21247 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21249 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 ++winnr;
21251 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021252 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021256 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021258 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259}
21260
21261/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021262 * "winrestview()" function
21263 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021265f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021266{
21267 dict_T *dict;
21268
21269 if (argvars[0].v_type != VAR_DICT
21270 || (dict = argvars[0].vval.v_dict) == NULL)
21271 EMSG(_(e_invarg));
21272 else
21273 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021274 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21275 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21276 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21277 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021278#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021279 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21280 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021281#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021282 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21283 {
21284 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21285 curwin->w_set_curswant = FALSE;
21286 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021287
Bram Moolenaar82c25852014-05-28 16:47:16 +020021288 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21289 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021290#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021291 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21292 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021293#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021294 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21295 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21296 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21297 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021298
21299 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021300 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021301# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021302 win_new_width(curwin, W_WIDTH(curwin));
21303# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021304 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021305
Bram Moolenaarb851a962014-10-31 15:45:52 +010021306 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021307 curwin->w_topline = 1;
21308 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21309 curwin->w_topline = curbuf->b_ml.ml_line_count;
21310#ifdef FEAT_DIFF
21311 check_topfill(curwin, TRUE);
21312#endif
21313 }
21314}
21315
21316/*
21317 * "winsaveview()" function
21318 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021320f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021321{
21322 dict_T *dict;
21323
Bram Moolenaara800b422010-06-27 01:15:55 +020021324 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021325 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021326 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021327
21328 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21329 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21330#ifdef FEAT_VIRTUALEDIT
21331 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21332#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021333 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021334 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21335
21336 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21337#ifdef FEAT_DIFF
21338 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21339#endif
21340 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21341 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21342}
21343
21344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 * "winwidth(nr)" function
21346 */
21347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021348f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349{
21350 win_T *wp;
21351
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021352 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021356#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021357 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021359 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360#endif
21361}
21362
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021364 * "wordcount()" function
21365 */
21366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021368{
21369 if (rettv_dict_alloc(rettv) == FAIL)
21370 return;
21371 cursor_pos_info(rettv->vval.v_dict);
21372}
21373
21374/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021375 * Write list of strings to file
21376 */
21377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021378write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021379{
21380 listitem_T *li;
21381 int c;
21382 int ret = OK;
21383 char_u *s;
21384
21385 for (li = list->lv_first; li != NULL; li = li->li_next)
21386 {
21387 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21388 {
21389 if (*s == '\n')
21390 c = putc(NUL, fd);
21391 else
21392 c = putc(*s, fd);
21393 if (c == EOF)
21394 {
21395 ret = FAIL;
21396 break;
21397 }
21398 }
21399 if (!binary || li->li_next != NULL)
21400 if (putc('\n', fd) == EOF)
21401 {
21402 ret = FAIL;
21403 break;
21404 }
21405 if (ret == FAIL)
21406 {
21407 EMSG(_(e_write));
21408 break;
21409 }
21410 }
21411 return ret;
21412}
21413
21414/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021415 * "writefile()" function
21416 */
21417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021418f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021419{
21420 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021421 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021422 char_u *fname;
21423 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021424 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021425
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021426 if (check_restricted() || check_secure())
21427 return;
21428
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021429 if (argvars[0].v_type != VAR_LIST)
21430 {
21431 EMSG2(_(e_listarg), "writefile()");
21432 return;
21433 }
21434 if (argvars[0].vval.v_list == NULL)
21435 return;
21436
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021437 if (argvars[2].v_type != VAR_UNKNOWN)
21438 {
21439 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21440 binary = TRUE;
21441 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21442 append = TRUE;
21443 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021444
21445 /* Always open the file in binary mode, library functions have a mind of
21446 * their own about CR-LF conversion. */
21447 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021448 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21449 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021450 {
21451 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21452 ret = -1;
21453 }
21454 else
21455 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021456 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21457 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021458 fclose(fd);
21459 }
21460
21461 rettv->vval.v_number = ret;
21462}
21463
21464/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021465 * "xor(expr, expr)" function
21466 */
21467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021468f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021469{
21470 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21471 ^ get_tv_number_chk(&argvars[1], NULL);
21472}
21473
21474
21475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021477 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 */
21479 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021480var2fpos(
21481 typval_T *varp,
21482 int dollar_lnum, /* TRUE when $ is last line */
21483 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021485 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021487 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488
Bram Moolenaara5525202006-03-02 22:52:09 +000021489 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021490 if (varp->v_type == VAR_LIST)
21491 {
21492 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021493 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021494 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021495 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021496
21497 l = varp->vval.v_list;
21498 if (l == NULL)
21499 return NULL;
21500
21501 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021502 pos.lnum = list_find_nr(l, 0L, &error);
21503 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021504 return NULL; /* invalid line number */
21505
21506 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021507 pos.col = list_find_nr(l, 1L, &error);
21508 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021509 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021510 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021511
21512 /* We accept "$" for the column number: last column. */
21513 li = list_find(l, 1L);
21514 if (li != NULL && li->li_tv.v_type == VAR_STRING
21515 && li->li_tv.vval.v_string != NULL
21516 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21517 pos.col = len + 1;
21518
Bram Moolenaara5525202006-03-02 22:52:09 +000021519 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021520 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021521 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021522 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021523
Bram Moolenaara5525202006-03-02 22:52:09 +000021524#ifdef FEAT_VIRTUALEDIT
21525 /* Get the virtual offset. Defaults to zero. */
21526 pos.coladd = list_find_nr(l, 2L, &error);
21527 if (error)
21528 pos.coladd = 0;
21529#endif
21530
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021531 return &pos;
21532 }
21533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021534 name = get_tv_string_chk(varp);
21535 if (name == NULL)
21536 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021537 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021539 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21540 {
21541 if (VIsual_active)
21542 return &VIsual;
21543 return &curwin->w_cursor;
21544 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021545 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021547 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21549 return NULL;
21550 return pp;
21551 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021552
21553#ifdef FEAT_VIRTUALEDIT
21554 pos.coladd = 0;
21555#endif
21556
Bram Moolenaar477933c2007-07-17 14:32:23 +000021557 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021558 {
21559 pos.col = 0;
21560 if (name[1] == '0') /* "w0": first visible line */
21561 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021562 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021563 pos.lnum = curwin->w_topline;
21564 return &pos;
21565 }
21566 else if (name[1] == '$') /* "w$": last visible line */
21567 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021568 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021569 pos.lnum = curwin->w_botline - 1;
21570 return &pos;
21571 }
21572 }
21573 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021575 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 {
21577 pos.lnum = curbuf->b_ml.ml_line_count;
21578 pos.col = 0;
21579 }
21580 else
21581 {
21582 pos.lnum = curwin->w_cursor.lnum;
21583 pos.col = (colnr_T)STRLEN(ml_get_curline());
21584 }
21585 return &pos;
21586 }
21587 return NULL;
21588}
21589
21590/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021591 * Convert list in "arg" into a position and optional file number.
21592 * When "fnump" is NULL there is no file number, only 3 items.
21593 * Note that the column is passed on as-is, the caller may want to decrement
21594 * it to use 1 for the first column.
21595 * Return FAIL when conversion is not possible, doesn't check the position for
21596 * validity.
21597 */
21598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021599list2fpos(
21600 typval_T *arg,
21601 pos_T *posp,
21602 int *fnump,
21603 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021604{
21605 list_T *l = arg->vval.v_list;
21606 long i = 0;
21607 long n;
21608
Bram Moolenaar493c1782014-05-28 14:34:46 +020021609 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21610 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021611 if (arg->v_type != VAR_LIST
21612 || l == NULL
21613 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021614 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021615 return FAIL;
21616
21617 if (fnump != NULL)
21618 {
21619 n = list_find_nr(l, i++, NULL); /* fnum */
21620 if (n < 0)
21621 return FAIL;
21622 if (n == 0)
21623 n = curbuf->b_fnum; /* current buffer */
21624 *fnump = n;
21625 }
21626
21627 n = list_find_nr(l, i++, NULL); /* lnum */
21628 if (n < 0)
21629 return FAIL;
21630 posp->lnum = n;
21631
21632 n = list_find_nr(l, i++, NULL); /* col */
21633 if (n < 0)
21634 return FAIL;
21635 posp->col = n;
21636
21637#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021638 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021639 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021640 posp->coladd = 0;
21641 else
21642 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021643#endif
21644
Bram Moolenaar493c1782014-05-28 14:34:46 +020021645 if (curswantp != NULL)
21646 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21647
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021648 return OK;
21649}
21650
21651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 * Get the length of an environment variable name.
21653 * Advance "arg" to the first character after the name.
21654 * Return 0 for error.
21655 */
21656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021657get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658{
21659 char_u *p;
21660 int len;
21661
21662 for (p = *arg; vim_isIDc(*p); ++p)
21663 ;
21664 if (p == *arg) /* no name found */
21665 return 0;
21666
21667 len = (int)(p - *arg);
21668 *arg = p;
21669 return len;
21670}
21671
21672/*
21673 * Get the length of the name of a function or internal variable.
21674 * "arg" is advanced to the first non-white character after the name.
21675 * Return 0 if something is wrong.
21676 */
21677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021678get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679{
21680 char_u *p;
21681 int len;
21682
21683 /* Find the end of the name. */
21684 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021685 {
21686 if (*p == ':')
21687 {
21688 /* "s:" is start of "s:var", but "n:" is not and can be used in
21689 * slice "[n:]". Also "xx:" is not a namespace. */
21690 len = (int)(p - *arg);
21691 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21692 || len > 1)
21693 break;
21694 }
21695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 if (p == *arg) /* no name found */
21697 return 0;
21698
21699 len = (int)(p - *arg);
21700 *arg = skipwhite(p);
21701
21702 return len;
21703}
21704
21705/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021706 * Get the length of the name of a variable or function.
21707 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021709 * Return -1 if curly braces expansion failed.
21710 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021711 * If the name contains 'magic' {}'s, expand them and return the
21712 * expanded name in an allocated string via 'alias' - caller must free.
21713 */
21714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021715get_name_len(
21716 char_u **arg,
21717 char_u **alias,
21718 int evaluate,
21719 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720{
21721 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 char_u *p;
21723 char_u *expr_start;
21724 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725
21726 *alias = NULL; /* default to no alias */
21727
21728 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21729 && (*arg)[2] == (int)KE_SNR)
21730 {
21731 /* hard coded <SNR>, already translated */
21732 *arg += 3;
21733 return get_id_len(arg) + 3;
21734 }
21735 len = eval_fname_script(*arg);
21736 if (len > 0)
21737 {
21738 /* literal "<SID>", "s:" or "<SNR>" */
21739 *arg += len;
21740 }
21741
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021743 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021745 p = find_name_end(*arg, &expr_start, &expr_end,
21746 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 if (expr_start != NULL)
21748 {
21749 char_u *temp_string;
21750
21751 if (!evaluate)
21752 {
21753 len += (int)(p - *arg);
21754 *arg = skipwhite(p);
21755 return len;
21756 }
21757
21758 /*
21759 * Include any <SID> etc in the expanded string:
21760 * Thus the -len here.
21761 */
21762 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21763 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021764 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 *alias = temp_string;
21766 *arg = skipwhite(p);
21767 return (int)STRLEN(temp_string);
21768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769
21770 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021771 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 EMSG2(_(e_invexpr2), *arg);
21773
21774 return len;
21775}
21776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021777/*
21778 * Find the end of a variable or function name, taking care of magic braces.
21779 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21780 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021781 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021782 * Return a pointer to just after the name. Equal to "arg" if there is no
21783 * valid name.
21784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021786find_name_end(
21787 char_u *arg,
21788 char_u **expr_start,
21789 char_u **expr_end,
21790 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021792 int mb_nest = 0;
21793 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021795 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021797 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021799 *expr_start = NULL;
21800 *expr_end = NULL;
21801 }
21802
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021803 /* Quick check for valid starting character. */
21804 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21805 return arg;
21806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021807 for (p = arg; *p != NUL
21808 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021809 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021810 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021811 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021812 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021813 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021814 if (*p == '\'')
21815 {
21816 /* skip over 'string' to avoid counting [ and ] inside it. */
21817 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21818 ;
21819 if (*p == NUL)
21820 break;
21821 }
21822 else if (*p == '"')
21823 {
21824 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21825 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21826 if (*p == '\\' && p[1] != NUL)
21827 ++p;
21828 if (*p == NUL)
21829 break;
21830 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021831 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21832 {
21833 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021834 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021835 len = (int)(p - arg);
21836 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021837 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021838 break;
21839 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021841 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021843 if (*p == '[')
21844 ++br_nest;
21845 else if (*p == ']')
21846 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021849 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021851 if (*p == '{')
21852 {
21853 mb_nest++;
21854 if (expr_start != NULL && *expr_start == NULL)
21855 *expr_start = p;
21856 }
21857 else if (*p == '}')
21858 {
21859 mb_nest--;
21860 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21861 *expr_end = p;
21862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 }
21865
21866 return p;
21867}
21868
21869/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021870 * Expands out the 'magic' {}'s in a variable/function name.
21871 * Note that this can call itself recursively, to deal with
21872 * constructs like foo{bar}{baz}{bam}
21873 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21874 * "in_start" ^
21875 * "expr_start" ^
21876 * "expr_end" ^
21877 * "in_end" ^
21878 *
21879 * Returns a new allocated string, which the caller must free.
21880 * Returns NULL for failure.
21881 */
21882 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021883make_expanded_name(
21884 char_u *in_start,
21885 char_u *expr_start,
21886 char_u *expr_end,
21887 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021888{
21889 char_u c1;
21890 char_u *retval = NULL;
21891 char_u *temp_result;
21892 char_u *nextcmd = NULL;
21893
21894 if (expr_end == NULL || in_end == NULL)
21895 return NULL;
21896 *expr_start = NUL;
21897 *expr_end = NUL;
21898 c1 = *in_end;
21899 *in_end = NUL;
21900
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021901 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021902 if (temp_result != NULL && nextcmd == NULL)
21903 {
21904 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21905 + (in_end - expr_end) + 1));
21906 if (retval != NULL)
21907 {
21908 STRCPY(retval, in_start);
21909 STRCAT(retval, temp_result);
21910 STRCAT(retval, expr_end + 1);
21911 }
21912 }
21913 vim_free(temp_result);
21914
21915 *in_end = c1; /* put char back for error messages */
21916 *expr_start = '{';
21917 *expr_end = '}';
21918
21919 if (retval != NULL)
21920 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021921 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021922 if (expr_start != NULL)
21923 {
21924 /* Further expansion! */
21925 temp_result = make_expanded_name(retval, expr_start,
21926 expr_end, temp_result);
21927 vim_free(retval);
21928 retval = temp_result;
21929 }
21930 }
21931
21932 return retval;
21933}
21934
21935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021937 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 */
21939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021940eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021942 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21943}
21944
21945/*
21946 * Return TRUE if character "c" can be used as the first character in a
21947 * variable or function name (excluding '{' and '}').
21948 */
21949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021950eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021951{
21952 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953}
21954
21955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 * Set number v: variable to "val".
21957 */
21958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021959set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021961 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962}
21963
21964/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021965 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 */
21967 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021968get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021970 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971}
21972
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021973/*
21974 * Get string v: variable value. Uses a static buffer, can only be used once.
21975 */
21976 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021977get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021978{
21979 return get_tv_string(&vimvars[idx].vv_tv);
21980}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021981
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021983 * Get List v: variable value. Caller must take care of reference count when
21984 * needed.
21985 */
21986 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021987get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021988{
21989 return vimvars[idx].vv_list;
21990}
21991
21992/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021993 * Set v:char to character "c".
21994 */
21995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021996set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021997{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021998 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021999
22000#ifdef FEAT_MBYTE
22001 if (has_mbyte)
22002 buf[(*mb_char2bytes)(c, buf)] = NUL;
22003 else
22004#endif
22005 {
22006 buf[0] = c;
22007 buf[1] = NUL;
22008 }
22009 set_vim_var_string(VV_CHAR, buf, -1);
22010}
22011
22012/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022013 * Set v:count to "count" and v:count1 to "count1".
22014 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022015 */
22016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022017set_vcount(
22018 long count,
22019 long count1,
22020 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022022 if (set_prevcount)
22023 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022024 vimvars[VV_COUNT].vv_nr = count;
22025 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026}
22027
22028/*
22029 * Set string v: variable to a copy of "val".
22030 */
22031 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022032set_vim_var_string(
22033 int idx,
22034 char_u *val,
22035 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036{
Bram Moolenaara542c682016-01-31 16:28:04 +010022037 clear_tv(&vimvars[idx].vv_di.di_tv);
22038 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022040 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022042 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022044 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045}
22046
22047/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022048 * Set List v: variable to "val".
22049 */
22050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022051set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022052{
Bram Moolenaara542c682016-01-31 16:28:04 +010022053 clear_tv(&vimvars[idx].vv_di.di_tv);
22054 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022055 vimvars[idx].vv_list = val;
22056 if (val != NULL)
22057 ++val->lv_refcount;
22058}
22059
22060/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022061 * Set Dictionary v: variable to "val".
22062 */
22063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022064set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022065{
22066 int todo;
22067 hashitem_T *hi;
22068
Bram Moolenaara542c682016-01-31 16:28:04 +010022069 clear_tv(&vimvars[idx].vv_di.di_tv);
22070 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022071 vimvars[idx].vv_dict = val;
22072 if (val != NULL)
22073 {
22074 ++val->dv_refcount;
22075
22076 /* Set readonly */
22077 todo = (int)val->dv_hashtab.ht_used;
22078 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22079 {
22080 if (HASHITEM_EMPTY(hi))
22081 continue;
22082 --todo;
22083 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22084 }
22085 }
22086}
22087
22088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 * Set v:register if needed.
22090 */
22091 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022092set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093{
22094 char_u regname;
22095
22096 if (c == 0 || c == ' ')
22097 regname = '"';
22098 else
22099 regname = c;
22100 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022101 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 set_vim_var_string(VV_REG, &regname, 1);
22103}
22104
22105/*
22106 * Get or set v:exception. If "oldval" == NULL, return the current value.
22107 * Otherwise, restore the value to "oldval" and return NULL.
22108 * Must always be called in pairs to save and restore v:exception! Does not
22109 * take care of memory allocations.
22110 */
22111 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022112v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022113{
22114 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022115 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116
Bram Moolenaare9a41262005-01-15 22:18:47 +000022117 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 return NULL;
22119}
22120
22121/*
22122 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22123 * Otherwise, restore the value to "oldval" and return NULL.
22124 * Must always be called in pairs to save and restore v:throwpoint! Does not
22125 * take care of memory allocations.
22126 */
22127 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022128v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129{
22130 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022131 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132
Bram Moolenaare9a41262005-01-15 22:18:47 +000022133 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022134 return NULL;
22135}
22136
22137#if defined(FEAT_AUTOCMD) || defined(PROTO)
22138/*
22139 * Set v:cmdarg.
22140 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22141 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22142 * Must always be called in pairs!
22143 */
22144 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022145set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146{
22147 char_u *oldval;
22148 char_u *newval;
22149 unsigned len;
22150
Bram Moolenaare9a41262005-01-15 22:18:47 +000022151 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022152 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022154 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022155 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022156 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022157 }
22158
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022159 if (eap->force_bin == FORCE_BIN)
22160 len = 6;
22161 else if (eap->force_bin == FORCE_NOBIN)
22162 len = 8;
22163 else
22164 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022165
22166 if (eap->read_edit)
22167 len += 7;
22168
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022169 if (eap->force_ff != 0)
22170 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22171# ifdef FEAT_MBYTE
22172 if (eap->force_enc != 0)
22173 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022174 if (eap->bad_char != 0)
22175 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022176# endif
22177
22178 newval = alloc(len + 1);
22179 if (newval == NULL)
22180 return NULL;
22181
22182 if (eap->force_bin == FORCE_BIN)
22183 sprintf((char *)newval, " ++bin");
22184 else if (eap->force_bin == FORCE_NOBIN)
22185 sprintf((char *)newval, " ++nobin");
22186 else
22187 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022188
22189 if (eap->read_edit)
22190 STRCAT(newval, " ++edit");
22191
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022192 if (eap->force_ff != 0)
22193 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22194 eap->cmd + eap->force_ff);
22195# ifdef FEAT_MBYTE
22196 if (eap->force_enc != 0)
22197 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22198 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022199 if (eap->bad_char == BAD_KEEP)
22200 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22201 else if (eap->bad_char == BAD_DROP)
22202 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22203 else if (eap->bad_char != 0)
22204 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022205# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022206 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022207 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208}
22209#endif
22210
22211/*
22212 * Get the value of internal variable "name".
22213 * Return OK or FAIL.
22214 */
22215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022216get_var_tv(
22217 char_u *name,
22218 int len, /* length of "name" */
22219 typval_T *rettv, /* NULL when only checking existence */
22220 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22221 int verbose, /* may give error message */
22222 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223{
22224 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022225 typval_T *tv = NULL;
22226 typval_T atv;
22227 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229
22230 /* truncate the name, so that we can use strcmp() */
22231 cc = name[len];
22232 name[len] = NUL;
22233
22234 /*
22235 * Check for "b:changedtick".
22236 */
22237 if (STRCMP(name, "b:changedtick") == 0)
22238 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022239 atv.v_type = VAR_NUMBER;
22240 atv.vval.v_number = curbuf->b_changedtick;
22241 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 }
22243
22244 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 * Check for user-defined variables.
22246 */
22247 else
22248 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022249 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022251 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022253 if (dip != NULL)
22254 *dip = v;
22255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 }
22257
Bram Moolenaare9a41262005-01-15 22:18:47 +000022258 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022260 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 ret = FAIL;
22263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022264 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022265 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266
22267 name[len] = cc;
22268
22269 return ret;
22270}
22271
22272/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022273 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22274 * Also handle function call with Funcref variable: func(expr)
22275 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22276 */
22277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022278handle_subscript(
22279 char_u **arg,
22280 typval_T *rettv,
22281 int evaluate, /* do more than finding the end */
22282 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022283{
22284 int ret = OK;
22285 dict_T *selfdict = NULL;
22286 char_u *s;
22287 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022288 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022289
22290 while (ret == OK
22291 && (**arg == '['
22292 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022293 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22294 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022295 && !vim_iswhite(*(*arg - 1)))
22296 {
22297 if (**arg == '(')
22298 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022299 partial_T *pt = NULL;
22300
Bram Moolenaard9fba312005-06-26 22:34:35 +000022301 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022302 if (evaluate)
22303 {
22304 functv = *rettv;
22305 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022306
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022307 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022308 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022309 {
22310 pt = functv.vval.v_partial;
22311 s = pt->pt_name;
22312 }
22313 else
22314 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022315 }
22316 else
22317 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022318 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022319 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022320 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022321
22322 /* Clear the funcref afterwards, so that deleting it while
22323 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022324 if (evaluate)
22325 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022326
22327 /* Stop the expression evaluation when immediately aborting on
22328 * error, or when an interrupt occurred or an exception was thrown
22329 * but not caught. */
22330 if (aborting())
22331 {
22332 if (ret == OK)
22333 clear_tv(rettv);
22334 ret = FAIL;
22335 }
22336 dict_unref(selfdict);
22337 selfdict = NULL;
22338 }
22339 else /* **arg == '[' || **arg == '.' */
22340 {
22341 dict_unref(selfdict);
22342 if (rettv->v_type == VAR_DICT)
22343 {
22344 selfdict = rettv->vval.v_dict;
22345 if (selfdict != NULL)
22346 ++selfdict->dv_refcount;
22347 }
22348 else
22349 selfdict = NULL;
22350 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22351 {
22352 clear_tv(rettv);
22353 ret = FAIL;
22354 }
22355 }
22356 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022357
Bram Moolenaar1d429612016-05-24 15:44:17 +020022358 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22359 * Don't do this when "Func" is already a partial that was bound
22360 * explicitly (pt_auto is FALSE). */
22361 if (selfdict != NULL
22362 && (rettv->v_type == VAR_FUNC
22363 || (rettv->v_type == VAR_PARTIAL
22364 && (rettv->vval.v_partial->pt_auto
22365 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022366 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022367 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22368 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022369 char_u *tofree = NULL;
22370 ufunc_T *fp;
22371 char_u fname_buf[FLEN_FIXED + 1];
22372 int error;
22373
22374 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022375 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022376 fp = find_func(fname);
22377 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022378
Bram Moolenaar65639032016-03-16 21:40:30 +010022379 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022380 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022381 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22382
22383 if (pt != NULL)
22384 {
22385 pt->pt_refcount = 1;
22386 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022387 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022388 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022389 if (rettv->v_type == VAR_FUNC)
22390 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022391 /* Just a function: Take over the function name and use
22392 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022393 pt->pt_name = rettv->vval.v_string;
22394 }
22395 else
22396 {
22397 partial_T *ret_pt = rettv->vval.v_partial;
22398 int i;
22399
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022400 /* Partial: copy the function name, use selfdict and copy
22401 * args. Can't take over name or args, the partial might
22402 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022403 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022404 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022405 if (ret_pt->pt_argc > 0)
22406 {
22407 pt->pt_argv = (typval_T *)alloc(
22408 sizeof(typval_T) * ret_pt->pt_argc);
22409 if (pt->pt_argv == NULL)
22410 /* out of memory: drop the arguments */
22411 pt->pt_argc = 0;
22412 else
22413 {
22414 pt->pt_argc = ret_pt->pt_argc;
22415 for (i = 0; i < pt->pt_argc; i++)
22416 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22417 }
22418 }
22419 partial_unref(ret_pt);
22420 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022421 rettv->v_type = VAR_PARTIAL;
22422 rettv->vval.v_partial = pt;
22423 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022424 }
22425 }
22426
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022427 dict_unref(selfdict);
22428 return ret;
22429}
22430
22431/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022432 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022433 * value).
22434 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022435 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022436alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022437{
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022439}
22440
22441/*
22442 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 * The string "s" must have been allocated, it is consumed.
22444 * Return NULL for out of memory, the variable otherwise.
22445 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022447alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448{
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022451 rettv = alloc_tv();
22452 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022454 rettv->v_type = VAR_STRING;
22455 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022456 }
22457 else
22458 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022459 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460}
22461
22462/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022463 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022465 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022466free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467{
22468 if (varp != NULL)
22469 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022470 switch (varp->v_type)
22471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022472 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022473 func_unref(varp->vval.v_string);
22474 /*FALLTHROUGH*/
22475 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022476 vim_free(varp->vval.v_string);
22477 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022478 case VAR_PARTIAL:
22479 partial_unref(varp->vval.v_partial);
22480 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022481 case VAR_LIST:
22482 list_unref(varp->vval.v_list);
22483 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022484 case VAR_DICT:
22485 dict_unref(varp->vval.v_dict);
22486 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022487 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022488#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022489 job_unref(varp->vval.v_job);
22490 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022491#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022492 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022494 channel_unref(varp->vval.v_channel);
22495 break;
22496#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022497 case VAR_NUMBER:
22498 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022499 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022500 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022501 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 vim_free(varp);
22504 }
22505}
22506
22507/*
22508 * Free the memory for a variable value and set the value to NULL or 0.
22509 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022511clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512{
22513 if (varp != NULL)
22514 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022517 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518 func_unref(varp->vval.v_string);
22519 /*FALLTHROUGH*/
22520 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022521 vim_free(varp->vval.v_string);
22522 varp->vval.v_string = NULL;
22523 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022524 case VAR_PARTIAL:
22525 partial_unref(varp->vval.v_partial);
22526 varp->vval.v_partial = NULL;
22527 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022528 case VAR_LIST:
22529 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022530 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022531 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022532 case VAR_DICT:
22533 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022534 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022535 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022536 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022537 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 varp->vval.v_number = 0;
22539 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022540 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022541#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022542 varp->vval.v_float = 0.0;
22543 break;
22544#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022545 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022546#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022547 job_unref(varp->vval.v_job);
22548 varp->vval.v_job = NULL;
22549#endif
22550 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022551 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022553 channel_unref(varp->vval.v_channel);
22554 varp->vval.v_channel = NULL;
22555#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022556 case VAR_UNKNOWN:
22557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022559 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 }
22561}
22562
22563/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022564 * Set the value of a variable to NULL without freeing items.
22565 */
22566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022568{
22569 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022571}
22572
22573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 * Get the number value of a variable.
22575 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022576 * For incompatible types, return 0.
22577 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22578 * caller of incompatible types: it sets *denote to TRUE if "denote"
22579 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022581 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022582get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022584 int error = FALSE;
22585
22586 return get_tv_number_chk(varp, &error); /* return 0L on error */
22587}
22588
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022589 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022590get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022591{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022592 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022594 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022596 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022597 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022598 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022599#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022600 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022601 break;
22602#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022603 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022604 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022605 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022606 break;
22607 case VAR_STRING:
22608 if (varp->vval.v_string != NULL)
22609 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022610 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022611 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022612 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022613 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022614 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022615 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022616 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022618 case VAR_SPECIAL:
22619 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22620 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022621 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022622#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022623 EMSG(_("E910: Using a Job as a Number"));
22624 break;
22625#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022626 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022627#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022628 EMSG(_("E913: Using a Channel as a Number"));
22629 break;
22630#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022631 case VAR_UNKNOWN:
22632 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022635 if (denote == NULL) /* useful for values that must be unsigned */
22636 n = -1;
22637 else
22638 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022639 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640}
22641
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022642#ifdef FEAT_FLOAT
22643 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022644get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022645{
22646 switch (varp->v_type)
22647 {
22648 case VAR_NUMBER:
22649 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022650 case VAR_FLOAT:
22651 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022652 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022653 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022654 EMSG(_("E891: Using a Funcref as a Float"));
22655 break;
22656 case VAR_STRING:
22657 EMSG(_("E892: Using a String as a Float"));
22658 break;
22659 case VAR_LIST:
22660 EMSG(_("E893: Using a List as a Float"));
22661 break;
22662 case VAR_DICT:
22663 EMSG(_("E894: Using a Dictionary as a Float"));
22664 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022665 case VAR_SPECIAL:
22666 EMSG(_("E907: Using a special value as a Float"));
22667 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022668 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022669# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022670 EMSG(_("E911: Using a Job as a Float"));
22671 break;
22672# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022673 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022674# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022675 EMSG(_("E914: Using a Channel as a Float"));
22676 break;
22677# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022678 case VAR_UNKNOWN:
22679 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022680 break;
22681 }
22682 return 0;
22683}
22684#endif
22685
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022687 * Get the lnum from the first argument.
22688 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022689 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 */
22691 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022692get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693{
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022695 linenr_T lnum;
22696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022697 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 if (lnum == 0) /* no valid number, try using line() */
22699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022700 rettv.v_type = VAR_NUMBER;
22701 f_line(argvars, &rettv);
22702 lnum = rettv.vval.v_number;
22703 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 }
22705 return lnum;
22706}
22707
22708/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022709 * Get the lnum from the first argument.
22710 * Also accepts "$", then "buf" is used.
22711 * Returns 0 on error.
22712 */
22713 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022714get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022715{
22716 if (argvars[0].v_type == VAR_STRING
22717 && argvars[0].vval.v_string != NULL
22718 && argvars[0].vval.v_string[0] == '$'
22719 && buf != NULL)
22720 return buf->b_ml.ml_line_count;
22721 return get_tv_number_chk(&argvars[0], NULL);
22722}
22723
22724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 * Get the string value of a variable.
22726 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022727 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22728 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 * If the String variable has never been set, return an empty string.
22730 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022731 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22732 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022734 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022735get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736{
22737 static char_u mybuf[NUMBUFLEN];
22738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022739 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740}
22741
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022742 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022743get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022745 char_u *res = get_tv_string_buf_chk(varp, buf);
22746
22747 return res != NULL ? res : (char_u *)"";
22748}
22749
Bram Moolenaar7d647822014-04-05 21:28:56 +020022750/*
22751 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22752 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022753 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022754get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022755{
22756 static char_u mybuf[NUMBUFLEN];
22757
22758 return get_tv_string_buf_chk(varp, mybuf);
22759}
22760
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022762get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022763{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022764 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 case VAR_NUMBER:
22767 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22768 return buf;
22769 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022770 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022772 break;
22773 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022774 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022775 break;
22776 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022777 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022778 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022779 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022780#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022781 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022782 break;
22783#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022784 case VAR_STRING:
22785 if (varp->vval.v_string != NULL)
22786 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022787 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022788 case VAR_SPECIAL:
22789 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22790 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022791 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022793 {
22794 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022795 char *status;
22796
22797 if (job == NULL)
22798 return (char_u *)"no process";
22799 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022800 : job->jv_status == JOB_ENDED ? "dead"
22801 : "run";
22802# ifdef UNIX
22803 vim_snprintf((char *)buf, NUMBUFLEN,
22804 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022805# elif defined(WIN32)
22806 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022807 "process %ld %s",
22808 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022809 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022810# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022811 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022812 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22813# endif
22814 return buf;
22815 }
22816#endif
22817 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022818 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022819#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022820 {
22821 channel_T *channel = varp->vval.v_channel;
22822 char *status = channel_status(channel);
22823
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022824 if (channel == NULL)
22825 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22826 else
22827 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022828 "channel %d %s", channel->ch_id, status);
22829 return buf;
22830 }
22831#endif
22832 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022833 case VAR_UNKNOWN:
22834 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022837 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838}
22839
22840/*
22841 * Find variable "name" in the list of variables.
22842 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022843 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022844 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022847 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022848find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852
Bram Moolenaara7043832005-01-21 11:56:39 +000022853 ht = find_var_ht(name, &varname);
22854 if (htp != NULL)
22855 *htp = ht;
22856 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022858 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859}
22860
22861/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022862 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022863 * Returns NULL if not found.
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_in_ht(
22867 hashtab_T *ht,
22868 int htname,
22869 char_u *varname,
22870 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022871{
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 hashitem_T *hi;
22873
22874 if (*varname == NUL)
22875 {
22876 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022877 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022878 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022879 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022880 case 'g': return &globvars_var;
22881 case 'v': return &vimvars_var;
22882 case 'b': return &curbuf->b_bufvar;
22883 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022884#ifdef FEAT_WINDOWS
22885 case 't': return &curtab->tp_winvar;
22886#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022887 case 'l': return current_funccal == NULL
22888 ? NULL : &current_funccal->l_vars_var;
22889 case 'a': return current_funccal == NULL
22890 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022891 }
22892 return NULL;
22893 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022894
22895 hi = hash_find(ht, varname);
22896 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022897 {
22898 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022899 * worked find the variable again. Don't auto-load a script if it was
22900 * loaded already, otherwise it would be loaded every time when
22901 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022902 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022903 {
22904 /* Note: script_autoload() may make "hi" invalid. It must either
22905 * be obtained again or not used. */
22906 if (!script_autoload(varname, FALSE) || aborting())
22907 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022908 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022909 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022910 if (HASHITEM_EMPTY(hi))
22911 return NULL;
22912 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022913 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022914}
22915
22916/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022917 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022918 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022919 * Set "varname" to the start of name without ':'.
22920 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022922find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022924 hashitem_T *hi;
22925
Bram Moolenaar73627d02015-08-11 15:46:09 +020022926 if (name[0] == NUL)
22927 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 if (name[1] != ':')
22929 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022930 /* The name must not start with a colon or #. */
22931 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 return NULL;
22933 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022934
22935 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022936 hi = hash_find(&compat_hashtab, name);
22937 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022938 return &compat_hashtab;
22939
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022941 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022942 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 }
22944 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022945 if (*name == 'g') /* global variable */
22946 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022947 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22948 */
22949 if (vim_strchr(name + 2, ':') != NULL
22950 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022951 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022953 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022955 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022956#ifdef FEAT_WINDOWS
22957 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022958 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022959#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022960 if (*name == 'v') /* v: variable */
22961 return &vimvarht;
22962 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022963 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022965 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966 if (*name == 's' /* script variable */
22967 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22968 return &SCRIPT_VARS(current_SID);
22969 return NULL;
22970}
22971
22972/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022973 * Get function call environment based on bactrace debug level
22974 */
22975 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022976get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022977{
22978 int i;
22979 funccall_T *funccal;
22980 funccall_T *temp_funccal;
22981
22982 funccal = current_funccal;
22983 if (debug_backtrace_level > 0)
22984 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022985 for (i = 0; i < debug_backtrace_level; i++)
22986 {
22987 temp_funccal = funccal->caller;
22988 if (temp_funccal)
22989 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022990 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022991 /* backtrace level overflow. reset to max */
22992 debug_backtrace_level = i;
22993 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022994 }
22995 return funccal;
22996}
22997
22998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023000 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 * Returns NULL when it doesn't exist.
23002 */
23003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023004get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005{
Bram Moolenaar33570922005-01-25 22:26:29 +000023006 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023008 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 if (v == NULL)
23010 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012}
23013
23014/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023015 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 * sourcing this script and when executing functions defined in the script.
23017 */
23018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023019new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020{
Bram Moolenaara7043832005-01-21 11:56:39 +000023021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 hashtab_T *ht;
23023 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023024
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23026 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023027 /* Re-allocating ga_data means that an ht_array pointing to
23028 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023029 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023030 for (i = 1; i <= ga_scripts.ga_len; ++i)
23031 {
23032 ht = &SCRIPT_VARS(i);
23033 if (ht->ht_mask == HT_INIT_SIZE - 1)
23034 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023035 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023037 }
23038
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 while (ga_scripts.ga_len < id)
23040 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023041 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023042 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023043 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 }
23046 }
23047}
23048
23049/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023050 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23051 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 */
23053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023054init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055{
Bram Moolenaar33570922005-01-25 22:26:29 +000023056 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023057 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023058 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023059 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023060 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023061 dict_var->di_tv.vval.v_dict = dict;
23062 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023063 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023064 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23065 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066}
23067
23068/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023069 * Unreference a dictionary initialized by init_var_dict().
23070 */
23071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023072unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023073{
23074 /* Now the dict needs to be freed if no one else is using it, go back to
23075 * normal reference counting. */
23076 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23077 dict_unref(dict);
23078}
23079
23080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023082 * Frees all allocated variables and the value they contain.
23083 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 */
23085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023086vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023087{
23088 vars_clear_ext(ht, TRUE);
23089}
23090
23091/*
23092 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23093 */
23094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023095vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096{
Bram Moolenaara7043832005-01-21 11:56:39 +000023097 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023098 hashitem_T *hi;
23099 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100
Bram Moolenaar33570922005-01-25 22:26:29 +000023101 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023102 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023103 for (hi = ht->ht_array; todo > 0; ++hi)
23104 {
23105 if (!HASHITEM_EMPTY(hi))
23106 {
23107 --todo;
23108
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023110 * ht_array might change then. hash_clear() takes care of it
23111 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023112 v = HI2DI(hi);
23113 if (free_val)
23114 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023115 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023117 }
23118 }
23119 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023120 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121}
23122
Bram Moolenaara7043832005-01-21 11:56:39 +000023123/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023124 * Delete a variable from hashtab "ht" at item "hi".
23125 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023128delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129{
Bram Moolenaar33570922005-01-25 22:26:29 +000023130 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023131
23132 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 clear_tv(&di->di_tv);
23134 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135}
23136
23137/*
23138 * List the value of one internal variable.
23139 */
23140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023141list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023143 char_u *tofree;
23144 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023145 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023146
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023147 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023148 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023149 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023150 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151}
23152
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023154list_one_var_a(
23155 char_u *prefix,
23156 char_u *name,
23157 int type,
23158 char_u *string,
23159 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160{
Bram Moolenaar31859182007-08-14 20:41:13 +000023161 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23162 msg_start();
23163 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 if (name != NULL) /* "a:" vars don't have a name stored */
23165 msg_puts(name);
23166 msg_putchar(' ');
23167 msg_advance(22);
23168 if (type == VAR_NUMBER)
23169 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023170 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023171 msg_putchar('*');
23172 else if (type == VAR_LIST)
23173 {
23174 msg_putchar('[');
23175 if (*string == '[')
23176 ++string;
23177 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023178 else if (type == VAR_DICT)
23179 {
23180 msg_putchar('{');
23181 if (*string == '{')
23182 ++string;
23183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 else
23185 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023186
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023188
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023189 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023190 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023191 if (*first)
23192 {
23193 msg_clr_eos();
23194 *first = FALSE;
23195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196}
23197
23198/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023199 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200 * If the variable already exists, the value is updated.
23201 * Otherwise the variable is created.
23202 */
23203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023204set_var(
23205 char_u *name,
23206 typval_T *tv,
23207 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208{
Bram Moolenaar33570922005-01-25 22:26:29 +000023209 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023213 ht = find_var_ht(name, &varname);
23214 if (ht == NULL || *varname == NUL)
23215 {
23216 EMSG2(_(e_illvar), name);
23217 return;
23218 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023219 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023220
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023221 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23222 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023223 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023224
Bram Moolenaar33570922005-01-25 22:26:29 +000023225 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023228 if (var_check_ro(v->di_flags, name, FALSE)
23229 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023230 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023231
23232 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023233 * Handle setting internal v: variables separately where needed to
23234 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 */
23236 if (ht == &vimvarht)
23237 {
23238 if (v->di_tv.v_type == VAR_STRING)
23239 {
23240 vim_free(v->di_tv.vval.v_string);
23241 if (copy || tv->v_type != VAR_STRING)
23242 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23243 else
23244 {
23245 /* Take over the string to avoid an extra alloc/free. */
23246 v->di_tv.vval.v_string = tv->vval.v_string;
23247 tv->vval.v_string = NULL;
23248 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023249 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023251 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023252 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023253 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023254 if (STRCMP(varname, "searchforward") == 0)
23255 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023256#ifdef FEAT_SEARCH_EXTRA
23257 else if (STRCMP(varname, "hlsearch") == 0)
23258 {
23259 no_hlsearch = !v->di_tv.vval.v_number;
23260 redraw_all_later(SOME_VALID);
23261 }
23262#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023263 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023264 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023265 else if (v->di_tv.v_type != tv->v_type)
23266 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023267 }
23268
23269 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 }
23271 else /* add a new variable */
23272 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023273 /* Can't add "v:" variable. */
23274 if (ht == &vimvarht)
23275 {
23276 EMSG2(_(e_illvar), name);
23277 return;
23278 }
23279
Bram Moolenaar92124a32005-06-17 22:03:40 +000023280 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023281 if (!valid_varname(varname))
23282 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023283
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023284 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23285 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023286 if (v == NULL)
23287 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023289 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023291 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 return;
23293 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023294 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023296
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023297 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023298 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023299 else
23300 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023302 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023303 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305}
23306
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023307/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023308 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023309 * Also give an error message.
23310 */
23311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023312var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023313{
23314 if (flags & DI_FLAGS_RO)
23315 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023316 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023317 return TRUE;
23318 }
23319 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23320 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023321 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023322 return TRUE;
23323 }
23324 return FALSE;
23325}
23326
23327/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023328 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23329 * Also give an error message.
23330 */
23331 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023332var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023333{
23334 if (flags & DI_FLAGS_FIX)
23335 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023336 EMSG2(_("E795: Cannot delete variable %s"),
23337 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023338 return TRUE;
23339 }
23340 return FALSE;
23341}
23342
23343/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023344 * Check if a funcref is assigned to a valid variable name.
23345 * Return TRUE and give an error if not.
23346 */
23347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023348var_check_func_name(
23349 char_u *name, /* points to start of variable name */
23350 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023351{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023352 /* Allow for w: b: s: and t:. */
23353 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023354 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23355 ? name[2] : name[0]))
23356 {
23357 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23358 name);
23359 return TRUE;
23360 }
23361 /* Don't allow hiding a function. When "v" is not NULL we might be
23362 * assigning another function to the same var, the type is checked
23363 * below. */
23364 if (new_var && function_exists(name))
23365 {
23366 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23367 name);
23368 return TRUE;
23369 }
23370 return FALSE;
23371}
23372
23373/*
23374 * Check if a variable name is valid.
23375 * Return FALSE and give an error if not.
23376 */
23377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023378valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023379{
23380 char_u *p;
23381
23382 for (p = varname; *p != NUL; ++p)
23383 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23384 && *p != AUTOLOAD_CHAR)
23385 {
23386 EMSG2(_(e_illvar), varname);
23387 return FALSE;
23388 }
23389 return TRUE;
23390}
23391
23392/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023393 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023394 * Also give an error message, using "name" or _("name") when use_gettext is
23395 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023396 */
23397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023398tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023399{
23400 if (lock & VAR_LOCKED)
23401 {
23402 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023403 name == NULL ? (char_u *)_("Unknown")
23404 : use_gettext ? (char_u *)_(name)
23405 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023406 return TRUE;
23407 }
23408 if (lock & VAR_FIXED)
23409 {
23410 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023411 name == NULL ? (char_u *)_("Unknown")
23412 : use_gettext ? (char_u *)_(name)
23413 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023414 return TRUE;
23415 }
23416 return FALSE;
23417}
23418
23419/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023420 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023421 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023422 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023423 * It is OK for "from" and "to" to point to the same item. This is used to
23424 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023425 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023427copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023429 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023430 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023431 switch (from->v_type)
23432 {
23433 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023434 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023435 to->vval.v_number = from->vval.v_number;
23436 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023437 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023438#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023439 to->vval.v_float = from->vval.v_float;
23440 break;
23441#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023442 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023443#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023444 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023445 if (to->vval.v_job != NULL)
23446 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023447 break;
23448#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023449 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023450#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023451 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023452 if (to->vval.v_channel != NULL)
23453 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023454 break;
23455#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023456 case VAR_STRING:
23457 case VAR_FUNC:
23458 if (from->vval.v_string == NULL)
23459 to->vval.v_string = NULL;
23460 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023461 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023462 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023463 if (from->v_type == VAR_FUNC)
23464 func_ref(to->vval.v_string);
23465 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023466 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023467 case VAR_PARTIAL:
23468 if (from->vval.v_partial == NULL)
23469 to->vval.v_partial = NULL;
23470 else
23471 {
23472 to->vval.v_partial = from->vval.v_partial;
23473 ++to->vval.v_partial->pt_refcount;
23474 }
23475 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023476 case VAR_LIST:
23477 if (from->vval.v_list == NULL)
23478 to->vval.v_list = NULL;
23479 else
23480 {
23481 to->vval.v_list = from->vval.v_list;
23482 ++to->vval.v_list->lv_refcount;
23483 }
23484 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023485 case VAR_DICT:
23486 if (from->vval.v_dict == NULL)
23487 to->vval.v_dict = NULL;
23488 else
23489 {
23490 to->vval.v_dict = from->vval.v_dict;
23491 ++to->vval.v_dict->dv_refcount;
23492 }
23493 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023494 case VAR_UNKNOWN:
23495 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023496 break;
23497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498}
23499
23500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023501 * Make a copy of an item.
23502 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023503 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23504 * reference to an already copied list/dict can be used.
23505 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023506 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023508item_copy(
23509 typval_T *from,
23510 typval_T *to,
23511 int deep,
23512 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023513{
23514 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023515 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023516
Bram Moolenaar33570922005-01-25 22:26:29 +000023517 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023518 {
23519 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023520 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023521 }
23522 ++recurse;
23523
23524 switch (from->v_type)
23525 {
23526 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023527 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023528 case VAR_STRING:
23529 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023530 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023531 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023532 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023533 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023534 copy_tv(from, to);
23535 break;
23536 case VAR_LIST:
23537 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023538 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023539 if (from->vval.v_list == NULL)
23540 to->vval.v_list = NULL;
23541 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23542 {
23543 /* use the copy made earlier */
23544 to->vval.v_list = from->vval.v_list->lv_copylist;
23545 ++to->vval.v_list->lv_refcount;
23546 }
23547 else
23548 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23549 if (to->vval.v_list == NULL)
23550 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023551 break;
23552 case VAR_DICT:
23553 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023554 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023555 if (from->vval.v_dict == NULL)
23556 to->vval.v_dict = NULL;
23557 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23558 {
23559 /* use the copy made earlier */
23560 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23561 ++to->vval.v_dict->dv_refcount;
23562 }
23563 else
23564 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23565 if (to->vval.v_dict == NULL)
23566 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023567 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023568 case VAR_UNKNOWN:
23569 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023570 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023571 }
23572 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023573 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023574}
23575
23576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 * ":echo expr1 ..." print each argument separated with a space, add a
23578 * newline at the end.
23579 * ":echon expr1 ..." print each argument plain.
23580 */
23581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023582ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583{
23584 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023585 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023586 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 char_u *p;
23588 int needclr = TRUE;
23589 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023590 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591
23592 if (eap->skip)
23593 ++emsg_skip;
23594 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23595 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023596 /* If eval1() causes an error message the text from the command may
23597 * still need to be cleared. E.g., "echo 22,44". */
23598 need_clr_eos = needclr;
23599
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023601 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 {
23603 /*
23604 * Report the invalid expression unless the expression evaluation
23605 * has been cancelled due to an aborting error, an interrupt, or an
23606 * exception.
23607 */
23608 if (!aborting())
23609 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023610 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 break;
23612 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023613 need_clr_eos = FALSE;
23614
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 if (!eap->skip)
23616 {
23617 if (atstart)
23618 {
23619 atstart = FALSE;
23620 /* Call msg_start() after eval1(), evaluating the expression
23621 * may cause a message to appear. */
23622 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023623 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023624 /* Mark the saved text as finishing the line, so that what
23625 * follows is displayed on a new line when scrolling back
23626 * at the more prompt. */
23627 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 }
23631 else if (eap->cmdidx == CMD_echo)
23632 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023633 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023634 if (p != NULL)
23635 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023637 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023639 if (*p != TAB && needclr)
23640 {
23641 /* remove any text still there from the command */
23642 msg_clr_eos();
23643 needclr = FALSE;
23644 }
23645 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 }
23647 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023648 {
23649#ifdef FEAT_MBYTE
23650 if (has_mbyte)
23651 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023652 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023653
23654 (void)msg_outtrans_len_attr(p, i, echo_attr);
23655 p += i - 1;
23656 }
23657 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023659 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023662 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 arg = skipwhite(arg);
23666 }
23667 eap->nextcmd = check_nextcmd(arg);
23668
23669 if (eap->skip)
23670 --emsg_skip;
23671 else
23672 {
23673 /* remove text that may still be there from the command */
23674 if (needclr)
23675 msg_clr_eos();
23676 if (eap->cmdidx == CMD_echo)
23677 msg_end();
23678 }
23679}
23680
23681/*
23682 * ":echohl {name}".
23683 */
23684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023685ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686{
23687 int id;
23688
23689 id = syn_name2id(eap->arg);
23690 if (id == 0)
23691 echo_attr = 0;
23692 else
23693 echo_attr = syn_id2attr(id);
23694}
23695
23696/*
23697 * ":execute expr1 ..." execute the result of an expression.
23698 * ":echomsg expr1 ..." Print a message
23699 * ":echoerr expr1 ..." Print an error
23700 * Each gets spaces around each argument and a newline at the end for
23701 * echo commands
23702 */
23703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023704ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705{
23706 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023707 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 int ret = OK;
23709 char_u *p;
23710 garray_T ga;
23711 int len;
23712 int save_did_emsg;
23713
23714 ga_init2(&ga, 1, 80);
23715
23716 if (eap->skip)
23717 ++emsg_skip;
23718 while (*arg != NUL && *arg != '|' && *arg != '\n')
23719 {
23720 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023721 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 {
23723 /*
23724 * Report the invalid expression unless the expression evaluation
23725 * has been cancelled due to an aborting error, an interrupt, or an
23726 * exception.
23727 */
23728 if (!aborting())
23729 EMSG2(_(e_invexpr2), p);
23730 ret = FAIL;
23731 break;
23732 }
23733
23734 if (!eap->skip)
23735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023736 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 len = (int)STRLEN(p);
23738 if (ga_grow(&ga, len + 2) == FAIL)
23739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 ret = FAIL;
23742 break;
23743 }
23744 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 ga.ga_len += len;
23748 }
23749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 arg = skipwhite(arg);
23752 }
23753
23754 if (ret != FAIL && ga.ga_data != NULL)
23755 {
23756 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023759 out_flush();
23760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 else if (eap->cmdidx == CMD_echoerr)
23762 {
23763 /* We don't want to abort following commands, restore did_emsg. */
23764 save_did_emsg = did_emsg;
23765 EMSG((char_u *)ga.ga_data);
23766 if (!force_abort)
23767 did_emsg = save_did_emsg;
23768 }
23769 else if (eap->cmdidx == CMD_execute)
23770 do_cmdline((char_u *)ga.ga_data,
23771 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23772 }
23773
23774 ga_clear(&ga);
23775
23776 if (eap->skip)
23777 --emsg_skip;
23778
23779 eap->nextcmd = check_nextcmd(arg);
23780}
23781
23782/*
23783 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23784 * "arg" points to the "&" or '+' when called, to "option" when returning.
23785 * Returns NULL when no option name found. Otherwise pointer to the char
23786 * after the option name.
23787 */
23788 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023789find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790{
23791 char_u *p = *arg;
23792
23793 ++p;
23794 if (*p == 'g' && p[1] == ':')
23795 {
23796 *opt_flags = OPT_GLOBAL;
23797 p += 2;
23798 }
23799 else if (*p == 'l' && p[1] == ':')
23800 {
23801 *opt_flags = OPT_LOCAL;
23802 p += 2;
23803 }
23804 else
23805 *opt_flags = 0;
23806
23807 if (!ASCII_ISALPHA(*p))
23808 return NULL;
23809 *arg = p;
23810
23811 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23812 p += 4; /* termcap option */
23813 else
23814 while (ASCII_ISALPHA(*p))
23815 ++p;
23816 return p;
23817}
23818
23819/*
23820 * ":function"
23821 */
23822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023823ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824{
23825 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023826 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 int j;
23828 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023830 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831 char_u *name = NULL;
23832 char_u *p;
23833 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023834 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 garray_T newargs;
23836 garray_T newlines;
23837 int varargs = FALSE;
23838 int mustend = FALSE;
23839 int flags = 0;
23840 ufunc_T *fp;
23841 int indent;
23842 int nesting;
23843 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023844 dictitem_T *v;
23845 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023846 static int func_nr = 0; /* number for nameless function */
23847 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023848 hashtab_T *ht;
23849 int todo;
23850 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023851 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852
23853 /*
23854 * ":function" without argument: list functions.
23855 */
23856 if (ends_excmd(*eap->arg))
23857 {
23858 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023859 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023860 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023861 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023862 {
23863 if (!HASHITEM_EMPTY(hi))
23864 {
23865 --todo;
23866 fp = HI2UF(hi);
23867 if (!isdigit(*fp->uf_name))
23868 list_func_head(fp, FALSE);
23869 }
23870 }
23871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 eap->nextcmd = check_nextcmd(eap->arg);
23873 return;
23874 }
23875
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023876 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023877 * ":function /pat": list functions matching pattern.
23878 */
23879 if (*eap->arg == '/')
23880 {
23881 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23882 if (!eap->skip)
23883 {
23884 regmatch_T regmatch;
23885
23886 c = *p;
23887 *p = NUL;
23888 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23889 *p = c;
23890 if (regmatch.regprog != NULL)
23891 {
23892 regmatch.rm_ic = p_ic;
23893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023894 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023895 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23896 {
23897 if (!HASHITEM_EMPTY(hi))
23898 {
23899 --todo;
23900 fp = HI2UF(hi);
23901 if (!isdigit(*fp->uf_name)
23902 && vim_regexec(&regmatch, fp->uf_name, 0))
23903 list_func_head(fp, FALSE);
23904 }
23905 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023906 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023907 }
23908 }
23909 if (*p == '/')
23910 ++p;
23911 eap->nextcmd = check_nextcmd(p);
23912 return;
23913 }
23914
23915 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 * Get the function name. There are these situations:
23917 * func normal function name
23918 * "name" == func, "fudi.fd_dict" == NULL
23919 * dict.func new dictionary entry
23920 * "name" == NULL, "fudi.fd_dict" set,
23921 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23922 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023923 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023924 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23925 * dict.func existing dict entry that's not a Funcref
23926 * "name" == NULL, "fudi.fd_dict" set,
23927 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023928 * s:func script-local function name
23929 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023932 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023933 paren = (vim_strchr(p, '(') != NULL);
23934 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 {
23936 /*
23937 * Return on an invalid expression in braces, unless the expression
23938 * evaluation has been cancelled due to an aborting error, an
23939 * interrupt, or an exception.
23940 */
23941 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023943 if (!eap->skip && fudi.fd_newkey != NULL)
23944 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023945 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 else
23949 eap->skip = TRUE;
23950 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023951
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 /* An error in a function call during evaluation of an expression in magic
23953 * braces should not cause the function not to be defined. */
23954 saved_did_emsg = did_emsg;
23955 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956
23957 /*
23958 * ":function func" with only function name: list function.
23959 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023960 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 {
23962 if (!ends_excmd(*skipwhite(p)))
23963 {
23964 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023965 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 }
23967 eap->nextcmd = check_nextcmd(p);
23968 if (eap->nextcmd != NULL)
23969 *p = NUL;
23970 if (!eap->skip && !got_int)
23971 {
23972 fp = find_func(name);
23973 if (fp != NULL)
23974 {
23975 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023976 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023978 if (FUNCLINE(fp, j) == NULL)
23979 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 msg_putchar('\n');
23981 msg_outnum((long)(j + 1));
23982 if (j < 9)
23983 msg_putchar(' ');
23984 if (j < 99)
23985 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023986 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 out_flush(); /* show a line at a time */
23988 ui_breakcheck();
23989 }
23990 if (!got_int)
23991 {
23992 msg_putchar('\n');
23993 msg_puts((char_u *)" endfunction");
23994 }
23995 }
23996 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023997 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 }
24001
24002 /*
24003 * ":function name(arg1, arg2)" Define function.
24004 */
24005 p = skipwhite(p);
24006 if (*p != '(')
24007 {
24008 if (!eap->skip)
24009 {
24010 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024011 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 }
24013 /* attempt to continue by skipping some text */
24014 if (vim_strchr(p, '(') != NULL)
24015 p = vim_strchr(p, '(');
24016 }
24017 p = skipwhite(p + 1);
24018
24019 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24020 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24021
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024022 if (!eap->skip)
24023 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024024 /* Check the name of the function. Unless it's a dictionary function
24025 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024026 if (name != NULL)
24027 arg = name;
24028 else
24029 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024030 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024031 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24032 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024033 {
24034 if (*arg == K_SPECIAL)
24035 j = 3;
24036 else
24037 j = 0;
24038 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24039 : eval_isnamec(arg[j])))
24040 ++j;
24041 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024042 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024043 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024044 /* Disallow using the g: dict. */
24045 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24046 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024047 }
24048
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 /*
24050 * Isolate the arguments: "arg1, arg2, ...)"
24051 */
24052 while (*p != ')')
24053 {
24054 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24055 {
24056 varargs = TRUE;
24057 p += 3;
24058 mustend = TRUE;
24059 }
24060 else
24061 {
24062 arg = p;
24063 while (ASCII_ISALNUM(*p) || *p == '_')
24064 ++p;
24065 if (arg == p || isdigit(*arg)
24066 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24067 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24068 {
24069 if (!eap->skip)
24070 EMSG2(_("E125: Illegal argument: %s"), arg);
24071 break;
24072 }
24073 if (ga_grow(&newargs, 1) == FAIL)
24074 goto erret;
24075 c = *p;
24076 *p = NUL;
24077 arg = vim_strsave(arg);
24078 if (arg == NULL)
24079 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024080
24081 /* Check for duplicate argument name. */
24082 for (i = 0; i < newargs.ga_len; ++i)
24083 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24084 {
24085 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024086 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024087 goto erret;
24088 }
24089
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24091 *p = c;
24092 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 if (*p == ',')
24094 ++p;
24095 else
24096 mustend = TRUE;
24097 }
24098 p = skipwhite(p);
24099 if (mustend && *p != ')')
24100 {
24101 if (!eap->skip)
24102 EMSG2(_(e_invarg2), eap->arg);
24103 break;
24104 }
24105 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024106 if (*p != ')')
24107 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 ++p; /* skip the ')' */
24109
Bram Moolenaare9a41262005-01-15 22:18:47 +000024110 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 for (;;)
24112 {
24113 p = skipwhite(p);
24114 if (STRNCMP(p, "range", 5) == 0)
24115 {
24116 flags |= FC_RANGE;
24117 p += 5;
24118 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024119 else if (STRNCMP(p, "dict", 4) == 0)
24120 {
24121 flags |= FC_DICT;
24122 p += 4;
24123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 else if (STRNCMP(p, "abort", 5) == 0)
24125 {
24126 flags |= FC_ABORT;
24127 p += 5;
24128 }
24129 else
24130 break;
24131 }
24132
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024133 /* When there is a line break use what follows for the function body.
24134 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24135 if (*p == '\n')
24136 line_arg = p + 1;
24137 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 EMSG(_(e_trailing));
24139
24140 /*
24141 * Read the body of the function, until ":endfunction" is found.
24142 */
24143 if (KeyTyped)
24144 {
24145 /* Check if the function already exists, don't let the user type the
24146 * whole function before telling him it doesn't work! For a script we
24147 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024148 if (!eap->skip && !eap->forceit)
24149 {
24150 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24151 EMSG(_(e_funcdict));
24152 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024153 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024155
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024156 if (!eap->skip && did_emsg)
24157 goto erret;
24158
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 msg_putchar('\n'); /* don't overwrite the function name */
24160 cmdline_row = msg_row;
24161 }
24162
24163 indent = 2;
24164 nesting = 0;
24165 for (;;)
24166 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024167 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024168 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024169 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024170 saved_wait_return = FALSE;
24171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024173 sourcing_lnum_off = sourcing_lnum;
24174
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024175 if (line_arg != NULL)
24176 {
24177 /* Use eap->arg, split up in parts by line breaks. */
24178 theline = line_arg;
24179 p = vim_strchr(theline, '\n');
24180 if (p == NULL)
24181 line_arg += STRLEN(line_arg);
24182 else
24183 {
24184 *p = NUL;
24185 line_arg = p + 1;
24186 }
24187 }
24188 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 theline = getcmdline(':', 0L, indent);
24190 else
24191 theline = eap->getline(':', eap->cookie, indent);
24192 if (KeyTyped)
24193 lines_left = Rows - 1;
24194 if (theline == NULL)
24195 {
24196 EMSG(_("E126: Missing :endfunction"));
24197 goto erret;
24198 }
24199
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024200 /* Detect line continuation: sourcing_lnum increased more than one. */
24201 if (sourcing_lnum > sourcing_lnum_off + 1)
24202 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24203 else
24204 sourcing_lnum_off = 0;
24205
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 if (skip_until != NULL)
24207 {
24208 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24209 * don't check for ":endfunc". */
24210 if (STRCMP(theline, skip_until) == 0)
24211 {
24212 vim_free(skip_until);
24213 skip_until = NULL;
24214 }
24215 }
24216 else
24217 {
24218 /* skip ':' and blanks*/
24219 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24220 ;
24221
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024222 /* Check for "endfunction". */
24223 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024224 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024225 if (line_arg == NULL)
24226 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 break;
24228 }
24229
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024230 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 * at "end". */
24232 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24233 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024234 else if (STRNCMP(p, "if", 2) == 0
24235 || STRNCMP(p, "wh", 2) == 0
24236 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 || STRNCMP(p, "try", 3) == 0)
24238 indent += 2;
24239
24240 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024241 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024243 if (*p == '!')
24244 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024246 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024247 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024249 ++nesting;
24250 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 }
24252 }
24253
24254 /* Check for ":append" or ":insert". */
24255 p = skip_range(p, NULL);
24256 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24257 || (p[0] == 'i'
24258 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24259 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24260 skip_until = vim_strsave((char_u *)".");
24261
24262 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24263 arg = skipwhite(skiptowhite(p));
24264 if (arg[0] == '<' && arg[1] =='<'
24265 && ((p[0] == 'p' && p[1] == 'y'
24266 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24267 || (p[0] == 'p' && p[1] == 'e'
24268 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24269 || (p[0] == 't' && p[1] == 'c'
24270 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024271 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24272 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24274 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024275 || (p[0] == 'm' && p[1] == 'z'
24276 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 ))
24278 {
24279 /* ":python <<" continues until a dot, like ":append" */
24280 p = skipwhite(arg + 2);
24281 if (*p == NUL)
24282 skip_until = vim_strsave((char_u *)".");
24283 else
24284 skip_until = vim_strsave(p);
24285 }
24286 }
24287
24288 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024289 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024290 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024291 if (line_arg == NULL)
24292 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024294 }
24295
24296 /* Copy the line to newly allocated memory. get_one_sourceline()
24297 * allocates 250 bytes per line, this saves 80% on average. The cost
24298 * is an extra alloc/free. */
24299 p = vim_strsave(theline);
24300 if (p != NULL)
24301 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024302 if (line_arg == NULL)
24303 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024304 theline = p;
24305 }
24306
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024307 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24308
24309 /* Add NULL lines for continuation lines, so that the line count is
24310 * equal to the index in the growarray. */
24311 while (sourcing_lnum_off-- > 0)
24312 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024313
24314 /* Check for end of eap->arg. */
24315 if (line_arg != NULL && *line_arg == NUL)
24316 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 }
24318
24319 /* Don't define the function when skipping commands or when an error was
24320 * detected. */
24321 if (eap->skip || did_emsg)
24322 goto erret;
24323
24324 /*
24325 * If there are no errors, add the function
24326 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024327 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024328 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024329 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024330 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024331 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024332 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024333 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024334 goto erret;
24335 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024336
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024337 fp = find_func(name);
24338 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024340 if (!eap->forceit)
24341 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024342 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024343 goto erret;
24344 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024345 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024346 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024347 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024348 name);
24349 goto erret;
24350 }
24351 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024352 ga_clear_strings(&(fp->uf_args));
24353 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024354 vim_free(name);
24355 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024357 }
24358 else
24359 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024360 char numbuf[20];
24361
24362 fp = NULL;
24363 if (fudi.fd_newkey == NULL && !eap->forceit)
24364 {
24365 EMSG(_(e_funcdict));
24366 goto erret;
24367 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024368 if (fudi.fd_di == NULL)
24369 {
24370 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024371 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024372 goto erret;
24373 }
24374 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024375 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024376 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024377
24378 /* Give the function a sequential number. Can only be used with a
24379 * Funcref! */
24380 vim_free(name);
24381 sprintf(numbuf, "%d", ++func_nr);
24382 name = vim_strsave((char_u *)numbuf);
24383 if (name == NULL)
24384 goto erret;
24385 }
24386
24387 if (fp == NULL)
24388 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024389 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024390 {
24391 int slen, plen;
24392 char_u *scriptname;
24393
24394 /* Check that the autoload name matches the script name. */
24395 j = FAIL;
24396 if (sourcing_name != NULL)
24397 {
24398 scriptname = autoload_name(name);
24399 if (scriptname != NULL)
24400 {
24401 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024402 plen = (int)STRLEN(p);
24403 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024404 if (slen > plen && fnamecmp(p,
24405 sourcing_name + slen - plen) == 0)
24406 j = OK;
24407 vim_free(scriptname);
24408 }
24409 }
24410 if (j == FAIL)
24411 {
24412 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24413 goto erret;
24414 }
24415 }
24416
24417 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418 if (fp == NULL)
24419 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024420
24421 if (fudi.fd_dict != NULL)
24422 {
24423 if (fudi.fd_di == NULL)
24424 {
24425 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024426 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024427 if (fudi.fd_di == NULL)
24428 {
24429 vim_free(fp);
24430 goto erret;
24431 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024432 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24433 {
24434 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024435 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024436 goto erret;
24437 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024438 }
24439 else
24440 /* overwrite existing dict entry */
24441 clear_tv(&fudi.fd_di->di_tv);
24442 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024443 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024444 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024445 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024446
24447 /* behave like "dict" was used */
24448 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024449 }
24450
Bram Moolenaar071d4272004-06-13 20:20:40 +000024451 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024452 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024453 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24454 {
24455 vim_free(fp);
24456 goto erret;
24457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024459 fp->uf_args = newargs;
24460 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024461#ifdef FEAT_PROFILE
24462 fp->uf_tml_count = NULL;
24463 fp->uf_tml_total = NULL;
24464 fp->uf_tml_self = NULL;
24465 fp->uf_profiling = FALSE;
24466 if (prof_def_func())
24467 func_do_profile(fp);
24468#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024469 fp->uf_varargs = varargs;
24470 fp->uf_flags = flags;
24471 fp->uf_calls = 0;
24472 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024473 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474
24475erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024476 ga_clear_strings(&newargs);
24477 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024478ret_free:
24479 vim_free(skip_until);
24480 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024483 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484}
24485
24486/*
24487 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024488 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024490 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024491 * TFN_INT: internal function name OK
24492 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024493 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494 * Advances "pp" to just after the function name (if no error).
24495 */
24496 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024497trans_function_name(
24498 char_u **pp,
24499 int skip, /* only find the end, don't evaluate */
24500 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024501 funcdict_T *fdp, /* return: info about dictionary used */
24502 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024504 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 char_u *start;
24506 char_u *end;
24507 int lead;
24508 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024510 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024511
24512 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024513 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024515
24516 /* Check for hard coded <SNR>: already translated function ID (from a user
24517 * command). */
24518 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24519 && (*pp)[2] == (int)KE_SNR)
24520 {
24521 *pp += 3;
24522 len = get_id_len(pp) + 3;
24523 return vim_strnsave(start, len);
24524 }
24525
24526 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24527 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024529 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024531
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024532 /* Note that TFN_ flags use the same values as GLV_ flags. */
24533 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024534 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024535 if (end == start)
24536 {
24537 if (!skip)
24538 EMSG(_("E129: Function name required"));
24539 goto theend;
24540 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024541 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024542 {
24543 /*
24544 * Report an invalid expression in braces, unless the expression
24545 * evaluation has been cancelled due to an aborting error, an
24546 * interrupt, or an exception.
24547 */
24548 if (!aborting())
24549 {
24550 if (end != NULL)
24551 EMSG2(_(e_invarg2), start);
24552 }
24553 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024554 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024555 goto theend;
24556 }
24557
24558 if (lv.ll_tv != NULL)
24559 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024560 if (fdp != NULL)
24561 {
24562 fdp->fd_dict = lv.ll_dict;
24563 fdp->fd_newkey = lv.ll_newkey;
24564 lv.ll_newkey = NULL;
24565 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024567 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24568 {
24569 name = vim_strsave(lv.ll_tv->vval.v_string);
24570 *pp = end;
24571 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024572 else if (lv.ll_tv->v_type == VAR_PARTIAL
24573 && lv.ll_tv->vval.v_partial != NULL)
24574 {
24575 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24576 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024577 if (partial != NULL)
24578 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024579 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024580 else
24581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024582 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24583 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024584 EMSG(_(e_funcref));
24585 else
24586 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024587 name = NULL;
24588 }
24589 goto theend;
24590 }
24591
24592 if (lv.ll_name == NULL)
24593 {
24594 /* Error found, but continue after the function name. */
24595 *pp = end;
24596 goto theend;
24597 }
24598
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024599 /* Check if the name is a Funcref. If so, use the value. */
24600 if (lv.ll_exp_name != NULL)
24601 {
24602 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024603 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024604 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024605 if (name == lv.ll_exp_name)
24606 name = NULL;
24607 }
24608 else
24609 {
24610 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024611 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024612 if (name == *pp)
24613 name = NULL;
24614 }
24615 if (name != NULL)
24616 {
24617 name = vim_strsave(name);
24618 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024619 if (STRNCMP(name, "<SNR>", 5) == 0)
24620 {
24621 /* Change "<SNR>" to the byte sequence. */
24622 name[0] = K_SPECIAL;
24623 name[1] = KS_EXTRA;
24624 name[2] = (int)KE_SNR;
24625 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24626 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024627 goto theend;
24628 }
24629
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024630 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024631 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024632 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024633 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24634 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24635 {
24636 /* When there was "s:" already or the name expanded to get a
24637 * leading "s:" then remove it. */
24638 lv.ll_name += 2;
24639 len -= 2;
24640 lead = 2;
24641 }
24642 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024643 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024644 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024645 /* skip over "s:" and "g:" */
24646 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024647 lv.ll_name += 2;
24648 len = (int)(end - lv.ll_name);
24649 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024650
24651 /*
24652 * Copy the function name to allocated memory.
24653 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24654 * Accept <SNR>123_name() outside a script.
24655 */
24656 if (skip)
24657 lead = 0; /* do nothing */
24658 else if (lead > 0)
24659 {
24660 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024661 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24662 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024663 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024664 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024665 if (current_SID <= 0)
24666 {
24667 EMSG(_(e_usingsid));
24668 goto theend;
24669 }
24670 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24671 lead += (int)STRLEN(sid_buf);
24672 }
24673 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024674 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024675 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024676 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024677 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024678 goto theend;
24679 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024680 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024681 {
24682 char_u *cp = vim_strchr(lv.ll_name, ':');
24683
24684 if (cp != NULL && cp < end)
24685 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024686 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024687 goto theend;
24688 }
24689 }
24690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024691 name = alloc((unsigned)(len + lead + 1));
24692 if (name != NULL)
24693 {
24694 if (lead > 0)
24695 {
24696 name[0] = K_SPECIAL;
24697 name[1] = KS_EXTRA;
24698 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024699 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024700 STRCPY(name + 3, sid_buf);
24701 }
24702 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024703 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024704 }
24705 *pp = end;
24706
24707theend:
24708 clear_lval(&lv);
24709 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710}
24711
24712/*
24713 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24714 * Return 2 if "p" starts with "s:".
24715 * Return 0 otherwise.
24716 */
24717 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024718eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024720 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24721 * the standard library function. */
24722 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24723 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724 return 5;
24725 if (p[0] == 's' && p[1] == ':')
24726 return 2;
24727 return 0;
24728}
24729
24730/*
24731 * Return TRUE if "p" starts with "<SID>" or "s:".
24732 * Only works if eval_fname_script() returned non-zero for "p"!
24733 */
24734 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024735eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736{
24737 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24738}
24739
24740/*
24741 * List the head of the function: "name(arg1, arg2)".
24742 */
24743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024744list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745{
24746 int j;
24747
24748 msg_start();
24749 if (indent)
24750 MSG_PUTS(" ");
24751 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024752 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753 {
24754 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024755 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 }
24757 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024758 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024759 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024760 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024761 {
24762 if (j)
24763 MSG_PUTS(", ");
24764 msg_puts(FUNCARG(fp, j));
24765 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024766 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767 {
24768 if (j)
24769 MSG_PUTS(", ");
24770 MSG_PUTS("...");
24771 }
24772 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024773 if (fp->uf_flags & FC_ABORT)
24774 MSG_PUTS(" abort");
24775 if (fp->uf_flags & FC_RANGE)
24776 MSG_PUTS(" range");
24777 if (fp->uf_flags & FC_DICT)
24778 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024779 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024780 if (p_verbose > 0)
24781 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782}
24783
24784/*
24785 * Find a function by name, return pointer to it in ufuncs.
24786 * Return NULL for unknown function.
24787 */
24788 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024789find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024791 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024793 hi = hash_find(&func_hashtab, name);
24794 if (!HASHITEM_EMPTY(hi))
24795 return HI2UF(hi);
24796 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797}
24798
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024799#if defined(EXITFREE) || defined(PROTO)
24800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024801free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024802{
24803 hashitem_T *hi;
24804
24805 /* Need to start all over every time, because func_free() may change the
24806 * hash table. */
24807 while (func_hashtab.ht_used > 0)
24808 for (hi = func_hashtab.ht_array; ; ++hi)
24809 if (!HASHITEM_EMPTY(hi))
24810 {
24811 func_free(HI2UF(hi));
24812 break;
24813 }
24814}
24815#endif
24816
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024818translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024819{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024820 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024821 return find_internal_func(name) >= 0;
24822 return find_func(name) != NULL;
24823}
24824
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024825/*
24826 * Return TRUE if a function "name" exists.
24827 */
24828 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024829function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024830{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024831 char_u *nm = name;
24832 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024833 int n = FALSE;
24834
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024835 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024836 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024837 nm = skipwhite(nm);
24838
24839 /* Only accept "funcname", "funcname ", "funcname (..." and
24840 * "funcname(...", not "funcname!...". */
24841 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024842 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024843 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024844 return n;
24845}
24846
Bram Moolenaara1544c02013-05-30 12:35:52 +020024847 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024848get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024849{
24850 char_u *nm = name;
24851 char_u *p;
24852
Bram Moolenaar65639032016-03-16 21:40:30 +010024853 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024854
24855 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024856 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024857 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024858
Bram Moolenaara1544c02013-05-30 12:35:52 +020024859 vim_free(p);
24860 return NULL;
24861}
24862
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024863/*
24864 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024865 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24866 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024867 */
24868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024869builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024870{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024871 char_u *p;
24872
24873 if (!ASCII_ISLOWER(name[0]))
24874 return FALSE;
24875 p = vim_strchr(name, AUTOLOAD_CHAR);
24876 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024877}
24878
Bram Moolenaar05159a02005-02-26 23:04:13 +000024879#if defined(FEAT_PROFILE) || defined(PROTO)
24880/*
24881 * Start profiling function "fp".
24882 */
24883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024884func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024885{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024886 int len = fp->uf_lines.ga_len;
24887
24888 if (len == 0)
24889 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024890 fp->uf_tm_count = 0;
24891 profile_zero(&fp->uf_tm_self);
24892 profile_zero(&fp->uf_tm_total);
24893 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024894 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024895 if (fp->uf_tml_total == NULL)
24896 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024897 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024898 if (fp->uf_tml_self == NULL)
24899 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024900 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024901 fp->uf_tml_idx = -1;
24902 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24903 || fp->uf_tml_self == NULL)
24904 return; /* out of memory */
24905
24906 fp->uf_profiling = TRUE;
24907}
24908
24909/*
24910 * Dump the profiling results for all functions in file "fd".
24911 */
24912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024913func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024914{
24915 hashitem_T *hi;
24916 int todo;
24917 ufunc_T *fp;
24918 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024919 ufunc_T **sorttab;
24920 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024921
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024922 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024923 if (todo == 0)
24924 return; /* nothing to dump */
24925
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024926 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024927
Bram Moolenaar05159a02005-02-26 23:04:13 +000024928 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24929 {
24930 if (!HASHITEM_EMPTY(hi))
24931 {
24932 --todo;
24933 fp = HI2UF(hi);
24934 if (fp->uf_profiling)
24935 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024936 if (sorttab != NULL)
24937 sorttab[st_len++] = fp;
24938
Bram Moolenaar05159a02005-02-26 23:04:13 +000024939 if (fp->uf_name[0] == K_SPECIAL)
24940 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24941 else
24942 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24943 if (fp->uf_tm_count == 1)
24944 fprintf(fd, "Called 1 time\n");
24945 else
24946 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24947 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24948 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24949 fprintf(fd, "\n");
24950 fprintf(fd, "count total (s) self (s)\n");
24951
24952 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24953 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024954 if (FUNCLINE(fp, i) == NULL)
24955 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024956 prof_func_line(fd, fp->uf_tml_count[i],
24957 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024958 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24959 }
24960 fprintf(fd, "\n");
24961 }
24962 }
24963 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024964
24965 if (sorttab != NULL && st_len > 0)
24966 {
24967 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24968 prof_total_cmp);
24969 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24970 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24971 prof_self_cmp);
24972 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24973 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024974
24975 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976}
Bram Moolenaar73830342005-02-28 22:48:19 +000024977
24978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024979prof_sort_list(
24980 FILE *fd,
24981 ufunc_T **sorttab,
24982 int st_len,
24983 char *title,
24984 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024985{
24986 int i;
24987 ufunc_T *fp;
24988
24989 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24990 fprintf(fd, "count total (s) self (s) function\n");
24991 for (i = 0; i < 20 && i < st_len; ++i)
24992 {
24993 fp = sorttab[i];
24994 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24995 prefer_self);
24996 if (fp->uf_name[0] == K_SPECIAL)
24997 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24998 else
24999 fprintf(fd, " %s()\n", fp->uf_name);
25000 }
25001 fprintf(fd, "\n");
25002}
25003
25004/*
25005 * Print the count and times for one function or function line.
25006 */
25007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025008prof_func_line(
25009 FILE *fd,
25010 int count,
25011 proftime_T *total,
25012 proftime_T *self,
25013 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025014{
25015 if (count > 0)
25016 {
25017 fprintf(fd, "%5d ", count);
25018 if (prefer_self && profile_equal(total, self))
25019 fprintf(fd, " ");
25020 else
25021 fprintf(fd, "%s ", profile_msg(total));
25022 if (!prefer_self && profile_equal(total, self))
25023 fprintf(fd, " ");
25024 else
25025 fprintf(fd, "%s ", profile_msg(self));
25026 }
25027 else
25028 fprintf(fd, " ");
25029}
25030
25031/*
25032 * Compare function for total time sorting.
25033 */
25034 static int
25035#ifdef __BORLANDC__
25036_RTLENTRYF
25037#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025038prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025039{
25040 ufunc_T *p1, *p2;
25041
25042 p1 = *(ufunc_T **)s1;
25043 p2 = *(ufunc_T **)s2;
25044 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25045}
25046
25047/*
25048 * Compare function for self time sorting.
25049 */
25050 static int
25051#ifdef __BORLANDC__
25052_RTLENTRYF
25053#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025054prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025055{
25056 ufunc_T *p1, *p2;
25057
25058 p1 = *(ufunc_T **)s1;
25059 p2 = *(ufunc_T **)s2;
25060 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25061}
25062
Bram Moolenaar05159a02005-02-26 23:04:13 +000025063#endif
25064
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025065/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025066 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025067 * Return TRUE if a package was loaded.
25068 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025070script_autoload(
25071 char_u *name,
25072 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025073{
25074 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025075 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025076 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025077 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025078
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025079 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025080 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025081 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025082 return FALSE;
25083
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025084 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025085
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025086 /* Find the name in the list of previously loaded package names. Skip
25087 * "autoload/", it's always the same. */
25088 for (i = 0; i < ga_loaded.ga_len; ++i)
25089 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25090 break;
25091 if (!reload && i < ga_loaded.ga_len)
25092 ret = FALSE; /* was loaded already */
25093 else
25094 {
25095 /* Remember the name if it wasn't loaded already. */
25096 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25097 {
25098 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25099 tofree = NULL;
25100 }
25101
25102 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025103 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025104 ret = TRUE;
25105 }
25106
25107 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025108 return ret;
25109}
25110
25111/*
25112 * Return the autoload script name for a function or variable name.
25113 * Returns NULL when out of memory.
25114 */
25115 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025116autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025117{
25118 char_u *p;
25119 char_u *scriptname;
25120
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025121 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025122 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25123 if (scriptname == NULL)
25124 return FALSE;
25125 STRCPY(scriptname, "autoload/");
25126 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025127 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025128 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025129 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025130 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025131 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025132}
25133
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25135
25136/*
25137 * Function given to ExpandGeneric() to obtain the list of user defined
25138 * function names.
25139 */
25140 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025141get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025143 static long_u done;
25144 static hashitem_T *hi;
25145 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146
25147 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025149 done = 0;
25150 hi = func_hashtab.ht_array;
25151 }
25152 if (done < func_hashtab.ht_used)
25153 {
25154 if (done++ > 0)
25155 ++hi;
25156 while (HASHITEM_EMPTY(hi))
25157 ++hi;
25158 fp = HI2UF(hi);
25159
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025160 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025161 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025162
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025163 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25164 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165
25166 cat_func_name(IObuff, fp);
25167 if (xp->xp_context != EXPAND_USER_FUNC)
25168 {
25169 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025170 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 STRCAT(IObuff, ")");
25172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025173 return IObuff;
25174 }
25175 return NULL;
25176}
25177
25178#endif /* FEAT_CMDL_COMPL */
25179
25180/*
25181 * Copy the function name of "fp" to buffer "buf".
25182 * "buf" must be able to hold the function name plus three bytes.
25183 * Takes care of script-local function names.
25184 */
25185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025186cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025188 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 {
25190 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025191 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192 }
25193 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025194 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195}
25196
25197/*
25198 * ":delfunction {name}"
25199 */
25200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025201ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025203 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 char_u *p;
25205 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025206 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207
25208 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025209 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025210 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025212 {
25213 if (fudi.fd_dict != NULL && !eap->skip)
25214 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217 if (!ends_excmd(*skipwhite(p)))
25218 {
25219 vim_free(name);
25220 EMSG(_(e_trailing));
25221 return;
25222 }
25223 eap->nextcmd = check_nextcmd(p);
25224 if (eap->nextcmd != NULL)
25225 *p = NUL;
25226
25227 if (!eap->skip)
25228 fp = find_func(name);
25229 vim_free(name);
25230
25231 if (!eap->skip)
25232 {
25233 if (fp == NULL)
25234 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025235 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 return;
25237 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025238 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 {
25240 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25241 return;
25242 }
25243
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025244 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025246 /* Delete the dict item that refers to the function, it will
25247 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025248 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025250 else
25251 func_free(fp);
25252 }
25253}
25254
25255/*
25256 * Free a function and remove it from the list of functions.
25257 */
25258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025259func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025260{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025261 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025262
25263 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025264 ga_clear_strings(&(fp->uf_args));
25265 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025266#ifdef FEAT_PROFILE
25267 vim_free(fp->uf_tml_count);
25268 vim_free(fp->uf_tml_total);
25269 vim_free(fp->uf_tml_self);
25270#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025271
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025272 /* remove the function from the function hashtable */
25273 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25274 if (HASHITEM_EMPTY(hi))
25275 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025276 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025277 hash_remove(&func_hashtab, hi);
25278
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025279 vim_free(fp);
25280}
25281
25282/*
25283 * Unreference a Function: decrement the reference count and free it when it
25284 * becomes zero. Only for numbered functions.
25285 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025287func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025288{
25289 ufunc_T *fp;
25290
25291 if (name != NULL && isdigit(*name))
25292 {
25293 fp = find_func(name);
25294 if (fp == NULL)
25295 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025296 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025297 {
25298 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025299 * when "uf_calls" becomes zero. */
25300 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025301 func_free(fp);
25302 }
25303 }
25304}
25305
25306/*
25307 * Count a reference to a Function.
25308 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025310func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025311{
25312 ufunc_T *fp;
25313
25314 if (name != NULL && isdigit(*name))
25315 {
25316 fp = find_func(name);
25317 if (fp == NULL)
25318 EMSG2(_(e_intern2), "func_ref()");
25319 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025320 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321 }
25322}
25323
25324/*
25325 * Call a user function.
25326 */
25327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025328call_user_func(
25329 ufunc_T *fp, /* pointer to function */
25330 int argcount, /* nr of args */
25331 typval_T *argvars, /* arguments */
25332 typval_T *rettv, /* return value */
25333 linenr_T firstline, /* first line of range */
25334 linenr_T lastline, /* last line of range */
25335 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336{
Bram Moolenaar33570922005-01-25 22:26:29 +000025337 char_u *save_sourcing_name;
25338 linenr_T save_sourcing_lnum;
25339 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025340 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 int save_did_emsg;
25342 static int depth = 0;
25343 dictitem_T *v;
25344 int fixvar_idx = 0; /* index in fixvar[] */
25345 int i;
25346 int ai;
25347 char_u numbuf[NUMBUFLEN];
25348 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025349 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025350#ifdef FEAT_PROFILE
25351 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025352 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354
25355 /* If depth of calling is getting too high, don't execute the function */
25356 if (depth >= p_mfd)
25357 {
25358 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025359 rettv->v_type = VAR_NUMBER;
25360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 return;
25362 }
25363 ++depth;
25364
25365 line_breakcheck(); /* check for CTRL-C hit */
25366
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025367 fc = (funccall_T *)alloc(sizeof(funccall_T));
25368 fc->caller = current_funccal;
25369 current_funccal = fc;
25370 fc->func = fp;
25371 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025372 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025373 fc->linenr = 0;
25374 fc->returned = FALSE;
25375 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025377 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25378 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379
Bram Moolenaar33570922005-01-25 22:26:29 +000025380 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025381 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025382 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25383 * each argument variable and saves a lot of time.
25384 */
25385 /*
25386 * Init l: variables.
25387 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025388 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025389 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025390 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025391 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25392 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025393 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025394 name = v->di_key;
25395 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025396 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025397 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025398 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025399 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025400 v->di_tv.vval.v_dict = selfdict;
25401 ++selfdict->dv_refcount;
25402 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025403
Bram Moolenaar33570922005-01-25 22:26:29 +000025404 /*
25405 * Init a: variables.
25406 * Set a:0 to "argcount".
25407 * Set a:000 to a list with room for the "..." arguments.
25408 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025409 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025410 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025411 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025412 /* Use "name" to avoid a warning from some compiler that checks the
25413 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025414 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025415 name = v->di_key;
25416 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025417 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025418 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025419 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025420 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025421 v->di_tv.vval.v_list = &fc->l_varlist;
25422 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25423 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25424 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025425
25426 /*
25427 * Set a:firstline to "firstline" and a:lastline to "lastline".
25428 * Set a:name to named arguments.
25429 * Set a:N to the "..." arguments.
25430 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025431 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025432 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025433 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025434 (varnumber_T)lastline);
25435 for (i = 0; i < argcount; ++i)
25436 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025437 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025438 if (ai < 0)
25439 /* named argument a:name */
25440 name = FUNCARG(fp, i);
25441 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025442 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025443 /* "..." argument a:1, a:2, etc. */
25444 sprintf((char *)numbuf, "%d", ai + 1);
25445 name = numbuf;
25446 }
25447 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25448 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025449 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025450 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25451 }
25452 else
25453 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025454 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25455 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 if (v == NULL)
25457 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025458 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025459 }
25460 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025461 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025462
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025463 /* Note: the values are copied directly to avoid alloc/free.
25464 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025465 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025466 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025467
25468 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25469 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025470 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25471 fc->l_listitems[ai].li_tv = argvars[i];
25472 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025473 }
25474 }
25475
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 /* Don't redraw while executing the function. */
25477 ++RedrawingDisabled;
25478 save_sourcing_name = sourcing_name;
25479 save_sourcing_lnum = sourcing_lnum;
25480 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025481 /* need space for function name + ("function " + 3) or "[number]" */
25482 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25483 + STRLEN(fp->uf_name) + 20;
25484 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 if (sourcing_name != NULL)
25486 {
25487 if (save_sourcing_name != NULL
25488 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025489 sprintf((char *)sourcing_name, "%s[%d]..",
25490 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 else
25492 STRCPY(sourcing_name, "function ");
25493 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25494
25495 if (p_verbose >= 12)
25496 {
25497 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025498 verbose_enter_scroll();
25499
Bram Moolenaar555b2802005-05-19 21:08:39 +000025500 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 if (p_verbose >= 14)
25502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025504 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025505 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025506 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507
25508 msg_puts((char_u *)"(");
25509 for (i = 0; i < argcount; ++i)
25510 {
25511 if (i > 0)
25512 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025513 if (argvars[i].v_type == VAR_NUMBER)
25514 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515 else
25516 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025517 /* Do not want errors such as E724 here. */
25518 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025519 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025520 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025521 if (s != NULL)
25522 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025523 if (vim_strsize(s) > MSG_BUF_CLEN)
25524 {
25525 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25526 s = buf;
25527 }
25528 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025529 vim_free(tofree);
25530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 }
25532 }
25533 msg_puts((char_u *)")");
25534 }
25535 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025536
25537 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 --no_wait_return;
25539 }
25540 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025541#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025542 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025543 {
25544 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25545 func_do_profile(fp);
25546 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025547 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025548 {
25549 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025550 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025551 profile_zero(&fp->uf_tm_children);
25552 }
25553 script_prof_save(&wait_start);
25554 }
25555#endif
25556
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025558 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 save_did_emsg = did_emsg;
25560 did_emsg = FALSE;
25561
25562 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025563 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25565
25566 --RedrawingDisabled;
25567
25568 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025569 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025571 clear_tv(rettv);
25572 rettv->v_type = VAR_NUMBER;
25573 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 }
25575
Bram Moolenaar05159a02005-02-26 23:04:13 +000025576#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025577 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025578 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025579 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025580 profile_end(&call_start);
25581 profile_sub_wait(&wait_start, &call_start);
25582 profile_add(&fp->uf_tm_total, &call_start);
25583 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025584 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025585 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025586 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25587 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025588 }
25589 }
25590#endif
25591
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 /* when being verbose, mention the return value */
25593 if (p_verbose >= 12)
25594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025596 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025599 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025600 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025601 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025602 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025603 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025605 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025606 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025607 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025608 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025609
Bram Moolenaar555b2802005-05-19 21:08:39 +000025610 /* The value may be very long. Skip the middle part, so that we
25611 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025612 * truncate it at the end. Don't want errors such as E724 here. */
25613 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025614 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025615 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025616 if (s != NULL)
25617 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025618 if (vim_strsize(s) > MSG_BUF_CLEN)
25619 {
25620 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25621 s = buf;
25622 }
25623 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025624 vim_free(tofree);
25625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 }
25627 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025628
25629 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025630 --no_wait_return;
25631 }
25632
25633 vim_free(sourcing_name);
25634 sourcing_name = save_sourcing_name;
25635 sourcing_lnum = save_sourcing_lnum;
25636 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025637#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025638 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025639 script_prof_restore(&wait_start);
25640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641
25642 if (p_verbose >= 12 && sourcing_name != NULL)
25643 {
25644 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025645 verbose_enter_scroll();
25646
Bram Moolenaar555b2802005-05-19 21:08:39 +000025647 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025649
25650 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651 --no_wait_return;
25652 }
25653
25654 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025655 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025657
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025658 /* If the a:000 list and the l: and a: dicts are not referenced we can
25659 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025660 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25661 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25662 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25663 {
25664 free_funccal(fc, FALSE);
25665 }
25666 else
25667 {
25668 hashitem_T *hi;
25669 listitem_T *li;
25670 int todo;
25671
25672 /* "fc" is still in use. This can happen when returning "a:000" or
25673 * assigning "l:" to a global variable.
25674 * Link "fc" in the list for garbage collection later. */
25675 fc->caller = previous_funccal;
25676 previous_funccal = fc;
25677
25678 /* Make a copy of the a: variables, since we didn't do that above. */
25679 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25680 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25681 {
25682 if (!HASHITEM_EMPTY(hi))
25683 {
25684 --todo;
25685 v = HI2DI(hi);
25686 copy_tv(&v->di_tv, &v->di_tv);
25687 }
25688 }
25689
25690 /* Make a copy of the a:000 items, since we didn't do that above. */
25691 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25692 copy_tv(&li->li_tv, &li->li_tv);
25693 }
25694}
25695
25696/*
25697 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025698 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025699 */
25700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025701can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025702{
25703 return (fc->l_varlist.lv_copyID != copyID
25704 && fc->l_vars.dv_copyID != copyID
25705 && fc->l_avars.dv_copyID != copyID);
25706}
25707
25708/*
25709 * Free "fc" and what it contains.
25710 */
25711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025712free_funccal(
25713 funccall_T *fc,
25714 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025715{
25716 listitem_T *li;
25717
25718 /* The a: variables typevals may not have been allocated, only free the
25719 * allocated variables. */
25720 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25721
25722 /* free all l: variables */
25723 vars_clear(&fc->l_vars.dv_hashtab);
25724
25725 /* Free the a:000 variables if they were allocated. */
25726 if (free_val)
25727 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25728 clear_tv(&li->li_tv);
25729
25730 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731}
25732
25733/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025734 * Add a number variable "name" to dict "dp" with value "nr".
25735 */
25736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025737add_nr_var(
25738 dict_T *dp,
25739 dictitem_T *v,
25740 char *name,
25741 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025742{
25743 STRCPY(v->di_key, name);
25744 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25745 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25746 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025747 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025748 v->di_tv.vval.v_number = nr;
25749}
25750
25751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 * ":return [expr]"
25753 */
25754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025755ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756{
25757 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025758 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 int returning = FALSE;
25760
25761 if (current_funccal == NULL)
25762 {
25763 EMSG(_("E133: :return not inside a function"));
25764 return;
25765 }
25766
25767 if (eap->skip)
25768 ++emsg_skip;
25769
25770 eap->nextcmd = NULL;
25771 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025772 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 {
25774 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025775 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025777 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 }
25779 /* It's safer to return also on error. */
25780 else if (!eap->skip)
25781 {
25782 /*
25783 * Return unless the expression evaluation has been cancelled due to an
25784 * aborting error, an interrupt, or an exception.
25785 */
25786 if (!aborting())
25787 returning = do_return(eap, FALSE, TRUE, NULL);
25788 }
25789
25790 /* When skipping or the return gets pending, advance to the next command
25791 * in this line (!returning). Otherwise, ignore the rest of the line.
25792 * Following lines will be ignored by get_func_line(). */
25793 if (returning)
25794 eap->nextcmd = NULL;
25795 else if (eap->nextcmd == NULL) /* no argument */
25796 eap->nextcmd = check_nextcmd(arg);
25797
25798 if (eap->skip)
25799 --emsg_skip;
25800}
25801
25802/*
25803 * Return from a function. Possibly makes the return pending. Also called
25804 * for a pending return at the ":endtry" or after returning from an extra
25805 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025806 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025807 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808 * FALSE when the return gets pending.
25809 */
25810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025811do_return(
25812 exarg_T *eap,
25813 int reanimate,
25814 int is_cmd,
25815 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816{
25817 int idx;
25818 struct condstack *cstack = eap->cstack;
25819
25820 if (reanimate)
25821 /* Undo the return. */
25822 current_funccal->returned = FALSE;
25823
25824 /*
25825 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25826 * not in its finally clause (which then is to be executed next) is found.
25827 * In this case, make the ":return" pending for execution at the ":endtry".
25828 * Otherwise, return normally.
25829 */
25830 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25831 if (idx >= 0)
25832 {
25833 cstack->cs_pending[idx] = CSTP_RETURN;
25834
25835 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025836 /* A pending return again gets pending. "rettv" points to an
25837 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025839 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025840 else
25841 {
25842 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025843 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025845 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025847 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 {
25849 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025850 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025851 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 else
25853 EMSG(_(e_outofmem));
25854 }
25855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025856 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857
25858 if (reanimate)
25859 {
25860 /* The pending return value could be overwritten by a ":return"
25861 * without argument in a finally clause; reset the default
25862 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025863 current_funccal->rettv->v_type = VAR_NUMBER;
25864 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 }
25866 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025867 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868 }
25869 else
25870 {
25871 current_funccal->returned = TRUE;
25872
25873 /* If the return is carried out now, store the return value. For
25874 * a return immediately after reanimation, the value is already
25875 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025876 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025878 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025879 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025881 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882 }
25883 }
25884
25885 return idx < 0;
25886}
25887
25888/*
25889 * Free the variable with a pending return value.
25890 */
25891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025892discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893{
Bram Moolenaar33570922005-01-25 22:26:29 +000025894 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895}
25896
25897/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025898 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 * is an allocated string. Used by report_pending() for verbose messages.
25900 */
25901 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025902get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025904 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025905 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025906 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025908 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025909 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025910 if (s == NULL)
25911 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025912
25913 STRCPY(IObuff, ":return ");
25914 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25915 if (STRLEN(s) + 8 >= IOSIZE)
25916 STRCPY(IObuff + IOSIZE - 4, "...");
25917 vim_free(tofree);
25918 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919}
25920
25921/*
25922 * Get next function line.
25923 * Called by do_cmdline() to get the next line.
25924 * Returns allocated string, or NULL for end of function.
25925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025927get_func_line(
25928 int c UNUSED,
25929 void *cookie,
25930 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931{
Bram Moolenaar33570922005-01-25 22:26:29 +000025932 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025933 ufunc_T *fp = fcp->func;
25934 char_u *retval;
25935 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936
25937 /* If breakpoints have been added/deleted need to check for it. */
25938 if (fcp->dbg_tick != debug_tick)
25939 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025940 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941 sourcing_lnum);
25942 fcp->dbg_tick = debug_tick;
25943 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025944#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025945 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025946 func_line_end(cookie);
25947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948
Bram Moolenaar05159a02005-02-26 23:04:13 +000025949 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025950 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25951 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 retval = NULL;
25953 else
25954 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025955 /* Skip NULL lines (continuation lines). */
25956 while (fcp->linenr < gap->ga_len
25957 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25958 ++fcp->linenr;
25959 if (fcp->linenr >= gap->ga_len)
25960 retval = NULL;
25961 else
25962 {
25963 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25964 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025965#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025966 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025967 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025968#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970 }
25971
25972 /* Did we encounter a breakpoint? */
25973 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25974 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025975 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025977 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978 sourcing_lnum);
25979 fcp->dbg_tick = debug_tick;
25980 }
25981
25982 return retval;
25983}
25984
Bram Moolenaar05159a02005-02-26 23:04:13 +000025985#if defined(FEAT_PROFILE) || defined(PROTO)
25986/*
25987 * Called when starting to read a function line.
25988 * "sourcing_lnum" must be correct!
25989 * When skipping lines it may not actually be executed, but we won't find out
25990 * until later and we need to store the time now.
25991 */
25992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025993func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025994{
25995 funccall_T *fcp = (funccall_T *)cookie;
25996 ufunc_T *fp = fcp->func;
25997
25998 if (fp->uf_profiling && sourcing_lnum >= 1
25999 && sourcing_lnum <= fp->uf_lines.ga_len)
26000 {
26001 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026002 /* Skip continuation lines. */
26003 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26004 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026005 fp->uf_tml_execed = FALSE;
26006 profile_start(&fp->uf_tml_start);
26007 profile_zero(&fp->uf_tml_children);
26008 profile_get_wait(&fp->uf_tml_wait);
26009 }
26010}
26011
26012/*
26013 * Called when actually executing a function line.
26014 */
26015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026016func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026017{
26018 funccall_T *fcp = (funccall_T *)cookie;
26019 ufunc_T *fp = fcp->func;
26020
26021 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26022 fp->uf_tml_execed = TRUE;
26023}
26024
26025/*
26026 * Called when done with a function line.
26027 */
26028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026029func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026030{
26031 funccall_T *fcp = (funccall_T *)cookie;
26032 ufunc_T *fp = fcp->func;
26033
26034 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26035 {
26036 if (fp->uf_tml_execed)
26037 {
26038 ++fp->uf_tml_count[fp->uf_tml_idx];
26039 profile_end(&fp->uf_tml_start);
26040 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026041 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026042 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26043 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026044 }
26045 fp->uf_tml_idx = -1;
26046 }
26047}
26048#endif
26049
Bram Moolenaar071d4272004-06-13 20:20:40 +000026050/*
26051 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026052 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026053 */
26054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026055func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056{
Bram Moolenaar33570922005-01-25 22:26:29 +000026057 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058
26059 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26060 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026061 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026062 || fcp->returned);
26063}
26064
26065/*
26066 * return TRUE if cookie indicates a function which "abort"s on errors.
26067 */
26068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026069func_has_abort(
26070 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026072 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073}
26074
26075#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26076typedef enum
26077{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026078 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26079 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26080 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081} var_flavour_T;
26082
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026083static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084
26085 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026086var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087{
26088 char_u *p = varname;
26089
26090 if (ASCII_ISUPPER(*p))
26091 {
26092 while (*(++p))
26093 if (ASCII_ISLOWER(*p))
26094 return VAR_FLAVOUR_SESSION;
26095 return VAR_FLAVOUR_VIMINFO;
26096 }
26097 else
26098 return VAR_FLAVOUR_DEFAULT;
26099}
26100#endif
26101
26102#if defined(FEAT_VIMINFO) || defined(PROTO)
26103/*
26104 * Restore global vars that start with a capital from the viminfo file
26105 */
26106 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026107read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108{
26109 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026110 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026111 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026112 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026113
26114 if (!writing && (find_viminfo_parameter('!') != NULL))
26115 {
26116 tab = vim_strchr(virp->vir_line + 1, '\t');
26117 if (tab != NULL)
26118 {
26119 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026120 switch (*tab)
26121 {
26122 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026123#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026124 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026125#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026126 case 'D': type = VAR_DICT; break;
26127 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026128 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130
26131 tab = vim_strchr(tab, '\t');
26132 if (tab != NULL)
26133 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026134 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026135 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026136 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026138#ifdef FEAT_FLOAT
26139 else if (type == VAR_FLOAT)
26140 (void)string2float(tab + 1, &tv.vval.v_float);
26141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026143 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026144 if (type == VAR_DICT || type == VAR_LIST)
26145 {
26146 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26147
26148 if (etv == NULL)
26149 /* Failed to parse back the dict or list, use it as a
26150 * string. */
26151 tv.v_type = VAR_STRING;
26152 else
26153 {
26154 vim_free(tv.vval.v_string);
26155 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026156 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026157 }
26158 }
26159
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026160 /* when in a function use global variables */
26161 save_funccal = current_funccal;
26162 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026163 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026164 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026165
26166 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026167 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026168 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26169 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170 }
26171 }
26172 }
26173
26174 return viminfo_readline(virp);
26175}
26176
26177/*
26178 * Write global vars that start with a capital to the viminfo file
26179 */
26180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026181write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182{
Bram Moolenaar33570922005-01-25 22:26:29 +000026183 hashitem_T *hi;
26184 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026185 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026186 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026187 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026188 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026189 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190
26191 if (find_viminfo_parameter('!') == NULL)
26192 return;
26193
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026194 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026196 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026197 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026199 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026201 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026202 this_var = HI2DI(hi);
26203 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026204 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026205 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026206 {
26207 case VAR_STRING: s = "STR"; break;
26208 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026209 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026210 case VAR_DICT: s = "DIC"; break;
26211 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026212 case VAR_SPECIAL: s = "XPL"; break;
26213
26214 case VAR_UNKNOWN:
26215 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026216 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026217 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026218 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026219 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026220 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026221 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026222 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026223 if (p != NULL)
26224 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026225 vim_free(tofree);
26226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227 }
26228 }
26229}
26230#endif
26231
26232#if defined(FEAT_SESSION) || defined(PROTO)
26233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026234store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235{
Bram Moolenaar33570922005-01-25 22:26:29 +000026236 hashitem_T *hi;
26237 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026238 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026239 char_u *p, *t;
26240
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026241 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026242 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026244 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026246 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026247 this_var = HI2DI(hi);
26248 if ((this_var->di_tv.v_type == VAR_NUMBER
26249 || this_var->di_tv.v_type == VAR_STRING)
26250 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026251 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026252 /* Escape special characters with a backslash. Turn a LF and
26253 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026254 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026255 (char_u *)"\\\"\n\r");
26256 if (p == NULL) /* out of memory */
26257 break;
26258 for (t = p; *t != NUL; ++t)
26259 if (*t == '\n')
26260 *t = 'n';
26261 else if (*t == '\r')
26262 *t = 'r';
26263 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026264 this_var->di_key,
26265 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26266 : ' ',
26267 p,
26268 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26269 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026270 || put_eol(fd) == FAIL)
26271 {
26272 vim_free(p);
26273 return FAIL;
26274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 vim_free(p);
26276 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026277#ifdef FEAT_FLOAT
26278 else if (this_var->di_tv.v_type == VAR_FLOAT
26279 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26280 {
26281 float_T f = this_var->di_tv.vval.v_float;
26282 int sign = ' ';
26283
26284 if (f < 0)
26285 {
26286 f = -f;
26287 sign = '-';
26288 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026289 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026290 this_var->di_key, sign, f) < 0)
26291 || put_eol(fd) == FAIL)
26292 return FAIL;
26293 }
26294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 }
26296 }
26297 return OK;
26298}
26299#endif
26300
Bram Moolenaar661b1822005-07-28 22:36:45 +000026301/*
26302 * Display script name where an item was last set.
26303 * Should only be invoked when 'verbose' is non-zero.
26304 */
26305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026306last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026307{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026308 char_u *p;
26309
Bram Moolenaar661b1822005-07-28 22:36:45 +000026310 if (scriptID != 0)
26311 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026312 p = home_replace_save(NULL, get_scriptname(scriptID));
26313 if (p != NULL)
26314 {
26315 verbose_enter();
26316 MSG_PUTS(_("\n\tLast set from "));
26317 MSG_PUTS(p);
26318 vim_free(p);
26319 verbose_leave();
26320 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026321 }
26322}
26323
Bram Moolenaard812df62008-11-09 12:46:09 +000026324/*
26325 * List v:oldfiles in a nice way.
26326 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026328ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026329{
26330 list_T *l = vimvars[VV_OLDFILES].vv_list;
26331 listitem_T *li;
26332 int nr = 0;
26333
26334 if (l == NULL)
26335 msg((char_u *)_("No old files"));
26336 else
26337 {
26338 msg_start();
26339 msg_scroll = TRUE;
26340 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26341 {
26342 msg_outnum((long)++nr);
26343 MSG_PUTS(": ");
26344 msg_outtrans(get_tv_string(&li->li_tv));
26345 msg_putchar('\n');
26346 out_flush(); /* output one line at a time */
26347 ui_breakcheck();
26348 }
26349 /* Assume "got_int" was set to truncate the listing. */
26350 got_int = FALSE;
26351
26352#ifdef FEAT_BROWSE_CMD
26353 if (cmdmod.browse)
26354 {
26355 quit_more = FALSE;
26356 nr = prompt_for_number(FALSE);
26357 msg_starthere();
26358 if (nr > 0)
26359 {
26360 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26361 (long)nr);
26362
26363 if (p != NULL)
26364 {
26365 p = expand_env_save(p);
26366 eap->arg = p;
26367 eap->cmdidx = CMD_edit;
26368 cmdmod.browse = FALSE;
26369 do_exedit(eap, NULL);
26370 vim_free(p);
26371 }
26372 }
26373 }
26374#endif
26375 }
26376}
26377
Bram Moolenaar53744302015-07-17 17:38:22 +020026378/* reset v:option_new, v:option_old and v:option_type */
26379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026380reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026381{
26382 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26383 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26384 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26385}
26386
26387
Bram Moolenaar071d4272004-06-13 20:20:40 +000026388#endif /* FEAT_EVAL */
26389
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026391#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026392
26393#ifdef WIN3264
26394/*
26395 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26396 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026397static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26398static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26399static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400
26401/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026402 * Get the short path (8.3) for the filename in "fnamep".
26403 * Only works for a valid file name.
26404 * When the path gets longer "fnamep" is changed and the allocated buffer
26405 * is put in "bufp".
26406 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26407 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408 */
26409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026410get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026412 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026413 char_u *newbuf;
26414
26415 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026416 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417 if (l > len - 1)
26418 {
26419 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026420 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 newbuf = vim_strnsave(*fnamep, l);
26422 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026423 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424
26425 vim_free(*bufp);
26426 *fnamep = *bufp = newbuf;
26427
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026428 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026429 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430 }
26431
26432 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434}
26435
26436/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026437 * Get the short path (8.3) for the filename in "fname". The converted
26438 * path is returned in "bufp".
26439 *
26440 * Some of the directories specified in "fname" may not exist. This function
26441 * will shorten the existing directories at the beginning of the path and then
26442 * append the remaining non-existing path.
26443 *
26444 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026445 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026446 * bufp - Pointer to an allocated buffer for the filename.
26447 * fnamelen - Length of the filename pointed to by fname
26448 *
26449 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026450 */
26451 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026452shortpath_for_invalid_fname(
26453 char_u **fname,
26454 char_u **bufp,
26455 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026457 char_u *short_fname, *save_fname, *pbuf_unused;
26458 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026459 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026460 int old_len, len;
26461 int new_len, sfx_len;
26462 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026463
26464 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026465 old_len = *fnamelen;
26466 save_fname = vim_strnsave(*fname, old_len);
26467 pbuf_unused = NULL;
26468 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026470 endp = save_fname + old_len - 1; /* Find the end of the copy */
26471 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026472
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026473 /*
26474 * Try shortening the supplied path till it succeeds by removing one
26475 * directory at a time from the tail of the path.
26476 */
26477 len = 0;
26478 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026480 /* go back one path-separator */
26481 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26482 --endp;
26483 if (endp <= save_fname)
26484 break; /* processed the complete path */
26485
26486 /*
26487 * Replace the path separator with a NUL and try to shorten the
26488 * resulting path.
26489 */
26490 ch = *endp;
26491 *endp = 0;
26492 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026493 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026494 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26495 {
26496 retval = FAIL;
26497 goto theend;
26498 }
26499 *endp = ch; /* preserve the string */
26500
26501 if (len > 0)
26502 break; /* successfully shortened the path */
26503
26504 /* failed to shorten the path. Skip the path separator */
26505 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026506 }
26507
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026508 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026510 /*
26511 * Succeeded in shortening the path. Now concatenate the shortened
26512 * path with the remaining path at the tail.
26513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026514
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026515 /* Compute the length of the new path. */
26516 sfx_len = (int)(save_endp - endp) + 1;
26517 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026519 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026520 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026521 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026523 /* There is not enough space in the currently allocated string,
26524 * copy it to a buffer big enough. */
26525 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026527 {
26528 retval = FAIL;
26529 goto theend;
26530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026531 }
26532 else
26533 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026534 /* Transfer short_fname to the main buffer (it's big enough),
26535 * unless get_short_pathname() did its work in-place. */
26536 *fname = *bufp = save_fname;
26537 if (short_fname != save_fname)
26538 vim_strncpy(save_fname, short_fname, len);
26539 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026541
26542 /* concat the not-shortened part of the path */
26543 vim_strncpy(*fname + len, endp, sfx_len);
26544 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026546
26547theend:
26548 vim_free(pbuf_unused);
26549 vim_free(save_fname);
26550
26551 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026552}
26553
26554/*
26555 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026557 */
26558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026559shortpath_for_partial(
26560 char_u **fnamep,
26561 char_u **bufp,
26562 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563{
26564 int sepcount, len, tflen;
26565 char_u *p;
26566 char_u *pbuf, *tfname;
26567 int hasTilde;
26568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026569 /* Count up the path separators from the RHS.. so we know which part
26570 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026571 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026572 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026573 if (vim_ispathsep(*p))
26574 ++sepcount;
26575
26576 /* Need full path first (use expand_env() to remove a "~/") */
26577 hasTilde = (**fnamep == '~');
26578 if (hasTilde)
26579 pbuf = tfname = expand_env_save(*fnamep);
26580 else
26581 pbuf = tfname = FullName_save(*fnamep, FALSE);
26582
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026583 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026584
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026585 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026587
26588 if (len == 0)
26589 {
26590 /* Don't have a valid filename, so shorten the rest of the
26591 * path if we can. This CAN give us invalid 8.3 filenames, but
26592 * there's not a lot of point in guessing what it might be.
26593 */
26594 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026595 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026597 }
26598
26599 /* Count the paths backward to find the beginning of the desired string. */
26600 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026601 {
26602#ifdef FEAT_MBYTE
26603 if (has_mbyte)
26604 p -= mb_head_off(tfname, p);
26605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026606 if (vim_ispathsep(*p))
26607 {
26608 if (sepcount == 0 || (hasTilde && sepcount == 1))
26609 break;
26610 else
26611 sepcount --;
26612 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026614 if (hasTilde)
26615 {
26616 --p;
26617 if (p >= tfname)
26618 *p = '~';
26619 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026620 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026621 }
26622 else
26623 ++p;
26624
26625 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26626 vim_free(*bufp);
26627 *fnamelen = (int)STRLEN(p);
26628 *bufp = pbuf;
26629 *fnamep = p;
26630
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026631 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026632}
26633#endif /* WIN3264 */
26634
26635/*
26636 * Adjust a filename, according to a string of modifiers.
26637 * *fnamep must be NUL terminated when called. When returning, the length is
26638 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026639 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026640 * When there is an error, *fnamep is set to NULL.
26641 */
26642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026643modify_fname(
26644 char_u *src, /* string with modifiers */
26645 int *usedlen, /* characters after src that are used */
26646 char_u **fnamep, /* file name so far */
26647 char_u **bufp, /* buffer for allocated file name or NULL */
26648 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026649{
26650 int valid = 0;
26651 char_u *tail;
26652 char_u *s, *p, *pbuf;
26653 char_u dirname[MAXPATHL];
26654 int c;
26655 int has_fullname = 0;
26656#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026657 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026658 int has_shortname = 0;
26659#endif
26660
26661repeat:
26662 /* ":p" - full path/file_name */
26663 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26664 {
26665 has_fullname = 1;
26666
26667 valid |= VALID_PATH;
26668 *usedlen += 2;
26669
26670 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26671 if ((*fnamep)[0] == '~'
26672#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26673 && ((*fnamep)[1] == '/'
26674# ifdef BACKSLASH_IN_FILENAME
26675 || (*fnamep)[1] == '\\'
26676# endif
26677 || (*fnamep)[1] == NUL)
26678
26679#endif
26680 )
26681 {
26682 *fnamep = expand_env_save(*fnamep);
26683 vim_free(*bufp); /* free any allocated file name */
26684 *bufp = *fnamep;
26685 if (*fnamep == NULL)
26686 return -1;
26687 }
26688
26689 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026690 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026691 {
26692 if (vim_ispathsep(*p)
26693 && p[1] == '.'
26694 && (p[2] == NUL
26695 || vim_ispathsep(p[2])
26696 || (p[2] == '.'
26697 && (p[3] == NUL || vim_ispathsep(p[3])))))
26698 break;
26699 }
26700
26701 /* FullName_save() is slow, don't use it when not needed. */
26702 if (*p != NUL || !vim_isAbsName(*fnamep))
26703 {
26704 *fnamep = FullName_save(*fnamep, *p != NUL);
26705 vim_free(*bufp); /* free any allocated file name */
26706 *bufp = *fnamep;
26707 if (*fnamep == NULL)
26708 return -1;
26709 }
26710
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026711#ifdef WIN3264
26712# if _WIN32_WINNT >= 0x0500
26713 if (vim_strchr(*fnamep, '~') != NULL)
26714 {
26715 /* Expand 8.3 filename to full path. Needed to make sure the same
26716 * file does not have two different names.
26717 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26718 p = alloc(_MAX_PATH + 1);
26719 if (p != NULL)
26720 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026721 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026722 {
26723 vim_free(*bufp);
26724 *bufp = *fnamep = p;
26725 }
26726 else
26727 vim_free(p);
26728 }
26729 }
26730# endif
26731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026732 /* Append a path separator to a directory. */
26733 if (mch_isdir(*fnamep))
26734 {
26735 /* Make room for one or two extra characters. */
26736 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26737 vim_free(*bufp); /* free any allocated file name */
26738 *bufp = *fnamep;
26739 if (*fnamep == NULL)
26740 return -1;
26741 add_pathsep(*fnamep);
26742 }
26743 }
26744
26745 /* ":." - path relative to the current directory */
26746 /* ":~" - path relative to the home directory */
26747 /* ":8" - shortname path - postponed till after */
26748 while (src[*usedlen] == ':'
26749 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26750 {
26751 *usedlen += 2;
26752 if (c == '8')
26753 {
26754#ifdef WIN3264
26755 has_shortname = 1; /* Postpone this. */
26756#endif
26757 continue;
26758 }
26759 pbuf = NULL;
26760 /* Need full path first (use expand_env() to remove a "~/") */
26761 if (!has_fullname)
26762 {
26763 if (c == '.' && **fnamep == '~')
26764 p = pbuf = expand_env_save(*fnamep);
26765 else
26766 p = pbuf = FullName_save(*fnamep, FALSE);
26767 }
26768 else
26769 p = *fnamep;
26770
26771 has_fullname = 0;
26772
26773 if (p != NULL)
26774 {
26775 if (c == '.')
26776 {
26777 mch_dirname(dirname, MAXPATHL);
26778 s = shorten_fname(p, dirname);
26779 if (s != NULL)
26780 {
26781 *fnamep = s;
26782 if (pbuf != NULL)
26783 {
26784 vim_free(*bufp); /* free any allocated file name */
26785 *bufp = pbuf;
26786 pbuf = NULL;
26787 }
26788 }
26789 }
26790 else
26791 {
26792 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26793 /* Only replace it when it starts with '~' */
26794 if (*dirname == '~')
26795 {
26796 s = vim_strsave(dirname);
26797 if (s != NULL)
26798 {
26799 *fnamep = s;
26800 vim_free(*bufp);
26801 *bufp = s;
26802 }
26803 }
26804 }
26805 vim_free(pbuf);
26806 }
26807 }
26808
26809 tail = gettail(*fnamep);
26810 *fnamelen = (int)STRLEN(*fnamep);
26811
26812 /* ":h" - head, remove "/file_name", can be repeated */
26813 /* Don't remove the first "/" or "c:\" */
26814 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26815 {
26816 valid |= VALID_HEAD;
26817 *usedlen += 2;
26818 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026819 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026820 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026821 *fnamelen = (int)(tail - *fnamep);
26822#ifdef VMS
26823 if (*fnamelen > 0)
26824 *fnamelen += 1; /* the path separator is part of the path */
26825#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026826 if (*fnamelen == 0)
26827 {
26828 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26829 p = vim_strsave((char_u *)".");
26830 if (p == NULL)
26831 return -1;
26832 vim_free(*bufp);
26833 *bufp = *fnamep = tail = p;
26834 *fnamelen = 1;
26835 }
26836 else
26837 {
26838 while (tail > s && !after_pathsep(s, tail))
26839 mb_ptr_back(*fnamep, tail);
26840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841 }
26842
26843 /* ":8" - shortname */
26844 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26845 {
26846 *usedlen += 2;
26847#ifdef WIN3264
26848 has_shortname = 1;
26849#endif
26850 }
26851
26852#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026853 /*
26854 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026855 */
26856 if (has_shortname)
26857 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026858 /* Copy the string if it is shortened by :h and when it wasn't copied
26859 * yet, because we are going to change it in place. Avoids changing
26860 * the buffer name for "%:8". */
26861 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026862 {
26863 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026864 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026865 return -1;
26866 vim_free(*bufp);
26867 *bufp = *fnamep = p;
26868 }
26869
26870 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026871 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026872 if (!has_fullname && !vim_isAbsName(*fnamep))
26873 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026874 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026875 return -1;
26876 }
26877 else
26878 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026879 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026880
Bram Moolenaardc935552011-08-17 15:23:23 +020026881 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026882 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026883 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026884 return -1;
26885
26886 if (l == 0)
26887 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026888 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026889 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026890 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026891 return -1;
26892 }
26893 *fnamelen = l;
26894 }
26895 }
26896#endif /* WIN3264 */
26897
26898 /* ":t" - tail, just the basename */
26899 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26900 {
26901 *usedlen += 2;
26902 *fnamelen -= (int)(tail - *fnamep);
26903 *fnamep = tail;
26904 }
26905
26906 /* ":e" - extension, can be repeated */
26907 /* ":r" - root, without extension, can be repeated */
26908 while (src[*usedlen] == ':'
26909 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26910 {
26911 /* find a '.' in the tail:
26912 * - for second :e: before the current fname
26913 * - otherwise: The last '.'
26914 */
26915 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26916 s = *fnamep - 2;
26917 else
26918 s = *fnamep + *fnamelen - 1;
26919 for ( ; s > tail; --s)
26920 if (s[0] == '.')
26921 break;
26922 if (src[*usedlen + 1] == 'e') /* :e */
26923 {
26924 if (s > tail)
26925 {
26926 *fnamelen += (int)(*fnamep - (s + 1));
26927 *fnamep = s + 1;
26928#ifdef VMS
26929 /* cut version from the extension */
26930 s = *fnamep + *fnamelen - 1;
26931 for ( ; s > *fnamep; --s)
26932 if (s[0] == ';')
26933 break;
26934 if (s > *fnamep)
26935 *fnamelen = s - *fnamep;
26936#endif
26937 }
26938 else if (*fnamep <= tail)
26939 *fnamelen = 0;
26940 }
26941 else /* :r */
26942 {
26943 if (s > tail) /* remove one extension */
26944 *fnamelen = (int)(s - *fnamep);
26945 }
26946 *usedlen += 2;
26947 }
26948
26949 /* ":s?pat?foo?" - substitute */
26950 /* ":gs?pat?foo?" - global substitute */
26951 if (src[*usedlen] == ':'
26952 && (src[*usedlen + 1] == 's'
26953 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26954 {
26955 char_u *str;
26956 char_u *pat;
26957 char_u *sub;
26958 int sep;
26959 char_u *flags;
26960 int didit = FALSE;
26961
26962 flags = (char_u *)"";
26963 s = src + *usedlen + 2;
26964 if (src[*usedlen + 1] == 'g')
26965 {
26966 flags = (char_u *)"g";
26967 ++s;
26968 }
26969
26970 sep = *s++;
26971 if (sep)
26972 {
26973 /* find end of pattern */
26974 p = vim_strchr(s, sep);
26975 if (p != NULL)
26976 {
26977 pat = vim_strnsave(s, (int)(p - s));
26978 if (pat != NULL)
26979 {
26980 s = p + 1;
26981 /* find end of substitution */
26982 p = vim_strchr(s, sep);
26983 if (p != NULL)
26984 {
26985 sub = vim_strnsave(s, (int)(p - s));
26986 str = vim_strnsave(*fnamep, *fnamelen);
26987 if (sub != NULL && str != NULL)
26988 {
26989 *usedlen = (int)(p + 1 - src);
26990 s = do_string_sub(str, pat, sub, flags);
26991 if (s != NULL)
26992 {
26993 *fnamep = s;
26994 *fnamelen = (int)STRLEN(s);
26995 vim_free(*bufp);
26996 *bufp = s;
26997 didit = TRUE;
26998 }
26999 }
27000 vim_free(sub);
27001 vim_free(str);
27002 }
27003 vim_free(pat);
27004 }
27005 }
27006 /* after using ":s", repeat all the modifiers */
27007 if (didit)
27008 goto repeat;
27009 }
27010 }
27011
Bram Moolenaar26df0922014-02-23 23:39:13 +010027012 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27013 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027014 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027015 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027016 if (c != NUL)
27017 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027018 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027019 if (c != NUL)
27020 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027021 if (p == NULL)
27022 return -1;
27023 vim_free(*bufp);
27024 *bufp = *fnamep = p;
27025 *fnamelen = (int)STRLEN(p);
27026 *usedlen += 2;
27027 }
27028
Bram Moolenaar071d4272004-06-13 20:20:40 +000027029 return valid;
27030}
27031
27032/*
27033 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27034 * "flags" can be "g" to do a global substitute.
27035 * Returns an allocated string, NULL for error.
27036 */
27037 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027038do_string_sub(
27039 char_u *str,
27040 char_u *pat,
27041 char_u *sub,
27042 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027043{
27044 int sublen;
27045 regmatch_T regmatch;
27046 int i;
27047 int do_all;
27048 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027049 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027050 garray_T ga;
27051 char_u *ret;
27052 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027053 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027054
27055 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27056 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027057 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027058
27059 ga_init2(&ga, 1, 200);
27060
27061 do_all = (flags[0] == 'g');
27062
27063 regmatch.rm_ic = p_ic;
27064 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27065 if (regmatch.regprog != NULL)
27066 {
27067 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027068 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027069 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27070 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027071 /* Skip empty match except for first match. */
27072 if (regmatch.startp[0] == regmatch.endp[0])
27073 {
27074 if (zero_width == regmatch.startp[0])
27075 {
27076 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027077 i = MB_PTR2LEN(tail);
27078 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27079 (size_t)i);
27080 ga.ga_len += i;
27081 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027082 continue;
27083 }
27084 zero_width = regmatch.startp[0];
27085 }
27086
Bram Moolenaar071d4272004-06-13 20:20:40 +000027087 /*
27088 * Get some space for a temporary buffer to do the substitution
27089 * into. It will contain:
27090 * - The text up to where the match is.
27091 * - The substituted text.
27092 * - The text after the match.
27093 */
27094 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027095 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027096 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27097 {
27098 ga_clear(&ga);
27099 break;
27100 }
27101
27102 /* copy the text up to where the match is */
27103 i = (int)(regmatch.startp[0] - tail);
27104 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27105 /* add the substituted text */
27106 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27107 + ga.ga_len + i, TRUE, TRUE, FALSE);
27108 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027109 tail = regmatch.endp[0];
27110 if (*tail == NUL)
27111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027112 if (!do_all)
27113 break;
27114 }
27115
27116 if (ga.ga_data != NULL)
27117 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27118
Bram Moolenaar473de612013-06-08 18:19:48 +020027119 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027120 }
27121
27122 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27123 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027124 if (p_cpo == empty_option)
27125 p_cpo = save_cpo;
27126 else
27127 /* Darn, evaluating {sub} expression changed the value. */
27128 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027129
27130 return ret;
27131}
27132
27133#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */