blob: 445b6b91c45cee7ced93cfe33d1014afb1fb13f1 [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},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static 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 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200558static void f_evalcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_eventhandler(typval_T *argvars, typval_T *rettv);
560static void f_executable(typval_T *argvars, typval_T *rettv);
561static void f_exepath(typval_T *argvars, typval_T *rettv);
562static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_expand(typval_T *argvars, typval_T *rettv);
567static void f_extend(typval_T *argvars, typval_T *rettv);
568static void f_feedkeys(typval_T *argvars, typval_T *rettv);
569static void f_filereadable(typval_T *argvars, typval_T *rettv);
570static void f_filewritable(typval_T *argvars, typval_T *rettv);
571static void f_filter(typval_T *argvars, typval_T *rettv);
572static void f_finddir(typval_T *argvars, typval_T *rettv);
573static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_float2nr(typval_T *argvars, typval_T *rettv);
576static void f_floor(typval_T *argvars, typval_T *rettv);
577static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000578#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100579static void f_fnameescape(typval_T *argvars, typval_T *rettv);
580static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
581static void f_foldclosed(typval_T *argvars, typval_T *rettv);
582static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
583static void f_foldlevel(typval_T *argvars, typval_T *rettv);
584static void f_foldtext(typval_T *argvars, typval_T *rettv);
585static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
586static void f_foreground(typval_T *argvars, typval_T *rettv);
587static void f_function(typval_T *argvars, typval_T *rettv);
588static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
589static void f_get(typval_T *argvars, typval_T *rettv);
590static void f_getbufline(typval_T *argvars, typval_T *rettv);
591static void f_getbufvar(typval_T *argvars, typval_T *rettv);
592static void f_getchar(typval_T *argvars, typval_T *rettv);
593static void f_getcharmod(typval_T *argvars, typval_T *rettv);
594static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
595static void f_getcmdline(typval_T *argvars, typval_T *rettv);
596static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
597static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
598static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
599static void f_getcwd(typval_T *argvars, typval_T *rettv);
600static void f_getfontname(typval_T *argvars, typval_T *rettv);
601static void f_getfperm(typval_T *argvars, typval_T *rettv);
602static void f_getfsize(typval_T *argvars, typval_T *rettv);
603static void f_getftime(typval_T *argvars, typval_T *rettv);
604static void f_getftype(typval_T *argvars, typval_T *rettv);
605static void f_getline(typval_T *argvars, typval_T *rettv);
606static void f_getmatches(typval_T *argvars, typval_T *rettv);
607static void f_getpid(typval_T *argvars, typval_T *rettv);
608static void f_getcurpos(typval_T *argvars, typval_T *rettv);
609static void f_getpos(typval_T *argvars, typval_T *rettv);
610static void f_getqflist(typval_T *argvars, typval_T *rettv);
611static void f_getreg(typval_T *argvars, typval_T *rettv);
612static void f_getregtype(typval_T *argvars, typval_T *rettv);
613static void f_gettabvar(typval_T *argvars, typval_T *rettv);
614static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
615static void f_getwinposx(typval_T *argvars, typval_T *rettv);
616static void f_getwinposy(typval_T *argvars, typval_T *rettv);
617static void f_getwinvar(typval_T *argvars, typval_T *rettv);
618static void f_glob(typval_T *argvars, typval_T *rettv);
619static void f_globpath(typval_T *argvars, typval_T *rettv);
620static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
621static void f_has(typval_T *argvars, typval_T *rettv);
622static void f_has_key(typval_T *argvars, typval_T *rettv);
623static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
624static void f_hasmapto(typval_T *argvars, typval_T *rettv);
625static void f_histadd(typval_T *argvars, typval_T *rettv);
626static void f_histdel(typval_T *argvars, typval_T *rettv);
627static void f_histget(typval_T *argvars, typval_T *rettv);
628static void f_histnr(typval_T *argvars, typval_T *rettv);
629static void f_hlID(typval_T *argvars, typval_T *rettv);
630static void f_hlexists(typval_T *argvars, typval_T *rettv);
631static void f_hostname(typval_T *argvars, typval_T *rettv);
632static void f_iconv(typval_T *argvars, typval_T *rettv);
633static void f_indent(typval_T *argvars, typval_T *rettv);
634static void f_index(typval_T *argvars, typval_T *rettv);
635static void f_input(typval_T *argvars, typval_T *rettv);
636static void f_inputdialog(typval_T *argvars, typval_T *rettv);
637static void f_inputlist(typval_T *argvars, typval_T *rettv);
638static void f_inputrestore(typval_T *argvars, typval_T *rettv);
639static void f_inputsave(typval_T *argvars, typval_T *rettv);
640static void f_inputsecret(typval_T *argvars, typval_T *rettv);
641static void f_insert(typval_T *argvars, typval_T *rettv);
642static void f_invert(typval_T *argvars, typval_T *rettv);
643static void f_isdirectory(typval_T *argvars, typval_T *rettv);
644static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100645#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
646static void f_isnan(typval_T *argvars, typval_T *rettv);
647#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100650static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100651static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100652static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100653static void f_job_start(typval_T *argvars, typval_T *rettv);
654static void f_job_stop(typval_T *argvars, typval_T *rettv);
655static void f_job_status(typval_T *argvars, typval_T *rettv);
656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100658static void f_js_decode(typval_T *argvars, typval_T *rettv);
659static void f_js_encode(typval_T *argvars, typval_T *rettv);
660static void f_json_decode(typval_T *argvars, typval_T *rettv);
661static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_keys(typval_T *argvars, typval_T *rettv);
663static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
664static void f_len(typval_T *argvars, typval_T *rettv);
665static void f_libcall(typval_T *argvars, typval_T *rettv);
666static void f_libcallnr(typval_T *argvars, typval_T *rettv);
667static void f_line(typval_T *argvars, typval_T *rettv);
668static void f_line2byte(typval_T *argvars, typval_T *rettv);
669static void f_lispindent(typval_T *argvars, typval_T *rettv);
670static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_log(typval_T *argvars, typval_T *rettv);
673static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_map(typval_T *argvars, typval_T *rettv);
679static void f_maparg(typval_T *argvars, typval_T *rettv);
680static void f_mapcheck(typval_T *argvars, typval_T *rettv);
681static void f_match(typval_T *argvars, typval_T *rettv);
682static void f_matchadd(typval_T *argvars, typval_T *rettv);
683static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
684static void f_matcharg(typval_T *argvars, typval_T *rettv);
685static void f_matchdelete(typval_T *argvars, typval_T *rettv);
686static void f_matchend(typval_T *argvars, typval_T *rettv);
687static void f_matchlist(typval_T *argvars, typval_T *rettv);
688static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200689static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_max(typval_T *argvars, typval_T *rettv);
691static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100698#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
700static void f_nr2char(typval_T *argvars, typval_T *rettv);
701static void f_or(typval_T *argvars, typval_T *rettv);
702static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100705#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000708#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
710static void f_printf(typval_T *argvars, typval_T *rettv);
711static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200714#endif
715#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_range(typval_T *argvars, typval_T *rettv);
719static void f_readfile(typval_T *argvars, typval_T *rettv);
720static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100721#ifdef FEAT_FLOAT
722static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_reltimestr(typval_T *argvars, typval_T *rettv);
725static void f_remote_expr(typval_T *argvars, typval_T *rettv);
726static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
727static void f_remote_peek(typval_T *argvars, typval_T *rettv);
728static void f_remote_read(typval_T *argvars, typval_T *rettv);
729static void f_remote_send(typval_T *argvars, typval_T *rettv);
730static void f_remove(typval_T *argvars, typval_T *rettv);
731static void f_rename(typval_T *argvars, typval_T *rettv);
732static void f_repeat(typval_T *argvars, typval_T *rettv);
733static void f_resolve(typval_T *argvars, typval_T *rettv);
734static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_screenattr(typval_T *argvars, typval_T *rettv);
739static void f_screenchar(typval_T *argvars, typval_T *rettv);
740static void f_screencol(typval_T *argvars, typval_T *rettv);
741static void f_screenrow(typval_T *argvars, typval_T *rettv);
742static void f_search(typval_T *argvars, typval_T *rettv);
743static void f_searchdecl(typval_T *argvars, typval_T *rettv);
744static void f_searchpair(typval_T *argvars, typval_T *rettv);
745static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
746static void f_searchpos(typval_T *argvars, typval_T *rettv);
747static void f_server2client(typval_T *argvars, typval_T *rettv);
748static void f_serverlist(typval_T *argvars, typval_T *rettv);
749static void f_setbufvar(typval_T *argvars, typval_T *rettv);
750static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
751static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100752static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_setline(typval_T *argvars, typval_T *rettv);
754static void f_setloclist(typval_T *argvars, typval_T *rettv);
755static void f_setmatches(typval_T *argvars, typval_T *rettv);
756static void f_setpos(typval_T *argvars, typval_T *rettv);
757static void f_setqflist(typval_T *argvars, typval_T *rettv);
758static void f_setreg(typval_T *argvars, typval_T *rettv);
759static void f_settabvar(typval_T *argvars, typval_T *rettv);
760static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
761static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100764#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_shellescape(typval_T *argvars, typval_T *rettv);
766static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
767static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sin(typval_T *argvars, typval_T *rettv);
770static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sort(typval_T *argvars, typval_T *rettv);
773static void f_soundfold(typval_T *argvars, typval_T *rettv);
774static void f_spellbadword(typval_T *argvars, typval_T *rettv);
775static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
776static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_sqrt(typval_T *argvars, typval_T *rettv);
779static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_str2nr(typval_T *argvars, typval_T *rettv);
782static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000785#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_stridx(typval_T *argvars, typval_T *rettv);
788static void f_string(typval_T *argvars, typval_T *rettv);
789static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200790static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_strpart(typval_T *argvars, typval_T *rettv);
792static void f_strridx(typval_T *argvars, typval_T *rettv);
793static void f_strtrans(typval_T *argvars, typval_T *rettv);
794static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
795static void f_strwidth(typval_T *argvars, typval_T *rettv);
796static void f_submatch(typval_T *argvars, typval_T *rettv);
797static void f_substitute(typval_T *argvars, typval_T *rettv);
798static void f_synID(typval_T *argvars, typval_T *rettv);
799static void f_synIDattr(typval_T *argvars, typval_T *rettv);
800static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
801static void f_synstack(typval_T *argvars, typval_T *rettv);
802static void f_synconcealed(typval_T *argvars, typval_T *rettv);
803static void f_system(typval_T *argvars, typval_T *rettv);
804static void f_systemlist(typval_T *argvars, typval_T *rettv);
805static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
806static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
807static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
808static void f_taglist(typval_T *argvars, typval_T *rettv);
809static void f_tagfiles(typval_T *argvars, typval_T *rettv);
810static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200811static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
812static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200813static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
814#ifdef FEAT_JOB_CHANNEL
815static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
816#endif
817static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
818#ifdef FEAT_JOB_CHANNEL
819static void f_test_null_job(typval_T *argvars, typval_T *rettv);
820#endif
821static void f_test_null_list(typval_T *argvars, typval_T *rettv);
822static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
823static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200824static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200825#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static void f_tan(typval_T *argvars, typval_T *rettv);
827static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200828#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100829#ifdef FEAT_TIMERS
830static void f_timer_start(typval_T *argvars, typval_T *rettv);
831static void f_timer_stop(typval_T *argvars, typval_T *rettv);
832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_tolower(typval_T *argvars, typval_T *rettv);
834static void f_toupper(typval_T *argvars, typval_T *rettv);
835static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000836#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static void f_type(typval_T *argvars, typval_T *rettv);
840static void f_undofile(typval_T *argvars, typval_T *rettv);
841static void f_undotree(typval_T *argvars, typval_T *rettv);
842static void f_uniq(typval_T *argvars, typval_T *rettv);
843static void f_values(typval_T *argvars, typval_T *rettv);
844static void f_virtcol(typval_T *argvars, typval_T *rettv);
845static void f_visualmode(typval_T *argvars, typval_T *rettv);
846static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100847static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100848static void f_win_getid(typval_T *argvars, typval_T *rettv);
849static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
850static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
851static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void f_winbufnr(typval_T *argvars, typval_T *rettv);
853static void f_wincol(typval_T *argvars, typval_T *rettv);
854static void f_winheight(typval_T *argvars, typval_T *rettv);
855static void f_winline(typval_T *argvars, typval_T *rettv);
856static void f_winnr(typval_T *argvars, typval_T *rettv);
857static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
858static void f_winrestview(typval_T *argvars, typval_T *rettv);
859static void f_winsaveview(typval_T *argvars, typval_T *rettv);
860static void f_winwidth(typval_T *argvars, typval_T *rettv);
861static void f_writefile(typval_T *argvars, typval_T *rettv);
862static void f_wordcount(typval_T *argvars, typval_T *rettv);
863static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
866static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
867static int get_env_len(char_u **arg);
868static int get_id_len(char_u **arg);
869static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
870static 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 +0000871#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
872#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
873 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
875static int eval_isnamec(int c);
876static int eval_isnamec1(int c);
877static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
878static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static typval_T *alloc_string_tv(char_u *string);
880static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100881#ifdef FEAT_FLOAT
882static float_T get_tv_float(typval_T *varp);
883#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static linenr_T get_tv_lnum(typval_T *argvars);
885static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
887static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
888static hashtab_T *find_var_ht(char_u *name, char_u **varname);
889static funccall_T *get_funccal(void);
890static void vars_clear_ext(hashtab_T *ht, int free_val);
891static void delete_var(hashtab_T *ht, hashitem_T *hi);
892static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
893static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
894static void set_var(char_u *name, typval_T *varp, int copy);
895static int var_check_ro(int flags, char_u *name, int use_gettext);
896static int var_check_fixed(int flags, char_u *name, int use_gettext);
897static int var_check_func_name(char_u *name, int new_var);
898static int valid_varname(char_u *varname);
899static int tv_check_lock(int lock, char_u *name, int use_gettext);
900static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
901static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100902static 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 +0100903static int eval_fname_script(char_u *p);
904static int eval_fname_sid(char_u *p);
905static void list_func_head(ufunc_T *fp, int indent);
906static ufunc_T *find_func(char_u *name);
907static int function_exists(char_u *name);
908static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100910static void func_do_profile(ufunc_T *fp);
911static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
912static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000913static int
914# ifdef __BORLANDC__
915 _RTLENTRYF
916# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000918static int
919# ifdef __BORLANDC__
920 _RTLENTRYF
921# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100922 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100924static int script_autoload(char_u *name, int reload);
925static char_u *autoload_name(char_u *name);
926static void cat_func_name(char_u *buf, ufunc_T *fp);
927static void func_free(ufunc_T *fp);
928static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
929static int can_free_funccal(funccall_T *fc, int copyID) ;
930static void free_funccal(funccall_T *fc, int free_val);
931static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
932static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
933static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
934static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
935static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
936static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
937static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
938static int write_list(FILE *fd, list_T *list, int binary);
939static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200941
942#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100943static int compare_func_name(const void *s1, const void *s2);
944static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200945#endif
946
Bram Moolenaar33570922005-01-25 22:26:29 +0000947/*
948 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000949 */
950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100951eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 int i;
954 struct vimvar *p;
955
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200956 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
957 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200958 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000959 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000961
962 for (i = 0; i < VV_LEN; ++i)
963 {
964 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200965 if (STRLEN(p->vv_name) > 16)
966 {
967 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
968 getout(1);
969 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 STRCPY(p->vv_di.di_key, p->vv_name);
971 if (p->vv_flags & VV_RO)
972 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
973 else if (p->vv_flags & VV_RO_SBX)
974 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
975 else
976 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000977
978 /* add to v: scope dict, unless the value is not always available */
979 if (p->vv_type != VAR_UNKNOWN)
980 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000982 /* add to compat scope dict */
983 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000984 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100985 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
986
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000987 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100988 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200989 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100990 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100991
992 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
993 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
994 set_vim_var_nr(VV_NONE, VVAL_NONE);
995 set_vim_var_nr(VV_NULL, VVAL_NULL);
996
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200997 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200998
999#ifdef EBCDIC
1000 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001001 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001002 */
1003 sortFunctions();
1004#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001005}
1006
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001007#if defined(EXITFREE) || defined(PROTO)
1008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001009eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010{
1011 int i;
1012 struct vimvar *p;
1013
1014 for (i = 0; i < VV_LEN; ++i)
1015 {
1016 p = &vimvars[i];
1017 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001018 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001019 vim_free(p->vv_str);
1020 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001021 }
1022 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1023 {
1024 list_unref(p->vv_list);
1025 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001027 }
1028 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001029 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030 hash_clear(&compat_hashtab);
1031
Bram Moolenaard9fba312005-06-26 22:34:35 +00001032 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001034 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001035# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001036
1037 /* global variables */
1038 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001039
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001040 /* autoloaded script names */
1041 ga_clear_strings(&ga_loaded);
1042
Bram Moolenaarcca74132013-09-25 21:00:28 +02001043 /* Script-local variables. First clear all the variables and in a second
1044 * loop free the scriptvar_T, because a variable in one script might hold
1045 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001047 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001048 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001049 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 ga_clear(&ga_scripts);
1051
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001052 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001053 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001054
1055 /* functions */
1056 free_all_functions();
1057 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001058}
1059#endif
1060
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * Return the name of the executed function.
1063 */
1064 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
1071 * Return the address holding the next breakpoint line for a funccall cookie.
1072 */
1073 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
1079/*
1080 * Return the address holding the debug tick for a funccall cookie.
1081 */
1082 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
Bram Moolenaar33570922005-01-25 22:26:29 +00001085 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086}
1087
1088/*
1089 * Return the nesting level for a funccall cookie.
1090 */
1091 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar33570922005-01-25 22:26:29 +00001094 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095}
1096
1097/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001098funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001100/* pointer to list of previously used funccal, still around because some
1101 * item in it is still being used. */
1102funccall_T *previous_funccal = NULL;
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Return TRUE when a function was ended by a ":return" command.
1106 */
1107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001108current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 return current_funccal->returned;
1111}
1112
1113
1114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * Set an internal variable to a string value. Creates the variable if it does
1116 * not already exist.
1117 */
1118 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001119set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001121 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 val = vim_strsave(value);
1125 if (val != NULL)
1126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001128 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001130 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134}
1135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001137#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139static char_u *redir_endp = NULL;
1140static char_u *redir_varname = NULL;
1141
1142/*
1143 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001144 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 * Returns OK if successfully completed the setup. FAIL otherwise.
1146 */
1147 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001148var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149{
1150 int save_emsg;
1151 int err;
1152 typval_T tv;
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001155 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 {
1157 EMSG(_(e_invarg));
1158 return FAIL;
1159 }
1160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 redir_varname = vim_strsave(name);
1163 if (redir_varname == NULL)
1164 return FAIL;
1165
1166 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1167 if (redir_lval == NULL)
1168 {
1169 var_redir_stop();
1170 return FAIL;
1171 }
1172
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 /* The output is stored in growarray "redir_ga" until redirection ends. */
1174 ga_init2(&redir_ga, (int)sizeof(char), 500);
1175
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001177 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001178 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1180 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 if (redir_endp != NULL && *redir_endp != NUL)
1183 /* Trailing characters are present after the variable name */
1184 EMSG(_(e_trailing));
1185 else
1186 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 var_redir_stop();
1189 return FAIL;
1190 }
1191
1192 /* check if we can write to the variable: set it to or append an empty
1193 * string */
1194 save_emsg = did_emsg;
1195 did_emsg = FALSE;
1196 tv.v_type = VAR_STRING;
1197 tv.vval.v_string = (char_u *)"";
1198 if (append)
1199 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1200 else
1201 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001202 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001204 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 if (err)
1206 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 var_redir_stop();
1209 return FAIL;
1210 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211
1212 return OK;
1213}
1214
1215/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 * Append "value[value_len]" to the variable set by var_redir_start().
1217 * The actual appending is postponed until redirection ends, because the value
1218 * appended may in fact be the string we write to, changing it may cause freed
1219 * memory to be used:
1220 * :redir => foo
1221 * :let foo
1222 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223 */
1224 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001225var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001227 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228
1229 if (redir_lval == NULL)
1230 return;
1231
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001235 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001238 {
1239 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001240 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001241 }
1242 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001244}
1245
1246/*
1247 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001248 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 */
1250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001253 typval_T tv;
1254
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001255 if (EVALCMD_BUSY)
1256 {
1257 redir_lval = NULL;
1258 return;
1259 }
1260
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001261 if (redir_lval != NULL)
1262 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 /* If there was no error: assign the text to the variable. */
1264 if (redir_endp != NULL)
1265 {
1266 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1267 tv.v_type = VAR_STRING;
1268 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001269 /* Call get_lval() again, if it's inside a Dict or List it may
1270 * have changed. */
1271 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001272 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001273 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1274 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1275 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001276 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001277
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001278 /* free the collected output */
1279 vim_free(redir_ga.ga_data);
1280 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001282 vim_free(redir_lval);
1283 redir_lval = NULL;
1284 }
1285 vim_free(redir_varname);
1286 redir_varname = NULL;
1287}
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289# if defined(FEAT_MBYTE) || defined(PROTO)
1290 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001291eval_charconvert(
1292 char_u *enc_from,
1293 char_u *enc_to,
1294 char_u *fname_from,
1295 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1300 set_vim_var_string(VV_CC_TO, enc_to, -1);
1301 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1302 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1303 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1304 err = TRUE;
1305 set_vim_var_string(VV_CC_FROM, NULL, -1);
1306 set_vim_var_string(VV_CC_TO, NULL, -1);
1307 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309
1310 if (err)
1311 return FAIL;
1312 return OK;
1313}
1314# endif
1315
1316# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err = FALSE;
1321
1322 set_vim_var_string(VV_FNAME_IN, fname, -1);
1323 set_vim_var_string(VV_CMDARG, args, -1);
1324 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1325 err = TRUE;
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_CMDARG, NULL, -1);
1328
1329 if (err)
1330 {
1331 mch_remove(fname);
1332 return FAIL;
1333 }
1334 return OK;
1335}
1336# endif
1337
1338# if defined(FEAT_DIFF) || defined(PROTO)
1339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001340eval_diff(
1341 char_u *origfile,
1342 char_u *newfile,
1343 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 int err = FALSE;
1346
1347 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1348 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1349 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1350 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1352 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1354}
1355
1356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001357eval_patch(
1358 char_u *origfile,
1359 char_u *difffile,
1360 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 int err;
1363
1364 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1365 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1366 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1367 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1369 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1370 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1371}
1372# endif
1373
1374/*
1375 * Top level evaluation function, returning a boolean.
1376 * Sets "error" to TRUE if there was an error.
1377 * Return TRUE or FALSE.
1378 */
1379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380eval_to_bool(
1381 char_u *arg,
1382 int *error,
1383 char_u **nextcmd,
1384 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001387 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 if (skip)
1390 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 else
1394 {
1395 *error = FALSE;
1396 if (!skip)
1397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001398 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 }
1402 if (skip)
1403 --emsg_skip;
1404
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001405 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408/*
1409 * Top level evaluation function, returning a string. If "skip" is TRUE,
1410 * only parsing to "nextcmd" is done, without reporting errors. Return
1411 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1412 */
1413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414eval_to_string_skip(
1415 char_u *arg,
1416 char_u **nextcmd,
1417 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 char_u *retval;
1421
1422 if (skip)
1423 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = NULL;
1426 else
1427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 retval = vim_strsave(get_tv_string(&tv));
1429 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 if (skip)
1432 --emsg_skip;
1433
1434 return retval;
1435}
1436
1437/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001438 * Skip over an expression at "*pp".
1439 * Return FAIL for an error, OK otherwise.
1440 */
1441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001442skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001443{
Bram Moolenaar33570922005-01-25 22:26:29 +00001444 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001445
1446 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001448}
1449
1450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001452 * When "convert" is TRUE convert a List into a sequence of lines and convert
1453 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 * Return pointer to allocated memory, or NULL for failure.
1455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
Bram Moolenaar33570922005-01-25 22:26:29 +00001462 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001465#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 retval = NULL;
1471 else
1472 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001473 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 {
1475 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001477 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001478 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001479 if (tv.vval.v_list->lv_len > 0)
1480 ga_append(&ga, NL);
1481 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 ga_append(&ga, NUL);
1483 retval = (char_u *)ga.ga_data;
1484 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001485#ifdef FEAT_FLOAT
1486 else if (convert && tv.v_type == VAR_FLOAT)
1487 {
1488 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1489 retval = vim_strsave(numbuf);
1490 }
1491#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001492 else
1493 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496
1497 return retval;
1498}
1499
1500/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 * Call eval_to_string() without using current local variables and using
1502 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
1504 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505eval_to_string_safe(
1506 char_u *arg,
1507 char_u **nextcmd,
1508 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 char_u *retval;
1511 void *save_funccalp;
1512
1513 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001514 if (use_sandbox)
1515 ++sandbox;
1516 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001517 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001518 if (use_sandbox)
1519 --sandbox;
1520 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 restore_funccal(save_funccalp);
1522 return retval;
1523}
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525/*
1526 * Top level evaluation function, returning a number.
1527 * Evaluates "expr" silently.
1528 * Returns -1 for an error.
1529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001530 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001531eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar33570922005-01-25 22:26:29 +00001533 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001535 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
1537 ++emsg_off;
1538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 retval = -1;
1541 else
1542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 --emsg_off;
1547
1548 return retval;
1549}
1550
Bram Moolenaara40058a2005-07-11 22:42:07 +00001551/*
1552 * Prepare v: variable "idx" to be used.
1553 * Save the current typeval in "save_tv".
1554 * When not used yet add the variable to the v: hashtable.
1555 */
1556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001558{
1559 *save_tv = vimvars[idx].vv_tv;
1560 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1562}
1563
1564/*
1565 * Restore v: variable "idx" to typeval "save_tv".
1566 * When no longer defined, remove the variable from the v: hashtable.
1567 */
1568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001570{
1571 hashitem_T *hi;
1572
Bram Moolenaara40058a2005-07-11 22:42:07 +00001573 vimvars[idx].vv_tv = *save_tv;
1574 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1575 {
1576 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1577 if (HASHITEM_EMPTY(hi))
1578 EMSG2(_(e_intern2), "restore_vimvar()");
1579 else
1580 hash_remove(&vimvarht, hi);
1581 }
1582}
1583
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001584#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001585/*
1586 * Evaluate an expression to a list with suggestions.
1587 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001588 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589 */
1590 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001591eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001592{
1593 typval_T save_val;
1594 typval_T rettv;
1595 list_T *list = NULL;
1596 char_u *p = skipwhite(expr);
1597
1598 /* Set "v:val" to the bad word. */
1599 prepare_vimvar(VV_VAL, &save_val);
1600 vimvars[VV_VAL].vv_type = VAR_STRING;
1601 vimvars[VV_VAL].vv_str = badword;
1602 if (p_verbose == 0)
1603 ++emsg_off;
1604
1605 if (eval1(&p, &rettv, TRUE) == OK)
1606 {
1607 if (rettv.v_type != VAR_LIST)
1608 clear_tv(&rettv);
1609 else
1610 list = rettv.vval.v_list;
1611 }
1612
1613 if (p_verbose == 0)
1614 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615 restore_vimvar(VV_VAL, &save_val);
1616
1617 return list;
1618}
1619
1620/*
1621 * "list" is supposed to contain two items: a word and a number. Return the
1622 * word in "pp" and the number as the return value.
1623 * Return -1 if anything isn't right.
1624 * Used to get the good word and score from the eval_spell_expr() result.
1625 */
1626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001628{
1629 listitem_T *li;
1630
1631 li = list->lv_first;
1632 if (li == NULL)
1633 return -1;
1634 *pp = get_tv_string(&li->li_tv);
1635
1636 li = li->li_next;
1637 if (li == NULL)
1638 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001639 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001640}
1641#endif
1642
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001643/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 * Top level evaluation function.
1645 * Returns an allocated typval_T with the result.
1646 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 */
1648 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001650{
1651 typval_T *tv;
1652
1653 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001654 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001655 {
1656 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001657 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001658 }
1659
1660 return tv;
1661}
1662
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 * Uses argv[argc] for the function arguments. Only Number and String
1667 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671call_vim_function(
1672 char_u *func,
1673 int argc,
1674 char_u **argv,
1675 int safe, /* use the sandbox */
1676 int str_arg_only, /* all arguments are strings */
1677 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001680 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 int len;
1682 int i;
1683 int doesrange;
1684 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001687 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691 for (i = 0; i < argc; i++)
1692 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001693 /* Pass a NULL or empty argument as an empty string */
1694 if (argv[i] == NULL || *argv[i] == NUL)
1695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001696 argvars[i].v_type = VAR_STRING;
1697 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001698 continue;
1699 }
1700
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001701 if (str_arg_only)
1702 len = 0;
1703 else
1704 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001705 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (len != 0 && len == (int)STRLEN(argv[i]))
1707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001708 argvars[i].v_type = VAR_NUMBER;
1709 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 else
1712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001713 argvars[i].v_type = VAR_STRING;
1714 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
1716 }
1717
1718 if (safe)
1719 {
1720 save_funccalp = save_funccal();
1721 ++sandbox;
1722 }
1723
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1725 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001727 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (safe)
1729 {
1730 --sandbox;
1731 restore_funccal(save_funccalp);
1732 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 vim_free(argvars);
1734
1735 if (ret == FAIL)
1736 clear_tv(rettv);
1737
1738 return ret;
1739}
1740
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001741/*
1742 * Call vimL function "func" and return the result as a number.
1743 * Returns -1 when calling the function fails.
1744 * Uses argv[argc] for the function arguments.
1745 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001746 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retnr(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001752{
1753 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001754 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001755
1756 /* All arguments are passed as strings, no conversion to number. */
1757 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1758 return -1;
1759
1760 retval = get_tv_number_chk(&rettv, NULL);
1761 clear_tv(&rettv);
1762 return retval;
1763}
1764
1765#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1766 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1767
Bram Moolenaar4f688582007-07-24 12:34:30 +00001768# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001769/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770 * Call vimL function "func" and return the result as a string.
1771 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772 * Uses argv[argc] for the function arguments.
1773 */
1774 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775call_func_retstr(
1776 char_u *func,
1777 int argc,
1778 char_u **argv,
1779 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780{
1781 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001782 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001784 /* All arguments are passed as strings, no conversion to number. */
1785 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 return NULL;
1787
1788 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 return retval;
1791}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001792# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001794/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001795 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001796 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001797 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001798 */
1799 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800call_func_retlist(
1801 char_u *func,
1802 int argc,
1803 char_u **argv,
1804 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001805{
1806 typval_T rettv;
1807
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001808 /* All arguments are passed as strings, no conversion to number. */
1809 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001810 return NULL;
1811
1812 if (rettv.v_type != VAR_LIST)
1813 {
1814 clear_tv(&rettv);
1815 return NULL;
1816 }
1817
1818 return rettv.vval.v_list;
1819}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#endif
1821
1822/*
1823 * Save the current function call pointer, and set it to NULL.
1824 * Used when executing autocommands and for ":source".
1825 */
1826 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001829 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 current_funccal = NULL;
1832 return (void *)fc;
1833}
1834
1835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001838 funccall_T *fc = (funccall_T *)vfc;
1839
1840 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
Bram Moolenaar05159a02005-02-26 23:04:13 +00001843#if defined(FEAT_PROFILE) || defined(PROTO)
1844/*
1845 * Prepare profiling for entering a child or something else that is not
1846 * counted for the script/function itself.
1847 * Should always be called in pair with prof_child_exit().
1848 */
1849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850prof_child_enter(
1851 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001852{
1853 funccall_T *fc = current_funccal;
1854
1855 if (fc != NULL && fc->func->uf_profiling)
1856 profile_start(&fc->prof_child);
1857 script_prof_save(tm);
1858}
1859
1860/*
1861 * Take care of time spent in a child.
1862 * Should always be called after prof_child_enter().
1863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865prof_child_exit(
1866 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001867{
1868 funccall_T *fc = current_funccal;
1869
1870 if (fc != NULL && fc->func->uf_profiling)
1871 {
1872 profile_end(&fc->prof_child);
1873 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1874 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1875 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1876 }
1877 script_prof_restore(tm);
1878}
1879#endif
1880
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882#ifdef FEAT_FOLDING
1883/*
1884 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1885 * it in "*cp". Doesn't give error messages.
1886 */
1887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001891 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001893 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1894 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001897 if (use_sandbox)
1898 ++sandbox;
1899 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 retval = 0;
1903 else
1904 {
1905 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 if (tv.v_type == VAR_NUMBER)
1907 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001908 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 retval = 0;
1910 else
1911 {
1912 /* If the result is a string, check if there is a non-digit before
1913 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (!VIM_ISDIGIT(*s) && *s != '-')
1916 *cp = *s++;
1917 retval = atol((char *)s);
1918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001922 if (use_sandbox)
1923 --sandbox;
1924 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001926 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927}
1928#endif
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 * ":let" list all variable values
1932 * ":let var1 var2" list variable values
1933 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 * ":let var += expr" assignment command.
1935 * ":let var -= expr" assignment command.
1936 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
1939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 int var_count = 0;
1947 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001949 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaardb552d602006-03-23 22:59:57 +00001952 argend = skip_var_list(arg, &var_count, &semicolon);
1953 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001955 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1956 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001957 expr = skipwhite(argend);
1958 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1959 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001961 /*
1962 * ":let" without "=": list variables
1963 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 if (*arg == '[')
1965 EMSG(_(e_invarg));
1966 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972 list_glob_vars(&first);
1973 list_buf_vars(&first);
1974 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001975#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001977#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001978 list_script_vars(&first);
1979 list_func_vars(&first);
1980 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 eap->nextcmd = check_nextcmd(arg);
1983 }
1984 else
1985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 op[0] = '=';
1987 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001988 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001990 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1991 op[0] = *expr; /* +=, -= or .= */
1992 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001994 else
1995 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (eap->skip)
1998 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (eap->skip)
2001 {
2002 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002003 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 --emsg_skip;
2005 }
2006 else if (i != FAIL)
2007 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 }
2013}
2014
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015/*
2016 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2017 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 * When "nextchars" is not NULL it points to a string with characters that
2019 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2020 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 * Returns OK or FAIL;
2022 */
2023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024ex_let_vars(
2025 char_u *arg_start,
2026 typval_T *tv,
2027 int copy, /* copy values from "tv", don't move */
2028 int semicolon, /* from skip_var_list() */
2029 int var_count, /* from skip_var_list() */
2030 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031{
2032 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002033 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002035 listitem_T *item;
2036 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037
2038 if (*arg != '[')
2039 {
2040 /*
2041 * ":let var = expr" or ":for var in list"
2042 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 return FAIL;
2045 return OK;
2046 }
2047
2048 /*
2049 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2050 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002051 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 {
2053 EMSG(_(e_listreq));
2054 return FAIL;
2055 }
2056
2057 i = list_len(l);
2058 if (semicolon == 0 && var_count < i)
2059 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002060 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 return FAIL;
2062 }
2063 if (var_count - semicolon > i)
2064 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002065 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 return FAIL;
2067 }
2068
2069 item = l->lv_first;
2070 while (*arg != ']')
2071 {
2072 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074 item = item->li_next;
2075 if (arg == NULL)
2076 return FAIL;
2077
2078 arg = skipwhite(arg);
2079 if (*arg == ';')
2080 {
2081 /* Put the rest of the list (may be empty) in the var after ';'.
2082 * Create a new list for this. */
2083 l = list_alloc();
2084 if (l == NULL)
2085 return FAIL;
2086 while (item != NULL)
2087 {
2088 list_append_tv(l, &item->li_tv);
2089 item = item->li_next;
2090 }
2091
2092 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002093 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 ltv.vval.v_list = l;
2095 l->lv_refcount = 1;
2096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2098 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099 clear_tv(&ltv);
2100 if (arg == NULL)
2101 return FAIL;
2102 break;
2103 }
2104 else if (*arg != ',' && *arg != ']')
2105 {
2106 EMSG2(_(e_intern2), "ex_let_vars()");
2107 return FAIL;
2108 }
2109 }
2110
2111 return OK;
2112}
2113
2114/*
2115 * Skip over assignable variable "var" or list of variables "[var, var]".
2116 * Used for ":let varvar = expr" and ":for varvar in expr".
2117 * For "[var, var]" increment "*var_count" for each variable.
2118 * for "[var, var; var]" set "semicolon".
2119 * Return NULL for an error.
2120 */
2121 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122skip_var_list(
2123 char_u *arg,
2124 int *var_count,
2125 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
2127 char_u *p, *s;
2128
2129 if (*arg == '[')
2130 {
2131 /* "[var, var]": find the matching ']'. */
2132 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002133 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134 {
2135 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2136 s = skip_var_one(p);
2137 if (s == p)
2138 {
2139 EMSG2(_(e_invarg2), p);
2140 return NULL;
2141 }
2142 ++*var_count;
2143
2144 p = skipwhite(s);
2145 if (*p == ']')
2146 break;
2147 else if (*p == ';')
2148 {
2149 if (*semicolon == 1)
2150 {
2151 EMSG(_("Double ; in list of variables"));
2152 return NULL;
2153 }
2154 *semicolon = 1;
2155 }
2156 else if (*p != ',')
2157 {
2158 EMSG2(_(e_invarg2), p);
2159 return NULL;
2160 }
2161 }
2162 return p + 1;
2163 }
2164 else
2165 return skip_var_one(arg);
2166}
2167
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002169 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002170 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002175 if (*arg == '@' && arg[1] != NUL)
2176 return arg + 2;
2177 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2178 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002179}
2180
Bram Moolenaara7043832005-01-21 11:56:39 +00002181/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 * List variables for hashtab "ht" with prefix "prefix".
2183 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_hashtable_vars(
2187 hashtab_T *ht,
2188 char_u *prefix,
2189 int empty,
2190 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 hashitem_T *hi;
2193 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002194 int todo;
2195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002196 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2198 {
2199 if (!HASHITEM_EMPTY(hi))
2200 {
2201 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 di = HI2DI(hi);
2203 if (empty || di->di_tv.v_type != VAR_STRING
2204 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002206 }
2207 }
2208}
2209
2210/*
2211 * List global variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002217}
2218
2219/*
2220 * List buffer variables.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002224{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 char_u numbuf[NUMBUFLEN];
2226
Bram Moolenaar429fa852013-04-15 12:27:36 +02002227 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229
2230 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2232 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002233}
2234
2235/*
2236 * List window variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002240{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002241 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002243}
2244
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_WINDOWS
2246/*
2247 * List tab page variables.
2248 */
2249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002252 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254}
2255#endif
2256
Bram Moolenaara7043832005-01-21 11:56:39 +00002257/*
2258 * List Vim variables.
2259 */
2260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002261list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264}
2265
2266/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002267 * List script-local variables, if there is a script.
2268 */
2269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271{
2272 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2274 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275}
2276
2277/*
2278 * List function variables, if there is a function.
2279 */
2280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002282{
2283 if (current_funccal != NULL)
2284 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002285 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002286}
2287
2288/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 * List variables in "arg".
2290 */
2291 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002292list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293{
2294 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 char_u *name_start;
2298 char_u *arg_subsc;
2299 char_u *tofree;
2300 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301
2302 while (!ends_excmd(*arg) && !got_int)
2303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002306 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2308 {
2309 emsg_severe = TRUE;
2310 EMSG(_(e_trailing));
2311 break;
2312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 /* get_name_len() takes care of expanding curly braces */
2317 name_start = name = arg;
2318 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2319 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 /* This is mainly to keep test 49 working: when expanding
2322 * curly braces fails overrule the exception error message. */
2323 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 emsg_severe = TRUE;
2326 EMSG2(_(e_invarg2), arg);
2327 break;
2328 }
2329 error = TRUE;
2330 }
2331 else
2332 {
2333 if (tofree != NULL)
2334 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002335 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 else
2338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 /* handle d.key, l[idx], f(expr) */
2340 arg_subsc = arg;
2341 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002342 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002347 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002349 case 'g': list_glob_vars(first); break;
2350 case 'b': list_buf_vars(first); break;
2351 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002352#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002353 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002354#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002355 case 'v': list_vim_vars(first); break;
2356 case 's': list_script_vars(first); break;
2357 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358 default:
2359 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002360 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002361 }
2362 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002363 {
2364 char_u numbuf[NUMBUFLEN];
2365 char_u *tf;
2366 int c;
2367 char_u *s;
2368
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002369 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002370 c = *arg;
2371 *arg = NUL;
2372 list_one_var_a((char_u *)"",
2373 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002374 tv.v_type,
2375 s == NULL ? (char_u *)"" : s,
2376 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002377 *arg = c;
2378 vim_free(tf);
2379 }
2380 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002384
2385 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002387
2388 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390
2391 return arg;
2392}
2393
2394/*
2395 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2396 * Returns a pointer to the char just after the var name.
2397 * Returns NULL if there is an error.
2398 */
2399 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002400ex_let_one(
2401 char_u *arg, /* points to variable name */
2402 typval_T *tv, /* value to assign to variable */
2403 int copy, /* copy value from "tv" */
2404 char_u *endchars, /* valid chars after variable name or NULL */
2405 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406{
2407 int c1;
2408 char_u *name;
2409 char_u *p;
2410 char_u *arg_end = NULL;
2411 int len;
2412 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414
2415 /*
2416 * ":let $VAR = expr": Set environment variable.
2417 */
2418 if (*arg == '$')
2419 {
2420 /* Find the end of the name. */
2421 ++arg;
2422 name = arg;
2423 len = get_env_len(&arg);
2424 if (len == 0)
2425 EMSG2(_(e_invarg2), name - 1);
2426 else
2427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (op != NULL && (*op == '+' || *op == '-'))
2429 EMSG2(_(e_letwrong), op);
2430 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002433 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
2435 c1 = name[len];
2436 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 p = get_tv_string_chk(tv);
2438 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 {
2440 int mustfree = FALSE;
2441 char_u *s = vim_getenv(name, &mustfree);
2442
2443 if (s != NULL)
2444 {
2445 p = tofree = concat_str(s, p);
2446 if (mustfree)
2447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 if (STRICMP(name, "HOME") == 0)
2454 init_homedir();
2455 else if (didset_vim && STRICMP(name, "VIM") == 0)
2456 didset_vim = FALSE;
2457 else if (didset_vimruntime
2458 && STRICMP(name, "VIMRUNTIME") == 0)
2459 didset_vimruntime = FALSE;
2460 arg_end = arg;
2461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466 }
2467
2468 /*
2469 * ":let &option = expr": Set option value.
2470 * ":let &l:option = expr": Set local option value.
2471 * ":let &g:option = expr": Set global option value.
2472 */
2473 else if (*arg == '&')
2474 {
2475 /* Find the end of the name. */
2476 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 if (p == NULL || (endchars != NULL
2478 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 long n;
2483 int opt_type;
2484 long numval;
2485 char_u *stringval = NULL;
2486 char_u *s;
2487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 c1 = *p;
2489 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002491 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 s = get_tv_string_chk(tv); /* != NULL if number or string */
2493 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
2495 opt_type = get_option_value(arg, &numval,
2496 &stringval, opt_flags);
2497 if ((opt_type == 1 && *op == '.')
2498 || (opt_type == 0 && *op != '.'))
2499 EMSG2(_(e_letwrong), op);
2500 else
2501 {
2502 if (opt_type == 1) /* number */
2503 {
2504 if (*op == '+')
2505 n = numval + n;
2506 else
2507 n = numval - n;
2508 }
2509 else if (opt_type == 0 && stringval != NULL) /* string */
2510 {
2511 s = concat_str(stringval, s);
2512 vim_free(stringval);
2513 stringval = s;
2514 }
2515 }
2516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 if (s != NULL)
2518 {
2519 set_option_value(arg, n, s, opt_flags);
2520 arg_end = p;
2521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525 }
2526
2527 /*
2528 * ":let @r = expr": Set register contents.
2529 */
2530 else if (*arg == '@')
2531 {
2532 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 if (op != NULL && (*op == '+' || *op == '-'))
2534 EMSG2(_(e_letwrong), op);
2535 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002536 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 EMSG(_(e_letunexp));
2538 else
2539 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002540 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 char_u *s;
2542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 p = get_tv_string_chk(tv);
2544 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002546 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 if (s != NULL)
2548 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002549 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 vim_free(s);
2551 }
2552 }
2553 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 arg_end = arg + 1;
2557 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002558 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 }
2560 }
2561
2562 /*
2563 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002570 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2574 EMSG(_(e_letunexp));
2575 else
2576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 arg_end = p;
2579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 }
2583
2584 else
2585 EMSG2(_(e_invarg2), arg);
2586
2587 return arg_end;
2588}
2589
2590/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2592 */
2593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002594check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595{
2596 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2597 {
2598 EMSG2(_(e_readonlyvar), arg);
2599 return TRUE;
2600 }
2601 return FALSE;
2602}
2603
2604/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Get an lval: variable, Dict item or List item that can be assigned a value
2606 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2607 * "name.key", "name.key[expr]" etc.
2608 * Indexing only works if "name" is an existing List or Dictionary.
2609 * "name" points to the start of the name.
2610 * If "rettv" is not NULL it points to the value to be assigned.
2611 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2612 * wrong; must end in space or cmd separator.
2613 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002614 * flags:
2615 * GLV_QUIET: do not give error messages
2616 * GLV_NO_AUTOLOAD: do not use script autoloading
2617 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002619 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 */
2622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623get_lval(
2624 char_u *name,
2625 typval_T *rettv,
2626 lval_T *lp,
2627 int unlet,
2628 int skip,
2629 int flags, /* GLV_ values */
2630 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 char_u *expr_start, *expr_end;
2634 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 dictitem_T *v;
2636 typval_T var1;
2637 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647
2648 if (skip)
2649 {
2650 /* When skipping just find the end of the name. */
2651 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002652 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 }
2654
2655 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002656 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (expr_start != NULL)
2658 {
2659 /* Don't expand the name when we already know there is an error. */
2660 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2661 && *p != '[' && *p != '.')
2662 {
2663 EMSG(_(e_trailing));
2664 return NULL;
2665 }
2666
2667 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2668 if (lp->ll_exp_name == NULL)
2669 {
2670 /* Report an invalid expression in braces, unless the
2671 * expression evaluation has been cancelled due to an
2672 * aborting error, an interrupt, or an exception. */
2673 if (!aborting() && !quiet)
2674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002675 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 EMSG2(_(e_invarg2), name);
2677 return NULL;
2678 }
2679 }
2680 lp->ll_name = lp->ll_exp_name;
2681 }
2682 else
2683 lp->ll_name = name;
2684
2685 /* Without [idx] or .key we are done. */
2686 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2687 return p;
2688
2689 cc = *p;
2690 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002691 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (v == NULL && !quiet)
2693 EMSG2(_(e_undefvar), lp->ll_name);
2694 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 if (v == NULL)
2696 return NULL;
2697
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 /*
2699 * Loop until no more [idx] or .key is following.
2700 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2705 && !(lp->ll_tv->v_type == VAR_DICT
2706 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E689: Can only index a List or Dictionary"));
2710 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_("E708: [:] must come last"));
2716 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 len = -1;
2720 if (*p == '.')
2721 {
2722 key = p + 1;
2723 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2724 ;
2725 if (len == 0)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (!quiet)
2728 EMSG(_(e_emptykey));
2729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
2731 p = key + len;
2732 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 else
2734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (*p == ':')
2738 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 else
2740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 empty1 = FALSE;
2742 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 if (get_tv_string_chk(&var1) == NULL)
2745 {
2746 /* not a number or string */
2747 clear_tv(&var1);
2748 return NULL;
2749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751
2752 /* Optionally get the second index [ :expr]. */
2753 if (*p == ':')
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002759 if (!empty1)
2760 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (rettv != NULL && (rettv->v_type != VAR_LIST
2764 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (!empty1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 p = skipwhite(p + 1);
2773 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 else
2776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (!empty1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 if (get_tv_string_chk(&var2) == NULL)
2785 {
2786 /* not a number or string */
2787 if (!empty1)
2788 clear_tv(&var1);
2789 clear_tv(&var2);
2790 return NULL;
2791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (!quiet)
2801 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (!empty1)
2803 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808
2809 /* Skip to past ']'. */
2810 ++p;
2811 }
2812
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
2815 if (len == -1)
2816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002818 key = get_tv_string_chk(&var1); /* is number or string */
2819 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 lp->ll_list = NULL;
2826 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002827 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002829 /* When assigning to a scope dictionary check that a function and
2830 * variable name is valid (only variable name unless it is l: or
2831 * g: dictionary). Disallow overwriting a builtin function. */
2832 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 int prevval;
2835 int wrong;
2836
2837 if (len != -1)
2838 {
2839 prevval = key[len];
2840 key[len] = NUL;
2841 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002842 else
2843 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002844 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2845 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002846 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002847 || !valid_varname(key);
2848 if (len != -1)
2849 key[len] = prevval;
2850 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 return NULL;
2852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002856 /* Can't add "v:" variable. */
2857 if (lp->ll_dict == &vimvardict)
2858 {
2859 EMSG2(_(e_illvar), name);
2860 return NULL;
2861 }
2862
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002863 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 if (len == -1)
2869 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 }
2872 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (len == -1)
2877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 p = NULL;
2880 break;
2881 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002882 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002883 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002884 return NULL;
2885
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 if (len == -1)
2887 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890 else
2891 {
2892 /*
2893 * Get the number and item for the only or first index of the List.
2894 */
2895 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 else
2898 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002899 lp->ll_n1 = (long)get_tv_number(&var1);
2900 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 clear_tv(&var1);
2902 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 lp->ll_list = lp->ll_tv->vval.v_list;
2905 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2906 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002908 if (lp->ll_n1 < 0)
2909 {
2910 lp->ll_n1 = 0;
2911 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2912 }
2913 }
2914 if (lp->ll_li == NULL)
2915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 if (!quiet)
2919 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 }
2922
2923 /*
2924 * May need to find the item or absolute index for the second
2925 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 * When no index given: "lp->ll_empty2" is TRUE.
2927 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002928 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002930 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002931 lp->ll_n2 = (long)get_tv_number(&var2);
2932 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002938 {
2939 if (!quiet)
2940 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2947 if (lp->ll_n1 < 0)
2948 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2949 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002950 {
2951 if (!quiet)
2952 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
2956
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 }
2960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 return p;
2962}
2963
2964/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 */
2967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969{
2970 vim_free(lp->ll_exp_name);
2971 vim_free(lp->ll_newkey);
2972}
2973
2974/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 */
2979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002980set_var_lval(
2981 lval_T *lp,
2982 char_u *endp,
2983 typval_T *rettv,
2984 int copy,
2985 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986{
2987 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 listitem_T *ri;
2989 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990
2991 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 cc = *endp;
2996 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 if (op != NULL && *op != '=')
2998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003001 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003002 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003003 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003004 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003006 if ((di == NULL
3007 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3008 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3009 FALSE)))
3010 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 set_var(lp->ll_name, &tv, FALSE);
3012 clear_tv(&tv);
3013 }
3014 }
3015 else
3016 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003020 else if (tv_check_lock(lp->ll_newkey == NULL
3021 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003022 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 else if (lp->ll_range)
3025 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003026 listitem_T *ll_li = lp->ll_li;
3027 int ll_n1 = lp->ll_n1;
3028
3029 /*
3030 * Check whether any of the list items is locked
3031 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003032 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003033 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003034 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003035 return;
3036 ri = ri->li_next;
3037 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3038 break;
3039 ll_li = ll_li->li_next;
3040 ++ll_n1;
3041 }
3042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /*
3044 * Assign the List values to the list items.
3045 */
3046 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 if (op != NULL && *op != '=')
3049 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3050 else
3051 {
3052 clear_tv(&lp->ll_li->li_tv);
3053 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 ri = ri->li_next;
3056 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3057 break;
3058 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003061 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003062 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 ri = NULL;
3064 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003066 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 lp->ll_li = lp->ll_li->li_next;
3068 ++lp->ll_n1;
3069 }
3070 if (ri != NULL)
3071 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 else if (lp->ll_empty2
3073 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 : lp->ll_n1 != lp->ll_n2)
3075 EMSG(_("E711: List value has not enough items"));
3076 }
3077 else
3078 {
3079 /*
3080 * Assign to a List or Dictionary item.
3081 */
3082 if (lp->ll_newkey != NULL)
3083 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 if (op != NULL && *op != '=')
3085 {
3086 EMSG2(_(e_letwrong), op);
3087 return;
3088 }
3089
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003090 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003091 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 if (di == NULL)
3093 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003094 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3095 {
3096 vim_free(di);
3097 return;
3098 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101 else if (op != NULL && *op != '=')
3102 {
3103 tv_op(lp->ll_tv, rettv, op);
3104 return;
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003107 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003108
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003109 /*
3110 * Assign the value to the variable or list item.
3111 */
3112 if (copy)
3113 copy_tv(rettv, lp->ll_tv);
3114 else
3115 {
3116 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003117 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003118 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 }
3120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003121}
3122
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3125 * Returns OK or FAIL.
3126 */
3127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003130 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 char_u numbuf[NUMBUFLEN];
3132 char_u *s;
3133
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003134 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3135 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3136 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 {
3138 switch (tv1->v_type)
3139 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003140 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 case VAR_DICT:
3142 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003143 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003144 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003145 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003146 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 break;
3148
3149 case VAR_LIST:
3150 if (*op != '+' || tv2->v_type != VAR_LIST)
3151 break;
3152 /* List += List */
3153 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3154 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3155 return OK;
3156
3157 case VAR_NUMBER:
3158 case VAR_STRING:
3159 if (tv2->v_type == VAR_LIST)
3160 break;
3161 if (*op == '+' || *op == '-')
3162 {
3163 /* nr += nr or nr -= nr*/
3164 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#ifdef FEAT_FLOAT
3166 if (tv2->v_type == VAR_FLOAT)
3167 {
3168 float_T f = n;
3169
3170 if (*op == '+')
3171 f += tv2->vval.v_float;
3172 else
3173 f -= tv2->vval.v_float;
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_FLOAT;
3176 tv1->vval.v_float = f;
3177 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003178 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#endif
3180 {
3181 if (*op == '+')
3182 n += get_tv_number(tv2);
3183 else
3184 n -= get_tv_number(tv2);
3185 clear_tv(tv1);
3186 tv1->v_type = VAR_NUMBER;
3187 tv1->vval.v_number = n;
3188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003189 }
3190 else
3191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 if (tv2->v_type == VAR_FLOAT)
3193 break;
3194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003195 /* str .= str */
3196 s = get_tv_string(tv1);
3197 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3198 clear_tv(tv1);
3199 tv1->v_type = VAR_STRING;
3200 tv1->vval.v_string = s;
3201 }
3202 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003205#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 {
3207 float_T f;
3208
3209 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3210 && tv2->v_type != VAR_NUMBER
3211 && tv2->v_type != VAR_STRING))
3212 break;
3213 if (tv2->v_type == VAR_FLOAT)
3214 f = tv2->vval.v_float;
3215 else
3216 f = get_tv_number(tv2);
3217 if (*op == '+')
3218 tv1->vval.v_float += f;
3219 else
3220 tv1->vval.v_float -= f;
3221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003223 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003224 }
3225 }
3226
3227 EMSG2(_(e_letwrong), op);
3228 return FAIL;
3229}
3230
3231/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 * Add a watcher to a list.
3233 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
3237 lw->lw_next = l->lv_watch;
3238 l->lv_watch = lw;
3239}
3240
3241/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003242 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 * No warning when it isn't found...
3244 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003246list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247{
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 lwp = &l->lv_watch;
3251 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3252 {
3253 if (lw == lwrem)
3254 {
3255 *lwp = lw->lw_next;
3256 break;
3257 }
3258 lwp = &lw->lw_next;
3259 }
3260}
3261
3262/*
3263 * Just before removing an item from a list: advance watchers to the next
3264 * item.
3265 */
3266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270
3271 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3272 if (lw->lw_item == item)
3273 lw->lw_item = item->li_next;
3274}
3275
3276/*
3277 * Evaluate the expression used in a ":for var in expr" command.
3278 * "arg" points to "var".
3279 * Set "*errp" to TRUE for an error, FALSE otherwise;
3280 * Return a pointer that holds the info. Null when there is an error.
3281 */
3282 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283eval_for_line(
3284 char_u *arg,
3285 int *errp,
3286 char_u **nextcmdp,
3287 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288{
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 typval_T tv;
3292 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
3294 *errp = TRUE; /* default: there is an error */
3295
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (fi == NULL)
3298 return NULL;
3299
3300 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3301 if (expr == NULL)
3302 return fi;
3303
3304 expr = skipwhite(expr);
3305 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3306 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003307 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 return fi;
3309 }
3310
3311 if (skip)
3312 ++emsg_skip;
3313 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3314 {
3315 *errp = FALSE;
3316 if (!skip)
3317 {
3318 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003319 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 clear_tv(&tv);
3323 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003324 else if (l == NULL)
3325 {
3326 /* a null list is like an empty list: do nothing */
3327 clear_tv(&tv);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 else
3330 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003331 /* No need to increment the refcount, it's already set for the
3332 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 fi->fi_list = l;
3334 list_add_watch(l, &fi->fi_lw);
3335 fi->fi_lw.lw_item = l->lv_first;
3336 }
3337 }
3338 }
3339 if (skip)
3340 --emsg_skip;
3341
3342 return fi;
3343}
3344
3345/*
3346 * Use the first item in a ":for" list. Advance to the next.
3347 * Assign the values to the variable (list). "arg" points to the first one.
3348 * Return TRUE when a valid item was found, FALSE when at end of list or
3349 * something wrong.
3350 */
3351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003354 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003356 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357
3358 item = fi->fi_lw.lw_item;
3359 if (item == NULL)
3360 result = FALSE;
3361 else
3362 {
3363 fi->fi_lw.lw_item = item->li_next;
3364 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3365 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3366 }
3367 return result;
3368}
3369
3370/*
3371 * Free the structure used to store info used by ":for".
3372 */
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375{
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003377
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003378 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003379 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003380 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003381 list_unref(fi->fi_list);
3382 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 vim_free(fi);
3384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3387
3388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389set_context_for_expression(
3390 expand_T *xp,
3391 char_u *arg,
3392 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 int got_eq = FALSE;
3395 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003396 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003398 if (cmdidx == CMD_let)
3399 {
3400 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003401 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 {
3403 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003404 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003405 {
3406 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 if (vim_iswhite(*p))
3409 break;
3410 }
3411 return;
3412 }
3413 }
3414 else
3415 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3416 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 while ((xp->xp_pattern = vim_strpbrk(arg,
3418 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3419 {
3420 c = *xp->xp_pattern;
3421 if (c == '&')
3422 {
3423 c = xp->xp_pattern[1];
3424 if (c == '&')
3425 {
3426 ++xp->xp_pattern;
3427 xp->xp_context = cmdidx != CMD_let || got_eq
3428 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3429 }
3430 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003433 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3434 xp->xp_pattern += 2;
3435
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 else if (c == '$')
3439 {
3440 /* environment variable */
3441 xp->xp_context = EXPAND_ENV_VARS;
3442 }
3443 else if (c == '=')
3444 {
3445 got_eq = TRUE;
3446 xp->xp_context = EXPAND_EXPRESSION;
3447 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003448 else if (c == '#'
3449 && xp->xp_context == EXPAND_EXPRESSION)
3450 {
3451 /* Autoload function/variable contains '#'. */
3452 break;
3453 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003454 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 && xp->xp_context == EXPAND_FUNCTIONS
3456 && vim_strchr(xp->xp_pattern, '(') == NULL)
3457 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003458 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 break;
3460 }
3461 else if (cmdidx != CMD_let || got_eq)
3462 {
3463 if (c == '"') /* string */
3464 {
3465 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3466 if (c == '\\' && xp->xp_pattern[1] != NUL)
3467 ++xp->xp_pattern;
3468 xp->xp_context = EXPAND_NOTHING;
3469 }
3470 else if (c == '\'') /* literal string */
3471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3474 /* skip */ ;
3475 xp->xp_context = EXPAND_NOTHING;
3476 }
3477 else if (c == '|')
3478 {
3479 if (xp->xp_pattern[1] == '|')
3480 {
3481 ++xp->xp_pattern;
3482 xp->xp_context = EXPAND_EXPRESSION;
3483 }
3484 else
3485 xp->xp_context = EXPAND_COMMANDS;
3486 }
3487 else
3488 xp->xp_context = EXPAND_EXPRESSION;
3489 }
3490 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003491 /* Doesn't look like something valid, expand as an expression
3492 * anyway. */
3493 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 arg = xp->xp_pattern;
3495 if (*arg != NUL)
3496 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3497 /* skip */ ;
3498 }
3499 xp->xp_pattern = arg;
3500}
3501
3502#endif /* FEAT_CMDL_COMPL */
3503
3504/*
3505 * ":1,25call func(arg1, arg2)" function call.
3506 */
3507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 char_u *arg = eap->arg;
3511 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003513 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 linenr_T lnum;
3517 int doesrange;
3518 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003520 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003522 if (eap->skip)
3523 {
3524 /* trans_function_name() doesn't work well when skipping, use eval0()
3525 * instead to skip to any following command, e.g. for:
3526 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003527 ++emsg_skip;
3528 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3529 clear_tv(&rettv);
3530 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003531 return;
3532 }
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003535 if (fudi.fd_newkey != NULL)
3536 {
3537 /* Still need to give an error message for missing key. */
3538 EMSG2(_(e_dictkey), fudi.fd_newkey);
3539 vim_free(fudi.fd_newkey);
3540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003541 if (tofree == NULL)
3542 return;
3543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003544 /* Increase refcount on dictionary, it could get deleted when evaluating
3545 * the arguments. */
3546 if (fudi.fd_dict != NULL)
3547 ++fudi.fd_dict->dv_refcount;
3548
Bram Moolenaar65639032016-03-16 21:40:30 +01003549 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3550 * contents. For VAR_PARTIAL get its partial, unless we already have one
3551 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003552 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003553 name = deref_func_name(tofree, &len,
3554 partial != NULL ? NULL : &partial, FALSE);
3555
Bram Moolenaar532c7802005-01-27 14:44:31 +00003556 /* Skip white space to allow ":call func ()". Not good, but required for
3557 * backward compatibility. */
3558 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 if (*startarg != '(')
3562 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003563 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 goto end;
3565 }
3566
3567 /*
3568 * When skipping, evaluate the function once, to find the end of the
3569 * arguments.
3570 * When the function takes a range, this is discovered after the first
3571 * call, and the loop is broken.
3572 */
3573 if (eap->skip)
3574 {
3575 ++emsg_skip;
3576 lnum = eap->line2; /* do it once, also with an invalid range */
3577 }
3578 else
3579 lnum = eap->line1;
3580 for ( ; lnum <= eap->line2; ++lnum)
3581 {
3582 if (!eap->skip && eap->addr_count > 0)
3583 {
3584 curwin->w_cursor.lnum = lnum;
3585 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003586#ifdef FEAT_VIRTUALEDIT
3587 curwin->w_cursor.coladd = 0;
3588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003591 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003592 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003593 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 failed = TRUE;
3596 break;
3597 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003598
3599 /* Handle a function returning a Funcref, Dictionary or List. */
3600 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3601 {
3602 failed = TRUE;
3603 break;
3604 }
3605
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (doesrange || eap->skip)
3608 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003611 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003613 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (aborting())
3615 break;
3616 }
3617 if (eap->skip)
3618 --emsg_skip;
3619
3620 if (!failed)
3621 {
3622 /* Check for trailing illegal characters and a following command. */
3623 if (!ends_excmd(*arg))
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 else
3629 eap->nextcmd = check_nextcmd(arg);
3630 }
3631
3632end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003633 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003634 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635}
3636
3637/*
3638 * ":unlet[!] var1 ... " command.
3639 */
3640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 ex_unletlock(eap, eap->arg, 0);
3644}
3645
3646/*
3647 * ":lockvar" and ":unlockvar" commands
3648 */
3649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003650ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 int deep = 2;
3654
3655 if (eap->forceit)
3656 deep = -1;
3657 else if (vim_isdigit(*arg))
3658 {
3659 deep = getdigits(&arg);
3660 arg = skipwhite(arg);
3661 }
3662
3663 ex_unletlock(eap, arg, deep);
3664}
3665
3666/*
3667 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3668 */
3669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670ex_unletlock(
3671 exarg_T *eap,
3672 char_u *argstart,
3673 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674{
3675 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 do
3681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003683 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003684 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 if (lv.ll_name == NULL)
3686 error = TRUE; /* error but continue parsing */
3687 if (name_end == NULL || (!vim_iswhite(*name_end)
3688 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 if (name_end != NULL)
3691 {
3692 emsg_severe = TRUE;
3693 EMSG(_(e_trailing));
3694 }
3695 if (!(eap->skip || error))
3696 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 break;
3698 }
3699
3700 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 {
3702 if (eap->cmdidx == CMD_unlet)
3703 {
3704 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3705 error = TRUE;
3706 }
3707 else
3708 {
3709 if (do_lock_var(&lv, name_end, deep,
3710 eap->cmdidx == CMD_lockvar) == FAIL)
3711 error = TRUE;
3712 }
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 if (!eap->skip)
3716 clear_lval(&lv);
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 arg = skipwhite(name_end);
3719 } while (!ends_excmd(*arg));
3720
3721 eap->nextcmd = check_nextcmd(arg);
3722}
3723
Bram Moolenaar8c711452005-01-14 21:53:12 +00003724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725do_unlet_var(
3726 lval_T *lp,
3727 char_u *name_end,
3728 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 int ret = OK;
3731 int cc;
3732
3733 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 cc = *name_end;
3736 *name_end = NUL;
3737
3738 /* Normal name or expanded name. */
3739 if (check_changedtick(lp->ll_name))
3740 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003743 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003744 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003745 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003747 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 else if (lp->ll_range)
3751 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003752 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003753 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003754 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003755
3756 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3757 {
3758 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003759 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003760 return FAIL;
3761 ll_li = li;
3762 ++ll_n1;
3763 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764
3765 /* Delete a range of List items. */
3766 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3767 {
3768 li = lp->ll_li->li_next;
3769 listitem_remove(lp->ll_list, lp->ll_li);
3770 lp->ll_li = li;
3771 ++lp->ll_n1;
3772 }
3773 }
3774 else
3775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003776 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003777 /* unlet a List item. */
3778 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003779 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003781 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003782 }
3783
3784 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003785}
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787/*
3788 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 hashtab_T *ht;
3795 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003796 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003797 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003798 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
Bram Moolenaar33570922005-01-25 22:26:29 +00003800 ht = find_var_ht(name, &varname);
3801 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003803 if (ht == &globvarht)
3804 d = &globvardict;
3805 else if (current_funccal != NULL
3806 && ht == &current_funccal->l_vars.dv_hashtab)
3807 d = &current_funccal->l_vars;
3808 else if (ht == &compat_hashtab)
3809 d = &vimvardict;
3810 else
3811 {
3812 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3813 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3814 }
3815 if (d == NULL)
3816 {
3817 EMSG2(_(e_intern2), "do_unlet()");
3818 return FAIL;
3819 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003820 hi = hash_find(ht, varname);
3821 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003822 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003823 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003824 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003825 || var_check_ro(di->di_flags, name, FALSE)
3826 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003827 return FAIL;
3828
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 delete_var(ht, hi);
3830 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 if (forceit)
3834 return OK;
3835 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837}
3838
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003839/*
3840 * Lock or unlock variable indicated by "lp".
3841 * "deep" is the levels to go (-1 for unlimited);
3842 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3843 */
3844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845do_lock_var(
3846 lval_T *lp,
3847 char_u *name_end,
3848 int deep,
3849 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003850{
3851 int ret = OK;
3852 int cc;
3853 dictitem_T *di;
3854
3855 if (deep == 0) /* nothing to do */
3856 return OK;
3857
3858 if (lp->ll_tv == NULL)
3859 {
3860 cc = *name_end;
3861 *name_end = NUL;
3862
3863 /* Normal name or expanded name. */
3864 if (check_changedtick(lp->ll_name))
3865 ret = FAIL;
3866 else
3867 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003868 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869 if (di == NULL)
3870 ret = FAIL;
3871 else
3872 {
3873 if (lock)
3874 di->di_flags |= DI_FLAGS_LOCK;
3875 else
3876 di->di_flags &= ~DI_FLAGS_LOCK;
3877 item_lock(&di->di_tv, deep, lock);
3878 }
3879 }
3880 *name_end = cc;
3881 }
3882 else if (lp->ll_range)
3883 {
3884 listitem_T *li = lp->ll_li;
3885
3886 /* (un)lock a range of List items. */
3887 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3888 {
3889 item_lock(&li->li_tv, deep, lock);
3890 li = li->li_next;
3891 ++lp->ll_n1;
3892 }
3893 }
3894 else if (lp->ll_list != NULL)
3895 /* (un)lock a List item. */
3896 item_lock(&lp->ll_li->li_tv, deep, lock);
3897 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003898 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 item_lock(&lp->ll_di->di_tv, deep, lock);
3900
3901 return ret;
3902}
3903
3904/*
3905 * Lock or unlock an item. "deep" is nr of levels to go.
3906 */
3907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003908item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003909{
3910 static int recurse = 0;
3911 list_T *l;
3912 listitem_T *li;
3913 dict_T *d;
3914 hashitem_T *hi;
3915 int todo;
3916
3917 if (recurse >= DICT_MAXNEST)
3918 {
3919 EMSG(_("E743: variable nested too deep for (un)lock"));
3920 return;
3921 }
3922 if (deep == 0)
3923 return;
3924 ++recurse;
3925
3926 /* lock/unlock the item itself */
3927 if (lock)
3928 tv->v_lock |= VAR_LOCKED;
3929 else
3930 tv->v_lock &= ~VAR_LOCKED;
3931
3932 switch (tv->v_type)
3933 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003934 case VAR_UNKNOWN:
3935 case VAR_NUMBER:
3936 case VAR_STRING:
3937 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003938 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003939 case VAR_FLOAT:
3940 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003942 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003943 break;
3944
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003945 case VAR_LIST:
3946 if ((l = tv->vval.v_list) != NULL)
3947 {
3948 if (lock)
3949 l->lv_lock |= VAR_LOCKED;
3950 else
3951 l->lv_lock &= ~VAR_LOCKED;
3952 if (deep < 0 || deep > 1)
3953 /* recursive: lock/unlock the items the List contains */
3954 for (li = l->lv_first; li != NULL; li = li->li_next)
3955 item_lock(&li->li_tv, deep - 1, lock);
3956 }
3957 break;
3958 case VAR_DICT:
3959 if ((d = tv->vval.v_dict) != NULL)
3960 {
3961 if (lock)
3962 d->dv_lock |= VAR_LOCKED;
3963 else
3964 d->dv_lock &= ~VAR_LOCKED;
3965 if (deep < 0 || deep > 1)
3966 {
3967 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003968 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003969 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3970 {
3971 if (!HASHITEM_EMPTY(hi))
3972 {
3973 --todo;
3974 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3975 }
3976 }
3977 }
3978 }
3979 }
3980 --recurse;
3981}
3982
Bram Moolenaara40058a2005-07-11 22:42:07 +00003983/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003984 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3985 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003986 */
3987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003989{
3990 return (tv->v_lock & VAR_LOCKED)
3991 || (tv->v_type == VAR_LIST
3992 && tv->vval.v_list != NULL
3993 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3994 || (tv->v_type == VAR_DICT
3995 && tv->vval.v_dict != NULL
3996 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3997}
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4000/*
4001 * Delete all "menutrans_" variables.
4002 */
4003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004010 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 {
4013 if (!HASHITEM_EMPTY(hi))
4014 {
4015 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004016 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4017 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 }
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021}
4022#endif
4023
4024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4025
4026/*
4027 * Local string buffer for the next two functions to store a variable name
4028 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4029 * get_user_var_name().
4030 */
4031
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004032static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034static char_u *varnamebuf = NULL;
4035static int varnamebuflen = 0;
4036
4037/*
4038 * Function to concatenate a prefix and a variable name.
4039 */
4040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043 int len;
4044
4045 len = (int)STRLEN(name) + 3;
4046 if (len > varnamebuflen)
4047 {
4048 vim_free(varnamebuf);
4049 len += 10; /* some additional space */
4050 varnamebuf = alloc(len);
4051 if (varnamebuf == NULL)
4052 {
4053 varnamebuflen = 0;
4054 return NULL;
4055 }
4056 varnamebuflen = len;
4057 }
4058 *varnamebuf = prefix;
4059 varnamebuf[1] = ':';
4060 STRCPY(varnamebuf + 2, name);
4061 return varnamebuf;
4062}
4063
4064/*
4065 * Function given to ExpandGeneric() to obtain the list of user defined
4066 * (global/buffer/window/built-in) variable names.
4067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004069get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004071 static long_u gdone;
4072 static long_u bdone;
4073 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074#ifdef FEAT_WINDOWS
4075 static long_u tdone;
4076#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 static int vidx;
4078 static hashitem_T *hi;
4079 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084#ifdef FEAT_WINDOWS
4085 tdone = 0;
4086#endif
4087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* Global variables */
4090 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = globvarht.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 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4099 return cat_prefix_varname('g', hi->hi_key);
4100 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004102
4103 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004104 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004105 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004107 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004109 else
4110 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004111 while (HASHITEM_EMPTY(hi))
4112 ++hi;
4113 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004115 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004117 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 return (char_u *)"b:changedtick";
4119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004120
4121 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004122 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004123 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004125 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004126 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004127 else
4128 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004129 while (HASHITEM_EMPTY(hi))
4130 ++hi;
4131 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004134#ifdef FEAT_WINDOWS
4135 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004136 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004137 if (tdone < ht->ht_used)
4138 {
4139 if (tdone++ == 0)
4140 hi = ht->ht_array;
4141 else
4142 ++hi;
4143 while (HASHITEM_EMPTY(hi))
4144 ++hi;
4145 return cat_prefix_varname('t', hi->hi_key);
4146 }
4147#endif
4148
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 /* v: variables */
4150 if (vidx < VV_LEN)
4151 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 vim_free(varnamebuf);
4154 varnamebuf = NULL;
4155 varnamebuflen = 0;
4156 return NULL;
4157}
4158
4159#endif /* FEAT_CMDL_COMPL */
4160
4161/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004162 * Return TRUE if "pat" matches "text".
4163 * Does not use 'cpo' and always uses 'magic'.
4164 */
4165 static int
4166pattern_match(char_u *pat, char_u *text, int ic)
4167{
4168 int matches = FALSE;
4169 char_u *save_cpo;
4170 regmatch_T regmatch;
4171
4172 /* avoid 'l' flag in 'cpoptions' */
4173 save_cpo = p_cpo;
4174 p_cpo = (char_u *)"";
4175 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4176 if (regmatch.regprog != NULL)
4177 {
4178 regmatch.rm_ic = ic;
4179 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4180 vim_regfree(regmatch.regprog);
4181 }
4182 p_cpo = save_cpo;
4183 return matches;
4184}
4185
4186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * types for expressions.
4188 */
4189typedef enum
4190{
4191 TYPE_UNKNOWN = 0
4192 , TYPE_EQUAL /* == */
4193 , TYPE_NEQUAL /* != */
4194 , TYPE_GREATER /* > */
4195 , TYPE_GEQUAL /* >= */
4196 , TYPE_SMALLER /* < */
4197 , TYPE_SEQUAL /* <= */
4198 , TYPE_MATCH /* =~ */
4199 , TYPE_NOMATCH /* !~ */
4200} exptype_T;
4201
4202/*
4203 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4206 */
4207
4208/*
4209 * Handle zero level expression.
4210 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004212 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * Return OK or FAIL.
4214 */
4215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004216eval0(
4217 char_u *arg,
4218 typval_T *rettv,
4219 char_u **nextcmd,
4220 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 int ret;
4223 char_u *p;
4224
4225 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (ret == FAIL || !ends_excmd(*p))
4228 {
4229 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /*
4232 * Report the invalid expression unless the expression evaluation has
4233 * been cancelled due to an aborting error, an interrupt, or an
4234 * exception.
4235 */
4236 if (!aborting())
4237 EMSG2(_(e_invexpr2), arg);
4238 ret = FAIL;
4239 }
4240 if (nextcmd != NULL)
4241 *nextcmd = check_nextcmd(p);
4242
4243 return ret;
4244}
4245
4246/*
4247 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004248 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 *
4250 * "arg" must point to the first non-white of the expression.
4251 * "arg" is advanced to the next non-white after the recognized expression.
4252 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004253 * Note: "rettv.v_lock" is not set.
4254 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 * Return OK or FAIL.
4256 */
4257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004258eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004261 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /*
4264 * Get the first variable.
4265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return FAIL;
4268
4269 if ((*arg)[0] == '?')
4270 {
4271 result = FALSE;
4272 if (evaluate)
4273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 int error = FALSE;
4275
4276 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Check for the ":".
4292 */
4293 if ((*arg)[0] != ':')
4294 {
4295 EMSG(_("E109: Missing ':' after '?'"));
4296 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299 }
4300
4301 /*
4302 * Get the third variable.
4303 */
4304 *arg = skipwhite(*arg + 1);
4305 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4306 {
4307 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310 }
4311 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
4315 return OK;
4316}
4317
4318/*
4319 * Handle first level expression:
4320 * expr2 || expr2 || expr2 logical OR
4321 *
4322 * "arg" must point to the first non-white of the expression.
4323 * "arg" is advanced to the next non-white after the recognized expression.
4324 *
4325 * Return OK or FAIL.
4326 */
4327 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004328eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar33570922005-01-25 22:26:29 +00004330 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 long result;
4332 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * Get the first variable.
4337 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 return FAIL;
4340
4341 /*
4342 * Repeat until there is no following "||".
4343 */
4344 first = TRUE;
4345 result = FALSE;
4346 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4347 {
4348 if (evaluate && first)
4349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004352 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 if (error)
4354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 first = FALSE;
4356 }
4357
4358 /*
4359 * Get the second variable.
4360 */
4361 *arg = skipwhite(*arg + 2);
4362 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4363 return FAIL;
4364
4365 /*
4366 * Compute the result.
4367 */
4368 if (evaluate && !result)
4369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004370 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 if (error)
4374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 if (evaluate)
4377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 rettv->v_type = VAR_NUMBER;
4379 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381 }
4382
4383 return OK;
4384}
4385
4386/*
4387 * Handle second level expression:
4388 * expr3 && expr3 && expr3 logical AND
4389 *
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to the next non-white after the recognized expression.
4392 *
4393 * Return OK or FAIL.
4394 */
4395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004396eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 long result;
4400 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 /*
4404 * Get the first variable.
4405 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 return FAIL;
4408
4409 /*
4410 * Repeat until there is no following "&&".
4411 */
4412 first = TRUE;
4413 result = TRUE;
4414 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4415 {
4416 if (evaluate && first)
4417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004418 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 if (error)
4422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 first = FALSE;
4424 }
4425
4426 /*
4427 * Get the second variable.
4428 */
4429 *arg = skipwhite(*arg + 2);
4430 if (eval4(arg, &var2, evaluate && result) == FAIL)
4431 return FAIL;
4432
4433 /*
4434 * Compute the result.
4435 */
4436 if (evaluate && result)
4437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (error)
4442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 if (evaluate)
4445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 rettv->v_type = VAR_NUMBER;
4447 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
4450
4451 return OK;
4452}
4453
4454/*
4455 * Handle third level expression:
4456 * var1 == var2
4457 * var1 =~ var2
4458 * var1 != var2
4459 * var1 !~ var2
4460 * var1 > var2
4461 * var1 >= var2
4462 * var1 < var2
4463 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 * var1 is var2
4465 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 *
4467 * "arg" must point to the first non-white of the expression.
4468 * "arg" is advanced to the next non-white after the recognized expression.
4469 *
4470 * Return OK or FAIL.
4471 */
4472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004473eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 char_u *p;
4477 int i;
4478 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004481 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 char_u *s1, *s2;
4483 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 /*
4487 * Get the first variable.
4488 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return FAIL;
4491
4492 p = *arg;
4493 switch (p[0])
4494 {
4495 case '=': if (p[1] == '=')
4496 type = TYPE_EQUAL;
4497 else if (p[1] == '~')
4498 type = TYPE_MATCH;
4499 break;
4500 case '!': if (p[1] == '=')
4501 type = TYPE_NEQUAL;
4502 else if (p[1] == '~')
4503 type = TYPE_NOMATCH;
4504 break;
4505 case '>': if (p[1] != '=')
4506 {
4507 type = TYPE_GREATER;
4508 len = 1;
4509 }
4510 else
4511 type = TYPE_GEQUAL;
4512 break;
4513 case '<': if (p[1] != '=')
4514 {
4515 type = TYPE_SMALLER;
4516 len = 1;
4517 }
4518 else
4519 type = TYPE_SEQUAL;
4520 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 case 'i': if (p[1] == 's')
4522 {
4523 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4524 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004525 i = p[len];
4526 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 {
4528 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4529 type_is = TRUE;
4530 }
4531 }
4532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534
4535 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
4538 if (type != TYPE_UNKNOWN)
4539 {
4540 /* extra question mark appended: ignore case */
4541 if (p[len] == '?')
4542 {
4543 ic = TRUE;
4544 ++len;
4545 }
4546 /* extra '#' appended: match case */
4547 else if (p[len] == '#')
4548 {
4549 ic = FALSE;
4550 ++len;
4551 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004552 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
4554 ic = p_ic;
4555
4556 /*
4557 * Get the second variable.
4558 */
4559 *arg = skipwhite(p + len);
4560 if (eval5(arg, &var2, evaluate) == FAIL)
4561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return FAIL;
4564 }
4565
4566 if (evaluate)
4567 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 if (type_is && rettv->v_type != var2.v_type)
4569 {
4570 /* For "is" a different type always means FALSE, for "notis"
4571 * it means TRUE. */
4572 n1 = (type == TYPE_NEQUAL);
4573 }
4574 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4575 {
4576 if (type_is)
4577 {
4578 n1 = (rettv->v_type == var2.v_type
4579 && rettv->vval.v_list == var2.vval.v_list);
4580 if (type == TYPE_NEQUAL)
4581 n1 = !n1;
4582 }
4583 else if (rettv->v_type != var2.v_type
4584 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4585 {
4586 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004587 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004588 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004589 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 clear_tv(rettv);
4591 clear_tv(&var2);
4592 return FAIL;
4593 }
4594 else
4595 {
4596 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004597 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4598 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 if (type == TYPE_NEQUAL)
4600 n1 = !n1;
4601 }
4602 }
4603
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004604 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4605 {
4606 if (type_is)
4607 {
4608 n1 = (rettv->v_type == var2.v_type
4609 && rettv->vval.v_dict == var2.vval.v_dict);
4610 if (type == TYPE_NEQUAL)
4611 n1 = !n1;
4612 }
4613 else if (rettv->v_type != var2.v_type
4614 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4615 {
4616 if (rettv->v_type != var2.v_type)
4617 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4618 else
4619 EMSG(_("E736: Invalid operation for Dictionary"));
4620 clear_tv(rettv);
4621 clear_tv(&var2);
4622 return FAIL;
4623 }
4624 else
4625 {
4626 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004627 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4628 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004629 if (type == TYPE_NEQUAL)
4630 n1 = !n1;
4631 }
4632 }
4633
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004634 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4635 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004636 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004637 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004638 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004639 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004640 clear_tv(rettv);
4641 clear_tv(&var2);
4642 return FAIL;
4643 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004644 if ((rettv->v_type == VAR_PARTIAL
4645 && rettv->vval.v_partial == NULL)
4646 || (var2.v_type == VAR_PARTIAL
4647 && var2.vval.v_partial == NULL))
4648 /* when a partial is NULL assume not equal */
4649 n1 = FALSE;
4650 else if (type_is)
4651 {
4652 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4653 /* strings are considered the same if their value is
4654 * the same */
4655 n1 = tv_equal(rettv, &var2, ic, FALSE);
4656 else if (rettv->v_type == VAR_PARTIAL
4657 && var2.v_type == VAR_PARTIAL)
4658 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4659 else
4660 n1 = FALSE;
4661 }
4662 else
4663 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004664 if (type == TYPE_NEQUAL)
4665 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004666 }
4667
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004668#ifdef FEAT_FLOAT
4669 /*
4670 * If one of the two variables is a float, compare as a float.
4671 * When using "=~" or "!~", always compare as string.
4672 */
4673 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4674 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4675 {
4676 float_T f1, f2;
4677
4678 if (rettv->v_type == VAR_FLOAT)
4679 f1 = rettv->vval.v_float;
4680 else
4681 f1 = get_tv_number(rettv);
4682 if (var2.v_type == VAR_FLOAT)
4683 f2 = var2.vval.v_float;
4684 else
4685 f2 = get_tv_number(&var2);
4686 n1 = FALSE;
4687 switch (type)
4688 {
4689 case TYPE_EQUAL: n1 = (f1 == f2); break;
4690 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4691 case TYPE_GREATER: n1 = (f1 > f2); break;
4692 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4693 case TYPE_SMALLER: n1 = (f1 < f2); break;
4694 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4695 case TYPE_UNKNOWN:
4696 case TYPE_MATCH:
4697 case TYPE_NOMATCH: break; /* avoid gcc warning */
4698 }
4699 }
4700#endif
4701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
4703 * If one of the two variables is a number, compare as a number.
4704 * When using "=~" or "!~", always compare as string.
4705 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 n1 = get_tv_number(rettv);
4710 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 switch (type)
4712 {
4713 case TYPE_EQUAL: n1 = (n1 == n2); break;
4714 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4715 case TYPE_GREATER: n1 = (n1 > n2); break;
4716 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4717 case TYPE_SMALLER: n1 = (n1 < n2); break;
4718 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4719 case TYPE_UNKNOWN:
4720 case TYPE_MATCH:
4721 case TYPE_NOMATCH: break; /* avoid gcc warning */
4722 }
4723 }
4724 else
4725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 s1 = get_tv_string_buf(rettv, buf1);
4727 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4729 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4730 else
4731 i = 0;
4732 n1 = FALSE;
4733 switch (type)
4734 {
4735 case TYPE_EQUAL: n1 = (i == 0); break;
4736 case TYPE_NEQUAL: n1 = (i != 0); break;
4737 case TYPE_GREATER: n1 = (i > 0); break;
4738 case TYPE_GEQUAL: n1 = (i >= 0); break;
4739 case TYPE_SMALLER: n1 = (i < 0); break;
4740 case TYPE_SEQUAL: n1 = (i <= 0); break;
4741
4742 case TYPE_MATCH:
4743 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004744 n1 = pattern_match(s2, s1, ic);
4745 if (type == TYPE_NOMATCH)
4746 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 break;
4748
4749 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4750 }
4751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 clear_tv(&var2);
4754 rettv->v_type = VAR_NUMBER;
4755 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 }
4758
4759 return OK;
4760}
4761
4762/*
4763 * Handle fourth level expression:
4764 * + number addition
4765 * - number subtraction
4766 * . string concatenation
4767 *
4768 * "arg" must point to the first non-white of the expression.
4769 * "arg" is advanced to the next non-white after the recognized expression.
4770 *
4771 * Return OK or FAIL.
4772 */
4773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004774eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar33570922005-01-25 22:26:29 +00004776 typval_T var2;
4777 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004779 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#ifdef FEAT_FLOAT
4781 float_T f1 = 0, f2 = 0;
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 char_u *s1, *s2;
4784 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4785 char_u *p;
4786
4787 /*
4788 * Get the first variable.
4789 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return FAIL;
4792
4793 /*
4794 * Repeat computing, until no '+', '-' or '.' is following.
4795 */
4796 for (;;)
4797 {
4798 op = **arg;
4799 if (op != '+' && op != '-' && op != '.')
4800 break;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 if ((op != '+' || rettv->v_type != VAR_LIST)
4803#ifdef FEAT_FLOAT
4804 && (op == '.' || rettv->v_type != VAR_FLOAT)
4805#endif
4806 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 {
4808 /* For "list + ...", an illegal use of the first operand as
4809 * a number cannot be determined before evaluating the 2nd
4810 * operand: if this is also a list, all is ok.
4811 * For "something . ...", "something - ..." or "non-list + ...",
4812 * we know that the first operand needs to be a string or number
4813 * without evaluating the 2nd operand. So check before to avoid
4814 * side effects after an error. */
4815 if (evaluate && get_tv_string_chk(rettv) == NULL)
4816 {
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820 }
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * Get the second variable.
4824 */
4825 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return FAIL;
4830 }
4831
4832 if (evaluate)
4833 {
4834 /*
4835 * Compute the result.
4836 */
4837 if (op == '.')
4838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4840 s2 = get_tv_string_buf_chk(&var2, buf2);
4841 if (s2 == NULL) /* type error ? */
4842 {
4843 clear_tv(rettv);
4844 clear_tv(&var2);
4845 return FAIL;
4846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004847 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
4849 rettv->v_type = VAR_STRING;
4850 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004852 else if (op == '+' && rettv->v_type == VAR_LIST
4853 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004854 {
4855 /* concatenate Lists */
4856 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4857 &var3) == FAIL)
4858 {
4859 clear_tv(rettv);
4860 clear_tv(&var2);
4861 return FAIL;
4862 }
4863 clear_tv(rettv);
4864 *rettv = var3;
4865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 else
4867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004868 int error = FALSE;
4869
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#ifdef FEAT_FLOAT
4871 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 f1 = rettv->vval.v_float;
4874 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 else
4877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 n1 = get_tv_number_chk(rettv, &error);
4880 if (error)
4881 {
4882 /* This can only happen for "list + non-list". For
4883 * "non-list + ..." or "something - ...", we returned
4884 * before evaluating the 2nd operand. */
4885 clear_tv(rettv);
4886 return FAIL;
4887 }
4888#ifdef FEAT_FLOAT
4889 if (var2.v_type == VAR_FLOAT)
4890 f1 = n1;
4891#endif
4892 }
4893#ifdef FEAT_FLOAT
4894 if (var2.v_type == VAR_FLOAT)
4895 {
4896 f2 = var2.vval.v_float;
4897 n2 = 0;
4898 }
4899 else
4900#endif
4901 {
4902 n2 = get_tv_number_chk(&var2, &error);
4903 if (error)
4904 {
4905 clear_tv(rettv);
4906 clear_tv(&var2);
4907 return FAIL;
4908 }
4909#ifdef FEAT_FLOAT
4910 if (rettv->v_type == VAR_FLOAT)
4911 f2 = n2;
4912#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915
4916#ifdef FEAT_FLOAT
4917 /* If there is a float on either side the result is a float. */
4918 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4919 {
4920 if (op == '+')
4921 f1 = f1 + f2;
4922 else
4923 f1 = f1 - f2;
4924 rettv->v_type = VAR_FLOAT;
4925 rettv->vval.v_float = f1;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928#endif
4929 {
4930 if (op == '+')
4931 n1 = n1 + n2;
4932 else
4933 n1 = n1 - n2;
4934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = n1;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 }
4941 return OK;
4942}
4943
4944/*
4945 * Handle fifth level expression:
4946 * * number multiplication
4947 * / number division
4948 * % number modulo
4949 *
4950 * "arg" must point to the first non-white of the expression.
4951 * "arg" is advanced to the next non-white after the recognized expression.
4952 *
4953 * Return OK or FAIL.
4954 */
4955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004956eval6(
4957 char_u **arg,
4958 typval_T *rettv,
4959 int evaluate,
4960 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
Bram Moolenaar33570922005-01-25 22:26:29 +00004962 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004964 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965#ifdef FEAT_FLOAT
4966 int use_float = FALSE;
4967 float_T f1 = 0, f2;
4968#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /*
4972 * Get the first variable.
4973 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004974 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return FAIL;
4976
4977 /*
4978 * Repeat computing, until no '*', '/' or '%' is following.
4979 */
4980 for (;;)
4981 {
4982 op = **arg;
4983 if (op != '*' && op != '/' && op != '%')
4984 break;
4985
4986 if (evaluate)
4987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#ifdef FEAT_FLOAT
4989 if (rettv->v_type == VAR_FLOAT)
4990 {
4991 f1 = rettv->vval.v_float;
4992 use_float = TRUE;
4993 n1 = 0;
4994 }
4995 else
4996#endif
4997 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004999 if (error)
5000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 else
5003 n1 = 0;
5004
5005 /*
5006 * Get the second variable.
5007 */
5008 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005009 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 return FAIL;
5011
5012 if (evaluate)
5013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014#ifdef FEAT_FLOAT
5015 if (var2.v_type == VAR_FLOAT)
5016 {
5017 if (!use_float)
5018 {
5019 f1 = n1;
5020 use_float = TRUE;
5021 }
5022 f2 = var2.vval.v_float;
5023 n2 = 0;
5024 }
5025 else
5026#endif
5027 {
5028 n2 = get_tv_number_chk(&var2, &error);
5029 clear_tv(&var2);
5030 if (error)
5031 return FAIL;
5032#ifdef FEAT_FLOAT
5033 if (use_float)
5034 f2 = n2;
5035#endif
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 /*
5039 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005040 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005042#ifdef FEAT_FLOAT
5043 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 f1 = f1 * f2;
5047 else if (op == '/')
5048 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005049# ifdef VMS
5050 /* VMS crashes on divide by zero, work around it */
5051 if (f2 == 0.0)
5052 {
5053 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005054 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005055 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005056 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005057 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005058 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005059 }
5060 else
5061 f1 = f1 / f2;
5062# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 /* We rely on the floating point library to handle divide
5064 * by zero to result in "inf" and not a crash. */
5065 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005066# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005069 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005070 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 return FAIL;
5072 }
5073 rettv->v_type = VAR_FLOAT;
5074 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 if (op == '*')
5080 n1 = n1 * n2;
5081 else if (op == '/')
5082 {
5083 if (n2 == 0) /* give an error message? */
5084 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005085#ifdef FEAT_NUM64
5086 if (n1 == 0)
5087 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5088 else if (n1 < 0)
5089 n1 = -0x7fffffffffffffff;
5090 else
5091 n1 = 0x7fffffffffffffff;
5092#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 if (n1 == 0)
5094 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5095 else if (n1 < 0)
5096 n1 = -0x7fffffffL;
5097 else
5098 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005099#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 }
5101 else
5102 n1 = n1 / n2;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106 if (n2 == 0) /* give an error message? */
5107 n1 = 0;
5108 else
5109 n1 = n1 % n2;
5110 }
5111 rettv->v_type = VAR_NUMBER;
5112 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 }
5116
5117 return OK;
5118}
5119
5120/*
5121 * Handle sixth level expression:
5122 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005123 * "string" string constant
5124 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 * &option-name option value
5126 * @r register contents
5127 * identifier variable value
5128 * function() function call
5129 * $VAR environment variable
5130 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005131 * [expr, expr] List
5132 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 *
5134 * Also handle:
5135 * ! in front logical NOT
5136 * - in front unary minus
5137 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 * trailing [] subscript in String or List
5139 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 *
5141 * "arg" must point to the first non-white of the expression.
5142 * "arg" is advanced to the next non-white after the recognized expression.
5143 *
5144 * Return OK or FAIL.
5145 */
5146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147eval7(
5148 char_u **arg,
5149 typval_T *rettv,
5150 int evaluate,
5151 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005153 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 int len;
5155 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 char_u *start_leader, *end_leader;
5157 int ret = OK;
5158 char_u *alias;
5159
5160 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 /*
5167 * Skip '!' and '-' characters. They are handled later.
5168 */
5169 start_leader = *arg;
5170 while (**arg == '!' || **arg == '-' || **arg == '+')
5171 *arg = skipwhite(*arg + 1);
5172 end_leader = *arg;
5173
5174 switch (**arg)
5175 {
5176 /*
5177 * Number constant.
5178 */
5179 case '0':
5180 case '1':
5181 case '2':
5182 case '3':
5183 case '4':
5184 case '5':
5185 case '6':
5186 case '7':
5187 case '8':
5188 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189 {
5190#ifdef FEAT_FLOAT
5191 char_u *p = skipdigits(*arg + 1);
5192 int get_float = FALSE;
5193
5194 /* We accept a float when the format matches
5195 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005196 * strict to avoid backwards compatibility problems.
5197 * Don't look for a float after the "." operator, so that
5198 * ":let vers = 1.2.3" doesn't fail. */
5199 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 get_float = TRUE;
5202 p = skipdigits(p + 2);
5203 if (*p == 'e' || *p == 'E')
5204 {
5205 ++p;
5206 if (*p == '-' || *p == '+')
5207 ++p;
5208 if (!vim_isdigit(*p))
5209 get_float = FALSE;
5210 else
5211 p = skipdigits(p + 1);
5212 }
5213 if (ASCII_ISALPHA(*p) || *p == '.')
5214 get_float = FALSE;
5215 }
5216 if (get_float)
5217 {
5218 float_T f;
5219
5220 *arg += string2float(*arg, &f);
5221 if (evaluate)
5222 {
5223 rettv->v_type = VAR_FLOAT;
5224 rettv->vval.v_float = f;
5225 }
5226 }
5227 else
5228#endif
5229 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005230 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005231 *arg += len;
5232 if (evaluate)
5233 {
5234 rettv->v_type = VAR_NUMBER;
5235 rettv->vval.v_number = n;
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 /*
5242 * String constant: "string".
5243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 break;
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005250 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 break;
5252
5253 /*
5254 * List: [expr, expr]
5255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 break;
5258
5259 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 * Dictionary: {key: val, key: val}
5261 */
5262 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5263 break;
5264
5265 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005266 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005268 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 break;
5270
5271 /*
5272 * Environment variable: $VAR.
5273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 break;
5276
5277 /*
5278 * Register contents: @r.
5279 */
5280 case '@': ++*arg;
5281 if (evaluate)
5282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005283 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005284 rettv->vval.v_string = get_reg_contents(**arg,
5285 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 if (**arg != NUL)
5288 ++*arg;
5289 break;
5290
5291 /*
5292 * nested expression: (expression).
5293 */
5294 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005295 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (**arg == ')')
5297 ++*arg;
5298 else if (ret == OK)
5299 {
5300 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 ret = FAIL;
5303 }
5304 break;
5305
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 break;
5308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309
5310 if (ret == NOTDONE)
5311 {
5312 /*
5313 * Must be a variable or function name.
5314 * Can also be a curly-braces kind of name: {expr}.
5315 */
5316 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 if (alias != NULL)
5319 s = alias;
5320
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 ret = FAIL;
5323 else
5324 {
5325 if (**arg == '(') /* recursive! */
5326 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005327 partial_T *partial;
5328
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329 /* If "s" is the name of a variable of type VAR_FUNC
5330 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005331 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332
5333 /* Invoke the function. */
5334 ret = get_func_tv(s, len, rettv, arg,
5335 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005336 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005337
5338 /* If evaluate is FALSE rettv->v_type was not set in
5339 * get_func_tv, but it's needed in handle_subscript() to parse
5340 * what follows. So set it here. */
5341 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5342 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005343 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005344 rettv->v_type = VAR_FUNC;
5345 }
5346
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 /* Stop the expression evaluation when immediately
5348 * aborting on error, or when an interrupt occurred or
5349 * an exception was thrown but not caught. */
5350 if (aborting())
5351 {
5352 if (ret == OK)
5353 clear_tv(rettv);
5354 ret = FAIL;
5355 }
5356 }
5357 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005358 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005359 else
5360 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005362 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 }
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 *arg = skipwhite(*arg);
5366
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5368 * expr(expr). */
5369 if (ret == OK)
5370 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /*
5373 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5374 */
5375 if (ret == OK && evaluate && end_leader > start_leader)
5376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005378 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005379#ifdef FEAT_FLOAT
5380 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005382 if (rettv->v_type == VAR_FLOAT)
5383 f = rettv->vval.v_float;
5384 else
5385#endif
5386 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005389 clear_tv(rettv);
5390 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005392 else
5393 {
5394 while (end_leader > start_leader)
5395 {
5396 --end_leader;
5397 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005398 {
5399#ifdef FEAT_FLOAT
5400 if (rettv->v_type == VAR_FLOAT)
5401 f = !f;
5402 else
5403#endif
5404 val = !val;
5405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005406 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005407 {
5408#ifdef FEAT_FLOAT
5409 if (rettv->v_type == VAR_FLOAT)
5410 f = -f;
5411 else
5412#endif
5413 val = -val;
5414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005415 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005416#ifdef FEAT_FLOAT
5417 if (rettv->v_type == VAR_FLOAT)
5418 {
5419 clear_tv(rettv);
5420 rettv->vval.v_float = f;
5421 }
5422 else
5423#endif
5424 {
5425 clear_tv(rettv);
5426 rettv->v_type = VAR_NUMBER;
5427 rettv->vval.v_number = val;
5428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
5432 return ret;
5433}
5434
5435/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005436 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5437 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5439 */
5440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005441eval_index(
5442 char_u **arg,
5443 typval_T *rettv,
5444 int evaluate,
5445 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446{
5447 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 long len = -1;
5451 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454
Bram Moolenaara03f2332016-02-06 18:09:59 +01005455 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005457 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005458 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005459 if (verbose)
5460 EMSG(_("E695: Cannot index a Funcref"));
5461 return FAIL;
5462 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005463#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005464 if (verbose)
5465 EMSG(_(e_float_as_string));
5466 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005467#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005468 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005470 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005471 if (verbose)
5472 EMSG(_("E909: Cannot index a special variable"));
5473 return FAIL;
5474 case VAR_UNKNOWN:
5475 if (evaluate)
5476 return FAIL;
5477 /* FALLTHROUGH */
5478
5479 case VAR_STRING:
5480 case VAR_NUMBER:
5481 case VAR_LIST:
5482 case VAR_DICT:
5483 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005486 init_tv(&var1);
5487 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 /*
5491 * dict.name
5492 */
5493 key = *arg + 1;
5494 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5495 ;
5496 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 /*
5503 * something[idx]
5504 *
5505 * Get the (first) variable from inside the [].
5506 */
5507 *arg = skipwhite(*arg + 1);
5508 if (**arg == ':')
5509 empty1 = TRUE;
5510 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5511 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005512 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5513 {
5514 /* not a number or string */
5515 clear_tv(&var1);
5516 return FAIL;
5517 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518
5519 /*
5520 * Get the second variable from inside the [:].
5521 */
5522 if (**arg == ':')
5523 {
5524 range = TRUE;
5525 *arg = skipwhite(*arg + 1);
5526 if (**arg == ']')
5527 empty2 = TRUE;
5528 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005530 if (!empty1)
5531 clear_tv(&var1);
5532 return FAIL;
5533 }
5534 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5535 {
5536 /* not a number or string */
5537 if (!empty1)
5538 clear_tv(&var1);
5539 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 return FAIL;
5541 }
5542 }
5543
5544 /* Check for the ']'. */
5545 if (**arg != ']')
5546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005547 if (verbose)
5548 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005549 clear_tv(&var1);
5550 if (range)
5551 clear_tv(&var2);
5552 return FAIL;
5553 }
5554 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556
5557 if (evaluate)
5558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 n1 = 0;
5560 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 n1 = get_tv_number(&var1);
5563 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 if (range)
5566 {
5567 if (empty2)
5568 n2 = -1;
5569 else
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 n2 = get_tv_number(&var2);
5572 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 }
5575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005578 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005579 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005580 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005581 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 case VAR_SPECIAL:
5583 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005584 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 break; /* not evaluating, skipping over subscript */
5586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 case VAR_NUMBER:
5588 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 len = (long)STRLEN(s);
5591 if (range)
5592 {
5593 /* The resulting variable is a substring. If the indexes
5594 * are out of range the result is empty. */
5595 if (n1 < 0)
5596 {
5597 n1 = len + n1;
5598 if (n1 < 0)
5599 n1 = 0;
5600 }
5601 if (n2 < 0)
5602 n2 = len + n2;
5603 else if (n2 >= len)
5604 n2 = len;
5605 if (n1 >= len || n2 < 0 || n1 > n2)
5606 s = NULL;
5607 else
5608 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5609 }
5610 else
5611 {
5612 /* The resulting variable is a string of a single
5613 * character. If the index is too big or negative the
5614 * result is empty. */
5615 if (n1 >= len || n1 < 0)
5616 s = NULL;
5617 else
5618 s = vim_strnsave(s + n1, 1);
5619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 clear_tv(rettv);
5621 rettv->v_type = VAR_STRING;
5622 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 break;
5624
5625 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 if (n1 < 0)
5628 n1 = len + n1;
5629 if (!empty1 && (n1 < 0 || n1 >= len))
5630 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005631 /* For a range we allow invalid values and return an empty
5632 * list. A list index out of range is an error. */
5633 if (!range)
5634 {
5635 if (verbose)
5636 EMSGN(_(e_listidx), n1);
5637 return FAIL;
5638 }
5639 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 }
5641 if (range)
5642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l;
5644 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645
5646 if (n2 < 0)
5647 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005648 else if (n2 >= len)
5649 n2 = len - 1;
5650 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005651 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 l = list_alloc();
5653 if (l == NULL)
5654 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 n1 <= n2; ++n1)
5657 {
5658 if (list_append_tv(l, &item->li_tv) == FAIL)
5659 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005660 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005661 return FAIL;
5662 }
5663 item = item->li_next;
5664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 clear_tv(rettv);
5666 rettv->v_type = VAR_LIST;
5667 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005668 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 }
5670 else
5671 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005672 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 clear_tv(rettv);
5674 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 }
5676 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677
5678 case VAR_DICT:
5679 if (range)
5680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 if (verbose)
5682 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 if (len == -1)
5684 clear_tv(&var1);
5685 return FAIL;
5686 }
5687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005688 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689
5690 if (len == -1)
5691 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005692 key = get_tv_string_chk(&var1);
5693 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 clear_tv(&var1);
5696 return FAIL;
5697 }
5698 }
5699
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005700 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005702 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005703 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 if (len == -1)
5705 clear_tv(&var1);
5706 if (item == NULL)
5707 return FAIL;
5708
5709 copy_tv(&item->di_tv, &var1);
5710 clear_tv(rettv);
5711 *rettv = var1;
5712 }
5713 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 }
5715 }
5716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 return OK;
5718}
5719
5720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 * Get an option value.
5722 * "arg" points to the '&' or '+' before the option name.
5723 * "arg" is advanced to character after the option name.
5724 * Return OK or FAIL.
5725 */
5726 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727get_option_tv(
5728 char_u **arg,
5729 typval_T *rettv, /* when NULL, only check if option exists */
5730 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 char_u *option_end;
5733 long numval;
5734 char_u *stringval;
5735 int opt_type;
5736 int c;
5737 int working = (**arg == '+'); /* has("+option") */
5738 int ret = OK;
5739 int opt_flags;
5740
5741 /*
5742 * Isolate the option name and find its value.
5743 */
5744 option_end = find_option_end(arg, &opt_flags);
5745 if (option_end == NULL)
5746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005747 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 EMSG2(_("E112: Option name missing: %s"), *arg);
5749 return FAIL;
5750 }
5751
5752 if (!evaluate)
5753 {
5754 *arg = option_end;
5755 return OK;
5756 }
5757
5758 c = *option_end;
5759 *option_end = NUL;
5760 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 if (opt_type == -3) /* invalid name */
5764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 EMSG2(_("E113: Unknown option: %s"), *arg);
5767 ret = FAIL;
5768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 if (opt_type == -2) /* hidden string option */
5772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 rettv->v_type = VAR_STRING;
5774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 else if (opt_type == -1) /* hidden number option */
5777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005778 rettv->v_type = VAR_NUMBER;
5779 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 else if (opt_type == 1) /* number option */
5782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005783 rettv->v_type = VAR_NUMBER;
5784 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786 else /* string option */
5787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005788 rettv->v_type = VAR_STRING;
5789 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 }
5792 else if (working && (opt_type == -2 || opt_type == -1))
5793 ret = FAIL;
5794
5795 *option_end = c; /* put back for error messages */
5796 *arg = option_end;
5797
5798 return ret;
5799}
5800
5801/*
5802 * Allocate a variable for a string constant.
5803 * Return OK or FAIL.
5804 */
5805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005806get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 char_u *p;
5809 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 int extra = 0;
5811
5812 /*
5813 * Find the end of the string, skipping backslashed characters.
5814 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
5817 if (*p == '\\' && p[1] != NUL)
5818 {
5819 ++p;
5820 /* A "\<x>" form occupies at least 4 characters, and produces up
5821 * to 6 characters: reserve space for 2 extra */
5822 if (*p == '<')
5823 extra += 2;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826
5827 if (*p != '"')
5828 {
5829 EMSG2(_("E114: Missing quote: %s"), *arg);
5830 return FAIL;
5831 }
5832
5833 /* If only parsing, set *arg and return here */
5834 if (!evaluate)
5835 {
5836 *arg = p + 1;
5837 return OK;
5838 }
5839
5840 /*
5841 * Copy the string into allocated memory, handling backslashed
5842 * characters.
5843 */
5844 name = alloc((unsigned)(p - *arg + extra));
5845 if (name == NULL)
5846 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 rettv->v_type = VAR_STRING;
5848 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
5852 if (*p == '\\')
5853 {
5854 switch (*++p)
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 case 'b': *name++ = BS; ++p; break;
5857 case 'e': *name++ = ESC; ++p; break;
5858 case 'f': *name++ = FF; ++p; break;
5859 case 'n': *name++ = NL; ++p; break;
5860 case 'r': *name++ = CAR; ++p; break;
5861 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 case 'X': /* hex: "\x1", "\x12" */
5864 case 'x':
5865 case 'u': /* Unicode: "\u0023" */
5866 case 'U':
5867 if (vim_isxdigit(p[1]))
5868 {
5869 int n, nr;
5870 int c = toupper(*p);
5871
5872 if (c == 'X')
5873 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005874 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005876 else
5877 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 nr = 0;
5879 while (--n >= 0 && vim_isxdigit(p[1]))
5880 {
5881 ++p;
5882 nr = (nr << 4) + hex2nr(*p);
5883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#ifdef FEAT_MBYTE
5886 /* For "\u" store the number according to
5887 * 'encoding'. */
5888 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 else
5891#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 break;
5895
5896 /* octal: "\1", "\12", "\123" */
5897 case '0':
5898 case '1':
5899 case '2':
5900 case '3':
5901 case '4':
5902 case '5':
5903 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 case '7': *name = *p++ - '0';
5905 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 *name = (*name << 3) + *p++ - '0';
5908 if (*p >= '0' && *p <= '7')
5909 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 break;
5913
5914 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (extra != 0)
5917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 break;
5920 }
5921 /* FALLTHROUGH */
5922
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 break;
5925 }
5926 }
5927 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 *arg = p + 1;
5933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 return OK;
5935}
5936
5937/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * Return OK or FAIL.
5940 */
5941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 int reduce = 0;
5947
5948 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 */
5951 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956 break;
5957 ++reduce;
5958 ++p;
5959 }
5960 }
5961
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 return FAIL;
5966 }
5967
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 if (!evaluate)
5970 {
5971 *arg = p + 1;
5972 return OK;
5973 }
5974
5975 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 */
5978 str = alloc((unsigned)((p - *arg) - reduce));
5979 if (str == NULL)
5980 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 rettv->v_type = VAR_STRING;
5982 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005986 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 break;
5990 ++p;
5991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 *arg = p + 1;
5996
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 return OK;
5998}
5999
Bram Moolenaarddecc252016-04-06 22:59:37 +02006000 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006001partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006002{
6003 int i;
6004
6005 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006006 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006007 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006008 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006009 func_unref(pt->pt_name);
6010 vim_free(pt->pt_name);
6011 vim_free(pt);
6012}
6013
6014/*
6015 * Unreference a closure: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
6018 void
6019partial_unref(partial_T *pt)
6020{
6021 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006022 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006023}
6024
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 * Allocate a variable for a List and fill it from "*arg".
6027 * Return OK or FAIL.
6028 */
6029 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 list_T *l = NULL;
6033 typval_T tv;
6034 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
6036 if (evaluate)
6037 {
6038 l = list_alloc();
6039 if (l == NULL)
6040 return FAIL;
6041 }
6042
6043 *arg = skipwhite(*arg + 1);
6044 while (**arg != ']' && **arg != NUL)
6045 {
6046 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6047 goto failret;
6048 if (evaluate)
6049 {
6050 item = listitem_alloc();
6051 if (item != NULL)
6052 {
6053 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006054 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 list_append(l, item);
6056 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006057 else
6058 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 }
6060
6061 if (**arg == ']')
6062 break;
6063 if (**arg != ',')
6064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006065 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 goto failret;
6067 }
6068 *arg = skipwhite(*arg + 1);
6069 }
6070
6071 if (**arg != ']')
6072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074failret:
6075 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006076 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 return FAIL;
6078 }
6079
6080 *arg = skipwhite(*arg + 1);
6081 if (evaluate)
6082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006083 rettv->v_type = VAR_LIST;
6084 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 ++l->lv_refcount;
6086 }
6087
6088 return OK;
6089}
6090
6091/*
6092 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006093 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006095 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006098 list_T *l;
6099
6100 l = (list_T *)alloc_clear(sizeof(list_T));
6101 if (l != NULL)
6102 {
6103 /* Prepend the list to the list of lists for garbage collection. */
6104 if (first_list != NULL)
6105 first_list->lv_used_prev = l;
6106 l->lv_used_prev = NULL;
6107 l->lv_used_next = first_list;
6108 first_list = l;
6109 }
6110 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111}
6112
6113/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006114 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006115 * Returns OK or FAIL.
6116 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006117 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006119{
6120 list_T *l = list_alloc();
6121
6122 if (l == NULL)
6123 return FAIL;
6124
6125 rettv->vval.v_list = l;
6126 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006127 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006128 ++l->lv_refcount;
6129 return OK;
6130}
6131
6132/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 * Unreference a list: decrement the reference count and free it when it
6134 * becomes zero.
6135 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006137list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006139 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006140 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141}
6142
6143/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006144 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 * Ignores the reference count.
6146 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006147 static void
6148list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149{
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006152 for (item = l->lv_first; item != NULL; item = l->lv_first)
6153 {
6154 /* Remove the item before deleting it. */
6155 l->lv_first = item->li_next;
6156 clear_tv(&item->li_tv);
6157 vim_free(item);
6158 }
6159}
6160
6161 static void
6162list_free_list(list_T *l)
6163{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006164 /* Remove the list from the list of lists for garbage collection. */
6165 if (l->lv_used_prev == NULL)
6166 first_list = l->lv_used_next;
6167 else
6168 l->lv_used_prev->lv_used_next = l->lv_used_next;
6169 if (l->lv_used_next != NULL)
6170 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 vim_free(l);
6173}
6174
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006175 void
6176list_free(list_T *l)
6177{
6178 if (!in_free_unref_items)
6179 {
6180 list_free_contents(l);
6181 list_free_list(l);
6182 }
6183}
6184
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185/*
6186 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006187 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006190listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193}
6194
6195/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006196 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006201 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 vim_free(item);
6203}
6204
6205/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006206 * Remove a list item from a List and free it. Also clears the value.
6207 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006210{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006211 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006212 listitem_free(item);
6213}
6214
6215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 * Get the number of items in a list.
6217 */
6218 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006219list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 if (l == NULL)
6222 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006223 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224}
6225
6226/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 * Return TRUE when two lists have exactly the same values.
6228 */
6229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230list_equal(
6231 list_T *l1,
6232 list_T *l2,
6233 int ic, /* ignore case for strings */
6234 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235{
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006237
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006238 if (l1 == NULL || l2 == NULL)
6239 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006240 if (l1 == l2)
6241 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 if (list_len(l1) != list_len(l2))
6243 return FALSE;
6244
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 for (item1 = l1->lv_first, item2 = l2->lv_first;
6246 item1 != NULL && item2 != NULL;
6247 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return FALSE;
6250 return item1 == NULL && item2 == NULL;
6251}
6252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006253/*
6254 * Return the dictitem that an entry in a hashtable points to.
6255 */
6256 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006258{
6259 return HI2DI(hi);
6260}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006261
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006263 * Return TRUE when two dictionaries have exactly the same key/values.
6264 */
6265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266dict_equal(
6267 dict_T *d1,
6268 dict_T *d2,
6269 int ic, /* ignore case for strings */
6270 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006271{
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 hashitem_T *hi;
6273 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006276 if (d1 == NULL && d2 == NULL)
6277 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006278 if (d1 == NULL || d2 == NULL)
6279 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006280 if (d1 == d2)
6281 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006282 if (dict_len(d1) != dict_len(d2))
6283 return FALSE;
6284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006285 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 if (!HASHITEM_EMPTY(hi))
6289 {
6290 item2 = dict_find(d2, hi->hi_key, -1);
6291 if (item2 == NULL)
6292 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006293 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006294 return FALSE;
6295 --todo;
6296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006297 }
6298 return TRUE;
6299}
6300
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006301static int tv_equal_recurse_limit;
6302
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006303 static int
6304func_equal(
6305 typval_T *tv1,
6306 typval_T *tv2,
6307 int ic) /* ignore case */
6308{
6309 char_u *s1, *s2;
6310 dict_T *d1, *d2;
6311 int a1, a2;
6312 int i;
6313
6314 /* empty and NULL function name considered the same */
6315 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6316 : tv1->vval.v_partial->pt_name;
6317 if (s1 != NULL && *s1 == NUL)
6318 s1 = NULL;
6319 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6320 : tv2->vval.v_partial->pt_name;
6321 if (s2 != NULL && *s2 == NUL)
6322 s2 = NULL;
6323 if (s1 == NULL || s2 == NULL)
6324 {
6325 if (s1 != s2)
6326 return FALSE;
6327 }
6328 else if (STRCMP(s1, s2) != 0)
6329 return FALSE;
6330
6331 /* empty dict and NULL dict is different */
6332 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6333 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6334 if (d1 == NULL || d2 == NULL)
6335 {
6336 if (d1 != d2)
6337 return FALSE;
6338 }
6339 else if (!dict_equal(d1, d2, ic, TRUE))
6340 return FALSE;
6341
6342 /* empty list and no list considered the same */
6343 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6344 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6345 if (a1 != a2)
6346 return FALSE;
6347 for (i = 0; i < a1; ++i)
6348 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6349 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6350 return FALSE;
6351
6352 return TRUE;
6353}
6354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006355/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006356 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006357 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006358 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 */
6360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361tv_equal(
6362 typval_T *tv1,
6363 typval_T *tv2,
6364 int ic, /* ignore case */
6365 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006366{
6367 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006368 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006369 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006370 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006372 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006373 * recursiveness to a limit. We guess they are equal then.
6374 * A fixed limit has the problem of still taking an awful long time.
6375 * Reduce the limit every time running into it. That should work fine for
6376 * deeply linked structures that are not recursively linked and catch
6377 * recursiveness quickly. */
6378 if (!recursive)
6379 tv_equal_recurse_limit = 1000;
6380 if (recursive_cnt >= tv_equal_recurse_limit)
6381 {
6382 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006383 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006384 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006385
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006386 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6387 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006388 if ((tv1->v_type == VAR_FUNC
6389 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6390 && (tv2->v_type == VAR_FUNC
6391 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6392 {
6393 ++recursive_cnt;
6394 r = func_equal(tv1, tv2, ic);
6395 --recursive_cnt;
6396 return r;
6397 }
6398
6399 if (tv1->v_type != tv2->v_type)
6400 return FALSE;
6401
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006402 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006404 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006405 ++recursive_cnt;
6406 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6407 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006408 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006409
6410 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006411 ++recursive_cnt;
6412 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6413 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006414 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006415
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006416 case VAR_NUMBER:
6417 return tv1->vval.v_number == tv2->vval.v_number;
6418
6419 case VAR_STRING:
6420 s1 = get_tv_string_buf(tv1, buf1);
6421 s2 = get_tv_string_buf(tv2, buf2);
6422 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006423
6424 case VAR_SPECIAL:
6425 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006426
6427 case VAR_FLOAT:
6428#ifdef FEAT_FLOAT
6429 return tv1->vval.v_float == tv2->vval.v_float;
6430#endif
6431 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006432#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006433 return tv1->vval.v_job == tv2->vval.v_job;
6434#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006435 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006437 return tv1->vval.v_channel == tv2->vval.v_channel;
6438#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006439 case VAR_FUNC:
6440 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006441 case VAR_UNKNOWN:
6442 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006444
Bram Moolenaara03f2332016-02-06 18:09:59 +01006445 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6446 * does not equal anything, not even itself. */
6447 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448}
6449
6450/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 * Locate item with index "n" in list "l" and return it.
6452 * A negative index is counted from the end; -1 is the last item.
6453 * Returns NULL when "n" is out of range.
6454 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006455 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 long idx;
6460
6461 if (l == NULL)
6462 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463
6464 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466 n = l->lv_len + n;
6467
6468 /* Check for index out of range. */
6469 if (n < 0 || n >= l->lv_len)
6470 return NULL;
6471
6472 /* When there is a cached index may start search from there. */
6473 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006475 if (n < l->lv_idx / 2)
6476 {
6477 /* closest to the start of the list */
6478 item = l->lv_first;
6479 idx = 0;
6480 }
6481 else if (n > (l->lv_idx + l->lv_len) / 2)
6482 {
6483 /* closest to the end of the list */
6484 item = l->lv_last;
6485 idx = l->lv_len - 1;
6486 }
6487 else
6488 {
6489 /* closest to the cached index */
6490 item = l->lv_idx_item;
6491 idx = l->lv_idx;
6492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 }
6494 else
6495 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 if (n < l->lv_len / 2)
6497 {
6498 /* closest to the start of the list */
6499 item = l->lv_first;
6500 idx = 0;
6501 }
6502 else
6503 {
6504 /* closest to the end of the list */
6505 item = l->lv_last;
6506 idx = l->lv_len - 1;
6507 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006509
6510 while (n > idx)
6511 {
6512 /* search forward */
6513 item = item->li_next;
6514 ++idx;
6515 }
6516 while (n < idx)
6517 {
6518 /* search backward */
6519 item = item->li_prev;
6520 --idx;
6521 }
6522
6523 /* cache the used index */
6524 l->lv_idx = idx;
6525 l->lv_idx_item = item;
6526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 return item;
6528}
6529
6530/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006531 * Get list item "l[idx]" as a number.
6532 */
6533 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_find_nr(
6535 list_T *l,
6536 long idx,
6537 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006538{
6539 listitem_T *li;
6540
6541 li = list_find(l, idx);
6542 if (li == NULL)
6543 {
6544 if (errorp != NULL)
6545 *errorp = TRUE;
6546 return -1L;
6547 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006548 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006549}
6550
6551/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006552 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6553 */
6554 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006556{
6557 listitem_T *li;
6558
6559 li = list_find(l, idx - 1);
6560 if (li == NULL)
6561 {
6562 EMSGN(_(e_listidx), idx);
6563 return NULL;
6564 }
6565 return get_tv_string(&li->li_tv);
6566}
6567
6568/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006569 * Locate "item" list "l" and return its index.
6570 * Returns -1 when "item" is not in the list.
6571 */
6572 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006574{
6575 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006577
6578 if (l == NULL)
6579 return -1;
6580 idx = 0;
6581 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6582 ++idx;
6583 if (li == NULL)
6584 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006585 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006586}
6587
6588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 * Append item "item" to the end of list "l".
6590 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
6594 if (l->lv_last == NULL)
6595 {
6596 /* empty list */
6597 l->lv_first = item;
6598 l->lv_last = item;
6599 item->li_prev = NULL;
6600 }
6601 else
6602 {
6603 l->lv_last->li_next = item;
6604 item->li_prev = l->lv_last;
6605 l->lv_last = item;
6606 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006607 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 item->li_next = NULL;
6609}
6610
6611/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006618 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
Bram Moolenaar05159a02005-02-26 23:04:13 +00006620 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 copy_tv(tv, &li->li_tv);
6623 list_append(l, li);
6624 return OK;
6625}
6626
6627/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006628 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006629 * Return FAIL when out of memory.
6630 */
6631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633{
6634 listitem_T *li = listitem_alloc();
6635
6636 if (li == NULL)
6637 return FAIL;
6638 li->li_tv.v_type = VAR_DICT;
6639 li->li_tv.v_lock = 0;
6640 li->li_tv.vval.v_dict = dict;
6641 list_append(list, li);
6642 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return OK;
6644}
6645
6646/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006647 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006648 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006649 * Returns FAIL when out of memory.
6650 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006652list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006653{
6654 listitem_T *li = listitem_alloc();
6655
6656 if (li == NULL)
6657 return FAIL;
6658 list_append(l, li);
6659 li->li_tv.v_type = VAR_STRING;
6660 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006661 if (str == NULL)
6662 li->li_tv.vval.v_string = NULL;
6663 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006664 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006665 return FAIL;
6666 return OK;
6667}
6668
6669/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006670 * Append "n" to list "l".
6671 * Returns FAIL when out of memory.
6672 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006675{
6676 listitem_T *li;
6677
6678 li = listitem_alloc();
6679 if (li == NULL)
6680 return FAIL;
6681 li->li_tv.v_type = VAR_NUMBER;
6682 li->li_tv.v_lock = 0;
6683 li->li_tv.vval.v_number = n;
6684 list_append(l, li);
6685 return OK;
6686}
6687
6688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006690 * If "item" is NULL append at the end.
6691 * Return FAIL when out of memory.
6692 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006695{
Bram Moolenaar33570922005-01-25 22:26:29 +00006696 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006697
6698 if (ni == NULL)
6699 return FAIL;
6700 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006701 list_insert(l, ni, item);
6702 return OK;
6703}
6704
6705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006707{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006708 if (item == NULL)
6709 /* Append new item at end of list. */
6710 list_append(l, ni);
6711 else
6712 {
6713 /* Insert new item before existing item. */
6714 ni->li_prev = item->li_prev;
6715 ni->li_next = item;
6716 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006717 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006719 ++l->lv_idx;
6720 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006722 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006724 l->lv_idx_item = NULL;
6725 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006727 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006728 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006729}
6730
6731/*
6732 * Extend "l1" with "l2".
6733 * If "bef" is NULL append at the end, otherwise insert before this item.
6734 * Returns FAIL when out of memory.
6735 */
6736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006738{
Bram Moolenaar33570922005-01-25 22:26:29 +00006739 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006740 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006742 /* We also quit the loop when we have inserted the original item count of
6743 * the list, avoid a hang when we extend a list with itself. */
6744 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6746 return FAIL;
6747 return OK;
6748}
6749
6750/*
6751 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6752 * Return FAIL when out of memory.
6753 */
6754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756{
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006758
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006759 if (l1 == NULL || l2 == NULL)
6760 return FAIL;
6761
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006763 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006764 if (l == NULL)
6765 return FAIL;
6766 tv->v_type = VAR_LIST;
6767 tv->vval.v_list = l;
6768
6769 /* append all items from the second list */
6770 return list_extend(l, l2, NULL);
6771}
6772
6773/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006776 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 * Returns NULL when out of memory.
6778 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781{
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 list_T *copy;
6783 listitem_T *item;
6784 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785
6786 if (orig == NULL)
6787 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788
6789 copy = list_alloc();
6790 if (copy != NULL)
6791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006792 if (copyID != 0)
6793 {
6794 /* Do this before adding the items, because one of the items may
6795 * refer back to this list. */
6796 orig->lv_copyID = copyID;
6797 orig->lv_copylist = copy;
6798 }
6799 for (item = orig->lv_first; item != NULL && !got_int;
6800 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 {
6802 ni = listitem_alloc();
6803 if (ni == NULL)
6804 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006806 {
6807 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6808 {
6809 vim_free(ni);
6810 break;
6811 }
6812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815 list_append(copy, ni);
6816 }
6817 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006818 if (item != NULL)
6819 {
6820 list_unref(copy);
6821 copy = NULL;
6822 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 }
6824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 return copy;
6826}
6827
6828/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006829 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006830 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006831 * This used to be called list_remove, but that conflicts with a Sun header
6832 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006836{
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006839 /* notify watchers */
6840 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006842 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006843 list_fix_watch(l, ip);
6844 if (ip == item2)
6845 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847
6848 if (item2->li_next == NULL)
6849 l->lv_last = item->li_prev;
6850 else
6851 item2->li_next->li_prev = item->li_prev;
6852 if (item->li_prev == NULL)
6853 l->lv_first = item2->li_next;
6854 else
6855 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006856 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857}
6858
6859/*
6860 * Return an allocated string with the string representation of a list.
6861 * May return NULL.
6862 */
6863 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006864list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865{
6866 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006867
6868 if (tv->vval.v_list == NULL)
6869 return NULL;
6870 ga_init2(&ga, (int)sizeof(char), 80);
6871 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006872 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6873 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006874 {
6875 vim_free(ga.ga_data);
6876 return NULL;
6877 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006878 ga_append(&ga, ']');
6879 ga_append(&ga, NUL);
6880 return (char_u *)ga.ga_data;
6881}
6882
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883typedef struct join_S {
6884 char_u *s;
6885 char_u *tofree;
6886} join_T;
6887
6888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006889list_join_inner(
6890 garray_T *gap, /* to store the result in */
6891 list_T *l,
6892 char_u *sep,
6893 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006894 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006895 int copyID,
6896 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897{
6898 int i;
6899 join_T *p;
6900 int len;
6901 int sumlen = 0;
6902 int first = TRUE;
6903 char_u *tofree;
6904 char_u numbuf[NUMBUFLEN];
6905 listitem_T *item;
6906 char_u *s;
6907
6908 /* Stringify each item in the list. */
6909 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6910 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006911 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6912 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006913 if (s == NULL)
6914 return FAIL;
6915
6916 len = (int)STRLEN(s);
6917 sumlen += len;
6918
Bram Moolenaarcde88542015-08-11 19:14:00 +02006919 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006920 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6921 if (tofree != NULL || s != numbuf)
6922 {
6923 p->s = s;
6924 p->tofree = tofree;
6925 }
6926 else
6927 {
6928 p->s = vim_strnsave(s, len);
6929 p->tofree = p->s;
6930 }
6931
6932 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006933 if (did_echo_string_emsg) /* recursion error, bail out */
6934 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006935 }
6936
6937 /* Allocate result buffer with its total size, avoid re-allocation and
6938 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6939 if (join_gap->ga_len >= 2)
6940 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6941 if (ga_grow(gap, sumlen + 2) == FAIL)
6942 return FAIL;
6943
6944 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6945 {
6946 if (first)
6947 first = FALSE;
6948 else
6949 ga_concat(gap, sep);
6950 p = ((join_T *)join_gap->ga_data) + i;
6951
6952 if (p->s != NULL)
6953 ga_concat(gap, p->s);
6954 line_breakcheck();
6955 }
6956
6957 return OK;
6958}
6959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006962 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006963 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006966list_join(
6967 garray_T *gap,
6968 list_T *l,
6969 char_u *sep,
6970 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006971 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006974 garray_T join_ga;
6975 int retval;
6976 join_T *p;
6977 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006978
Bram Moolenaard39a7512015-04-16 22:51:22 +02006979 if (l->lv_len < 1)
6980 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006981 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006982 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6983 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006984
6985 /* Dispose each item in join_ga. */
6986 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006987 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988 p = (join_T *)join_ga.ga_data;
6989 for (i = 0; i < join_ga.ga_len; ++i)
6990 {
6991 vim_free(p->tofree);
6992 ++p;
6993 }
6994 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006995 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006996
6997 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006998}
6999
7000/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007001 * Return the next (unique) copy ID.
7002 * Used for serializing nested structures.
7003 */
7004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007006{
7007 current_copyID += COPYID_INC;
7008 return current_copyID;
7009}
7010
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007011/* Used by get_func_tv() */
7012static garray_T funcargs = GA_EMPTY;
7013
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 * Garbage collection for lists and dictionaries.
7016 *
7017 * We use reference counts to be able to free most items right away when they
7018 * are no longer used. But for composite items it's possible that it becomes
7019 * unused while the reference count is > 0: When there is a recursive
7020 * reference. Example:
7021 * :let l = [1, 2, 3]
7022 * :let d = {9: l}
7023 * :let l[1] = d
7024 *
7025 * Since this is quite unusual we handle this with garbage collection: every
7026 * once in a while find out which lists and dicts are not referenced from any
7027 * variable.
7028 *
7029 * Here is a good reference text about garbage collection (refers to Python
7030 * but it applies to all reference-counting mechanisms):
7031 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033
7034/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007036 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007038 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007040garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007042 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 buf_T *buf;
7045 win_T *wp;
7046 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007047 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007048 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007050#ifdef FEAT_WINDOWS
7051 tabpage_T *tp;
7052#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007054 if (!testing)
7055 {
7056 /* Only do this once. */
7057 want_garbage_collect = FALSE;
7058 may_garbage_collect = FALSE;
7059 garbage_collect_at_exit = FALSE;
7060 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007061
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007062 /* We advance by two because we add one for items referenced through
7063 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007064 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066 /*
7067 * 1. Go through all accessible variables and mark all lists and dicts
7068 * with copyID.
7069 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070
7071 /* Don't free variables in the previous_funccal list unless they are only
7072 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007073 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7075 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7077 NULL);
7078 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7079 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007080 }
7081
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* script-local variables */
7083 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
7086 /* buffer-local variables */
7087 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7089 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
7091 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007092 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7094 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007095#ifdef FEAT_AUTOCMD
7096 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7098 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007099#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007101#ifdef FEAT_WINDOWS
7102 /* tabpage-local variables */
7103 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7105 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007106#endif
7107
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110
7111 /* function-local variables */
7112 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7113 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7115 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116 }
7117
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007118 /* function call arguments, if v:testing is set. */
7119 for (i = 0; i < funcargs.ga_len; ++i)
7120 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7121 copyID, NULL, NULL);
7122
Bram Moolenaard812df62008-11-09 12:46:09 +00007123 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007125
Bram Moolenaar1dced572012-04-05 16:54:08 +02007126#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007128#endif
7129
Bram Moolenaardb913952012-06-29 12:54:53 +02007130#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007132#endif
7133
7134#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007136#endif
7137
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007139 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007140 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007141#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007142#ifdef FEAT_NETBEANS_INTG
7143 abort = abort || set_ref_in_nb_channel(copyID);
7144#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007145
Bram Moolenaare3188e22016-05-31 21:13:04 +02007146#ifdef FEAT_TIMERS
7147 abort = abort || set_ref_in_timer(copyID);
7148#endif
7149
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007151 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 /*
7153 * 2. Free lists and dictionaries that are not referenced.
7154 */
7155 did_free = free_unref_items(copyID);
7156
7157 /*
7158 * 3. Check if any funccal can be freed now.
7159 */
7160 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007161 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007162 if (can_free_funccal(*pfc, copyID))
7163 {
7164 fc = *pfc;
7165 *pfc = fc->caller;
7166 free_funccal(fc, TRUE);
7167 did_free = TRUE;
7168 did_free_funccal = TRUE;
7169 }
7170 else
7171 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 if (did_free_funccal)
7174 /* When a funccal was freed some more items might be garbage
7175 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007176 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 else if (p_verbose > 0)
7179 {
7180 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7181 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007182
7183 return did_free;
7184}
7185
7186/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007187 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007188 */
7189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007191{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007192 dict_T *dd, *dd_next;
7193 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007194 int did_free = FALSE;
7195
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007196 /* Let all "free" functions know that we are here. This means no
7197 * dictionaries, lists, channels or jobs are to be freed, because we will
7198 * do that here. */
7199 in_free_unref_items = TRUE;
7200
7201 /*
7202 * PASS 1: free the contents of the items. We don't free the items
7203 * themselves yet, so that it is possible to decrement refcount counters
7204 */
7205
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007207 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007208 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007209 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007210 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007212 /* Free the Dictionary and ordinary items it contains, but don't
7213 * recurse into Lists and Dictionaries, they will be in the list
7214 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007215 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218
7219 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007220 * Go through the list of lists and free items without the copyID.
7221 * But don't free a list that has a watcher (used in a for loop), these
7222 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007224 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7225 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7226 && ll->lv_watch == NULL)
7227 {
7228 /* Free the List and ordinary items it contains, but don't recurse
7229 * into Lists and Dictionaries, they will be in the list of dicts
7230 * or list of lists. */
7231 list_free_contents(ll);
7232 did_free = TRUE;
7233 }
7234
7235#ifdef FEAT_JOB_CHANNEL
7236 /* Go through the list of jobs and free items without the copyID. This
7237 * must happen before doing channels, because jobs refer to channels, but
7238 * the reference from the channel to the job isn't tracked. */
7239 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7240
7241 /* Go through the list of channels and free items without the copyID. */
7242 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7243#endif
7244
7245 /*
7246 * PASS 2: free the items themselves.
7247 */
7248 for (dd = first_dict; dd != NULL; dd = dd_next)
7249 {
7250 dd_next = dd->dv_used_next;
7251 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7252 dict_free_dict(dd);
7253 }
7254
7255 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007256 {
7257 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007258 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7259 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 /* Free the List and ordinary items it contains, but don't recurse
7262 * into Lists and Dictionaries, they will be in the list of dicts
7263 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007264 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007266 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007267
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268#ifdef FEAT_JOB_CHANNEL
7269 /* Go through the list of jobs and free items without the copyID. This
7270 * must happen before doing channels, because jobs refer to channels, but
7271 * the reference from the channel to the job isn't tracked. */
7272 free_unused_jobs(copyID, COPYID_MASK);
7273
7274 /* Go through the list of channels and free items without the copyID. */
7275 free_unused_channels(copyID, COPYID_MASK);
7276#endif
7277
7278 in_free_unref_items = FALSE;
7279
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 return did_free;
7281}
7282
7283/*
7284 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 * "list_stack" is used to add lists to be marked. Can be NULL.
7286 *
7287 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007288 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007291{
7292 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007294 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007295 hashtab_T *cur_ht;
7296 ht_stack_T *ht_stack = NULL;
7297 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007298
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007299 cur_ht = ht;
7300 for (;;)
7301 {
7302 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007303 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007304 /* Mark each item in the hashtab. If the item contains a hashtab
7305 * it is added to ht_stack, if it contains a list it is added to
7306 * list_stack. */
7307 todo = (int)cur_ht->ht_used;
7308 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7309 if (!HASHITEM_EMPTY(hi))
7310 {
7311 --todo;
7312 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7313 &ht_stack, list_stack);
7314 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007315 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007316
7317 if (ht_stack == NULL)
7318 break;
7319
7320 /* take an item from the stack */
7321 cur_ht = ht_stack->ht;
7322 tempitem = ht_stack;
7323 ht_stack = ht_stack->prev;
7324 free(tempitem);
7325 }
7326
7327 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007328}
7329
7330/*
7331 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007332 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7333 *
7334 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007335 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007337set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007338{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007339 listitem_T *li;
7340 int abort = FALSE;
7341 list_T *cur_l;
7342 list_stack_T *list_stack = NULL;
7343 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007344
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007345 cur_l = l;
7346 for (;;)
7347 {
7348 if (!abort)
7349 /* Mark each item in the list. If the item contains a hashtab
7350 * it is added to ht_stack, if it contains a list it is added to
7351 * list_stack. */
7352 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7353 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7354 ht_stack, &list_stack);
7355 if (list_stack == NULL)
7356 break;
7357
7358 /* take an item from the stack */
7359 cur_l = list_stack->list;
7360 tempitem = list_stack;
7361 list_stack = list_stack->prev;
7362 free(tempitem);
7363 }
7364
7365 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007366}
7367
7368/*
7369 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007370 * "list_stack" is used to add lists to be marked. Can be NULL.
7371 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7372 *
7373 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007374 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007376set_ref_in_item(
7377 typval_T *tv,
7378 int copyID,
7379 ht_stack_T **ht_stack,
7380 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007381{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007382 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007383
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007384 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007385 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007386 dict_T *dd = tv->vval.v_dict;
7387
Bram Moolenaara03f2332016-02-06 18:09:59 +01007388 if (dd != NULL && dd->dv_copyID != copyID)
7389 {
7390 /* Didn't see this dict yet. */
7391 dd->dv_copyID = copyID;
7392 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007393 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007394 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7395 }
7396 else
7397 {
7398 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7399 if (newitem == NULL)
7400 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007401 else
7402 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007403 newitem->ht = &dd->dv_hashtab;
7404 newitem->prev = *ht_stack;
7405 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007406 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007407 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007408 }
7409 }
7410 else if (tv->v_type == VAR_LIST)
7411 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007412 list_T *ll = tv->vval.v_list;
7413
Bram Moolenaara03f2332016-02-06 18:09:59 +01007414 if (ll != NULL && ll->lv_copyID != copyID)
7415 {
7416 /* Didn't see this list yet. */
7417 ll->lv_copyID = copyID;
7418 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007419 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007420 abort = set_ref_in_list(ll, copyID, ht_stack);
7421 }
7422 else
7423 {
7424 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007425 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007426 if (newitem == NULL)
7427 abort = TRUE;
7428 else
7429 {
7430 newitem->list = ll;
7431 newitem->prev = *list_stack;
7432 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007433 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007434 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007435 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007436 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007437 else if (tv->v_type == VAR_PARTIAL)
7438 {
7439 partial_T *pt = tv->vval.v_partial;
7440 int i;
7441
7442 /* A partial does not have a copyID, because it cannot contain itself.
7443 */
7444 if (pt != NULL)
7445 {
7446 if (pt->pt_dict != NULL)
7447 {
7448 typval_T dtv;
7449
7450 dtv.v_type = VAR_DICT;
7451 dtv.vval.v_dict = pt->pt_dict;
7452 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7453 }
7454
7455 for (i = 0; i < pt->pt_argc; ++i)
7456 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7457 ht_stack, list_stack);
7458 }
7459 }
7460#ifdef FEAT_JOB_CHANNEL
7461 else if (tv->v_type == VAR_JOB)
7462 {
7463 job_T *job = tv->vval.v_job;
7464 typval_T dtv;
7465
7466 if (job != NULL && job->jv_copyID != copyID)
7467 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007468 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007469 if (job->jv_channel != NULL)
7470 {
7471 dtv.v_type = VAR_CHANNEL;
7472 dtv.vval.v_channel = job->jv_channel;
7473 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7474 }
7475 if (job->jv_exit_partial != NULL)
7476 {
7477 dtv.v_type = VAR_PARTIAL;
7478 dtv.vval.v_partial = job->jv_exit_partial;
7479 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7480 }
7481 }
7482 }
7483 else if (tv->v_type == VAR_CHANNEL)
7484 {
7485 channel_T *ch =tv->vval.v_channel;
7486 int part;
7487 typval_T dtv;
7488 jsonq_T *jq;
7489 cbq_T *cq;
7490
7491 if (ch != NULL && ch->ch_copyID != copyID)
7492 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007493 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007494 for (part = PART_SOCK; part <= PART_IN; ++part)
7495 {
7496 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7497 jq = jq->jq_next)
7498 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7499 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7500 cq = cq->cq_next)
7501 if (cq->cq_partial != NULL)
7502 {
7503 dtv.v_type = VAR_PARTIAL;
7504 dtv.vval.v_partial = cq->cq_partial;
7505 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7506 }
7507 if (ch->ch_part[part].ch_partial != NULL)
7508 {
7509 dtv.v_type = VAR_PARTIAL;
7510 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7511 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7512 }
7513 }
7514 if (ch->ch_partial != NULL)
7515 {
7516 dtv.v_type = VAR_PARTIAL;
7517 dtv.vval.v_partial = ch->ch_partial;
7518 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7519 }
7520 if (ch->ch_close_partial != NULL)
7521 {
7522 dtv.v_type = VAR_PARTIAL;
7523 dtv.vval.v_partial = ch->ch_close_partial;
7524 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7525 }
7526 }
7527 }
7528#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007529 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007530}
7531
7532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 * Allocate an empty header for a dictionary.
7534 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007535 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537{
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007542 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007543 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007544 if (first_dict != NULL)
7545 first_dict->dv_used_prev = d;
7546 d->dv_used_next = first_dict;
7547 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007548 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007549
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007551 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007552 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007553 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007554 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557}
7558
7559/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007560 * Allocate an empty dict for a return value.
7561 * Returns OK or FAIL.
7562 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007565{
7566 dict_T *d = dict_alloc();
7567
7568 if (d == NULL)
7569 return FAIL;
7570
7571 rettv->vval.v_dict = d;
7572 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007573 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007574 ++d->dv_refcount;
7575 return OK;
7576}
7577
7578
7579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 * Unreference a Dictionary: decrement the reference count and free it when it
7581 * becomes zero.
7582 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007586 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007587 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588}
7589
7590/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007591 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 * Ignores the reference count.
7593 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007594 static void
7595dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007599 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007601 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007602 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 if (!HASHITEM_EMPTY(hi))
7607 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007608 /* Remove the item before deleting it, just in case there is
7609 * something recursive causing trouble. */
7610 di = HI2DI(hi);
7611 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007612 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007613 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 --todo;
7615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007618}
7619
7620 static void
7621dict_free_dict(dict_T *d)
7622{
7623 /* Remove the dict from the list of dicts for garbage collection. */
7624 if (d->dv_used_prev == NULL)
7625 first_dict = d->dv_used_next;
7626 else
7627 d->dv_used_prev->dv_used_next = d->dv_used_next;
7628 if (d->dv_used_next != NULL)
7629 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 vim_free(d);
7631}
7632
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007633 void
7634dict_free(dict_T *d)
7635{
7636 if (!in_free_unref_items)
7637 {
7638 dict_free_contents(d);
7639 dict_free_dict(d);
7640 }
7641}
7642
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643/*
7644 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 * The "key" is copied to the new item.
7646 * Note that the value of the item "di_tv" still needs to be initialized!
7647 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007649 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007650dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651{
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007654 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007658 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007659 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661}
7662
7663/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 * Make a copy of a Dictionary item.
7665 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7672 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 if (di != NULL)
7674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007676 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 copy_tv(&org->di_tv, &di->di_tv);
7678 }
7679 return di;
7680}
7681
7682/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 * Remove item "item" from Dictionary "dict" and free it.
7684 */
7685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687{
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 if (HASHITEM_EMPTY(hi))
7692 EMSG2(_(e_intern2), "dictitem_remove()");
7693 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 dictitem_free(item);
7696}
7697
7698/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 * Free a dict item. Also clears the value.
7700 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007705 if (item->di_flags & DI_FLAGS_ALLOC)
7706 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707}
7708
7709/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7711 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007712 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 * Returns NULL when out of memory.
7714 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007716dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717{
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 dict_T *copy;
7719 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722
7723 if (orig == NULL)
7724 return NULL;
7725
7726 copy = dict_alloc();
7727 if (copy != NULL)
7728 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007729 if (copyID != 0)
7730 {
7731 orig->dv_copyID = copyID;
7732 orig->dv_copydict = copy;
7733 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007734 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007735 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 --todo;
7740
7741 di = dictitem_alloc(hi->hi_key);
7742 if (di == NULL)
7743 break;
7744 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007745 {
7746 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7747 copyID) == FAIL)
7748 {
7749 vim_free(di);
7750 break;
7751 }
7752 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 else
7754 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7755 if (dict_add(copy, di) == FAIL)
7756 {
7757 dictitem_free(di);
7758 break;
7759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762
Bram Moolenaare9a41262005-01-15 22:18:47 +00007763 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007764 if (todo > 0)
7765 {
7766 dict_unref(copy);
7767 copy = NULL;
7768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007769 }
7770
7771 return copy;
7772}
7773
7774/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007776 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007779dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780{
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782}
7783
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 * Add a number or string entry to dictionary "d".
7786 * When "str" is NULL use number "nr", otherwise use "str".
7787 * Returns FAIL when out of memory and when key already exists.
7788 */
7789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007790dict_add_nr_str(
7791 dict_T *d,
7792 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007793 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007795{
7796 dictitem_T *item;
7797
7798 item = dictitem_alloc((char_u *)key);
7799 if (item == NULL)
7800 return FAIL;
7801 item->di_tv.v_lock = 0;
7802 if (str == NULL)
7803 {
7804 item->di_tv.v_type = VAR_NUMBER;
7805 item->di_tv.vval.v_number = nr;
7806 }
7807 else
7808 {
7809 item->di_tv.v_type = VAR_STRING;
7810 item->di_tv.vval.v_string = vim_strsave(str);
7811 }
7812 if (dict_add(d, item) == FAIL)
7813 {
7814 dictitem_free(item);
7815 return FAIL;
7816 }
7817 return OK;
7818}
7819
7820/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007821 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007822 * Returns FAIL when out of memory and when key already exists.
7823 */
7824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007826{
7827 dictitem_T *item;
7828
7829 item = dictitem_alloc((char_u *)key);
7830 if (item == NULL)
7831 return FAIL;
7832 item->di_tv.v_lock = 0;
7833 item->di_tv.v_type = VAR_LIST;
7834 item->di_tv.vval.v_list = list;
7835 if (dict_add(d, item) == FAIL)
7836 {
7837 dictitem_free(item);
7838 return FAIL;
7839 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007840 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007841 return OK;
7842}
7843
7844/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 * Get the number of items in a Dictionary.
7846 */
7847 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 if (d == NULL)
7851 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007852 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853}
7854
7855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 * Find item "key[len]" in Dictionary "d".
7857 * If "len" is negative use strlen(key).
7858 * Returns NULL when not found.
7859 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007860 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863#define AKEYLEN 200
7864 char_u buf[AKEYLEN];
7865 char_u *akey;
7866 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007869 if (d == NULL)
7870 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007871 if (len < 0)
7872 akey = key;
7873 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007874 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 tofree = akey = vim_strnsave(key, len);
7876 if (akey == NULL)
7877 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007878 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 else
7880 {
7881 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007882 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 akey = buf;
7884 }
7885
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007887 vim_free(tofree);
7888 if (HASHITEM_EMPTY(hi))
7889 return NULL;
7890 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891}
7892
7893/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007894 * Get a string item from a dictionary.
7895 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007896 * Returns NULL if the entry doesn't exist or out of memory.
7897 */
7898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007899get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007900{
7901 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007902 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007903
7904 di = dict_find(d, key, -1);
7905 if (di == NULL)
7906 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007907 s = get_tv_string(&di->di_tv);
7908 if (save && s != NULL)
7909 s = vim_strsave(s);
7910 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007911}
7912
7913/*
7914 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007915 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007916 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007917 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007918get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007919{
7920 dictitem_T *di;
7921
7922 di = dict_find(d, key, -1);
7923 if (di == NULL)
7924 return 0;
7925 return get_tv_number(&di->di_tv);
7926}
7927
7928/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 * Return an allocated string with the string representation of a Dictionary.
7930 * May return NULL.
7931 */
7932 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007933dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934{
7935 garray_T ga;
7936 int first = TRUE;
7937 char_u *tofree;
7938 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007942 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007944 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 return NULL;
7946 ga_init2(&ga, (int)sizeof(char), 80);
7947 ga_append(&ga, '{');
7948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007950 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007952 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007954 --todo;
7955
7956 if (first)
7957 first = FALSE;
7958 else
7959 ga_concat(&ga, (char_u *)", ");
7960
7961 tofree = string_quote(hi->hi_key, FALSE);
7962 if (tofree != NULL)
7963 {
7964 ga_concat(&ga, tofree);
7965 vim_free(tofree);
7966 }
7967 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007968 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7969 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007970 if (s != NULL)
7971 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007973 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007974 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007975 line_breakcheck();
7976
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007979 if (todo > 0)
7980 {
7981 vim_free(ga.ga_data);
7982 return NULL;
7983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007984
7985 ga_append(&ga, '}');
7986 ga_append(&ga, NUL);
7987 return (char_u *)ga.ga_data;
7988}
7989
7990/*
7991 * Allocate a variable for a Dictionary and fill it from "*arg".
7992 * Return OK or FAIL. Returns NOTDONE for {expr}.
7993 */
7994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007995get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996{
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 dict_T *d = NULL;
7998 typval_T tvkey;
7999 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008000 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008002 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008003 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008004
8005 /*
8006 * First check if it's not a curly-braces thing: {expr}.
8007 * Must do this without evaluating, otherwise a function may be called
8008 * twice. Unfortunately this means we need to call eval1() twice for the
8009 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 if (*start != '}')
8013 {
8014 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8015 return FAIL;
8016 if (*start == '}')
8017 return NOTDONE;
8018 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019
8020 if (evaluate)
8021 {
8022 d = dict_alloc();
8023 if (d == NULL)
8024 return FAIL;
8025 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008026 tvkey.v_type = VAR_UNKNOWN;
8027 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028
8029 *arg = skipwhite(*arg + 1);
8030 while (**arg != '}' && **arg != NUL)
8031 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008032 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 goto failret;
8034 if (**arg != ':')
8035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008036 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008037 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 goto failret;
8039 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008040 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008042 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008043 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008044 {
8045 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 clear_tv(&tvkey);
8047 goto failret;
8048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008050
8051 *arg = skipwhite(*arg + 1);
8052 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8053 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008054 if (evaluate)
8055 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 goto failret;
8057 }
8058 if (evaluate)
8059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008060 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 if (item != NULL)
8062 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008063 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008064 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 clear_tv(&tv);
8066 goto failret;
8067 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008068 item = dictitem_alloc(key);
8069 clear_tv(&tvkey);
8070 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008072 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008073 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008074 if (dict_add(d, item) == FAIL)
8075 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 }
8077 }
8078
8079 if (**arg == '}')
8080 break;
8081 if (**arg != ',')
8082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008083 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008084 goto failret;
8085 }
8086 *arg = skipwhite(*arg + 1);
8087 }
8088
8089 if (**arg != '}')
8090 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008091 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008092failret:
8093 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008094 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008095 return FAIL;
8096 }
8097
8098 *arg = skipwhite(*arg + 1);
8099 if (evaluate)
8100 {
8101 rettv->v_type = VAR_DICT;
8102 rettv->vval.v_dict = d;
8103 ++d->dv_refcount;
8104 }
8105
8106 return OK;
8107}
8108
Bram Moolenaar17a13432016-01-24 14:22:10 +01008109 static char *
8110get_var_special_name(int nr)
8111{
8112 switch (nr)
8113 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008114 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008115 case VVAL_TRUE: return "v:true";
8116 case VVAL_NONE: return "v:none";
8117 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008118 }
8119 EMSG2(_(e_intern2), "get_var_special_name()");
8120 return "42";
8121}
8122
Bram Moolenaar8c711452005-01-14 21:53:12 +00008123/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 * Return a string with the string representation of a variable.
8125 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008127 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008128 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8129 * "string()", otherwise does not put quotes around strings, as ":echo"
8130 * displays values.
8131 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8132 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008133 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 */
8135 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008136echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008137 typval_T *tv,
8138 char_u **tofree,
8139 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008140 int copyID,
8141 int echo_style,
8142 int restore_copyID,
8143 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 static int recurse = 0;
8146 char_u *r = NULL;
8147
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008150 if (!did_echo_string_emsg)
8151 {
8152 /* Only give this message once for a recursive call to avoid
8153 * flooding the user with errors. And stop iterating over lists
8154 * and dicts. */
8155 did_echo_string_emsg = TRUE;
8156 EMSG(_("E724: variable nested too deep for displaying"));
8157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008159 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 }
8161 ++recurse;
8162
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008163 switch (tv->v_type)
8164 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008165 case VAR_STRING:
8166 if (echo_style && !dict_val)
8167 {
8168 *tofree = NULL;
8169 r = get_tv_string_buf(tv, numbuf);
8170 }
8171 else
8172 {
8173 *tofree = string_quote(tv->vval.v_string, FALSE);
8174 r = *tofree;
8175 }
8176 break;
8177
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008179 if (echo_style)
8180 {
8181 *tofree = NULL;
8182 r = tv->vval.v_string;
8183 }
8184 else
8185 {
8186 *tofree = string_quote(tv->vval.v_string, TRUE);
8187 r = *tofree;
8188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008190
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008191 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008192 {
8193 partial_T *pt = tv->vval.v_partial;
8194 char_u *fname = string_quote(pt == NULL ? NULL
8195 : pt->pt_name, FALSE);
8196 garray_T ga;
8197 int i;
8198 char_u *tf;
8199
8200 ga_init2(&ga, 1, 100);
8201 ga_concat(&ga, (char_u *)"function(");
8202 if (fname != NULL)
8203 {
8204 ga_concat(&ga, fname);
8205 vim_free(fname);
8206 }
8207 if (pt != NULL && pt->pt_argc > 0)
8208 {
8209 ga_concat(&ga, (char_u *)", [");
8210 for (i = 0; i < pt->pt_argc; ++i)
8211 {
8212 if (i > 0)
8213 ga_concat(&ga, (char_u *)", ");
8214 ga_concat(&ga,
8215 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8216 vim_free(tf);
8217 }
8218 ga_concat(&ga, (char_u *)"]");
8219 }
8220 if (pt != NULL && pt->pt_dict != NULL)
8221 {
8222 typval_T dtv;
8223
8224 ga_concat(&ga, (char_u *)", ");
8225 dtv.v_type = VAR_DICT;
8226 dtv.vval.v_dict = pt->pt_dict;
8227 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8228 vim_free(tf);
8229 }
8230 ga_concat(&ga, (char_u *)")");
8231
8232 *tofree = ga.ga_data;
8233 r = *tofree;
8234 break;
8235 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008236
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008237 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008238 if (tv->vval.v_list == NULL)
8239 {
8240 *tofree = NULL;
8241 r = NULL;
8242 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008243 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8244 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008245 {
8246 *tofree = NULL;
8247 r = (char_u *)"[...]";
8248 }
8249 else
8250 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008251 int old_copyID = tv->vval.v_list->lv_copyID;
8252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008253 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008254 *tofree = list2string(tv, copyID, restore_copyID);
8255 if (restore_copyID)
8256 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 r = *tofree;
8258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008260
Bram Moolenaar8c711452005-01-14 21:53:12 +00008261 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008262 if (tv->vval.v_dict == NULL)
8263 {
8264 *tofree = NULL;
8265 r = NULL;
8266 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008267 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8268 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008269 {
8270 *tofree = NULL;
8271 r = (char_u *)"{...}";
8272 }
8273 else
8274 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008275 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008276 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008277 *tofree = dict2string(tv, copyID, restore_copyID);
8278 if (restore_copyID)
8279 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008280 r = *tofree;
8281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008285 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008286 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008287 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008288 *tofree = NULL;
8289 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008292 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008293#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294 *tofree = NULL;
8295 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8296 r = numbuf;
8297 break;
8298#endif
8299
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008300 case VAR_SPECIAL:
8301 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008302 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008303 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305
Bram Moolenaar8502c702014-06-17 12:51:16 +02008306 if (--recurse == 0)
8307 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008309}
8310
8311/*
8312 * Return a string with the string representation of a variable.
8313 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8314 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008315 * Does not put quotes around strings, as ":echo" displays values.
8316 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8317 * May return NULL.
8318 */
8319 static char_u *
8320echo_string(
8321 typval_T *tv,
8322 char_u **tofree,
8323 char_u *numbuf,
8324 int copyID)
8325{
8326 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8327}
8328
8329/*
8330 * Return a string with the string representation of a variable.
8331 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8332 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008334 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008335 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008337tv2string(
8338 typval_T *tv,
8339 char_u **tofree,
8340 char_u *numbuf,
8341 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008343 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344}
8345
8346/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 * Return string "str" in ' quotes, doubling ' characters.
8348 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008349 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008350 */
8351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008352string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008353{
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008355 char_u *p, *r, *s;
8356
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 len = (function ? 13 : 3);
8358 if (str != NULL)
8359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008360 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 for (p = str; *p != NUL; mb_ptr_adv(p))
8362 if (*p == '\'')
8363 ++len;
8364 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008365 s = r = alloc(len);
8366 if (r != NULL)
8367 {
8368 if (function)
8369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008370 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008371 r += 10;
8372 }
8373 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 if (str != NULL)
8376 for (p = str; *p != NUL; )
8377 {
8378 if (*p == '\'')
8379 *r++ = '\'';
8380 MB_COPY_CHAR(p, r);
8381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008382 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008383 if (function)
8384 *r++ = ')';
8385 *r++ = NUL;
8386 }
8387 return s;
8388}
8389
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008390#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008391/*
8392 * Convert the string "text" to a floating point number.
8393 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8394 * this always uses a decimal point.
8395 * Returns the length of the text that was consumed.
8396 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008398string2float(
8399 char_u *text,
8400 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401{
8402 char *s = (char *)text;
8403 float_T f;
8404
8405 f = strtod(s, &s);
8406 *value = f;
8407 return (int)((char_u *)s - text);
8408}
8409#endif
8410
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 * Get the value of an environment variable.
8413 * "arg" is pointing to the '$'. It is advanced to after the name.
8414 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008415 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 */
8417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008418get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 char_u *string = NULL;
8421 int len;
8422 int cc;
8423 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008424 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
8426 ++*arg;
8427 name = *arg;
8428 len = get_env_len(arg);
8429 if (evaluate)
8430 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008431 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008432 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008433
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008434 cc = name[len];
8435 name[len] = NUL;
8436 /* first try vim_getenv(), fast for normal environment vars */
8437 string = vim_getenv(name, &mustfree);
8438 if (string != NULL && *string != NUL)
8439 {
8440 if (!mustfree)
8441 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008443 else
8444 {
8445 if (mustfree)
8446 vim_free(string);
8447
8448 /* next try expanding things like $VIM and ${HOME} */
8449 string = expand_env_save(name - 1);
8450 if (string != NULL && *string == '$')
8451 {
8452 vim_free(string);
8453 string = NULL;
8454 }
8455 }
8456 name[len] = cc;
8457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 rettv->v_type = VAR_STRING;
8459 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461
8462 return OK;
8463}
8464
8465/*
8466 * Array with names and number of arguments of all internal functions
8467 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8468 */
8469static struct fst
8470{
8471 char *f_name; /* function name */
8472 char f_min_argc; /* minimal number of arguments */
8473 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008474 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008475 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476} functions[] =
8477{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478#ifdef FEAT_FLOAT
8479 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008483 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"append", 2, 2, f_append},
8485 {"argc", 0, 0, f_argc},
8486 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008487 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008488 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008489#ifdef FEAT_FLOAT
8490 {"asin", 1, 1, f_asin}, /* WJMc */
8491#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008492 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008493 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008494 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008495 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008496 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008497 {"assert_notequal", 2, 3, f_assert_notequal},
8498 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008499 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008500#ifdef FEAT_FLOAT
8501 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008502 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008505 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"bufexists", 1, 1, f_bufexists},
8507 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8508 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8509 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8510 {"buflisted", 1, 1, f_buflisted},
8511 {"bufloaded", 1, 1, f_bufloaded},
8512 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008514 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"bufwinnr", 1, 1, f_bufwinnr},
8516 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008517 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008518 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008520#ifdef FEAT_FLOAT
8521 {"ceil", 1, 1, f_ceil},
8522#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008524 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008525 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8526 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008527 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008528 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008529 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008530 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008531 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008532 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008533 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008534 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008535 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8536 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008537 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008538 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008539#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008540 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008541 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008543 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008545#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008546 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008547 {"complete_add", 1, 1, f_complete_add},
8548 {"complete_check", 0, 0, f_complete_check},
8549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552#ifdef FEAT_FLOAT
8553 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008554 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008555#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008556 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008558 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008559 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008560 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008562 {"diff_filler", 1, 1, f_diff_filler},
8563 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008564 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008566 {"eval", 1, 1, f_eval},
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02008567 {"evalcmd", 1, 1, f_evalcmd},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"eventhandler", 0, 0, f_eventhandler},
8569 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008570 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008572#ifdef FEAT_FLOAT
8573 {"exp", 1, 1, f_exp},
8574#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008575 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008576 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008577 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8579 {"filereadable", 1, 1, f_filereadable},
8580 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008581 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008582 {"finddir", 1, 3, f_finddir},
8583 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584#ifdef FEAT_FLOAT
8585 {"float2nr", 1, 1, f_float2nr},
8586 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008587 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008589 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 {"fnamemodify", 2, 2, f_fnamemodify},
8591 {"foldclosed", 1, 1, f_foldclosed},
8592 {"foldclosedend", 1, 1, f_foldclosedend},
8593 {"foldlevel", 1, 1, f_foldlevel},
8594 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008595 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008597 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008598 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008599 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008600 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008601 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"getchar", 0, 1, f_getchar},
8603 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008604 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {"getcmdline", 0, 0, f_getcmdline},
8606 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008607 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008608 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008609 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008610 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008611 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008612 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {"getfsize", 1, 1, f_getfsize},
8614 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008615 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008616 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008617 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008618 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008619 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008620 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008621 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008622 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008624 {"gettabvar", 2, 3, f_gettabvar},
8625 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"getwinposx", 0, 0, f_getwinposx},
8627 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008628 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008629 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008630 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008631 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008634 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008635 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8637 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8638 {"histadd", 2, 2, f_histadd},
8639 {"histdel", 1, 2, f_histdel},
8640 {"histget", 1, 2, f_histget},
8641 {"histnr", 1, 1, f_histnr},
8642 {"hlID", 1, 1, f_hlID},
8643 {"hlexists", 1, 1, f_hlexists},
8644 {"hostname", 0, 0, f_hostname},
8645 {"iconv", 3, 3, f_iconv},
8646 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008648 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008650 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"inputrestore", 0, 0, f_inputrestore},
8652 {"inputsave", 0, 0, f_inputsave},
8653 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008655 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008657 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008658#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8659 {"isnan", 1, 1, f_isnan},
8660#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008661 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008662#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008663 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008664 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008665 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008666 {"job_start", 1, 2, f_job_start},
8667 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008668 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008669#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008670 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008671 {"js_decode", 1, 1, f_js_decode},
8672 {"js_encode", 1, 1, f_js_encode},
8673 {"json_decode", 1, 1, f_json_decode},
8674 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008675 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"libcall", 3, 3, f_libcall},
8679 {"libcallnr", 3, 3, f_libcallnr},
8680 {"line", 1, 1, f_line},
8681 {"line2byte", 1, 1, f_line2byte},
8682 {"lispindent", 1, 1, f_lispindent},
8683 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008684#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008685 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686 {"log10", 1, 1, f_log10},
8687#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008688#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008689 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008690#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008691 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008692 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008693 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008695 {"matchadd", 2, 5, f_matchadd},
8696 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008697 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008698 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008699 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008700 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008702 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008703 {"max", 1, 1, f_max},
8704 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008705#ifdef vim_mkdir
8706 {"mkdir", 1, 3, f_mkdir},
8707#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008708 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008709#ifdef FEAT_MZSCHEME
8710 {"mzeval", 1, 1, f_mzeval},
8711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008713 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008714 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008715 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008716#ifdef FEAT_PERL
8717 {"perleval", 1, 1, f_perleval},
8718#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719#ifdef FEAT_FLOAT
8720 {"pow", 2, 2, f_pow},
8721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008723 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008724 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008725#ifdef FEAT_PYTHON3
8726 {"py3eval", 1, 1, f_py3eval},
8727#endif
8728#ifdef FEAT_PYTHON
8729 {"pyeval", 1, 1, f_pyeval},
8730#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008733 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008734#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008735 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008736#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008737 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {"remote_expr", 2, 3, f_remote_expr},
8739 {"remote_foreground", 1, 1, f_remote_foreground},
8740 {"remote_peek", 1, 2, f_remote_peek},
8741 {"remote_read", 1, 1, f_remote_read},
8742 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008743 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008745 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008748#ifdef FEAT_FLOAT
8749 {"round", 1, 1, f_round},
8750#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008751 {"screenattr", 2, 2, f_screenattr},
8752 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008753 {"screencol", 0, 0, f_screencol},
8754 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008755 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008756 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008757 {"searchpair", 3, 7, f_searchpair},
8758 {"searchpairpos", 3, 7, f_searchpairpos},
8759 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {"server2client", 2, 2, f_server2client},
8761 {"serverlist", 0, 0, f_serverlist},
8762 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008763 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008765 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008767 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008768 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008769 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008770 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008772 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008773 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008775#ifdef FEAT_CRYPT
8776 {"sha256", 1, 1, f_sha256},
8777#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008778 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008779 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008783 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008784#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008785 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008786 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008787 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008788 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008789 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#ifdef FEAT_FLOAT
8791 {"sqrt", 1, 1, f_sqrt},
8792 {"str2float", 1, 1, f_str2float},
8793#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008794 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008795 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008796 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008797 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#ifdef HAVE_STRFTIME
8799 {"strftime", 1, 2, f_strftime},
8800#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008801 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008803 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {"strlen", 1, 1, f_strlen},
8805 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008806 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008808 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008809 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {"substitute", 4, 4, f_substitute},
8811 {"synID", 3, 3, f_synID},
8812 {"synIDattr", 2, 3, f_synIDattr},
8813 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008814 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008815 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008816 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008817 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008818 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008819 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008820 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008821 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008822 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008823#ifdef FEAT_FLOAT
8824 {"tan", 1, 1, f_tan},
8825 {"tanh", 1, 1, f_tanh},
8826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008828 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8829 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008830 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8831#ifdef FEAT_JOB_CHANNEL
8832 {"test_null_channel", 0, 0, f_test_null_channel},
8833#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008834 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008835#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008836 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008837#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008838 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008839 {"test_null_partial", 0, 0, f_test_null_partial},
8840 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008841 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008842#ifdef FEAT_TIMERS
8843 {"timer_start", 2, 3, f_timer_start},
8844 {"timer_stop", 1, 1, f_timer_stop},
8845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {"tolower", 1, 1, f_tolower},
8847 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008848 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849#ifdef FEAT_FLOAT
8850 {"trunc", 1, 1, f_trunc},
8851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008853 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008854 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008855 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008856 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {"virtcol", 1, 1, f_virtcol},
8858 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008859 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008860 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008861 {"win_getid", 0, 2, f_win_getid},
8862 {"win_gotoid", 1, 1, f_win_gotoid},
8863 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8864 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {"winbufnr", 1, 1, f_winbufnr},
8866 {"wincol", 0, 0, f_wincol},
8867 {"winheight", 1, 1, f_winheight},
8868 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008869 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008871 {"winrestview", 1, 1, f_winrestview},
8872 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008874 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008875 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008876 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877};
8878
8879#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8880
8881/*
8882 * Function given to ExpandGeneric() to obtain the list of internal
8883 * or user defined function names.
8884 */
8885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008886get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 static int intidx = -1;
8889 char_u *name;
8890
8891 if (idx == 0)
8892 intidx = -1;
8893 if (intidx < 0)
8894 {
8895 name = get_user_func_name(xp, idx);
8896 if (name != NULL)
8897 return name;
8898 }
8899 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8900 {
8901 STRCPY(IObuff, functions[intidx].f_name);
8902 STRCAT(IObuff, "(");
8903 if (functions[intidx].f_max_argc == 0)
8904 STRCAT(IObuff, ")");
8905 return IObuff;
8906 }
8907
8908 return NULL;
8909}
8910
8911/*
8912 * Function given to ExpandGeneric() to obtain the list of internal or
8913 * user defined variable or function names.
8914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 static int intidx = -1;
8919 char_u *name;
8920
8921 if (idx == 0)
8922 intidx = -1;
8923 if (intidx < 0)
8924 {
8925 name = get_function_name(xp, idx);
8926 if (name != NULL)
8927 return name;
8928 }
8929 return get_user_var_name(xp, ++intidx);
8930}
8931
8932#endif /* FEAT_CMDL_COMPL */
8933
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008934#if defined(EBCDIC) || defined(PROTO)
8935/*
8936 * Compare struct fst by function name.
8937 */
8938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008939compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008940{
8941 struct fst *p1 = (struct fst *)s1;
8942 struct fst *p2 = (struct fst *)s2;
8943
8944 return STRCMP(p1->f_name, p2->f_name);
8945}
8946
8947/*
8948 * Sort the function table by function name.
8949 * The sorting of the table above is ASCII dependant.
8950 * On machines using EBCDIC we have to sort it.
8951 */
8952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008954{
8955 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8956
8957 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8958}
8959#endif
8960
8961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962/*
8963 * Find internal function in table above.
8964 * Return index, or -1 if not found
8965 */
8966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008967find_internal_func(
8968 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 int first = 0;
8971 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8972 int cmp;
8973 int x;
8974
8975 /*
8976 * Find the function name in the table. Binary search.
8977 */
8978 while (first <= last)
8979 {
8980 x = first + ((unsigned)(last - first) >> 1);
8981 cmp = STRCMP(name, functions[x].f_name);
8982 if (cmp < 0)
8983 last = x - 1;
8984 else if (cmp > 0)
8985 first = x + 1;
8986 else
8987 return x;
8988 }
8989 return -1;
8990}
8991
8992/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8994 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008995 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8996 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 */
8998 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008999deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000{
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 int cc;
9003
Bram Moolenaar65639032016-03-16 21:40:30 +01009004 if (partialp != NULL)
9005 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009006
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 cc = name[*lenp];
9008 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009009 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 {
9015 *lenp = 0;
9016 return (char_u *)""; /* just in case */
9017 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009018 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 }
9021
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009022 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9023 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009024 partial_T *pt = v->di_tv.vval.v_partial;
9025
9026 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009027 {
9028 *lenp = 0;
9029 return (char_u *)""; /* just in case */
9030 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009031 if (partialp != NULL)
9032 *partialp = pt;
9033 *lenp = (int)STRLEN(pt->pt_name);
9034 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 }
9036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009037 return name;
9038}
9039
9040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 * Allocate a variable for the result of a function.
9042 * Return OK or FAIL.
9043 */
9044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009045get_func_tv(
9046 char_u *name, /* name of the function */
9047 int len, /* length of "name" */
9048 typval_T *rettv,
9049 char_u **arg, /* argument, pointing to the '(' */
9050 linenr_T firstline, /* first line of range */
9051 linenr_T lastline, /* last line of range */
9052 int *doesrange, /* return: function handled range */
9053 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009055 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 char_u *argp;
9058 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009059 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 int argcount = 0; /* number of arguments found */
9061
9062 /*
9063 * Get the arguments.
9064 */
9065 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009066 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 {
9068 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9069 if (*argp == ')' || *argp == ',' || *argp == NUL)
9070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9072 {
9073 ret = FAIL;
9074 break;
9075 }
9076 ++argcount;
9077 if (*argp != ',')
9078 break;
9079 }
9080 if (*argp == ')')
9081 ++argp;
9082 else
9083 ret = FAIL;
9084
9085 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009086 {
9087 int i = 0;
9088
9089 if (get_vim_var_nr(VV_TESTING))
9090 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009091 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009092 * what variables are used on the call stack. */
9093 if (funcargs.ga_itemsize == 0)
9094 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9095 for (i = 0; i < argcount; ++i)
9096 if (ga_grow(&funcargs, 1) == OK)
9097 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9098 &argvars[i];
9099 }
9100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009102 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009103
9104 funcargs.ga_len -= i;
9105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 {
9108 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009109 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009111 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116
9117 *arg = skipwhite(argp);
9118 return ret;
9119}
9120
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009121#define ERROR_UNKNOWN 0
9122#define ERROR_TOOMANY 1
9123#define ERROR_TOOFEW 2
9124#define ERROR_SCRIPT 3
9125#define ERROR_DICT 4
9126#define ERROR_NONE 5
9127#define ERROR_OTHER 6
9128#define FLEN_FIXED 40
9129
9130/*
9131 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9132 * Change <SNR>123_name() to K_SNR 123_name().
9133 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9134 * (slow).
9135 */
9136 static char_u *
9137fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9138{
9139 int llen;
9140 char_u *fname;
9141 int i;
9142
9143 llen = eval_fname_script(name);
9144 if (llen > 0)
9145 {
9146 fname_buf[0] = K_SPECIAL;
9147 fname_buf[1] = KS_EXTRA;
9148 fname_buf[2] = (int)KE_SNR;
9149 i = 3;
9150 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9151 {
9152 if (current_SID <= 0)
9153 *error = ERROR_SCRIPT;
9154 else
9155 {
9156 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9157 i = (int)STRLEN(fname_buf);
9158 }
9159 }
9160 if (i + STRLEN(name + llen) < FLEN_FIXED)
9161 {
9162 STRCPY(fname_buf + i, name + llen);
9163 fname = fname_buf;
9164 }
9165 else
9166 {
9167 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9168 if (fname == NULL)
9169 *error = ERROR_OTHER;
9170 else
9171 {
9172 *tofree = fname;
9173 mch_memmove(fname, fname_buf, (size_t)i);
9174 STRCPY(fname + i, name + llen);
9175 }
9176 }
9177 }
9178 else
9179 fname = name;
9180 return fname;
9181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183/*
9184 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009185 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009186 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189call_func(
9190 char_u *funcname, /* name of the function */
9191 int len, /* length of "name" */
9192 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009193 int argcount_in, /* number of "argvars" */
9194 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009195 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196 linenr_T firstline, /* first line of range */
9197 linenr_T lastline, /* last line of range */
9198 int *doesrange, /* return: function handled range */
9199 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009200 partial_T *partial, /* optional, can be NULL */
9201 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 int error = ERROR_NONE;
9205 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009208 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009210 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009211 int argcount = argcount_in;
9212 typval_T *argvars = argvars_in;
9213 dict_T *selfdict = selfdict_in;
9214 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9215 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009216
9217 /* Make a copy of the name, if it comes from a funcref variable it could
9218 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009219 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009220 if (name == NULL)
9221 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009223 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225 *doesrange = FALSE;
9226
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009227 if (partial != NULL)
9228 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009229 /* When the function has a partial with a dict and there is a dict
9230 * argument, use the dict argument. That is backwards compatible.
9231 * When the dict was bound explicitly use the one from the partial. */
9232 if (partial->pt_dict != NULL
9233 && (selfdict_in == NULL || !partial->pt_auto))
9234 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 if (error == ERROR_NONE && partial->pt_argc > 0)
9236 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009237 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9238 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9239 for (i = 0; i < argcount_in; ++i)
9240 argv[i + argv_clear] = argvars_in[i];
9241 argvars = argv;
9242 argcount = partial->pt_argc + argcount_in;
9243 }
9244 }
9245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
9247 /* execute the function if no errors detected and executing */
9248 if (evaluate && error == ERROR_NONE)
9249 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009250 char_u *rfname = fname;
9251
9252 /* Ignore "g:" before a function name. */
9253 if (fname[0] == 'g' && fname[1] == ':')
9254 rfname = fname + 2;
9255
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009256 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9257 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 error = ERROR_UNKNOWN;
9259
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009260 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 {
9262 /*
9263 * User defined function.
9264 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009265 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009268 /* Trigger FuncUndefined event, may load the function. */
9269 if (fp == NULL
9270 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009271 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009272 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009275 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 }
9277#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009278 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009279 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 {
9281 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009282 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009283 }
9284
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 if (fp != NULL)
9286 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009287 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009289 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009291 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009293 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 else
9296 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009297 int did_save_redo = FALSE;
9298
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 /*
9300 * Call the user function.
9301 * Save and restore search patterns, script variables and
9302 * redo buffer.
9303 */
9304 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009305#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009306 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009307#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009308 {
9309 saveRedobuff();
9310 did_save_redo = TRUE;
9311 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009312 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009314 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009315 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9316 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9317 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009318 /* Function was unreferenced while being used, free it
9319 * now. */
9320 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009321 if (did_save_redo)
9322 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 restore_search_patterns();
9324 error = ERROR_NONE;
9325 }
9326 }
9327 }
9328 else
9329 {
9330 /*
9331 * Find the function name in the table, call its implementation.
9332 */
9333 i = find_internal_func(fname);
9334 if (i >= 0)
9335 {
9336 if (argcount < functions[i].f_min_argc)
9337 error = ERROR_TOOFEW;
9338 else if (argcount > functions[i].f_max_argc)
9339 error = ERROR_TOOMANY;
9340 else
9341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009342 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 error = ERROR_NONE;
9345 }
9346 }
9347 }
9348 /*
9349 * The function call (or "FuncUndefined" autocommand sequence) might
9350 * have been aborted by an error, an interrupt, or an explicitly thrown
9351 * exception that has not been caught so far. This situation can be
9352 * tested for by calling aborting(). For an error in an internal
9353 * function or for the "E132" error in call_user_func(), however, the
9354 * throw point at which the "force_abort" flag (temporarily reset by
9355 * emsg()) is normally updated has not been reached yet. We need to
9356 * update that flag first to make aborting() reliable.
9357 */
9358 update_force_abort();
9359 }
9360 if (error == ERROR_NONE)
9361 ret = OK;
9362
9363 /*
9364 * Report an error unless the argument evaluation or function call has been
9365 * cancelled due to an aborting error, an interrupt, or an exception.
9366 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009367 if (!aborting())
9368 {
9369 switch (error)
9370 {
9371 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009372 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009373 break;
9374 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009375 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009376 break;
9377 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009378 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009379 name);
9380 break;
9381 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009382 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009383 name);
9384 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 name);
9388 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009389 }
9390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009392 while (argv_clear > 0)
9393 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009394 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009395 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396
9397 return ret;
9398}
9399
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009400/*
9401 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009402 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009403 */
9404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406{
9407 char_u *p;
9408
9409 if (*name == K_SPECIAL)
9410 p = concat_str((char_u *)"<SNR>", name + 3);
9411 else
9412 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009413 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009414 if (p != name)
9415 vim_free(p);
9416}
9417
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009418/*
9419 * Return TRUE for a non-zero Number and a non-empty String.
9420 */
9421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009423{
9424 return ((argvars[0].v_type == VAR_NUMBER
9425 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009426 || (argvars[0].v_type == VAR_SPECIAL
9427 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009428 || (argvars[0].v_type == VAR_STRING
9429 && argvars[0].vval.v_string != NULL
9430 && *argvars[0].vval.v_string != NUL));
9431}
9432
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433/*********************************************
9434 * Implementation of the built-in functions
9435 */
9436
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009437#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009438static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009439
9440/*
9441 * Get the float value of "argvars[0]" into "f".
9442 * Returns FAIL when the argument is not a Number or Float.
9443 */
9444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009445get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009446{
9447 if (argvars[0].v_type == VAR_FLOAT)
9448 {
9449 *f = argvars[0].vval.v_float;
9450 return OK;
9451 }
9452 if (argvars[0].v_type == VAR_NUMBER)
9453 {
9454 *f = (float_T)argvars[0].vval.v_number;
9455 return OK;
9456 }
9457 EMSG(_("E808: Number or Float required"));
9458 return FAIL;
9459}
9460
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009461/*
9462 * "abs(expr)" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009466{
9467 if (argvars[0].v_type == VAR_FLOAT)
9468 {
9469 rettv->v_type = VAR_FLOAT;
9470 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9471 }
9472 else
9473 {
9474 varnumber_T n;
9475 int error = FALSE;
9476
9477 n = get_tv_number_chk(&argvars[0], &error);
9478 if (error)
9479 rettv->vval.v_number = -1;
9480 else if (n > 0)
9481 rettv->vval.v_number = n;
9482 else
9483 rettv->vval.v_number = -n;
9484 }
9485}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486
9487/*
9488 * "acos()" function
9489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009492{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009493 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494
9495 rettv->v_type = VAR_FLOAT;
9496 if (get_float_arg(argvars, &f) == OK)
9497 rettv->vval.v_float = acos(f);
9498 else
9499 rettv->vval.v_float = 0.0;
9500}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009501#endif
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009504 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 */
9506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009514 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009515 && !tv_check_lock(l->lv_lock,
9516 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009517 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009519 }
9520 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009521 EMSG(_(e_listreq));
9522}
9523
9524/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009525 * "and(expr, expr)" function
9526 */
9527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009529{
9530 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9531 & get_tv_number_chk(&argvars[1], NULL);
9532}
9533
9534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009535 * "append(lnum, string/list)" function
9536 */
9537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009538f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539{
9540 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009541 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 list_T *l = NULL;
9543 listitem_T *li = NULL;
9544 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009545 long added = 0;
9546
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009547 /* When coming here from Insert mode, sync undo, so that this can be
9548 * undone separately from what was previously inserted. */
9549 if (u_sync_once == 2)
9550 {
9551 u_sync_once = 1; /* notify that u_sync() was called */
9552 u_sync(TRUE);
9553 }
9554
Bram Moolenaar0d660222005-01-07 21:51:51 +00009555 lnum = get_tv_lnum(argvars);
9556 if (lnum >= 0
9557 && lnum <= curbuf->b_ml.ml_line_count
9558 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009560 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009562 l = argvars[1].vval.v_list;
9563 if (l == NULL)
9564 return;
9565 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009567 for (;;)
9568 {
9569 if (l == NULL)
9570 tv = &argvars[1]; /* append a string */
9571 else if (li == NULL)
9572 break; /* end of list */
9573 else
9574 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 line = get_tv_string_chk(tv);
9576 if (line == NULL) /* type error */
9577 {
9578 rettv->vval.v_number = 1; /* Failed */
9579 break;
9580 }
9581 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009582 ++added;
9583 if (l == NULL)
9584 break;
9585 li = li->li_next;
9586 }
9587
9588 appended_lines_mark(lnum, added);
9589 if (curwin->w_cursor.lnum > lnum)
9590 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 else
9593 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594}
9595
9596/*
9597 * "argc()" function
9598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "argidx()" function
9607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612}
9613
9614/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009615 * "arglistid()" function
9616 */
9617 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009618f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009619{
9620 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009621
9622 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009623 wp = find_tabwin(&argvars[0], &argvars[1]);
9624 if (wp != NULL)
9625 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009626}
9627
9628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * "argv(nr)" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 int idx;
9635
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009636 if (argvars[0].v_type != VAR_UNKNOWN)
9637 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009638 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009639 if (idx >= 0 && idx < ARGCOUNT)
9640 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9641 else
9642 rettv->vval.v_string = NULL;
9643 rettv->v_type = VAR_STRING;
9644 }
9645 else if (rettv_list_alloc(rettv) == OK)
9646 for (idx = 0; idx < ARGCOUNT; ++idx)
9647 list_append_string(rettv->vval.v_list,
9648 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009651typedef enum
9652{
9653 ASSERT_EQUAL,
9654 ASSERT_NOTEQUAL,
9655 ASSERT_MATCH,
9656 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009657 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009658} assert_type_T;
9659
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009660static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009661static 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 +01009662static void assert_error(garray_T *gap);
9663static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009664
9665/*
9666 * Prepare "gap" for an assert error and add the sourcing position.
9667 */
9668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670{
9671 char buf[NUMBUFLEN];
9672
9673 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009674 if (sourcing_name != NULL)
9675 {
9676 ga_concat(gap, sourcing_name);
9677 if (sourcing_lnum > 0)
9678 ga_concat(gap, (char_u *)" ");
9679 }
9680 if (sourcing_lnum > 0)
9681 {
9682 sprintf(buf, "line %ld", (long)sourcing_lnum);
9683 ga_concat(gap, (char_u *)buf);
9684 }
9685 if (sourcing_name != NULL || sourcing_lnum > 0)
9686 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009687}
9688
9689/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009690 * Append "str" to "gap", escaping unprintable characters.
9691 * Changes NL to \n, CR to \r, etc.
9692 */
9693 static void
9694ga_concat_esc(garray_T *gap, char_u *str)
9695{
9696 char_u *p;
9697 char_u buf[NUMBUFLEN];
9698
Bram Moolenaarf1551962016-03-15 12:55:58 +01009699 if (str == NULL)
9700 {
9701 ga_concat(gap, (char_u *)"NULL");
9702 return;
9703 }
9704
Bram Moolenaar23689172016-02-15 22:37:37 +01009705 for (p = str; *p != NUL; ++p)
9706 switch (*p)
9707 {
9708 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9709 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9710 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9711 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9712 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9713 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9714 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9715 default:
9716 if (*p < ' ')
9717 {
9718 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9719 ga_concat(gap, buf);
9720 }
9721 else
9722 ga_append(gap, *p);
9723 break;
9724 }
9725}
9726
9727/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009728 * Fill "gap" with information about an assert error.
9729 */
9730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009731fill_assert_error(
9732 garray_T *gap,
9733 typval_T *opt_msg_tv,
9734 char_u *exp_str,
9735 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009736 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009738{
9739 char_u numbuf[NUMBUFLEN];
9740 char_u *tofree;
9741
9742 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9743 {
9744 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9745 vim_free(tofree);
9746 }
9747 else
9748 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009749 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009750 ga_concat(gap, (char_u *)"Pattern ");
9751 else
9752 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009753 if (exp_str == NULL)
9754 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009755 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009756 vim_free(tofree);
9757 }
9758 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009759 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009760 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009761 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009762 else if (atype == ASSERT_NOTMATCH)
9763 ga_concat(gap, (char_u *)" does match ");
9764 else if (atype == ASSERT_NOTEQUAL)
9765 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009766 else
9767 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009768 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009769 vim_free(tofree);
9770 }
9771}
Bram Moolenaar43345542015-11-29 17:35:35 +01009772
9773/*
9774 * Add an assert error to v:errors.
9775 */
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009778{
9779 struct vimvar *vp = &vimvars[VV_ERRORS];
9780
9781 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9782 /* Make sure v:errors is a list. */
9783 set_vim_var_list(VV_ERRORS, list_alloc());
9784 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9785}
9786
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009787 static void
9788assert_equal_common(typval_T *argvars, assert_type_T atype)
9789{
9790 garray_T ga;
9791
9792 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9793 != (atype == ASSERT_EQUAL))
9794 {
9795 prepare_assert_error(&ga);
9796 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9797 atype);
9798 assert_error(&ga);
9799 ga_clear(&ga);
9800 }
9801}
9802
Bram Moolenaar43345542015-11-29 17:35:35 +01009803/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009804 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009805 */
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009808{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009809 assert_equal_common(argvars, ASSERT_EQUAL);
9810}
Bram Moolenaar43345542015-11-29 17:35:35 +01009811
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009812/*
9813 * "assert_notequal(expected, actual[, msg])" function
9814 */
9815 static void
9816f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9817{
9818 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009819}
9820
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009821/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009822 * "assert_exception(string[, msg])" function
9823 */
9824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009825f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009826{
9827 garray_T ga;
9828 char *error;
9829
9830 error = (char *)get_tv_string_chk(&argvars[0]);
9831 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9832 {
9833 prepare_assert_error(&ga);
9834 ga_concat(&ga, (char_u *)"v:exception is not set");
9835 assert_error(&ga);
9836 ga_clear(&ga);
9837 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009838 else if (error != NULL
9839 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009840 {
9841 prepare_assert_error(&ga);
9842 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009843 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009844 assert_error(&ga);
9845 ga_clear(&ga);
9846 }
9847}
9848
9849/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009850 * "assert_fails(cmd [, error])" function
9851 */
9852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009853f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009854{
9855 char_u *cmd = get_tv_string_chk(&argvars[0]);
9856 garray_T ga;
9857
9858 called_emsg = FALSE;
9859 suppress_errthrow = TRUE;
9860 emsg_silent = TRUE;
9861 do_cmdline_cmd(cmd);
9862 if (!called_emsg)
9863 {
9864 prepare_assert_error(&ga);
9865 ga_concat(&ga, (char_u *)"command did not fail: ");
9866 ga_concat(&ga, cmd);
9867 assert_error(&ga);
9868 ga_clear(&ga);
9869 }
9870 else if (argvars[1].v_type != VAR_UNKNOWN)
9871 {
9872 char_u buf[NUMBUFLEN];
9873 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9874
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009875 if (error == NULL
9876 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009877 {
9878 prepare_assert_error(&ga);
9879 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009880 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009881 assert_error(&ga);
9882 ga_clear(&ga);
9883 }
9884 }
9885
9886 called_emsg = FALSE;
9887 suppress_errthrow = FALSE;
9888 emsg_silent = FALSE;
9889 emsg_on_display = FALSE;
9890 set_vim_var_string(VV_ERRMSG, NULL, 0);
9891}
9892
9893/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009894 * Common for assert_true() and assert_false().
9895 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009898{
9899 int error = FALSE;
9900 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009901
Bram Moolenaar37127922016-02-06 20:29:28 +01009902 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009903 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009904 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009905 if (argvars[0].v_type != VAR_NUMBER
9906 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9907 || error)
9908 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009909 prepare_assert_error(&ga);
9910 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009911 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009912 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009913 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009914 ga_clear(&ga);
9915 }
9916}
9917
9918/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009919 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009920 */
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009923{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009924 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009925}
9926
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009927 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009928assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009929{
9930 garray_T ga;
9931 char_u buf1[NUMBUFLEN];
9932 char_u buf2[NUMBUFLEN];
9933 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9934 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9935
Bram Moolenaar72188e92016-03-28 22:48:29 +02009936 if (pat == NULL || text == NULL)
9937 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009938 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009939 {
9940 prepare_assert_error(&ga);
9941 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009942 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009943 assert_error(&ga);
9944 ga_clear(&ga);
9945 }
9946}
9947
9948/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009949 * "assert_match(pattern, actual[, msg])" function
9950 */
9951 static void
9952f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9953{
9954 assert_match_common(argvars, ASSERT_MATCH);
9955}
9956
9957/*
9958 * "assert_notmatch(pattern, actual[, msg])" function
9959 */
9960 static void
9961f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9962{
9963 assert_match_common(argvars, ASSERT_NOTMATCH);
9964}
9965
9966/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009967 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009968 */
9969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009970f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009971{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009972 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009973}
9974
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009976/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009977 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009978 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009980f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009981{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009982 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009983
9984 rettv->v_type = VAR_FLOAT;
9985 if (get_float_arg(argvars, &f) == OK)
9986 rettv->vval.v_float = asin(f);
9987 else
9988 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989}
9990
9991/*
9992 * "atan()" function
9993 */
9994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009996{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009997 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009998
9999 rettv->v_type = VAR_FLOAT;
10000 if (get_float_arg(argvars, &f) == OK)
10001 rettv->vval.v_float = atan(f);
10002 else
10003 rettv->vval.v_float = 0.0;
10004}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010005
10006/*
10007 * "atan2()" function
10008 */
10009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010010f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010011{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010012 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010013
10014 rettv->v_type = VAR_FLOAT;
10015 if (get_float_arg(argvars, &fx) == OK
10016 && get_float_arg(&argvars[1], &fy) == OK)
10017 rettv->vval.v_float = atan2(fx, fy);
10018 else
10019 rettv->vval.v_float = 0.0;
10020}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010021#endif
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023/*
10024 * "browse(save, title, initdir, default)" function
10025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029#ifdef FEAT_BROWSE
10030 int save;
10031 char_u *title;
10032 char_u *initdir;
10033 char_u *defname;
10034 char_u buf[NUMBUFLEN];
10035 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010036 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010038 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010039 title = get_tv_string_chk(&argvars[1]);
10040 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10041 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 if (error || title == NULL || initdir == NULL || defname == NULL)
10044 rettv->vval.v_string = NULL;
10045 else
10046 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010047 do_browse(save ? BROWSE_SAVE : 0,
10048 title, defname, NULL, initdir, NULL, curbuf);
10049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010053}
10054
10055/*
10056 * "browsedir(title, initdir)" function
10057 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010059f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010060{
10061#ifdef FEAT_BROWSE
10062 char_u *title;
10063 char_u *initdir;
10064 char_u buf[NUMBUFLEN];
10065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 title = get_tv_string_chk(&argvars[0]);
10067 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 if (title == NULL || initdir == NULL)
10070 rettv->vval.v_string = NULL;
10071 else
10072 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010073 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010080static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010081
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082/*
10083 * Find a buffer by number or exact name.
10084 */
10085 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010086find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010090 if (avar->v_type == VAR_NUMBER)
10091 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010092 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010094 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010095 if (buf == NULL)
10096 {
10097 /* No full path name match, try a match with a URL or a "nofile"
10098 * buffer, these don't use the full path. */
10099 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10100 if (buf->b_fname != NULL
10101 && (path_with_url(buf->b_fname)
10102#ifdef FEAT_QUICKFIX
10103 || bt_nofile(buf)
10104#endif
10105 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010106 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010107 break;
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110 return buf;
10111}
10112
10113/*
10114 * "bufexists(expr)" function
10115 */
10116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121
10122/*
10123 * "buflisted(expr)" function
10124 */
10125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 buf_T *buf;
10129
10130 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132}
10133
10134/*
10135 * "bufloaded(expr)" function
10136 */
10137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010138f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140 buf_T *buf;
10141
10142 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144}
10145
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010146 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010147buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 int save_magic;
10150 char_u *save_cpo;
10151 buf_T *buf;
10152
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10154 save_magic = p_magic;
10155 p_magic = TRUE;
10156 save_cpo = p_cpo;
10157 p_cpo = (char_u *)"";
10158
10159 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010160 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
10162 p_magic = save_magic;
10163 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010164 return buf;
10165}
10166
10167/*
10168 * Get buffer by number or pattern.
10169 */
10170 static buf_T *
10171get_buf_tv(typval_T *tv, int curtab_only)
10172{
10173 char_u *name = tv->vval.v_string;
10174 buf_T *buf;
10175
10176 if (tv->v_type == VAR_NUMBER)
10177 return buflist_findnr((int)tv->vval.v_number);
10178 if (tv->v_type != VAR_STRING)
10179 return NULL;
10180 if (name == NULL || *name == NUL)
10181 return curbuf;
10182 if (name[0] == '$' && name[1] == NUL)
10183 return lastbuf;
10184
10185 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
10187 /* If not found, try expanding the name, like done for bufexists(). */
10188 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
10191 return buf;
10192}
10193
10194/*
10195 * "bufname(expr)" function
10196 */
10197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010198f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199{
10200 buf_T *buf;
10201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010204 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 --emsg_off;
10211}
10212
10213/*
10214 * "bufnr(expr)" function
10215 */
10216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010217f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218{
10219 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010220 int error = FALSE;
10221 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010225 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010226 --emsg_off;
10227
10228 /* If the buffer isn't found and the second argument is not zero create a
10229 * new buffer. */
10230 if (buf == NULL
10231 && argvars[1].v_type != VAR_UNKNOWN
10232 && get_tv_number_chk(&argvars[1], &error) != 0
10233 && !error
10234 && (name = get_tv_string_chk(&argvars[0])) != NULL
10235 && !error)
10236 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10237
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242}
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010245buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246{
10247#ifdef FEAT_WINDOWS
10248 win_T *wp;
10249 int winnr = 0;
10250#endif
10251 buf_T *buf;
10252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010255 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_WINDOWS
10257 for (wp = firstwin; wp; wp = wp->w_next)
10258 {
10259 ++winnr;
10260 if (wp->w_buffer == buf)
10261 break;
10262 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010263 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010265 rettv->vval.v_number = (curwin->w_buffer == buf
10266 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267#endif
10268 --emsg_off;
10269}
10270
10271/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010272 * "bufwinid(nr)" function
10273 */
10274 static void
10275f_bufwinid(typval_T *argvars, typval_T *rettv)
10276{
10277 buf_win_common(argvars, rettv, FALSE);
10278}
10279
10280/*
10281 * "bufwinnr(nr)" function
10282 */
10283 static void
10284f_bufwinnr(typval_T *argvars, typval_T *rettv)
10285{
10286 buf_win_common(argvars, rettv, TRUE);
10287}
10288
10289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 * "byte2line(byte)" function
10291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010293f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
10295#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#else
10298 long boff = 0;
10299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 (linenr_T)0, &boff);
10306#endif
10307}
10308
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010310byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010311{
10312#ifdef FEAT_MBYTE
10313 char_u *t;
10314#endif
10315 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010316 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 str = get_tv_string_chk(&argvars[0]);
10319 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010322 return;
10323
10324#ifdef FEAT_MBYTE
10325 t = str;
10326 for ( ; idx > 0; idx--)
10327 {
10328 if (*t == NUL) /* EOL reached */
10329 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010330 if (enc_utf8 && comp)
10331 t += utf_ptr2len(t);
10332 else
10333 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010334 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010335 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010336#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010337 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010339#endif
10340}
10341
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010342/*
10343 * "byteidx()" function
10344 */
10345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010346f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010347{
10348 byteidx(argvars, rettv, FALSE);
10349}
10350
10351/*
10352 * "byteidxcomp()" function
10353 */
10354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010355f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010356{
10357 byteidx(argvars, rettv, TRUE);
10358}
10359
Bram Moolenaardb913952012-06-29 12:54:53 +020010360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010361func_call(
10362 char_u *name,
10363 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010364 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010365 dict_T *selfdict,
10366 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010367{
10368 listitem_T *item;
10369 typval_T argv[MAX_FUNC_ARGS + 1];
10370 int argc = 0;
10371 int dummy;
10372 int r = 0;
10373
10374 for (item = args->vval.v_list->lv_first; item != NULL;
10375 item = item->li_next)
10376 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010377 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010378 {
10379 EMSG(_("E699: Too many arguments"));
10380 break;
10381 }
10382 /* Make a copy of each argument. This is needed to be able to set
10383 * v_lock to VAR_FIXED in the copy without changing the original list.
10384 */
10385 copy_tv(&item->li_tv, &argv[argc++]);
10386 }
10387
10388 if (item == NULL)
10389 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10390 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010391 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010392
10393 /* Free the arguments. */
10394 while (argc > 0)
10395 clear_tv(&argv[--argc]);
10396
10397 return r;
10398}
10399
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010400/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010401 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010402 */
10403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010404f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010405{
10406 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010407 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010409
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 if (argvars[1].v_type != VAR_LIST)
10411 {
10412 EMSG(_(e_listreq));
10413 return;
10414 }
10415 if (argvars[1].vval.v_list == NULL)
10416 return;
10417
10418 if (argvars[0].v_type == VAR_FUNC)
10419 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010420 else if (argvars[0].v_type == VAR_PARTIAL)
10421 {
10422 partial = argvars[0].vval.v_partial;
10423 func = partial->pt_name;
10424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010425 else
10426 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 if (*func == NUL)
10428 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010429
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 if (argvars[2].v_type != VAR_UNKNOWN)
10431 {
10432 if (argvars[2].v_type != VAR_DICT)
10433 {
10434 EMSG(_(e_dictreq));
10435 return;
10436 }
10437 selfdict = argvars[2].vval.v_dict;
10438 }
10439
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010440 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010441}
10442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010443#ifdef FEAT_FLOAT
10444/*
10445 * "ceil({float})" function
10446 */
10447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010449{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010450 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451
10452 rettv->v_type = VAR_FLOAT;
10453 if (get_float_arg(argvars, &f) == OK)
10454 rettv->vval.v_float = ceil(f);
10455 else
10456 rettv->vval.v_float = 0.0;
10457}
10458#endif
10459
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010461/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010462 * "ch_close()" function
10463 */
10464 static void
10465f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10466{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010467 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010468
10469 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010470 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010471 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010472 channel_clear(channel);
10473 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010474}
10475
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010476/*
10477 * "ch_getbufnr()" function
10478 */
10479 static void
10480f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10481{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010482 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010483
10484 rettv->vval.v_number = -1;
10485 if (channel != NULL)
10486 {
10487 char_u *what = get_tv_string(&argvars[1]);
10488 int part;
10489
10490 if (STRCMP(what, "err") == 0)
10491 part = PART_ERR;
10492 else if (STRCMP(what, "out") == 0)
10493 part = PART_OUT;
10494 else if (STRCMP(what, "in") == 0)
10495 part = PART_IN;
10496 else
10497 part = PART_SOCK;
10498 if (channel->ch_part[part].ch_buffer != NULL)
10499 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10500 }
10501}
10502
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010503/*
10504 * "ch_getjob()" function
10505 */
10506 static void
10507f_ch_getjob(typval_T *argvars, typval_T *rettv)
10508{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010509 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010510
10511 if (channel != NULL)
10512 {
10513 rettv->v_type = VAR_JOB;
10514 rettv->vval.v_job = channel->ch_job;
10515 if (channel->ch_job != NULL)
10516 ++channel->ch_job->jv_refcount;
10517 }
10518}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010519
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010520/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010521 * "ch_info()" function
10522 */
10523 static void
10524f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10525{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010526 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010527
10528 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10529 channel_info(channel, rettv->vval.v_dict);
10530}
10531
10532/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010533 * "ch_log()" function
10534 */
10535 static void
10536f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10537{
10538 char_u *msg = get_tv_string(&argvars[0]);
10539 channel_T *channel = NULL;
10540
10541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010542 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010543
10544 ch_log(channel, (char *)msg);
10545}
10546
10547/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010548 * "ch_logfile()" function
10549 */
10550 static void
10551f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10552{
10553 char_u *fname;
10554 char_u *opt = (char_u *)"";
10555 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010556
10557 fname = get_tv_string(&argvars[0]);
10558 if (argvars[1].v_type == VAR_STRING)
10559 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010560 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010561}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010562
10563/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010564 * "ch_open()" function
10565 */
10566 static void
10567f_ch_open(typval_T *argvars, typval_T *rettv)
10568{
Bram Moolenaar77073442016-02-13 23:23:53 +010010569 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010570 if (check_restricted() || check_secure())
10571 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010572 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010573}
10574
10575/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010576 * "ch_read()" function
10577 */
10578 static void
10579f_ch_read(typval_T *argvars, typval_T *rettv)
10580{
10581 common_channel_read(argvars, rettv, FALSE);
10582}
10583
10584/*
10585 * "ch_readraw()" function
10586 */
10587 static void
10588f_ch_readraw(typval_T *argvars, typval_T *rettv)
10589{
10590 common_channel_read(argvars, rettv, TRUE);
10591}
10592
10593/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010594 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010595 */
10596 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010597f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10598{
10599 ch_expr_common(argvars, rettv, TRUE);
10600}
10601
10602/*
10603 * "ch_sendexpr()" function
10604 */
10605 static void
10606f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10607{
10608 ch_expr_common(argvars, rettv, FALSE);
10609}
10610
10611/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010612 * "ch_evalraw()" function
10613 */
10614 static void
10615f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10616{
10617 ch_raw_common(argvars, rettv, TRUE);
10618}
10619
10620/*
10621 * "ch_sendraw()" function
10622 */
10623 static void
10624f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10625{
10626 ch_raw_common(argvars, rettv, FALSE);
10627}
10628
10629/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010630 * "ch_setoptions()" function
10631 */
10632 static void
10633f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10634{
10635 channel_T *channel;
10636 jobopt_T opt;
10637
Bram Moolenaar437905c2016-04-26 19:01:05 +020010638 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010639 if (channel == NULL)
10640 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010641 clear_job_options(&opt);
10642 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010643 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10644 channel_set_options(channel, &opt);
10645 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010646}
10647
10648/*
10649 * "ch_status()" function
10650 */
10651 static void
10652f_ch_status(typval_T *argvars, typval_T *rettv)
10653{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010654 channel_T *channel;
10655
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010656 /* return an empty string by default */
10657 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010658 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010659
Bram Moolenaar437905c2016-04-26 19:01:05 +020010660 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010661 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010662}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010663#endif
10664
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010665/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010666 * "changenr()" function
10667 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010669f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010670{
10671 rettv->vval.v_number = curbuf->b_u_seq_cur;
10672}
10673
10674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 * "char2nr(string)" function
10676 */
10677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
10680#ifdef FEAT_MBYTE
10681 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010682 {
10683 int utf8 = 0;
10684
10685 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010686 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010687
10688 if (utf8)
10689 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10690 else
10691 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 else
10694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * "cindent(lnum)" function
10700 */
10701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010702f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704#ifdef FEAT_CINDENT
10705 pos_T pos;
10706 linenr_T lnum;
10707
10708 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10711 {
10712 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 curwin->w_cursor = pos;
10715 }
10716 else
10717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719}
10720
10721/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010722 * "clearmatches()" function
10723 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010725f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010726{
10727#ifdef FEAT_SEARCH_EXTRA
10728 clear_matches(curwin);
10729#endif
10730}
10731
10732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 * "col(string)" function
10734 */
10735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010736f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738 colnr_T col = 0;
10739 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010740 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010742 fp = var2fpos(&argvars[0], FALSE, &fnum);
10743 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 {
10745 if (fp->col == MAXCOL)
10746 {
10747 /* '> can be MAXCOL, get the length of the line then */
10748 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010749 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 else
10751 col = MAXCOL;
10752 }
10753 else
10754 {
10755 col = fp->col + 1;
10756#ifdef FEAT_VIRTUALEDIT
10757 /* col(".") when the cursor is on the NUL at the end of the line
10758 * because of "coladd" can be seen as an extra column. */
10759 if (virtual_active() && fp == &curwin->w_cursor)
10760 {
10761 char_u *p = ml_get_cursor();
10762
10763 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10764 curwin->w_virtcol - curwin->w_cursor.coladd))
10765 {
10766# ifdef FEAT_MBYTE
10767 int l;
10768
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010769 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 col += l;
10771# else
10772 if (*p != NUL && p[1] == NUL)
10773 ++col;
10774# endif
10775 }
10776 }
10777#endif
10778 }
10779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781}
10782
Bram Moolenaar572cb562005-08-05 21:35:02 +000010783#if defined(FEAT_INS_EXPAND)
10784/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010785 * "complete()" function
10786 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010789{
10790 int startcol;
10791
10792 if ((State & INSERT) == 0)
10793 {
10794 EMSG(_("E785: complete() can only be used in Insert mode"));
10795 return;
10796 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010797
10798 /* Check for undo allowed here, because if something was already inserted
10799 * the line was already saved for undo and this check isn't done. */
10800 if (!undo_allowed())
10801 return;
10802
Bram Moolenaarade00832006-03-10 21:46:58 +000010803 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10804 {
10805 EMSG(_(e_invarg));
10806 return;
10807 }
10808
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010809 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010810 if (startcol <= 0)
10811 return;
10812
10813 set_completion(startcol - 1, argvars[1].vval.v_list);
10814}
10815
10816/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010817 * "complete_add()" function
10818 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010821{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010822 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010823}
10824
10825/*
10826 * "complete_check()" function
10827 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010830{
10831 int saved = RedrawingDisabled;
10832
10833 RedrawingDisabled = 0;
10834 ins_compl_check_keys(0);
10835 rettv->vval.v_number = compl_interrupted;
10836 RedrawingDisabled = saved;
10837}
10838#endif
10839
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840/*
10841 * "confirm(message, buttons[, default [, type]])" function
10842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010844f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845{
10846#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10847 char_u *message;
10848 char_u *buttons = NULL;
10849 char_u buf[NUMBUFLEN];
10850 char_u buf2[NUMBUFLEN];
10851 int def = 1;
10852 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 char_u *typestr;
10854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 message = get_tv_string_chk(&argvars[0]);
10857 if (message == NULL)
10858 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10862 if (buttons == NULL)
10863 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010866 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010867 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10870 if (typestr == NULL)
10871 error = TRUE;
10872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 switch (TOUPPER_ASC(*typestr))
10875 {
10876 case 'E': type = VIM_ERROR; break;
10877 case 'Q': type = VIM_QUESTION; break;
10878 case 'I': type = VIM_INFO; break;
10879 case 'W': type = VIM_WARNING; break;
10880 case 'G': type = VIM_GENERIC; break;
10881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883 }
10884 }
10885 }
10886
10887 if (buttons == NULL || *buttons == NUL)
10888 buttons = (char_u *)_("&Ok");
10889
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010890 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010892 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893#endif
10894}
10895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010896/*
10897 * "copy()" function
10898 */
10899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010900f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010901{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010902 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010903}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010905#ifdef FEAT_FLOAT
10906/*
10907 * "cos()" function
10908 */
10909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010910f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010911{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010912 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010913
10914 rettv->v_type = VAR_FLOAT;
10915 if (get_float_arg(argvars, &f) == OK)
10916 rettv->vval.v_float = cos(f);
10917 else
10918 rettv->vval.v_float = 0.0;
10919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010920
10921/*
10922 * "cosh()" function
10923 */
10924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010925f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010926{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010927 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010928
10929 rettv->v_type = VAR_FLOAT;
10930 if (get_float_arg(argvars, &f) == OK)
10931 rettv->vval.v_float = cosh(f);
10932 else
10933 rettv->vval.v_float = 0.0;
10934}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010935#endif
10936
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010938 * "count()" function
10939 */
10940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010941f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010942{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010943 long n = 0;
10944 int ic = FALSE;
10945
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 listitem_T *li;
10949 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010951
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if ((l = argvars[0].vval.v_list) != NULL)
10953 {
10954 li = l->lv_first;
10955 if (argvars[2].v_type != VAR_UNKNOWN)
10956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 int error = FALSE;
10958
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010959 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 if (argvars[3].v_type != VAR_UNKNOWN)
10961 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010962 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 if (!error)
10964 {
10965 li = list_find(l, idx);
10966 if (li == NULL)
10967 EMSGN(_(e_listidx), idx);
10968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 if (error)
10971 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 }
10973
10974 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010975 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 ++n;
10977 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 else if (argvars[0].v_type == VAR_DICT)
10980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 int todo;
10982 dict_T *d;
10983 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010985 if ((d = argvars[0].vval.v_dict) != NULL)
10986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 int error = FALSE;
10988
Bram Moolenaare9a41262005-01-15 22:18:47 +000010989 if (argvars[2].v_type != VAR_UNKNOWN)
10990 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010991 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 if (argvars[3].v_type != VAR_UNKNOWN)
10993 EMSG(_(e_invarg));
10994 }
10995
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010996 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010998 {
10999 if (!HASHITEM_EMPTY(hi))
11000 {
11001 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011002 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011003 ++n;
11004 }
11005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 }
11007 }
11008 else
11009 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011010 rettv->vval.v_number = n;
11011}
11012
11013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11015 *
11016 * Checks the existence of a cscope connection.
11017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021#ifdef FEAT_CSCOPE
11022 int num = 0;
11023 char_u *dbpath = NULL;
11024 char_u *prepend = NULL;
11025 char_u buf[NUMBUFLEN];
11026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011027 if (argvars[0].v_type != VAR_UNKNOWN
11028 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 num = (int)get_tv_number(&argvars[0]);
11031 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037#endif
11038}
11039
11040/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011041 * "cursor(lnum, col)" function, or
11042 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011044 * Moves the cursor to the specified line and column.
11045 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011048f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011051#ifdef FEAT_VIRTUALEDIT
11052 long coladd = 0;
11053#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011054 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011056 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011057 if (argvars[1].v_type == VAR_UNKNOWN)
11058 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011059 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011060 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011061
Bram Moolenaar493c1782014-05-28 14:34:46 +020011062 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011063 {
11064 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011065 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011066 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011067 line = pos.lnum;
11068 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011069#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011070 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011071#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011072 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011073 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011074 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011075 set_curswant = FALSE;
11076 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011077 }
11078 else
11079 {
11080 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011081 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011082#ifdef FEAT_VIRTUALEDIT
11083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011084 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011085#endif
11086 }
11087 if (line < 0 || col < 0
11088#ifdef FEAT_VIRTUALEDIT
11089 || coladd < 0
11090#endif
11091 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 if (line > 0)
11094 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 if (col > 0)
11096 curwin->w_cursor.col = col - 1;
11097#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011098 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099#endif
11100
11101 /* Make sure the cursor is in a valid position. */
11102 check_cursor();
11103#ifdef FEAT_MBYTE
11104 /* Correct cursor for multi-byte character. */
11105 if (has_mbyte)
11106 mb_adjust_cursor();
11107#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011108
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011109 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111}
11112
11113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 */
11116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011117f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011119 int noref = 0;
11120
11121 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011122 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011123 if (noref < 0 || noref > 1)
11124 EMSG(_(e_invarg));
11125 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011126 {
11127 current_copyID += COPYID_INC;
11128 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131
11132/*
11133 * "delete()" function
11134 */
11135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011136f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011138 char_u nbuf[NUMBUFLEN];
11139 char_u *name;
11140 char_u *flags;
11141
11142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011144 return;
11145
11146 name = get_tv_string(&argvars[0]);
11147 if (name == NULL || *name == NUL)
11148 {
11149 EMSG(_(e_invarg));
11150 return;
11151 }
11152
11153 if (argvars[1].v_type != VAR_UNKNOWN)
11154 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011156 flags = (char_u *)"";
11157
11158 if (*flags == NUL)
11159 /* delete a file */
11160 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11161 else if (STRCMP(flags, "d") == 0)
11162 /* delete an empty directory */
11163 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11164 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011165 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011166 rettv->vval.v_number = delete_recursive(name);
11167 else
11168 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169}
11170
11171/*
11172 * "did_filetype()" function
11173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011175f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176{
11177#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179#endif
11180}
11181
11182/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011183 * "diff_filler()" function
11184 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011186f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011187{
11188#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011190#endif
11191}
11192
11193/*
11194 * "diff_hlID()" function
11195 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011197f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011198{
11199#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011201 static linenr_T prev_lnum = 0;
11202 static int changedtick = 0;
11203 static int fnum = 0;
11204 static int change_start = 0;
11205 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011206 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011207 int filler_lines;
11208 int col;
11209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 if (lnum < 0) /* ignore type error in {lnum} arg */
11211 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011212 if (lnum != prev_lnum
11213 || changedtick != curbuf->b_changedtick
11214 || fnum != curbuf->b_fnum)
11215 {
11216 /* New line, buffer, change: need to get the values. */
11217 filler_lines = diff_check(curwin, lnum);
11218 if (filler_lines < 0)
11219 {
11220 if (filler_lines == -1)
11221 {
11222 change_start = MAXCOL;
11223 change_end = -1;
11224 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11225 hlID = HLF_ADD; /* added line */
11226 else
11227 hlID = HLF_CHD; /* changed line */
11228 }
11229 else
11230 hlID = HLF_ADD; /* added line */
11231 }
11232 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011233 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011234 prev_lnum = lnum;
11235 changedtick = curbuf->b_changedtick;
11236 fnum = curbuf->b_fnum;
11237 }
11238
11239 if (hlID == HLF_CHD || hlID == HLF_TXD)
11240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011242 if (col >= change_start && col <= change_end)
11243 hlID = HLF_TXD; /* changed text */
11244 else
11245 hlID = HLF_CHD; /* changed line */
11246 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011247 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011248#endif
11249}
11250
11251/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011252 * "empty({expr})" function
11253 */
11254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011255f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011256{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011257 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011258
11259 switch (argvars[0].v_type)
11260 {
11261 case VAR_STRING:
11262 case VAR_FUNC:
11263 n = argvars[0].vval.v_string == NULL
11264 || *argvars[0].vval.v_string == NUL;
11265 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011266 case VAR_PARTIAL:
11267 n = FALSE;
11268 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011269 case VAR_NUMBER:
11270 n = argvars[0].vval.v_number == 0;
11271 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011272 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011273#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011274 n = argvars[0].vval.v_float == 0.0;
11275 break;
11276#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011277 case VAR_LIST:
11278 n = argvars[0].vval.v_list == NULL
11279 || argvars[0].vval.v_list->lv_first == NULL;
11280 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011281 case VAR_DICT:
11282 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011284 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011285 case VAR_SPECIAL:
11286 n = argvars[0].vval.v_number != VVAL_TRUE;
11287 break;
11288
Bram Moolenaar835dc632016-02-07 14:27:38 +010011289 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011290#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011291 n = argvars[0].vval.v_job == NULL
11292 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11293 break;
11294#endif
11295 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011297 n = argvars[0].vval.v_channel == NULL
11298 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011299 break;
11300#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011301 case VAR_UNKNOWN:
11302 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11303 n = TRUE;
11304 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011305 }
11306
11307 rettv->vval.v_number = n;
11308}
11309
11310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 * "escape({string}, {chars})" function
11312 */
11313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011314f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315{
11316 char_u buf[NUMBUFLEN];
11317
Bram Moolenaar758711c2005-02-02 23:11:38 +000011318 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11319 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321}
11322
11323/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011324 * "eval()" function
11325 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011327f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011329 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 s = get_tv_string_chk(&argvars[0]);
11332 if (s != NULL)
11333 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334
Bram Moolenaar615b9972015-01-14 17:15:05 +010011335 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11337 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011338 if (p != NULL && !aborting())
11339 EMSG2(_(e_invexpr2), p);
11340 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011344 else if (*s != NUL)
11345 EMSG(_(e_trailing));
11346}
11347
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011348static garray_T redir_evalcmd_ga;
11349
11350/*
11351 * Append "value[value_len]" to the evalcmd() output.
11352 */
11353 void
11354evalcmd_redir_str(char_u *value, int value_len)
11355{
11356 int len;
11357
11358 if (value_len == -1)
11359 len = (int)STRLEN(value); /* Append the entire string */
11360 else
11361 len = value_len; /* Append only "value_len" characters */
11362 if (ga_grow(&redir_evalcmd_ga, len) == OK)
11363 {
11364 mch_memmove((char *)redir_evalcmd_ga.ga_data
11365 + redir_evalcmd_ga.ga_len, value, len);
11366 redir_evalcmd_ga.ga_len += len;
11367 }
11368}
11369
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370/*
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011371 * "evalcmd()" function
11372 */
11373 static void
11374f_evalcmd(typval_T *argvars, typval_T *rettv)
11375{
11376 char_u *s;
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011377 int save_msg_silent = msg_silent;
11378 int save_redir_evalcmd = redir_evalcmd;
11379 garray_T save_ga;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011380
11381 rettv->vval.v_string = NULL;
11382 rettv->v_type = VAR_STRING;
11383
11384 s = get_tv_string_chk(&argvars[0]);
11385 if (s != NULL)
11386 {
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011387 if (redir_evalcmd)
11388 save_ga = redir_evalcmd_ga;
11389 ga_init2(&redir_evalcmd_ga, (int)sizeof(char), 500);
11390 redir_evalcmd = TRUE;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011391
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011392 ++msg_silent;
11393 do_cmdline_cmd(s);
11394 rettv->vval.v_string = redir_evalcmd_ga.ga_data;
11395 msg_silent = save_msg_silent;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011396
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011397 redir_evalcmd = save_redir_evalcmd;
11398 if (redir_evalcmd)
11399 redir_evalcmd_ga = save_ga;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011400 }
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011401}
11402
11403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 * "eventhandler()" function
11405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011407f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410}
11411
11412/*
11413 * "executable()" function
11414 */
11415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011416f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011418 char_u *name = get_tv_string(&argvars[0]);
11419
11420 /* Check in $PATH and also check directly if there is a directory name. */
11421 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11422 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011423}
11424
11425/*
11426 * "exepath()" function
11427 */
11428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011429f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011430{
11431 char_u *p = NULL;
11432
Bram Moolenaarb5971142015-03-21 17:32:19 +010011433 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011434 rettv->v_type = VAR_STRING;
11435 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436}
11437
11438/*
11439 * "exists()" function
11440 */
11441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 char_u *p;
11445 char_u *name;
11446 int n = FALSE;
11447 int len = 0;
11448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 if (*p == '$') /* environment variable */
11451 {
11452 /* first try "normal" environment variables (fast) */
11453 if (mch_getenv(p + 1) != NULL)
11454 n = TRUE;
11455 else
11456 {
11457 /* try expanding things like $VIM and ${HOME} */
11458 p = expand_env_save(p);
11459 if (p != NULL && *p != '$')
11460 n = TRUE;
11461 vim_free(p);
11462 }
11463 }
11464 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011467 if (*skipwhite(p) != NUL)
11468 n = FALSE; /* trailing garbage */
11469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 else if (*p == '*') /* internal or user defined function */
11471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474 else if (*p == ':')
11475 {
11476 n = cmd_exists(p + 1);
11477 }
11478 else if (*p == '#')
11479 {
11480#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011481 if (p[1] == '#')
11482 n = autocmd_supported(p + 2);
11483 else
11484 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485#endif
11486 }
11487 else /* internal variable */
11488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011489 char_u *tofree;
11490 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011492 /* get_name_len() takes care of expanding curly braces */
11493 name = p;
11494 len = get_name_len(&p, &tofree, TRUE, FALSE);
11495 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011497 if (tofree != NULL)
11498 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011499 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011500 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011502 /* handle d.key, l[idx], f(expr) */
11503 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11504 if (n)
11505 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 }
11507 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011508 if (*p != NUL)
11509 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011511 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011517#ifdef FEAT_FLOAT
11518/*
11519 * "exp()" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011523{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011524 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011525
11526 rettv->v_type = VAR_FLOAT;
11527 if (get_float_arg(argvars, &f) == OK)
11528 rettv->vval.v_float = exp(f);
11529 else
11530 rettv->vval.v_float = 0.0;
11531}
11532#endif
11533
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534/*
11535 * "expand()" function
11536 */
11537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011538f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539{
11540 char_u *s;
11541 int len;
11542 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011543 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011546 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011549 if (argvars[1].v_type != VAR_UNKNOWN
11550 && argvars[2].v_type != VAR_UNKNOWN
11551 && get_tv_number_chk(&argvars[2], &error)
11552 && !error)
11553 {
11554 rettv->v_type = VAR_LIST;
11555 rettv->vval.v_list = NULL;
11556 }
11557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 if (*s == '%' || *s == '#' || *s == '<')
11560 {
11561 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011562 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011564 if (rettv->v_type == VAR_LIST)
11565 {
11566 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11567 list_append_string(rettv->vval.v_list, result, -1);
11568 else
11569 vim_free(result);
11570 }
11571 else
11572 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 }
11574 else
11575 {
11576 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011577 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578 if (argvars[1].v_type != VAR_UNKNOWN
11579 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011580 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011581 if (!error)
11582 {
11583 ExpandInit(&xpc);
11584 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011585 if (p_wic)
11586 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011587 if (rettv->v_type == VAR_STRING)
11588 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11589 options, WILD_ALL);
11590 else if (rettv_list_alloc(rettv) != FAIL)
11591 {
11592 int i;
11593
11594 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11595 for (i = 0; i < xpc.xp_numfiles; i++)
11596 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11597 ExpandCleanup(&xpc);
11598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 }
11600 else
11601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 }
11603}
11604
11605/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011606 * Go over all entries in "d2" and add them to "d1".
11607 * When "action" is "error" then a duplicate key is an error.
11608 * When "action" is "force" then a duplicate key is overwritten.
11609 * Otherwise duplicate keys are ignored ("action" is "keep").
11610 */
11611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011612dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011613{
11614 dictitem_T *di1;
11615 hashitem_T *hi2;
11616 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011617 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011618
11619 todo = (int)d2->dv_hashtab.ht_used;
11620 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11621 {
11622 if (!HASHITEM_EMPTY(hi2))
11623 {
11624 --todo;
11625 di1 = dict_find(d1, hi2->hi_key, -1);
11626 if (d1->dv_scope != 0)
11627 {
11628 /* Disallow replacing a builtin function in l: and g:.
11629 * Check the key to be valid when adding to any
11630 * scope. */
11631 if (d1->dv_scope == VAR_DEF_SCOPE
11632 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11633 && var_check_func_name(hi2->hi_key,
11634 di1 == NULL))
11635 break;
11636 if (!valid_varname(hi2->hi_key))
11637 break;
11638 }
11639 if (di1 == NULL)
11640 {
11641 di1 = dictitem_copy(HI2DI(hi2));
11642 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11643 dictitem_free(di1);
11644 }
11645 else if (*action == 'e')
11646 {
11647 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11648 break;
11649 }
11650 else if (*action == 'f' && HI2DI(hi2) != di1)
11651 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011652 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11653 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011654 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011655 clear_tv(&di1->di_tv);
11656 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11657 }
11658 }
11659 }
11660}
11661
11662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011663 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011664 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011665 */
11666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011667f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011668{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011669 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011670
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011672 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011673 list_T *l1, *l2;
11674 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011675 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011677
Bram Moolenaare9a41262005-01-15 22:18:47 +000011678 l1 = argvars[0].vval.v_list;
11679 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011680 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011681 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011682 {
11683 if (argvars[2].v_type != VAR_UNKNOWN)
11684 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011685 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 if (error)
11687 return; /* type error; errmsg already given */
11688
Bram Moolenaar758711c2005-02-02 23:11:38 +000011689 if (before == l1->lv_len)
11690 item = NULL;
11691 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011692 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011693 item = list_find(l1, before);
11694 if (item == NULL)
11695 {
11696 EMSGN(_(e_listidx), before);
11697 return;
11698 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 }
11700 }
11701 else
11702 item = NULL;
11703 list_extend(l1, l2, item);
11704
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 copy_tv(&argvars[0], rettv);
11706 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11709 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011710 dict_T *d1, *d2;
11711 char_u *action;
11712 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713
11714 d1 = argvars[0].vval.v_dict;
11715 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011716 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011717 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 {
11719 /* Check the third argument. */
11720 if (argvars[2].v_type != VAR_UNKNOWN)
11721 {
11722 static char *(av[]) = {"keep", "force", "error"};
11723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 action = get_tv_string_chk(&argvars[2]);
11725 if (action == NULL)
11726 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011727 for (i = 0; i < 3; ++i)
11728 if (STRCMP(action, av[i]) == 0)
11729 break;
11730 if (i == 3)
11731 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011732 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 return;
11734 }
11735 }
11736 else
11737 action = (char_u *)"force";
11738
Bram Moolenaara9922d62013-05-30 13:01:18 +020011739 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 copy_tv(&argvars[0], rettv);
11742 }
11743 }
11744 else
11745 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011746}
11747
11748/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011749 * "feedkeys()" function
11750 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011752f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011753{
11754 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011755 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011756 char_u *keys, *flags;
11757 char_u nbuf[NUMBUFLEN];
11758 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011759 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011760 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011761 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011762
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011763 /* This is not allowed in the sandbox. If the commands would still be
11764 * executed in the sandbox it would be OK, but it probably happens later,
11765 * when "sandbox" is no longer set. */
11766 if (check_secure())
11767 return;
11768
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011769 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011770
11771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011772 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011773 flags = get_tv_string_buf(&argvars[1], nbuf);
11774 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011775 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011776 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011777 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011778 case 'n': remap = FALSE; break;
11779 case 'm': remap = TRUE; break;
11780 case 't': typed = TRUE; break;
11781 case 'i': insert = TRUE; break;
11782 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011783 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011784 }
11785 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011786 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011787
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011788 if (*keys != NUL || execute)
11789 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011790 /* Need to escape K_SPECIAL and CSI before putting the string in the
11791 * typeahead buffer. */
11792 keys_esc = vim_strsave_escape_csi(keys);
11793 if (keys_esc != NULL)
11794 {
11795 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011796 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011797 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011798 if (vgetc_busy)
11799 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011800 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011801 {
11802 int save_msg_scroll = msg_scroll;
11803
11804 /* Avoid a 1 second delay when the keys start Insert mode. */
11805 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011806
Bram Moolenaar245c4102016-04-20 17:37:41 +020011807 if (!dangerous)
11808 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011809 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011810 if (!dangerous)
11811 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011812 msg_scroll |= save_msg_scroll;
11813 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011814 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011815 }
11816}
11817
11818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 * "filereadable()" function
11820 */
11821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011822f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011824 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 char_u *p;
11826 int n;
11827
Bram Moolenaarc236c162008-07-13 17:41:49 +000011828#ifndef O_NONBLOCK
11829# define O_NONBLOCK 0
11830#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011832 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11833 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 {
11835 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011836 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 }
11838 else
11839 n = FALSE;
11840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842}
11843
11844/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011845 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 * rights to write into.
11847 */
11848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011849f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011851 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852}
11853
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011854 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011855findfilendir(
11856 typval_T *argvars UNUSED,
11857 typval_T *rettv,
11858 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011859{
11860#ifdef FEAT_SEARCHPATH
11861 char_u *fname;
11862 char_u *fresult = NULL;
11863 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11864 char_u *p;
11865 char_u pathbuf[NUMBUFLEN];
11866 int count = 1;
11867 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011868 int error = FALSE;
11869#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011870
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011871 rettv->vval.v_string = NULL;
11872 rettv->v_type = VAR_STRING;
11873
11874#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011876
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11880 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011881 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011882 else
11883 {
11884 if (*p != NUL)
11885 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011888 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011890 }
11891
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011892 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11893 error = TRUE;
11894
11895 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011897 do
11898 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011899 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011900 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 fresult = find_file_in_path_option(first ? fname : NULL,
11902 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011903 0, first, path,
11904 find_what,
11905 curbuf->b_ffname,
11906 find_what == FINDFILE_DIR
11907 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011909
11910 if (fresult != NULL && rettv->v_type == VAR_LIST)
11911 list_append_string(rettv->vval.v_list, fresult, -1);
11912
11913 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011915
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011916 if (rettv->v_type == VAR_STRING)
11917 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011918#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011919}
11920
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011921static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011922static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923
11924/*
11925 * Implementation of map() and filter().
11926 */
11927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011930 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 listitem_T *li, *nli;
11932 list_T *l = NULL;
11933 dictitem_T *di;
11934 hashtab_T *ht;
11935 hashitem_T *hi;
11936 dict_T *d = NULL;
11937 typval_T save_val;
11938 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011939 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011940 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011941 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011942 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011943 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011944 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011945 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011946
Bram Moolenaare9a41262005-01-15 22:18:47 +000011947 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011949 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011950 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951 return;
11952 }
11953 else if (argvars[0].v_type == VAR_DICT)
11954 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011955 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011956 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011957 return;
11958 }
11959 else
11960 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011961 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011962 return;
11963 }
11964
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011965 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 /* On type errors, the preceding call has already displayed an error
11967 * message. Avoid a misleading error message for an empty string that
11968 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011969 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011972
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011973 /* We reset "did_emsg" to be able to detect whether an error
11974 * occurred during evaluation of the expression. */
11975 save_did_emsg = did_emsg;
11976 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011977
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011978 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 vimvars[VV_KEY].vv_type = VAR_STRING;
11982
11983 ht = &d->dv_hashtab;
11984 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011985 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 if (!HASHITEM_EMPTY(hi))
11989 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011990 int r;
11991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 --todo;
11993 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011994 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011995 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11996 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 break;
11998 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011999 r = filter_map_one(&di->di_tv, expr, map, &rem);
12000 clear_tv(&vimvars[VV_KEY].vv_tv);
12001 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 break;
12003 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012004 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012005 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12006 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012007 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 }
12011 }
12012 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 }
12014 else
12015 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012016 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 for (li = l->lv_first; li != NULL; li = nli)
12019 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012020 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012021 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012022 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012023 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012024 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012025 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012026 break;
12027 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012029 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012031 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012032
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012033 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012035
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012036 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012038
12039 copy_tv(&argvars[0], rettv);
12040}
12041
12042 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012043filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012044{
Bram Moolenaar33570922005-01-25 22:26:29 +000012045 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012046 typval_T argv[3];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012047 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012048 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012049 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012050
Bram Moolenaar33570922005-01-25 22:26:29 +000012051 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012052 argv[0] = vimvars[VV_KEY].vv_tv;
12053 argv[1] = vimvars[VV_VAL].vv_tv;
12054 s = expr->vval.v_string;
12055 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012056 {
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012057 if (call_func(s, (int)STRLEN(s),
12058 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12059 goto theend;
12060 }
12061 else if (expr->v_type == VAR_PARTIAL)
12062 {
12063 partial_T *partial = expr->vval.v_partial;
12064
12065 s = partial->pt_name;
12066 if (call_func(s, (int)STRLEN(s),
12067 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12068 == FAIL)
12069 goto theend;
12070 }
12071 else
12072 {
12073 s = skipwhite(s);
12074 if (eval1(&s, &rettv, TRUE) == FAIL)
12075 goto theend;
12076 if (*s != NUL) /* check for trailing chars after expr */
12077 {
12078 EMSG2(_(e_invexpr2), s);
12079 goto theend;
12080 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012081 }
12082 if (map)
12083 {
12084 /* map(): replace the list item value */
12085 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012086 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012087 *tv = rettv;
12088 }
12089 else
12090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012091 int error = FALSE;
12092
Bram Moolenaare9a41262005-01-15 22:18:47 +000012093 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012094 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012095 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012096 /* On type error, nothing has been removed; return FAIL to stop the
12097 * loop. The error message was given by get_tv_number_chk(). */
12098 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012099 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012100 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012101 retval = OK;
12102theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012103 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012104 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012105}
12106
12107/*
12108 * "filter()" function
12109 */
12110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012111f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012112{
12113 filter_map(argvars, rettv, FALSE);
12114}
12115
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012116/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012117 * "finddir({fname}[, {path}[, {count}]])" function
12118 */
12119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012120f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012121{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012122 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012123}
12124
12125/*
12126 * "findfile({fname}[, {path}[, {count}]])" function
12127 */
12128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012129f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012131 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132}
12133
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012134#ifdef FEAT_FLOAT
12135/*
12136 * "float2nr({float})" function
12137 */
12138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012139f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012140{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012141 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012142
12143 if (get_float_arg(argvars, &f) == OK)
12144 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012145# ifdef FEAT_NUM64
12146 if (f < -0x7fffffffffffffff)
12147 rettv->vval.v_number = -0x7fffffffffffffff;
12148 else if (f > 0x7fffffffffffffff)
12149 rettv->vval.v_number = 0x7fffffffffffffff;
12150 else
12151 rettv->vval.v_number = (varnumber_T)f;
12152# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012153 if (f < -0x7fffffff)
12154 rettv->vval.v_number = -0x7fffffff;
12155 else if (f > 0x7fffffff)
12156 rettv->vval.v_number = 0x7fffffff;
12157 else
12158 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012159# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012160 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012161}
12162
12163/*
12164 * "floor({float})" function
12165 */
12166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012167f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012168{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012169 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012170
12171 rettv->v_type = VAR_FLOAT;
12172 if (get_float_arg(argvars, &f) == OK)
12173 rettv->vval.v_float = floor(f);
12174 else
12175 rettv->vval.v_float = 0.0;
12176}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012177
12178/*
12179 * "fmod()" function
12180 */
12181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012182f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012183{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012184 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012185
12186 rettv->v_type = VAR_FLOAT;
12187 if (get_float_arg(argvars, &fx) == OK
12188 && get_float_arg(&argvars[1], &fy) == OK)
12189 rettv->vval.v_float = fmod(fx, fy);
12190 else
12191 rettv->vval.v_float = 0.0;
12192}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012193#endif
12194
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012196 * "fnameescape({string})" function
12197 */
12198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012199f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012200{
12201 rettv->vval.v_string = vim_strsave_fnameescape(
12202 get_tv_string(&argvars[0]), FALSE);
12203 rettv->v_type = VAR_STRING;
12204}
12205
12206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 * "fnamemodify({fname}, {mods})" function
12208 */
12209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012210f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
12212 char_u *fname;
12213 char_u *mods;
12214 int usedlen = 0;
12215 int len;
12216 char_u *fbuf = NULL;
12217 char_u buf[NUMBUFLEN];
12218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012219 fname = get_tv_string_chk(&argvars[0]);
12220 mods = get_tv_string_buf_chk(&argvars[1], buf);
12221 if (fname == NULL || mods == NULL)
12222 fname = NULL;
12223 else
12224 {
12225 len = (int)STRLEN(fname);
12226 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 vim_free(fbuf);
12235}
12236
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012237static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238
12239/*
12240 * "foldclosed()" function
12241 */
12242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012243foldclosed_both(
12244 typval_T *argvars UNUSED,
12245 typval_T *rettv,
12246 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247{
12248#ifdef FEAT_FOLDING
12249 linenr_T lnum;
12250 linenr_T first, last;
12251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12254 {
12255 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12256 {
12257 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261 return;
12262 }
12263 }
12264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266}
12267
12268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012269 * "foldclosed()" function
12270 */
12271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012272f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012273{
12274 foldclosed_both(argvars, rettv, FALSE);
12275}
12276
12277/*
12278 * "foldclosedend()" function
12279 */
12280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012281f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282{
12283 foldclosed_both(argvars, rettv, TRUE);
12284}
12285
12286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 * "foldlevel()" function
12288 */
12289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012290f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291{
12292#ifdef FEAT_FOLDING
12293 linenr_T lnum;
12294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012296 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299}
12300
12301/*
12302 * "foldtext()" function
12303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012305f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306{
12307#ifdef FEAT_FOLDING
12308 linenr_T lnum;
12309 char_u *s;
12310 char_u *r;
12311 int len;
12312 char *txt;
12313#endif
12314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->v_type = VAR_STRING;
12316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012318 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12319 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12320 <= curbuf->b_ml.ml_line_count
12321 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 {
12323 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012324 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12325 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 {
12327 if (!linewhite(lnum))
12328 break;
12329 ++lnum;
12330 }
12331
12332 /* Find interesting text in this line. */
12333 s = skipwhite(ml_get(lnum));
12334 /* skip C comment-start */
12335 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012338 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012339 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012340 {
12341 s = skipwhite(ml_get(lnum + 1));
12342 if (*s == '*')
12343 s = skipwhite(s + 1);
12344 }
12345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012346 txt = _("+-%s%3ld lines: ");
12347 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012348 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 + 20 /* for %3ld */
12350 + STRLEN(s))); /* concatenated */
12351 if (r != NULL)
12352 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012353 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12354 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12355 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 len = (int)STRLEN(r);
12357 STRCAT(r, s);
12358 /* remove 'foldmarker' and 'commentstring' */
12359 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 }
12362 }
12363#endif
12364}
12365
12366/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012367 * "foldtextresult(lnum)" function
12368 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012370f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012371{
12372#ifdef FEAT_FOLDING
12373 linenr_T lnum;
12374 char_u *text;
12375 char_u buf[51];
12376 foldinfo_T foldinfo;
12377 int fold_count;
12378#endif
12379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->v_type = VAR_STRING;
12381 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012382#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 /* treat illegal types and illegal string values for {lnum} the same */
12385 if (lnum < 0)
12386 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012387 fold_count = foldedCount(curwin, lnum, &foldinfo);
12388 if (fold_count > 0)
12389 {
12390 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12391 &foldinfo, buf);
12392 if (text == buf)
12393 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012395 }
12396#endif
12397}
12398
12399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 * "foreground()" function
12401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012403f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405#ifdef FEAT_GUI
12406 if (gui.in_use)
12407 gui_mch_set_foreground();
12408#else
12409# ifdef WIN32
12410 win32_set_foreground();
12411# endif
12412#endif
12413}
12414
12415/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 * "function()" function
12417 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012419f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012420{
12421 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012422 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012423 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012424 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012425
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012426 if (argvars[0].v_type == VAR_FUNC)
12427 {
12428 /* function(MyFunc, [arg], dict) */
12429 s = argvars[0].vval.v_string;
12430 }
12431 else if (argvars[0].v_type == VAR_PARTIAL
12432 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012433 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012434 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012435 arg_pt = argvars[0].vval.v_partial;
12436 s = arg_pt->pt_name;
12437 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012438 else
12439 {
12440 /* function('MyFunc', [arg], dict) */
12441 s = get_tv_string(&argvars[0]);
12442 use_string = TRUE;
12443 }
12444
12445 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012446 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012447 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012448 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12449 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012450 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012451 else
12452 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012453 int dict_idx = 0;
12454 int arg_idx = 0;
12455 list_T *list = NULL;
12456
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012457 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012458 {
12459 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012460 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012461
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012462 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12463 * also be called from another script. Using trans_function_name()
12464 * would also work, but some plugins depend on the name being
12465 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012466 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012467 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12468 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012469 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012470 STRCPY(name, sid_buf);
12471 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012472 }
12473 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012474 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012475 name = vim_strsave(s);
12476
12477 if (argvars[1].v_type != VAR_UNKNOWN)
12478 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012479 if (argvars[2].v_type != VAR_UNKNOWN)
12480 {
12481 /* function(name, [args], dict) */
12482 arg_idx = 1;
12483 dict_idx = 2;
12484 }
12485 else if (argvars[1].v_type == VAR_DICT)
12486 /* function(name, dict) */
12487 dict_idx = 1;
12488 else
12489 /* function(name, [args]) */
12490 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012491 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012492 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012493 if (argvars[dict_idx].v_type != VAR_DICT)
12494 {
12495 EMSG(_("E922: expected a dict"));
12496 vim_free(name);
12497 return;
12498 }
12499 if (argvars[dict_idx].vval.v_dict == NULL)
12500 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012501 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012502 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012503 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012504 if (argvars[arg_idx].v_type != VAR_LIST)
12505 {
12506 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12507 vim_free(name);
12508 return;
12509 }
12510 list = argvars[arg_idx].vval.v_list;
12511 if (list == NULL || list->lv_len == 0)
12512 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012513 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012514 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012515 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012516 {
12517 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012518
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012519 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012520 if (pt == NULL)
12521 vim_free(name);
12522 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012523 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012524 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012525 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012526 listitem_T *li;
12527 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012528 int arg_len = 0;
12529 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012530
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012531 if (arg_pt != NULL)
12532 arg_len = arg_pt->pt_argc;
12533 if (list != NULL)
12534 lv_len = list->lv_len;
12535 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012536 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012537 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012538 if (pt->pt_argv == NULL)
12539 {
12540 vim_free(pt);
12541 vim_free(name);
12542 return;
12543 }
12544 else
12545 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012546 for (i = 0; i < arg_len; i++)
12547 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12548 if (lv_len > 0)
12549 for (li = list->lv_first; li != NULL;
12550 li = li->li_next)
12551 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012552 }
12553 }
12554
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012555 /* For "function(dict.func, [], dict)" and "func" is a partial
12556 * use "dict". That is backwards compatible. */
12557 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012558 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012559 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012560 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12561 ++pt->pt_dict->dv_refcount;
12562 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012563 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012564 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012565 /* If the dict was bound automatically the result is also
12566 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012567 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012568 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012569 if (pt->pt_dict != NULL)
12570 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012571 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012572
12573 pt->pt_refcount = 1;
12574 pt->pt_name = name;
12575 func_ref(pt->pt_name);
12576 }
12577 rettv->v_type = VAR_PARTIAL;
12578 rettv->vval.v_partial = pt;
12579 }
12580 else
12581 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012582 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012583 rettv->v_type = VAR_FUNC;
12584 rettv->vval.v_string = name;
12585 func_ref(name);
12586 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012587 }
12588}
12589
12590/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012591 * "garbagecollect()" function
12592 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012594f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012595{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012596 /* This is postponed until we are back at the toplevel, because we may be
12597 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12598 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012599
12600 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12601 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012602}
12603
12604/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605 * "get()" function
12606 */
12607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012608f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609{
Bram Moolenaar33570922005-01-25 22:26:29 +000012610 listitem_T *li;
12611 list_T *l;
12612 dictitem_T *di;
12613 dict_T *d;
12614 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615
Bram Moolenaare9a41262005-01-15 22:18:47 +000012616 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012618 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012620 int error = FALSE;
12621
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012622 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012624 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012625 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012627 else if (argvars[0].v_type == VAR_DICT)
12628 {
12629 if ((d = argvars[0].vval.v_dict) != NULL)
12630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012631 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012632 if (di != NULL)
12633 tv = &di->di_tv;
12634 }
12635 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012636 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012637 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012638 partial_T *pt;
12639 partial_T fref_pt;
12640
12641 if (argvars[0].v_type == VAR_PARTIAL)
12642 pt = argvars[0].vval.v_partial;
12643 else
12644 {
12645 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12646 fref_pt.pt_name = argvars[0].vval.v_string;
12647 pt = &fref_pt;
12648 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012649
12650 if (pt != NULL)
12651 {
12652 char_u *what = get_tv_string(&argvars[1]);
12653
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012654 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012655 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012656 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012657 if (pt->pt_name == NULL)
12658 rettv->vval.v_string = NULL;
12659 else
12660 rettv->vval.v_string = vim_strsave(pt->pt_name);
12661 }
12662 else if (STRCMP(what, "dict") == 0)
12663 {
12664 rettv->v_type = VAR_DICT;
12665 rettv->vval.v_dict = pt->pt_dict;
12666 if (pt->pt_dict != NULL)
12667 ++pt->pt_dict->dv_refcount;
12668 }
12669 else if (STRCMP(what, "args") == 0)
12670 {
12671 rettv->v_type = VAR_LIST;
12672 if (rettv_list_alloc(rettv) == OK)
12673 {
12674 int i;
12675
12676 for (i = 0; i < pt->pt_argc; ++i)
12677 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12678 }
12679 }
12680 else
12681 EMSG2(_(e_invarg2), what);
12682 return;
12683 }
12684 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012685 else
12686 EMSG2(_(e_listdictarg), "get()");
12687
12688 if (tv == NULL)
12689 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012690 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012691 copy_tv(&argvars[2], rettv);
12692 }
12693 else
12694 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012695}
12696
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012697static 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 +000012698
12699/*
12700 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012701 * Return a range (from start to end) of lines in rettv from the specified
12702 * buffer.
12703 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012704 */
12705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012706get_buffer_lines(
12707 buf_T *buf,
12708 linenr_T start,
12709 linenr_T end,
12710 int retlist,
12711 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012712{
12713 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012714
Bram Moolenaar959a1432013-12-14 12:17:38 +010012715 rettv->v_type = VAR_STRING;
12716 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012717 if (retlist && rettv_list_alloc(rettv) == FAIL)
12718 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012719
12720 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12721 return;
12722
12723 if (!retlist)
12724 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012725 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12726 p = ml_get_buf(buf, start, FALSE);
12727 else
12728 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012729 rettv->vval.v_string = vim_strsave(p);
12730 }
12731 else
12732 {
12733 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012734 return;
12735
12736 if (start < 1)
12737 start = 1;
12738 if (end > buf->b_ml.ml_line_count)
12739 end = buf->b_ml.ml_line_count;
12740 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012741 if (list_append_string(rettv->vval.v_list,
12742 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012743 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012744 }
12745}
12746
12747/*
12748 * "getbufline()" function
12749 */
12750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012751f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012752{
12753 linenr_T lnum;
12754 linenr_T end;
12755 buf_T *buf;
12756
12757 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12758 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012759 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012760 --emsg_off;
12761
Bram Moolenaar661b1822005-07-28 22:36:45 +000012762 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012763 if (argvars[2].v_type == VAR_UNKNOWN)
12764 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012765 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012766 end = get_tv_lnum_buf(&argvars[2], buf);
12767
Bram Moolenaar342337a2005-07-21 21:11:17 +000012768 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012769}
12770
Bram Moolenaar0d660222005-01-07 21:51:51 +000012771/*
12772 * "getbufvar()" function
12773 */
12774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012775f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012776{
12777 buf_T *buf;
12778 buf_T *save_curbuf;
12779 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012780 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012781 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12784 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012785 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012786 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012787
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012788 rettv->v_type = VAR_STRING;
12789 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012790
12791 if (buf != NULL && varname != NULL)
12792 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012793 /* set curbuf to be our buf, temporarily */
12794 save_curbuf = curbuf;
12795 curbuf = buf;
12796
Bram Moolenaar0d660222005-01-07 21:51:51 +000012797 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012798 {
12799 if (get_option_tv(&varname, rettv, TRUE) == OK)
12800 done = TRUE;
12801 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012802 else if (STRCMP(varname, "changedtick") == 0)
12803 {
12804 rettv->v_type = VAR_NUMBER;
12805 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012806 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012807 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012808 else
12809 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012810 /* Look up the variable. */
12811 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12812 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12813 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012814 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012815 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012816 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012817 done = TRUE;
12818 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012819 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012820
12821 /* restore previous notion of curbuf */
12822 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012823 }
12824
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012825 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12826 /* use the default value */
12827 copy_tv(&argvars[2], rettv);
12828
Bram Moolenaar0d660222005-01-07 21:51:51 +000012829 --emsg_off;
12830}
12831
12832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 * "getchar()" function
12834 */
12835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012836f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
12838 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012841 /* Position the cursor. Needed after a message that ends in a space. */
12842 windgoto(msg_row, msg_col);
12843
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 ++no_mapping;
12845 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012846 for (;;)
12847 {
12848 if (argvars[0].v_type == VAR_UNKNOWN)
12849 /* getchar(): blocking wait. */
12850 n = safe_vgetc();
12851 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12852 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012853 n = vpeekc_any();
12854 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012855 /* illegal argument or getchar(0) and no char avail: return zero */
12856 n = 0;
12857 else
12858 /* getchar(0) and char avail: return char */
12859 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012860
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012861 if (n == K_IGNORE)
12862 continue;
12863 break;
12864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 --no_mapping;
12866 --allow_keys;
12867
Bram Moolenaar219b8702006-11-01 14:32:36 +000012868 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012869 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012870 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12871 vimvars[VV_MOUSE_COL].vv_nr = 0;
12872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 if (IS_SPECIAL(n) || mod_mask != 0)
12875 {
12876 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12877 int i = 0;
12878
12879 /* Turn a special key into three bytes, plus modifier. */
12880 if (mod_mask != 0)
12881 {
12882 temp[i++] = K_SPECIAL;
12883 temp[i++] = KS_MODIFIER;
12884 temp[i++] = mod_mask;
12885 }
12886 if (IS_SPECIAL(n))
12887 {
12888 temp[i++] = K_SPECIAL;
12889 temp[i++] = K_SECOND(n);
12890 temp[i++] = K_THIRD(n);
12891 }
12892#ifdef FEAT_MBYTE
12893 else if (has_mbyte)
12894 i += (*mb_char2bytes)(n, temp + i);
12895#endif
12896 else
12897 temp[i++] = n;
12898 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->v_type = VAR_STRING;
12900 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012901
12902#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012903 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012904 {
12905 int row = mouse_row;
12906 int col = mouse_col;
12907 win_T *win;
12908 linenr_T lnum;
12909# ifdef FEAT_WINDOWS
12910 win_T *wp;
12911# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012912 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012913
12914 if (row >= 0 && col >= 0)
12915 {
12916 /* Find the window at the mouse coordinates and compute the
12917 * text position. */
12918 win = mouse_find_win(&row, &col);
12919 (void)mouse_comp_pos(win, &row, &col, &lnum);
12920# ifdef FEAT_WINDOWS
12921 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012922 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012923# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012924 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012925 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012926 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12927 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12928 }
12929 }
12930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 }
12932}
12933
12934/*
12935 * "getcharmod()" function
12936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012938f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941}
12942
12943/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012944 * "getcharsearch()" function
12945 */
12946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012947f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012948{
12949 if (rettv_dict_alloc(rettv) != FAIL)
12950 {
12951 dict_T *dict = rettv->vval.v_dict;
12952
12953 dict_add_nr_str(dict, "char", 0L, last_csearch());
12954 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12955 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12956 }
12957}
12958
12959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 * "getcmdline()" function
12961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012963f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 rettv->v_type = VAR_STRING;
12966 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967}
12968
12969/*
12970 * "getcmdpos()" function
12971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012973f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976}
12977
12978/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012979 * "getcmdtype()" function
12980 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012982f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012983{
12984 rettv->v_type = VAR_STRING;
12985 rettv->vval.v_string = alloc(2);
12986 if (rettv->vval.v_string != NULL)
12987 {
12988 rettv->vval.v_string[0] = get_cmdline_type();
12989 rettv->vval.v_string[1] = NUL;
12990 }
12991}
12992
12993/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012994 * "getcmdwintype()" function
12995 */
12996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012997f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012998{
12999 rettv->v_type = VAR_STRING;
13000 rettv->vval.v_string = NULL;
13001#ifdef FEAT_CMDWIN
13002 rettv->vval.v_string = alloc(2);
13003 if (rettv->vval.v_string != NULL)
13004 {
13005 rettv->vval.v_string[0] = cmdwin_type;
13006 rettv->vval.v_string[1] = NUL;
13007 }
13008#endif
13009}
13010
13011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 * "getcwd()" function
13013 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013015f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013017 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013018 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013021 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013022
13023 wp = find_tabwin(&argvars[0], &argvars[1]);
13024 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013026 if (wp->w_localdir != NULL)
13027 rettv->vval.v_string = vim_strsave(wp->w_localdir);
13028 else if(globaldir != NULL)
13029 rettv->vval.v_string = vim_strsave(globaldir);
13030 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013031 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013032 cwd = alloc(MAXPATHL);
13033 if (cwd != NULL)
13034 {
13035 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13036 rettv->vval.v_string = vim_strsave(cwd);
13037 vim_free(cwd);
13038 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013039 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013040#ifdef BACKSLASH_IN_FILENAME
13041 if (rettv->vval.v_string != NULL)
13042 slash_adjust(rettv->vval.v_string);
13043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 }
13045}
13046
13047/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013048 * "getfontname()" function
13049 */
13050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013051f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 rettv->v_type = VAR_STRING;
13054 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013055#ifdef FEAT_GUI
13056 if (gui.in_use)
13057 {
13058 GuiFont font;
13059 char_u *name = NULL;
13060
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013061 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013062 {
13063 /* Get the "Normal" font. Either the name saved by
13064 * hl_set_font_name() or from the font ID. */
13065 font = gui.norm_font;
13066 name = hl_get_font_name();
13067 }
13068 else
13069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013070 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013071 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13072 return;
13073 font = gui_mch_get_font(name, FALSE);
13074 if (font == NOFONT)
13075 return; /* Invalid font name, return empty string. */
13076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013078 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013079 gui_mch_free_font(font);
13080 }
13081#endif
13082}
13083
13084/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013085 * "getfperm({fname})" function
13086 */
13087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013088f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013089{
13090 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013091 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013092 char_u *perm = NULL;
13093 char_u flags[] = "rwx";
13094 int i;
13095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013098 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013099 if (mch_stat((char *)fname, &st) >= 0)
13100 {
13101 perm = vim_strsave((char_u *)"---------");
13102 if (perm != NULL)
13103 {
13104 for (i = 0; i < 9; i++)
13105 {
13106 if (st.st_mode & (1 << (8 - i)))
13107 perm[i] = flags[i % 3];
13108 }
13109 }
13110 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013112}
13113
13114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 * "getfsize({fname})" function
13116 */
13117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013118f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119{
13120 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013121 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126
13127 if (mch_stat((char *)fname, &st) >= 0)
13128 {
13129 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013130 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013134
13135 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013136 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013137 rettv->vval.v_number = -2;
13138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 }
13140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142}
13143
13144/*
13145 * "getftime({fname})" function
13146 */
13147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013148f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149{
13150 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013151 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154
13155 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013162 * "getftype({fname})" function
13163 */
13164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013165f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013166{
13167 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013168 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013169 char_u *type = NULL;
13170 char *t;
13171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013175 if (mch_lstat((char *)fname, &st) >= 0)
13176 {
13177#ifdef S_ISREG
13178 if (S_ISREG(st.st_mode))
13179 t = "file";
13180 else if (S_ISDIR(st.st_mode))
13181 t = "dir";
13182# ifdef S_ISLNK
13183 else if (S_ISLNK(st.st_mode))
13184 t = "link";
13185# endif
13186# ifdef S_ISBLK
13187 else if (S_ISBLK(st.st_mode))
13188 t = "bdev";
13189# endif
13190# ifdef S_ISCHR
13191 else if (S_ISCHR(st.st_mode))
13192 t = "cdev";
13193# endif
13194# ifdef S_ISFIFO
13195 else if (S_ISFIFO(st.st_mode))
13196 t = "fifo";
13197# endif
13198# ifdef S_ISSOCK
13199 else if (S_ISSOCK(st.st_mode))
13200 t = "fifo";
13201# endif
13202 else
13203 t = "other";
13204#else
13205# ifdef S_IFMT
13206 switch (st.st_mode & S_IFMT)
13207 {
13208 case S_IFREG: t = "file"; break;
13209 case S_IFDIR: t = "dir"; break;
13210# ifdef S_IFLNK
13211 case S_IFLNK: t = "link"; break;
13212# endif
13213# ifdef S_IFBLK
13214 case S_IFBLK: t = "bdev"; break;
13215# endif
13216# ifdef S_IFCHR
13217 case S_IFCHR: t = "cdev"; break;
13218# endif
13219# ifdef S_IFIFO
13220 case S_IFIFO: t = "fifo"; break;
13221# endif
13222# ifdef S_IFSOCK
13223 case S_IFSOCK: t = "socket"; break;
13224# endif
13225 default: t = "other";
13226 }
13227# else
13228 if (mch_isdir(fname))
13229 t = "dir";
13230 else
13231 t = "file";
13232# endif
13233#endif
13234 type = vim_strsave((char_u *)t);
13235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013237}
13238
13239/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013240 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013241 */
13242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013243f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013244{
13245 linenr_T lnum;
13246 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013247 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013248
13249 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013250 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013251 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013252 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013253 retlist = FALSE;
13254 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013256 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013257 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013258 retlist = TRUE;
13259 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013260
Bram Moolenaar342337a2005-07-21 21:11:17 +000013261 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013262}
13263
13264/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013265 * "getmatches()" function
13266 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013268f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013269{
13270#ifdef FEAT_SEARCH_EXTRA
13271 dict_T *dict;
13272 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013273 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013274
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013275 if (rettv_list_alloc(rettv) == OK)
13276 {
13277 while (cur != NULL)
13278 {
13279 dict = dict_alloc();
13280 if (dict == NULL)
13281 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013282 if (cur->match.regprog == NULL)
13283 {
13284 /* match added with matchaddpos() */
13285 for (i = 0; i < MAXPOSMATCH; ++i)
13286 {
13287 llpos_T *llpos;
13288 char buf[6];
13289 list_T *l;
13290
13291 llpos = &cur->pos.pos[i];
13292 if (llpos->lnum == 0)
13293 break;
13294 l = list_alloc();
13295 if (l == NULL)
13296 break;
13297 list_append_number(l, (varnumber_T)llpos->lnum);
13298 if (llpos->col > 0)
13299 {
13300 list_append_number(l, (varnumber_T)llpos->col);
13301 list_append_number(l, (varnumber_T)llpos->len);
13302 }
13303 sprintf(buf, "pos%d", i + 1);
13304 dict_add_list(dict, buf, l);
13305 }
13306 }
13307 else
13308 {
13309 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13310 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013311 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013312 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13313 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013314# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013315 if (cur->conceal_char)
13316 {
13317 char_u buf[MB_MAXBYTES + 1];
13318
13319 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13320 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13321 }
13322# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013323 list_append_dict(rettv->vval.v_list, dict);
13324 cur = cur->next;
13325 }
13326 }
13327#endif
13328}
13329
13330/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013331 * "getpid()" function
13332 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013334f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013335{
13336 rettv->vval.v_number = mch_get_pid();
13337}
13338
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013340getpos_both(
13341 typval_T *argvars,
13342 typval_T *rettv,
13343 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013344{
Bram Moolenaara5525202006-03-02 22:52:09 +000013345 pos_T *fp;
13346 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013347 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013348
13349 if (rettv_list_alloc(rettv) == OK)
13350 {
13351 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013352 if (getcurpos)
13353 fp = &curwin->w_cursor;
13354 else
13355 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013356 if (fnum != -1)
13357 list_append_number(l, (varnumber_T)fnum);
13358 else
13359 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013360 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13361 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013362 list_append_number(l, (fp != NULL)
13363 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013364 : (varnumber_T)0);
13365 list_append_number(l,
13366#ifdef FEAT_VIRTUALEDIT
13367 (fp != NULL) ? (varnumber_T)fp->coladd :
13368#endif
13369 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013370 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013371 {
13372 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013373 list_append_number(l, curwin->w_curswant == MAXCOL ?
13374 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013375 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013376 }
13377 else
13378 rettv->vval.v_number = FALSE;
13379}
13380
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013381
13382/*
13383 * "getcurpos()" function
13384 */
13385 static void
13386f_getcurpos(typval_T *argvars, typval_T *rettv)
13387{
13388 getpos_both(argvars, rettv, TRUE);
13389}
13390
13391/*
13392 * "getpos(string)" function
13393 */
13394 static void
13395f_getpos(typval_T *argvars, typval_T *rettv)
13396{
13397 getpos_both(argvars, rettv, FALSE);
13398}
13399
Bram Moolenaara5525202006-03-02 22:52:09 +000013400/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013401 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013402 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013404f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013405{
13406#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013407 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013408#endif
13409
Bram Moolenaar2641f772005-03-25 21:58:17 +000013410#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013411 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013412 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013413 wp = NULL;
13414 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13415 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013416 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013417 if (wp == NULL)
13418 return;
13419 }
13420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013421 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013422 }
13423#endif
13424}
13425
13426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 * "getreg()" function
13428 */
13429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013430f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431{
13432 char_u *strregname;
13433 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013434 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013435 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013436 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013438 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 strregname = get_tv_string_chk(&argvars[0]);
13441 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013442 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013443 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013444 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013445 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013446 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013447 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013450 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013451
13452 if (error)
13453 return;
13454
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 regname = (strregname == NULL ? '"' : *strregname);
13456 if (regname == 0)
13457 regname = '"';
13458
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013459 if (return_list)
13460 {
13461 rettv->v_type = VAR_LIST;
13462 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13463 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013464 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013465 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013466 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013467 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013468 }
13469 else
13470 {
13471 rettv->v_type = VAR_STRING;
13472 rettv->vval.v_string = get_reg_contents(regname,
13473 arg2 ? GREG_EXPR_SRC : 0);
13474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475}
13476
13477/*
13478 * "getregtype()" function
13479 */
13480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013481f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482{
13483 char_u *strregname;
13484 int regname;
13485 char_u buf[NUMBUFLEN + 2];
13486 long reglen = 0;
13487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013488 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 {
13490 strregname = get_tv_string_chk(&argvars[0]);
13491 if (strregname == NULL) /* type error; errmsg already given */
13492 {
13493 rettv->v_type = VAR_STRING;
13494 rettv->vval.v_string = NULL;
13495 return;
13496 }
13497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 else
13499 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013500 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501
13502 regname = (strregname == NULL ? '"' : *strregname);
13503 if (regname == 0)
13504 regname = '"';
13505
13506 buf[0] = NUL;
13507 buf[1] = NUL;
13508 switch (get_reg_type(regname, &reglen))
13509 {
13510 case MLINE: buf[0] = 'V'; break;
13511 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 case MBLOCK:
13513 buf[0] = Ctrl_V;
13514 sprintf((char *)buf + 1, "%ld", reglen + 1);
13515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013517 rettv->v_type = VAR_STRING;
13518 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519}
13520
13521/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013522 * "gettabvar()" function
13523 */
13524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013525f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013526{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013527 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013528 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013529 dictitem_T *v;
13530 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013531 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013532
13533 rettv->v_type = VAR_STRING;
13534 rettv->vval.v_string = NULL;
13535
13536 varname = get_tv_string_chk(&argvars[1]);
13537 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13538 if (tp != NULL && varname != NULL)
13539 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013540 /* Set tp to be our tabpage, temporarily. Also set the window to the
13541 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013542 if (switch_win(&oldcurwin, &oldtabpage,
13543 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013544 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013545 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013546 /* look up the variable */
13547 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13548 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13549 if (v != NULL)
13550 {
13551 copy_tv(&v->di_tv, rettv);
13552 done = TRUE;
13553 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013554 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013555
13556 /* restore previous notion of curwin */
13557 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013558 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013559
13560 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013561 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013562}
13563
13564/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013565 * "gettabwinvar()" function
13566 */
13567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013568f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013569{
13570 getwinvar(argvars, rettv, 1);
13571}
13572
13573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 * "getwinposx()" function
13575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013577f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013579 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580#ifdef FEAT_GUI
13581 if (gui.in_use)
13582 {
13583 int x, y;
13584
13585 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013586 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 }
13588#endif
13589}
13590
13591/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013592 * "win_findbuf()" function
13593 */
13594 static void
13595f_win_findbuf(typval_T *argvars, typval_T *rettv)
13596{
13597 if (rettv_list_alloc(rettv) != FAIL)
13598 win_findbuf(argvars, rettv->vval.v_list);
13599}
13600
13601/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013602 * "win_getid()" function
13603 */
13604 static void
13605f_win_getid(typval_T *argvars, typval_T *rettv)
13606{
13607 rettv->vval.v_number = win_getid(argvars);
13608}
13609
13610/*
13611 * "win_gotoid()" function
13612 */
13613 static void
13614f_win_gotoid(typval_T *argvars, typval_T *rettv)
13615{
13616 rettv->vval.v_number = win_gotoid(argvars);
13617}
13618
13619/*
13620 * "win_id2tabwin()" function
13621 */
13622 static void
13623f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13624{
13625 if (rettv_list_alloc(rettv) != FAIL)
13626 win_id2tabwin(argvars, rettv->vval.v_list);
13627}
13628
13629/*
13630 * "win_id2win()" function
13631 */
13632 static void
13633f_win_id2win(typval_T *argvars, typval_T *rettv)
13634{
13635 rettv->vval.v_number = win_id2win(argvars);
13636}
13637
13638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 * "getwinposy()" function
13640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013642f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645#ifdef FEAT_GUI
13646 if (gui.in_use)
13647 {
13648 int x, y;
13649
13650 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013651 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 }
13653#endif
13654}
13655
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013656/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013657 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013658 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013659 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013660find_win_by_nr(
13661 typval_T *vp,
13662 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013663{
13664#ifdef FEAT_WINDOWS
13665 win_T *wp;
13666#endif
13667 int nr;
13668
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013669 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013670
13671#ifdef FEAT_WINDOWS
13672 if (nr < 0)
13673 return NULL;
13674 if (nr == 0)
13675 return curwin;
13676
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013677 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13678 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013679 if (nr >= LOWEST_WIN_ID)
13680 {
13681 if (wp->w_id == nr)
13682 return wp;
13683 }
13684 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013685 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013686 if (nr >= LOWEST_WIN_ID)
13687 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013688 return wp;
13689#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013690 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013691 return curwin;
13692 return NULL;
13693#endif
13694}
13695
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013697 * Find window specified by "wvp" in tabpage "tvp".
13698 */
13699 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013700find_tabwin(
13701 typval_T *wvp, /* VAR_UNKNOWN for current window */
13702 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013703{
13704 win_T *wp = NULL;
13705 tabpage_T *tp = NULL;
13706 long n;
13707
13708 if (wvp->v_type != VAR_UNKNOWN)
13709 {
13710 if (tvp->v_type != VAR_UNKNOWN)
13711 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013712 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013713 if (n >= 0)
13714 tp = find_tabpage(n);
13715 }
13716 else
13717 tp = curtab;
13718
13719 if (tp != NULL)
13720 wp = find_win_by_nr(wvp, tp);
13721 }
13722 else
13723 wp = curwin;
13724
13725 return wp;
13726}
13727
13728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729 * "getwinvar()" function
13730 */
13731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013732f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013734 getwinvar(argvars, rettv, 0);
13735}
13736
13737/*
13738 * getwinvar() and gettabwinvar()
13739 */
13740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013741getwinvar(
13742 typval_T *argvars,
13743 typval_T *rettv,
13744 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013745{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013746 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013748 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013749 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013750 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013751#ifdef FEAT_WINDOWS
13752 win_T *oldcurwin;
13753 tabpage_T *oldtabpage;
13754 int need_switch_win;
13755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013757#ifdef FEAT_WINDOWS
13758 if (off == 1)
13759 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13760 else
13761 tp = curtab;
13762#endif
13763 win = find_win_by_nr(&argvars[off], tp);
13764 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013767 rettv->v_type = VAR_STRING;
13768 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769
13770 if (win != NULL && varname != NULL)
13771 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013772#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013773 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013774 * otherwise the window is not valid. Only do this when needed,
13775 * autocommands get blocked. */
13776 need_switch_win = !(tp == curtab && win == curwin);
13777 if (!need_switch_win
13778 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13779#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013780 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013781 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013782 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013783 if (get_option_tv(&varname, rettv, 1) == OK)
13784 done = TRUE;
13785 }
13786 else
13787 {
13788 /* Look up the variable. */
13789 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13790 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13791 varname, FALSE);
13792 if (v != NULL)
13793 {
13794 copy_tv(&v->di_tv, rettv);
13795 done = TRUE;
13796 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013799
Bram Moolenaarba117c22015-09-29 16:53:22 +020013800#ifdef FEAT_WINDOWS
13801 if (need_switch_win)
13802 /* restore previous notion of curwin */
13803 restore_win(oldcurwin, oldtabpage, TRUE);
13804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 }
13806
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013807 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13808 /* use the default return value */
13809 copy_tv(&argvars[off + 2], rettv);
13810
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 --emsg_off;
13812}
13813
13814/*
13815 * "glob()" function
13816 */
13817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013818f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013820 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013822 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013824 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013825 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013827 if (argvars[1].v_type != VAR_UNKNOWN)
13828 {
13829 if (get_tv_number_chk(&argvars[1], &error))
13830 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013831 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013832 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013833 if (get_tv_number_chk(&argvars[2], &error))
13834 {
13835 rettv->v_type = VAR_LIST;
13836 rettv->vval.v_list = NULL;
13837 }
13838 if (argvars[3].v_type != VAR_UNKNOWN
13839 && get_tv_number_chk(&argvars[3], &error))
13840 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013841 }
13842 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013843 if (!error)
13844 {
13845 ExpandInit(&xpc);
13846 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013847 if (p_wic)
13848 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013849 if (rettv->v_type == VAR_STRING)
13850 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013851 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013852 else if (rettv_list_alloc(rettv) != FAIL)
13853 {
13854 int i;
13855
13856 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13857 NULL, options, WILD_ALL_KEEP);
13858 for (i = 0; i < xpc.xp_numfiles; i++)
13859 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13860
13861 ExpandCleanup(&xpc);
13862 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013863 }
13864 else
13865 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866}
13867
13868/*
13869 * "globpath()" function
13870 */
13871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013872f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013874 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013876 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013877 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013878 garray_T ga;
13879 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013881 /* When the optional second argument is non-zero, don't remove matches
13882 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013883 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013884 if (argvars[2].v_type != VAR_UNKNOWN)
13885 {
13886 if (get_tv_number_chk(&argvars[2], &error))
13887 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013888 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013889 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013890 if (get_tv_number_chk(&argvars[3], &error))
13891 {
13892 rettv->v_type = VAR_LIST;
13893 rettv->vval.v_list = NULL;
13894 }
13895 if (argvars[4].v_type != VAR_UNKNOWN
13896 && get_tv_number_chk(&argvars[4], &error))
13897 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013898 }
13899 }
13900 if (file != NULL && !error)
13901 {
13902 ga_init2(&ga, (int)sizeof(char_u *), 10);
13903 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13904 if (rettv->v_type == VAR_STRING)
13905 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13906 else if (rettv_list_alloc(rettv) != FAIL)
13907 for (i = 0; i < ga.ga_len; ++i)
13908 list_append_string(rettv->vval.v_list,
13909 ((char_u **)(ga.ga_data))[i], -1);
13910 ga_clear_strings(&ga);
13911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914}
13915
13916/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013917 * "glob2regpat()" function
13918 */
13919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013920f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013921{
13922 char_u *pat = get_tv_string_chk(&argvars[0]);
13923
13924 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013925 rettv->vval.v_string = (pat == NULL)
13926 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013927}
13928
13929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 * "has()" function
13931 */
13932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013933f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
13935 int i;
13936 char_u *name;
13937 int n = FALSE;
13938 static char *(has_list[]) =
13939 {
13940#ifdef AMIGA
13941 "amiga",
13942# ifdef FEAT_ARP
13943 "arp",
13944# endif
13945#endif
13946#ifdef __BEOS__
13947 "beos",
13948#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013949#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 "mac",
13951#endif
13952#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013953 "macunix", /* built with 'darwin' enabled */
13954#endif
13955#if defined(__APPLE__) && __APPLE__ == 1
13956 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958#ifdef __QNX__
13959 "qnx",
13960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961#ifdef UNIX
13962 "unix",
13963#endif
13964#ifdef VMS
13965 "vms",
13966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef WIN32
13968 "win32",
13969#endif
13970#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13971 "win32unix",
13972#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013973#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 "win64",
13975#endif
13976#ifdef EBCDIC
13977 "ebcdic",
13978#endif
13979#ifndef CASE_INSENSITIVE_FILENAME
13980 "fname_case",
13981#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013982#ifdef HAVE_ACL
13983 "acl",
13984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985#ifdef FEAT_ARABIC
13986 "arabic",
13987#endif
13988#ifdef FEAT_AUTOCMD
13989 "autocmd",
13990#endif
13991#ifdef FEAT_BEVAL
13992 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013993# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13994 "balloon_multiline",
13995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996#endif
13997#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13998 "builtin_terms",
13999# ifdef ALL_BUILTIN_TCAPS
14000 "all_builtin_terms",
14001# endif
14002#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014003#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14004 || defined(FEAT_GUI_W32) \
14005 || defined(FEAT_GUI_MOTIF))
14006 "browsefilter",
14007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008#ifdef FEAT_BYTEOFF
14009 "byte_offset",
14010#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014011#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014012 "channel",
14013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014#ifdef FEAT_CINDENT
14015 "cindent",
14016#endif
14017#ifdef FEAT_CLIENTSERVER
14018 "clientserver",
14019#endif
14020#ifdef FEAT_CLIPBOARD
14021 "clipboard",
14022#endif
14023#ifdef FEAT_CMDL_COMPL
14024 "cmdline_compl",
14025#endif
14026#ifdef FEAT_CMDHIST
14027 "cmdline_hist",
14028#endif
14029#ifdef FEAT_COMMENTS
14030 "comments",
14031#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014032#ifdef FEAT_CONCEAL
14033 "conceal",
14034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035#ifdef FEAT_CRYPT
14036 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014037 "crypt-blowfish",
14038 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039#endif
14040#ifdef FEAT_CSCOPE
14041 "cscope",
14042#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014043#ifdef FEAT_CURSORBIND
14044 "cursorbind",
14045#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014046#ifdef CURSOR_SHAPE
14047 "cursorshape",
14048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049#ifdef DEBUG
14050 "debug",
14051#endif
14052#ifdef FEAT_CON_DIALOG
14053 "dialog_con",
14054#endif
14055#ifdef FEAT_GUI_DIALOG
14056 "dialog_gui",
14057#endif
14058#ifdef FEAT_DIFF
14059 "diff",
14060#endif
14061#ifdef FEAT_DIGRAPHS
14062 "digraphs",
14063#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014064#ifdef FEAT_DIRECTX
14065 "directx",
14066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067#ifdef FEAT_DND
14068 "dnd",
14069#endif
14070#ifdef FEAT_EMACS_TAGS
14071 "emacs_tags",
14072#endif
14073 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014074 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075#ifdef FEAT_SEARCH_EXTRA
14076 "extra_search",
14077#endif
14078#ifdef FEAT_FKMAP
14079 "farsi",
14080#endif
14081#ifdef FEAT_SEARCHPATH
14082 "file_in_path",
14083#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014084#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014085 "filterpipe",
14086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087#ifdef FEAT_FIND_ID
14088 "find_in_path",
14089#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014090#ifdef FEAT_FLOAT
14091 "float",
14092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093#ifdef FEAT_FOLDING
14094 "folding",
14095#endif
14096#ifdef FEAT_FOOTER
14097 "footer",
14098#endif
14099#if !defined(USE_SYSTEM) && defined(UNIX)
14100 "fork",
14101#endif
14102#ifdef FEAT_GETTEXT
14103 "gettext",
14104#endif
14105#ifdef FEAT_GUI
14106 "gui",
14107#endif
14108#ifdef FEAT_GUI_ATHENA
14109# ifdef FEAT_GUI_NEXTAW
14110 "gui_neXtaw",
14111# else
14112 "gui_athena",
14113# endif
14114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115#ifdef FEAT_GUI_GTK
14116 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014117# ifdef USE_GTK3
14118 "gui_gtk3",
14119# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014123#ifdef FEAT_GUI_GNOME
14124 "gui_gnome",
14125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126#ifdef FEAT_GUI_MAC
14127 "gui_mac",
14128#endif
14129#ifdef FEAT_GUI_MOTIF
14130 "gui_motif",
14131#endif
14132#ifdef FEAT_GUI_PHOTON
14133 "gui_photon",
14134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135#ifdef FEAT_GUI_W32
14136 "gui_win32",
14137#endif
14138#ifdef FEAT_HANGULIN
14139 "hangul_input",
14140#endif
14141#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14142 "iconv",
14143#endif
14144#ifdef FEAT_INS_EXPAND
14145 "insert_expand",
14146#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014148 "job",
14149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150#ifdef FEAT_JUMPLIST
14151 "jumplist",
14152#endif
14153#ifdef FEAT_KEYMAP
14154 "keymap",
14155#endif
14156#ifdef FEAT_LANGMAP
14157 "langmap",
14158#endif
14159#ifdef FEAT_LIBCALL
14160 "libcall",
14161#endif
14162#ifdef FEAT_LINEBREAK
14163 "linebreak",
14164#endif
14165#ifdef FEAT_LISP
14166 "lispindent",
14167#endif
14168#ifdef FEAT_LISTCMDS
14169 "listcmds",
14170#endif
14171#ifdef FEAT_LOCALMAP
14172 "localmap",
14173#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014174#ifdef FEAT_LUA
14175# ifndef DYNAMIC_LUA
14176 "lua",
14177# endif
14178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179#ifdef FEAT_MENU
14180 "menu",
14181#endif
14182#ifdef FEAT_SESSION
14183 "mksession",
14184#endif
14185#ifdef FEAT_MODIFY_FNAME
14186 "modify_fname",
14187#endif
14188#ifdef FEAT_MOUSE
14189 "mouse",
14190#endif
14191#ifdef FEAT_MOUSESHAPE
14192 "mouseshape",
14193#endif
14194#if defined(UNIX) || defined(VMS)
14195# ifdef FEAT_MOUSE_DEC
14196 "mouse_dec",
14197# endif
14198# ifdef FEAT_MOUSE_GPM
14199 "mouse_gpm",
14200# endif
14201# ifdef FEAT_MOUSE_JSB
14202 "mouse_jsbterm",
14203# endif
14204# ifdef FEAT_MOUSE_NET
14205 "mouse_netterm",
14206# endif
14207# ifdef FEAT_MOUSE_PTERM
14208 "mouse_pterm",
14209# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014210# ifdef FEAT_MOUSE_SGR
14211 "mouse_sgr",
14212# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014213# ifdef FEAT_SYSMOUSE
14214 "mouse_sysmouse",
14215# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014216# ifdef FEAT_MOUSE_URXVT
14217 "mouse_urxvt",
14218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219# ifdef FEAT_MOUSE_XTERM
14220 "mouse_xterm",
14221# endif
14222#endif
14223#ifdef FEAT_MBYTE
14224 "multi_byte",
14225#endif
14226#ifdef FEAT_MBYTE_IME
14227 "multi_byte_ime",
14228#endif
14229#ifdef FEAT_MULTI_LANG
14230 "multi_lang",
14231#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014232#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014233#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014234 "mzscheme",
14235#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014236#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014237#ifdef FEAT_NUM64
14238 "num64",
14239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240#ifdef FEAT_OLE
14241 "ole",
14242#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014243 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244#ifdef FEAT_PATH_EXTRA
14245 "path_extra",
14246#endif
14247#ifdef FEAT_PERL
14248#ifndef DYNAMIC_PERL
14249 "perl",
14250#endif
14251#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014252#ifdef FEAT_PERSISTENT_UNDO
14253 "persistent_undo",
14254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#ifdef FEAT_PYTHON
14256#ifndef DYNAMIC_PYTHON
14257 "python",
14258#endif
14259#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014260#ifdef FEAT_PYTHON3
14261#ifndef DYNAMIC_PYTHON3
14262 "python3",
14263#endif
14264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265#ifdef FEAT_POSTSCRIPT
14266 "postscript",
14267#endif
14268#ifdef FEAT_PRINTER
14269 "printer",
14270#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014271#ifdef FEAT_PROFILE
14272 "profile",
14273#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014274#ifdef FEAT_RELTIME
14275 "reltime",
14276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277#ifdef FEAT_QUICKFIX
14278 "quickfix",
14279#endif
14280#ifdef FEAT_RIGHTLEFT
14281 "rightleft",
14282#endif
14283#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14284 "ruby",
14285#endif
14286#ifdef FEAT_SCROLLBIND
14287 "scrollbind",
14288#endif
14289#ifdef FEAT_CMDL_INFO
14290 "showcmd",
14291 "cmdline_info",
14292#endif
14293#ifdef FEAT_SIGNS
14294 "signs",
14295#endif
14296#ifdef FEAT_SMARTINDENT
14297 "smartindent",
14298#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014299#ifdef STARTUPTIME
14300 "startuptime",
14301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302#ifdef FEAT_STL_OPT
14303 "statusline",
14304#endif
14305#ifdef FEAT_SUN_WORKSHOP
14306 "sun_workshop",
14307#endif
14308#ifdef FEAT_NETBEANS_INTG
14309 "netbeans_intg",
14310#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014311#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014312 "spell",
14313#endif
14314#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 "syntax",
14316#endif
14317#if defined(USE_SYSTEM) || !defined(UNIX)
14318 "system",
14319#endif
14320#ifdef FEAT_TAG_BINS
14321 "tag_binary",
14322#endif
14323#ifdef FEAT_TAG_OLDSTATIC
14324 "tag_old_static",
14325#endif
14326#ifdef FEAT_TAG_ANYWHITE
14327 "tag_any_white",
14328#endif
14329#ifdef FEAT_TCL
14330# ifndef DYNAMIC_TCL
14331 "tcl",
14332# endif
14333#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014334#ifdef FEAT_TERMGUICOLORS
14335 "termguicolors",
14336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337#ifdef TERMINFO
14338 "terminfo",
14339#endif
14340#ifdef FEAT_TERMRESPONSE
14341 "termresponse",
14342#endif
14343#ifdef FEAT_TEXTOBJ
14344 "textobjects",
14345#endif
14346#ifdef HAVE_TGETENT
14347 "tgetent",
14348#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014349#ifdef FEAT_TIMERS
14350 "timers",
14351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352#ifdef FEAT_TITLE
14353 "title",
14354#endif
14355#ifdef FEAT_TOOLBAR
14356 "toolbar",
14357#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014358#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14359 "unnamedplus",
14360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361#ifdef FEAT_USR_CMDS
14362 "user-commands", /* was accidentally included in 5.4 */
14363 "user_commands",
14364#endif
14365#ifdef FEAT_VIMINFO
14366 "viminfo",
14367#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014368#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 "vertsplit",
14370#endif
14371#ifdef FEAT_VIRTUALEDIT
14372 "virtualedit",
14373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375#ifdef FEAT_VISUALEXTRA
14376 "visualextra",
14377#endif
14378#ifdef FEAT_VREPLACE
14379 "vreplace",
14380#endif
14381#ifdef FEAT_WILDIGN
14382 "wildignore",
14383#endif
14384#ifdef FEAT_WILDMENU
14385 "wildmenu",
14386#endif
14387#ifdef FEAT_WINDOWS
14388 "windows",
14389#endif
14390#ifdef FEAT_WAK
14391 "winaltkeys",
14392#endif
14393#ifdef FEAT_WRITEBACKUP
14394 "writebackup",
14395#endif
14396#ifdef FEAT_XIM
14397 "xim",
14398#endif
14399#ifdef FEAT_XFONTSET
14400 "xfontset",
14401#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014402#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014403 "xpm",
14404 "xpm_w32", /* for backward compatibility */
14405#else
14406# if defined(HAVE_XPM)
14407 "xpm",
14408# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410#ifdef USE_XSMP
14411 "xsmp",
14412#endif
14413#ifdef USE_XSMP_INTERACT
14414 "xsmp_interact",
14415#endif
14416#ifdef FEAT_XCLIPBOARD
14417 "xterm_clipboard",
14418#endif
14419#ifdef FEAT_XTERM_SAVE
14420 "xterm_save",
14421#endif
14422#if defined(UNIX) && defined(FEAT_X11)
14423 "X11",
14424#endif
14425 NULL
14426 };
14427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 for (i = 0; has_list[i] != NULL; ++i)
14430 if (STRICMP(name, has_list[i]) == 0)
14431 {
14432 n = TRUE;
14433 break;
14434 }
14435
14436 if (n == FALSE)
14437 {
14438 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014439 {
14440 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014441 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014442 && vim_isdigit(name[6])
14443 && vim_isdigit(name[8])
14444 && vim_isdigit(name[10]))
14445 {
14446 int major = atoi((char *)name + 6);
14447 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014448
14449 /* Expect "patch-9.9.01234". */
14450 n = (major < VIM_VERSION_MAJOR
14451 || (major == VIM_VERSION_MAJOR
14452 && (minor < VIM_VERSION_MINOR
14453 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014454 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014455 }
14456 else
14457 n = has_patch(atoi((char *)name + 5));
14458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 else if (STRICMP(name, "vim_starting") == 0)
14460 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014461#ifdef FEAT_MBYTE
14462 else if (STRICMP(name, "multi_byte_encoding") == 0)
14463 n = has_mbyte;
14464#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014465#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14466 else if (STRICMP(name, "balloon_multiline") == 0)
14467 n = multiline_balloon_available();
14468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469#ifdef DYNAMIC_TCL
14470 else if (STRICMP(name, "tcl") == 0)
14471 n = tcl_enabled(FALSE);
14472#endif
14473#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14474 else if (STRICMP(name, "iconv") == 0)
14475 n = iconv_enabled(FALSE);
14476#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014477#ifdef DYNAMIC_LUA
14478 else if (STRICMP(name, "lua") == 0)
14479 n = lua_enabled(FALSE);
14480#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014481#ifdef DYNAMIC_MZSCHEME
14482 else if (STRICMP(name, "mzscheme") == 0)
14483 n = mzscheme_enabled(FALSE);
14484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485#ifdef DYNAMIC_RUBY
14486 else if (STRICMP(name, "ruby") == 0)
14487 n = ruby_enabled(FALSE);
14488#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014489#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490#ifdef DYNAMIC_PYTHON
14491 else if (STRICMP(name, "python") == 0)
14492 n = python_enabled(FALSE);
14493#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014494#endif
14495#ifdef FEAT_PYTHON3
14496#ifdef DYNAMIC_PYTHON3
14497 else if (STRICMP(name, "python3") == 0)
14498 n = python3_enabled(FALSE);
14499#endif
14500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501#ifdef DYNAMIC_PERL
14502 else if (STRICMP(name, "perl") == 0)
14503 n = perl_enabled(FALSE);
14504#endif
14505#ifdef FEAT_GUI
14506 else if (STRICMP(name, "gui_running") == 0)
14507 n = (gui.in_use || gui.starting);
14508# ifdef FEAT_GUI_W32
14509 else if (STRICMP(name, "gui_win32s") == 0)
14510 n = gui_is_win32s();
14511# endif
14512# ifdef FEAT_BROWSE
14513 else if (STRICMP(name, "browse") == 0)
14514 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14515# endif
14516#endif
14517#ifdef FEAT_SYN_HL
14518 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014519 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520#endif
14521#if defined(WIN3264)
14522 else if (STRICMP(name, "win95") == 0)
14523 n = mch_windows95();
14524#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014525#ifdef FEAT_NETBEANS_INTG
14526 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014527 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529 }
14530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532}
14533
14534/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014535 * "has_key()" function
14536 */
14537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014538f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014539{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014540 if (argvars[0].v_type != VAR_DICT)
14541 {
14542 EMSG(_(e_dictreq));
14543 return;
14544 }
14545 if (argvars[0].vval.v_dict == NULL)
14546 return;
14547
14548 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014549 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014550}
14551
14552/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014553 * "haslocaldir()" function
14554 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014556f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014557{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014558 win_T *wp = NULL;
14559
14560 wp = find_tabwin(&argvars[0], &argvars[1]);
14561 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014562}
14563
14564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 * "hasmapto()" function
14566 */
14567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014568f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569{
14570 char_u *name;
14571 char_u *mode;
14572 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014573 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014575 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014576 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577 mode = (char_u *)"nvo";
14578 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014581 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014582 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584
Bram Moolenaar2c932302006-03-18 21:42:09 +000014585 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589}
14590
14591/*
14592 * "histadd()" function
14593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014595f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
14597#ifdef FEAT_CMDHIST
14598 int histype;
14599 char_u *str;
14600 char_u buf[NUMBUFLEN];
14601#endif
14602
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014603 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 if (check_restricted() || check_secure())
14605 return;
14606#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14608 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 if (histype >= 0)
14610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014611 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 if (*str != NUL)
14613 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014614 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 return;
14618 }
14619 }
14620#endif
14621}
14622
14623/*
14624 * "histdel()" function
14625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014627f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628{
14629#ifdef FEAT_CMDHIST
14630 int n;
14631 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014632 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014634 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14635 if (str == NULL)
14636 n = 0;
14637 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014640 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014643 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644 else
14645 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 get_tv_string_buf(&argvars[1], buf));
14648 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014649#endif
14650}
14651
14652/*
14653 * "histget()" function
14654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014656f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657{
14658#ifdef FEAT_CMDHIST
14659 int type;
14660 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014663 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14664 if (str == NULL)
14665 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 {
14668 type = get_histtype(str);
14669 if (argvars[1].v_type == VAR_UNKNOWN)
14670 idx = get_history_idx(type);
14671 else
14672 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14673 /* -1 on type error */
14674 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014677 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680}
14681
14682/*
14683 * "histnr()" function
14684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014686f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687{
14688 int i;
14689
14690#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014691 char_u *history = get_tv_string_chk(&argvars[0]);
14692
14693 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694 if (i >= HIST_CMD && i < HIST_COUNT)
14695 i = get_history_idx(i);
14696 else
14697#endif
14698 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014699 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700}
14701
14702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 * "highlightID(name)" function
14704 */
14705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014706f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014708 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709}
14710
14711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014712 * "highlight_exists()" function
14713 */
14714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014715f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716{
14717 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14718}
14719
14720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 * "hostname()" function
14722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014724f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725{
14726 char_u hostname[256];
14727
14728 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014729 rettv->v_type = VAR_STRING;
14730 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731}
14732
14733/*
14734 * iconv() function
14735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014737f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
14739#ifdef FEAT_MBYTE
14740 char_u buf1[NUMBUFLEN];
14741 char_u buf2[NUMBUFLEN];
14742 char_u *from, *to, *str;
14743 vimconv_T vimconv;
14744#endif
14745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 rettv->v_type = VAR_STRING;
14747 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748
14749#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014750 str = get_tv_string(&argvars[0]);
14751 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14752 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 vimconv.vc_type = CONV_NONE;
14754 convert_setup(&vimconv, from, to);
14755
14756 /* If the encodings are equal, no conversion needed. */
14757 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014758 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761
14762 convert_setup(&vimconv, NULL, NULL);
14763 vim_free(from);
14764 vim_free(to);
14765#endif
14766}
14767
14768/*
14769 * "indent()" function
14770 */
14771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014772f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773{
14774 linenr_T lnum;
14775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014776 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781}
14782
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014783/*
14784 * "index()" function
14785 */
14786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014787f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014788{
Bram Moolenaar33570922005-01-25 22:26:29 +000014789 list_T *l;
14790 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014791 long idx = 0;
14792 int ic = FALSE;
14793
14794 rettv->vval.v_number = -1;
14795 if (argvars[0].v_type != VAR_LIST)
14796 {
14797 EMSG(_(e_listreq));
14798 return;
14799 }
14800 l = argvars[0].vval.v_list;
14801 if (l != NULL)
14802 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014803 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806 int error = FALSE;
14807
Bram Moolenaar758711c2005-02-02 23:11:38 +000014808 /* Start at specified item. Use the cached index that list_find()
14809 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014810 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014811 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014812 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014813 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 if (error)
14815 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014816 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014817
Bram Moolenaar758711c2005-02-02 23:11:38 +000014818 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014819 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014820 {
14821 rettv->vval.v_number = idx;
14822 break;
14823 }
14824 }
14825}
14826
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827static int inputsecret_flag = 0;
14828
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014829static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014830
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014832 * This function is used by f_input() and f_inputdialog() functions. The third
14833 * argument to f_input() specifies the type of completion to use at the
14834 * prompt. The third argument to f_inputdialog() specifies the value to return
14835 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 */
14837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014838get_user_input(
14839 typval_T *argvars,
14840 typval_T *rettv,
14841 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 char_u *p = NULL;
14845 int c;
14846 char_u buf[NUMBUFLEN];
14847 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014848 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014849 int xp_type = EXPAND_NOTHING;
14850 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014853 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854
14855#ifdef NO_CONSOLE_INPUT
14856 /* While starting up, there is no place to enter text. */
14857 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859#endif
14860
14861 cmd_silent = FALSE; /* Want to see the prompt. */
14862 if (prompt != NULL)
14863 {
14864 /* Only the part of the message after the last NL is considered as
14865 * prompt for the command line */
14866 p = vim_strrchr(prompt, '\n');
14867 if (p == NULL)
14868 p = prompt;
14869 else
14870 {
14871 ++p;
14872 c = *p;
14873 *p = NUL;
14874 msg_start();
14875 msg_clr_eos();
14876 msg_puts_attr(prompt, echo_attr);
14877 msg_didout = FALSE;
14878 msg_starthere();
14879 *p = c;
14880 }
14881 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 if (argvars[1].v_type != VAR_UNKNOWN)
14884 {
14885 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14886 if (defstr != NULL)
14887 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014889 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014890 {
14891 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014892 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014893 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014894
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014895 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014896 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014897
Bram Moolenaar4463f292005-09-25 22:20:24 +000014898 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14899 if (xp_name == NULL)
14900 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014901
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014902 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014903
Bram Moolenaar4463f292005-09-25 22:20:24 +000014904 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14905 &xp_arg) == FAIL)
14906 return;
14907 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014908 }
14909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014911 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014912 int save_ex_normal_busy = ex_normal_busy;
14913 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014915 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14916 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014917 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014918 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014919 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014920 && argvars[1].v_type != VAR_UNKNOWN
14921 && argvars[2].v_type != VAR_UNKNOWN)
14922 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14923 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014924
14925 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014927 /* since the user typed this, no need to wait for return */
14928 need_wait_return = FALSE;
14929 msg_didout = FALSE;
14930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931 cmd_silent = cmd_silent_save;
14932}
14933
14934/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014935 * "input()" function
14936 * Also handles inputsecret() when inputsecret is set.
14937 */
14938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014939f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014940{
14941 get_user_input(argvars, rettv, FALSE);
14942}
14943
14944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 * "inputdialog()" function
14946 */
14947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014948f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949{
14950#if defined(FEAT_GUI_TEXTDIALOG)
14951 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14952 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14953 {
14954 char_u *message;
14955 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014956 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014958 message = get_tv_string_chk(&argvars[0]);
14959 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014960 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014961 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962 else
14963 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014964 if (message != NULL && defstr != NULL
14965 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014966 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014967 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 else
14969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014970 if (message != NULL && defstr != NULL
14971 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014972 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014973 rettv->vval.v_string = vim_strsave(
14974 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 }
14980 else
14981#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014982 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983}
14984
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014985/*
14986 * "inputlist()" function
14987 */
14988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014989f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014990{
14991 listitem_T *li;
14992 int selected;
14993 int mouse_used;
14994
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014995#ifdef NO_CONSOLE_INPUT
14996 /* While starting up, there is no place to enter text. */
14997 if (no_console_input())
14998 return;
14999#endif
15000 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15001 {
15002 EMSG2(_(e_listarg), "inputlist()");
15003 return;
15004 }
15005
15006 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015007 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015008 lines_left = Rows; /* avoid more prompt */
15009 msg_scroll = TRUE;
15010 msg_clr_eos();
15011
15012 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15013 {
15014 msg_puts(get_tv_string(&li->li_tv));
15015 msg_putchar('\n');
15016 }
15017
15018 /* Ask for choice. */
15019 selected = prompt_for_number(&mouse_used);
15020 if (mouse_used)
15021 selected -= lines_left;
15022
15023 rettv->vval.v_number = selected;
15024}
15025
15026
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15028
15029/*
15030 * "inputrestore()" function
15031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015033f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034{
15035 if (ga_userinput.ga_len > 0)
15036 {
15037 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15039 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015040 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 }
15042 else if (p_verbose > 1)
15043 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015044 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015045 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015046 }
15047}
15048
15049/*
15050 * "inputsave()" function
15051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015053f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015055 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 if (ga_grow(&ga_userinput, 1) == OK)
15057 {
15058 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15059 + ga_userinput.ga_len);
15060 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015061 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 }
15063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015064 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065}
15066
15067/*
15068 * "inputsecret()" function
15069 */
15070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072{
15073 ++cmdline_star;
15074 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 --cmdline_star;
15077 --inputsecret_flag;
15078}
15079
15080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015081 * "insert()" function
15082 */
15083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015084f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015085{
15086 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015087 listitem_T *item;
15088 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015089 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015090
15091 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015093 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015094 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015095 {
15096 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015097 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 if (error)
15099 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015100
Bram Moolenaar758711c2005-02-02 23:11:38 +000015101 if (before == l->lv_len)
15102 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015103 else
15104 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015105 item = list_find(l, before);
15106 if (item == NULL)
15107 {
15108 EMSGN(_(e_listidx), before);
15109 l = NULL;
15110 }
15111 }
15112 if (l != NULL)
15113 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015114 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015115 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015116 }
15117 }
15118}
15119
15120/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015121 * "invert(expr)" function
15122 */
15123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015124f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015125{
15126 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15127}
15128
15129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130 * "isdirectory()" function
15131 */
15132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015133f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136}
15137
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015138/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015139 * "islocked()" function
15140 */
15141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015142f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015143{
15144 lval_T lv;
15145 char_u *end;
15146 dictitem_T *di;
15147
15148 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015149 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15150 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015151 if (end != NULL && lv.ll_name != NULL)
15152 {
15153 if (*end != NUL)
15154 EMSG(_(e_trailing));
15155 else
15156 {
15157 if (lv.ll_tv == NULL)
15158 {
15159 if (check_changedtick(lv.ll_name))
15160 rettv->vval.v_number = 1; /* always locked */
15161 else
15162 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015163 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015164 if (di != NULL)
15165 {
15166 /* Consider a variable locked when:
15167 * 1. the variable itself is locked
15168 * 2. the value of the variable is locked.
15169 * 3. the List or Dict value is locked.
15170 */
15171 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15172 || tv_islocked(&di->di_tv));
15173 }
15174 }
15175 }
15176 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015177 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015178 else if (lv.ll_newkey != NULL)
15179 EMSG2(_(e_dictkey), lv.ll_newkey);
15180 else if (lv.ll_list != NULL)
15181 /* List item. */
15182 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15183 else
15184 /* Dictionary item. */
15185 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15186 }
15187 }
15188
15189 clear_lval(&lv);
15190}
15191
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015192#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15193/*
15194 * "isnan()" function
15195 */
15196 static void
15197f_isnan(typval_T *argvars, typval_T *rettv)
15198{
15199 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15200 && isnan(argvars[0].vval.v_float);
15201}
15202#endif
15203
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015204static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015205
15206/*
15207 * Turn a dict into a list:
15208 * "what" == 0: list of keys
15209 * "what" == 1: list of values
15210 * "what" == 2: list of items
15211 */
15212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015213dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015214{
Bram Moolenaar33570922005-01-25 22:26:29 +000015215 list_T *l2;
15216 dictitem_T *di;
15217 hashitem_T *hi;
15218 listitem_T *li;
15219 listitem_T *li2;
15220 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015221 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222
Bram Moolenaar8c711452005-01-14 21:53:12 +000015223 if (argvars[0].v_type != VAR_DICT)
15224 {
15225 EMSG(_(e_dictreq));
15226 return;
15227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015228 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015229 return;
15230
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015231 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015232 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015233
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015234 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015237 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015239 --todo;
15240 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015241
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015242 li = listitem_alloc();
15243 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015245 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015246
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015247 if (what == 0)
15248 {
15249 /* keys() */
15250 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015251 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015252 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15253 }
15254 else if (what == 1)
15255 {
15256 /* values() */
15257 copy_tv(&di->di_tv, &li->li_tv);
15258 }
15259 else
15260 {
15261 /* items() */
15262 l2 = list_alloc();
15263 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015264 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015265 li->li_tv.vval.v_list = l2;
15266 if (l2 == NULL)
15267 break;
15268 ++l2->lv_refcount;
15269
15270 li2 = listitem_alloc();
15271 if (li2 == NULL)
15272 break;
15273 list_append(l2, li2);
15274 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015275 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015276 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15277
15278 li2 = listitem_alloc();
15279 if (li2 == NULL)
15280 break;
15281 list_append(l2, li2);
15282 copy_tv(&di->di_tv, &li2->li_tv);
15283 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015284 }
15285 }
15286}
15287
15288/*
15289 * "items(dict)" function
15290 */
15291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015292f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015293{
15294 dict_list(argvars, rettv, 2);
15295}
15296
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015297#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015298/*
15299 * Get the job from the argument.
15300 * Returns NULL if the job is invalid.
15301 */
15302 static job_T *
15303get_job_arg(typval_T *tv)
15304{
15305 job_T *job;
15306
15307 if (tv->v_type != VAR_JOB)
15308 {
15309 EMSG2(_(e_invarg2), get_tv_string(tv));
15310 return NULL;
15311 }
15312 job = tv->vval.v_job;
15313
15314 if (job == NULL)
15315 EMSG(_("E916: not a valid job"));
15316 return job;
15317}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015318
Bram Moolenaar835dc632016-02-07 14:27:38 +010015319/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015320 * "job_getchannel()" function
15321 */
15322 static void
15323f_job_getchannel(typval_T *argvars, typval_T *rettv)
15324{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015325 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015326
Bram Moolenaar65edff82016-02-21 16:40:11 +010015327 if (job != NULL)
15328 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015329 rettv->v_type = VAR_CHANNEL;
15330 rettv->vval.v_channel = job->jv_channel;
15331 if (job->jv_channel != NULL)
15332 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015333 }
15334}
15335
15336/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015337 * "job_info()" function
15338 */
15339 static void
15340f_job_info(typval_T *argvars, typval_T *rettv)
15341{
15342 job_T *job = get_job_arg(&argvars[0]);
15343
15344 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15345 job_info(job, rettv->vval.v_dict);
15346}
15347
15348/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015349 * "job_setoptions()" function
15350 */
15351 static void
15352f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15353{
15354 job_T *job = get_job_arg(&argvars[0]);
15355 jobopt_T opt;
15356
15357 if (job == NULL)
15358 return;
15359 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015360 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15361 job_set_options(job, &opt);
15362 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015363}
15364
15365/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015366 * "job_start()" function
15367 */
15368 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015369f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015370{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015371 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015372 if (check_restricted() || check_secure())
15373 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015374 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015375}
15376
15377/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015378 * "job_status()" function
15379 */
15380 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015381f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015382{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015383 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015384
Bram Moolenaar65edff82016-02-21 16:40:11 +010015385 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015386 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015387 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015388 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015389 }
15390}
15391
15392/*
15393 * "job_stop()" function
15394 */
15395 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015396f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015397{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015398 job_T *job = get_job_arg(&argvars[0]);
15399
15400 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015401 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015402}
15403#endif
15404
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015406 * "join()" function
15407 */
15408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015409f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015410{
15411 garray_T ga;
15412 char_u *sep;
15413
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015414 if (argvars[0].v_type != VAR_LIST)
15415 {
15416 EMSG(_(e_listreq));
15417 return;
15418 }
15419 if (argvars[0].vval.v_list == NULL)
15420 return;
15421 if (argvars[1].v_type == VAR_UNKNOWN)
15422 sep = (char_u *)" ";
15423 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015424 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015425
15426 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015427
15428 if (sep != NULL)
15429 {
15430 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015431 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015432 ga_append(&ga, NUL);
15433 rettv->vval.v_string = (char_u *)ga.ga_data;
15434 }
15435 else
15436 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015437}
15438
15439/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015440 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015441 */
15442 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015443f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015444{
15445 js_read_T reader;
15446
15447 reader.js_buf = get_tv_string(&argvars[0]);
15448 reader.js_fill = NULL;
15449 reader.js_used = 0;
15450 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15451 EMSG(_(e_invarg));
15452}
15453
15454/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015455 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015456 */
15457 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015458f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015459{
15460 rettv->v_type = VAR_STRING;
15461 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15462}
15463
15464/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015465 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015466 */
15467 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015468f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015469{
15470 js_read_T reader;
15471
15472 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015473 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015474 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015475 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015476 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015477}
15478
15479/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015480 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015481 */
15482 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015483f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015484{
15485 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015486 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015487}
15488
15489/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015490 * "keys()" function
15491 */
15492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015493f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015494{
15495 dict_list(argvars, rettv, 0);
15496}
15497
15498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 * "last_buffer_nr()" function.
15500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015502f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503{
15504 int n = 0;
15505 buf_T *buf;
15506
15507 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15508 if (n < buf->b_fnum)
15509 n = buf->b_fnum;
15510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015512}
15513
15514/*
15515 * "len()" function
15516 */
15517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015518f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015519{
15520 switch (argvars[0].v_type)
15521 {
15522 case VAR_STRING:
15523 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 rettv->vval.v_number = (varnumber_T)STRLEN(
15525 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015526 break;
15527 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015529 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015530 case VAR_DICT:
15531 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15532 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015533 case VAR_UNKNOWN:
15534 case VAR_SPECIAL:
15535 case VAR_FLOAT:
15536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015537 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015538 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015539 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015540 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015541 break;
15542 }
15543}
15544
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015545static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015546
15547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015548libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015549{
15550#ifdef FEAT_LIBCALL
15551 char_u *string_in;
15552 char_u **string_result;
15553 int nr_result;
15554#endif
15555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015557 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015558 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015559
15560 if (check_restricted() || check_secure())
15561 return;
15562
15563#ifdef FEAT_LIBCALL
15564 /* The first two args must be strings, otherwise its meaningless */
15565 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15566 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015567 string_in = NULL;
15568 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015569 string_in = argvars[2].vval.v_string;
15570 if (type == VAR_NUMBER)
15571 string_result = NULL;
15572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015574 if (mch_libcall(argvars[0].vval.v_string,
15575 argvars[1].vval.v_string,
15576 string_in,
15577 argvars[2].vval.v_number,
15578 string_result,
15579 &nr_result) == OK
15580 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015581 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015582 }
15583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584}
15585
15586/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015587 * "libcall()" function
15588 */
15589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015590f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015591{
15592 libcall_common(argvars, rettv, VAR_STRING);
15593}
15594
15595/*
15596 * "libcallnr()" function
15597 */
15598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015599f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600{
15601 libcall_common(argvars, rettv, VAR_NUMBER);
15602}
15603
15604/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 * "line(string)" function
15606 */
15607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015608f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609{
15610 linenr_T lnum = 0;
15611 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015612 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015614 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 if (fp != NULL)
15616 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015617 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618}
15619
15620/*
15621 * "line2byte(lnum)" function
15622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015624f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625{
15626#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628#else
15629 linenr_T lnum;
15630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015635 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15636 if (rettv->vval.v_number >= 0)
15637 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638#endif
15639}
15640
15641/*
15642 * "lispindent(lnum)" function
15643 */
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646{
15647#ifdef FEAT_LISP
15648 pos_T pos;
15649 linenr_T lnum;
15650
15651 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15654 {
15655 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 curwin->w_cursor = pos;
15658 }
15659 else
15660#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662}
15663
15664/*
15665 * "localtime()" function
15666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015668f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671}
15672
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015673static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674
15675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015676get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677{
15678 char_u *keys;
15679 char_u *which;
15680 char_u buf[NUMBUFLEN];
15681 char_u *keys_buf = NULL;
15682 char_u *rhs;
15683 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015684 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015685 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015686 mapblock_T *mp;
15687 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688
15689 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015690 rettv->v_type = VAR_STRING;
15691 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 if (*keys == NUL)
15695 return;
15696
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015697 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015699 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015700 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015701 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015702 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015703 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015704 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015705 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 else
15708 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015709 if (which == NULL)
15710 return;
15711
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 mode = get_map_mode(&which, 0);
15713
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015714 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015715 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015717
15718 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015720 /* Return a string. */
15721 if (rhs != NULL)
15722 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723
Bram Moolenaarbd743252010-10-20 21:23:33 +020015724 }
15725 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15726 {
15727 /* Return a dictionary. */
15728 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15729 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15730 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731
Bram Moolenaarbd743252010-10-20 21:23:33 +020015732 dict_add_nr_str(dict, "lhs", 0L, lhs);
15733 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15734 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15735 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15736 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15737 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15738 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015739 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015740 dict_add_nr_str(dict, "mode", 0L, mapmode);
15741
15742 vim_free(lhs);
15743 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 }
15745}
15746
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015747#ifdef FEAT_FLOAT
15748/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015749 * "log()" function
15750 */
15751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015752f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015753{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015754 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015755
15756 rettv->v_type = VAR_FLOAT;
15757 if (get_float_arg(argvars, &f) == OK)
15758 rettv->vval.v_float = log(f);
15759 else
15760 rettv->vval.v_float = 0.0;
15761}
15762
15763/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015764 * "log10()" function
15765 */
15766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015767f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015768{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015769 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015770
15771 rettv->v_type = VAR_FLOAT;
15772 if (get_float_arg(argvars, &f) == OK)
15773 rettv->vval.v_float = log10(f);
15774 else
15775 rettv->vval.v_float = 0.0;
15776}
15777#endif
15778
Bram Moolenaar1dced572012-04-05 16:54:08 +020015779#ifdef FEAT_LUA
15780/*
15781 * "luaeval()" function
15782 */
15783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015784f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015785{
15786 char_u *str;
15787 char_u buf[NUMBUFLEN];
15788
15789 str = get_tv_string_buf(&argvars[0], buf);
15790 do_luaeval(str, argvars + 1, rettv);
15791}
15792#endif
15793
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015795 * "map()" function
15796 */
15797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015798f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015799{
15800 filter_map(argvars, rettv, TRUE);
15801}
15802
15803/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015804 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805 */
15806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015807f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015809 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810}
15811
15812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 */
15815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015816f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819}
15820
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015821static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
15823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015824find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015826 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015827 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015828 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 char_u *pat;
15830 regmatch_T regmatch;
15831 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015832 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 char_u *save_cpo;
15834 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015835 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015836 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015837 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015838 list_T *l = NULL;
15839 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015841 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842
15843 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15844 save_cpo = p_cpo;
15845 p_cpo = (char_u *)"";
15846
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015847 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015848 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015849 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015850 /* type 3: return empty list when there are no matches.
15851 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015852 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015853 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015854 if (type == 4
15855 && (list_append_string(rettv->vval.v_list,
15856 (char_u *)"", 0) == FAIL
15857 || list_append_number(rettv->vval.v_list,
15858 (varnumber_T)-1) == FAIL
15859 || list_append_number(rettv->vval.v_list,
15860 (varnumber_T)-1) == FAIL
15861 || list_append_number(rettv->vval.v_list,
15862 (varnumber_T)-1) == FAIL))
15863 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015864 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015865 rettv->vval.v_list = NULL;
15866 goto theend;
15867 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015868 }
15869 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015871 rettv->v_type = VAR_STRING;
15872 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015875 if (argvars[0].v_type == VAR_LIST)
15876 {
15877 if ((l = argvars[0].vval.v_list) == NULL)
15878 goto theend;
15879 li = l->lv_first;
15880 }
15881 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015882 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015883 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015884 len = (long)STRLEN(str);
15885 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15888 if (pat == NULL)
15889 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015890
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015891 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015893 int error = FALSE;
15894
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015895 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015896 if (error)
15897 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015898 if (l != NULL)
15899 {
15900 li = list_find(l, start);
15901 if (li == NULL)
15902 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015903 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015904 }
15905 else
15906 {
15907 if (start < 0)
15908 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015909 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015910 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015911 /* When "count" argument is there ignore matches before "start",
15912 * otherwise skip part of the string. Differs when pattern is "^"
15913 * or "\<". */
15914 if (argvars[3].v_type != VAR_UNKNOWN)
15915 startcol = start;
15916 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015917 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015918 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015919 len -= start;
15920 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015921 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015922
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015923 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015924 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 if (error)
15926 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 }
15928
15929 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15930 if (regmatch.regprog != NULL)
15931 {
15932 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015933
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015934 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015935 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015936 if (l != NULL)
15937 {
15938 if (li == NULL)
15939 {
15940 match = FALSE;
15941 break;
15942 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015943 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015944 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015945 if (str == NULL)
15946 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015947 }
15948
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015949 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015950
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015951 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015952 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015953 if (l == NULL && !match)
15954 break;
15955
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015956 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015957 if (l != NULL)
15958 {
15959 li = li->li_next;
15960 ++idx;
15961 }
15962 else
15963 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015964#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015965 startcol = (colnr_T)(regmatch.startp[0]
15966 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015967#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015968 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015969#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015970 if (startcol > (colnr_T)len
15971 || str + startcol <= regmatch.startp[0])
15972 {
15973 match = FALSE;
15974 break;
15975 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015976 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015977 }
15978
15979 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015981 if (type == 4)
15982 {
15983 listitem_T *li1 = rettv->vval.v_list->lv_first;
15984 listitem_T *li2 = li1->li_next;
15985 listitem_T *li3 = li2->li_next;
15986 listitem_T *li4 = li3->li_next;
15987
Bram Moolenaar3c809342016-06-01 22:34:48 +020015988 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015989 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15990 (int)(regmatch.endp[0] - regmatch.startp[0]));
15991 li3->li_tv.vval.v_number =
15992 (varnumber_T)(regmatch.startp[0] - expr);
15993 li4->li_tv.vval.v_number =
15994 (varnumber_T)(regmatch.endp[0] - expr);
15995 if (l != NULL)
15996 li2->li_tv.vval.v_number = (varnumber_T)idx;
15997 }
15998 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015999 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016000 int i;
16001
16002 /* return list with matched string and submatches */
16003 for (i = 0; i < NSUBEXP; ++i)
16004 {
16005 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016006 {
16007 if (list_append_string(rettv->vval.v_list,
16008 (char_u *)"", 0) == FAIL)
16009 break;
16010 }
16011 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016012 regmatch.startp[i],
16013 (int)(regmatch.endp[i] - regmatch.startp[i]))
16014 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016015 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016016 }
16017 }
16018 else if (type == 2)
16019 {
16020 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016021 if (l != NULL)
16022 copy_tv(&li->li_tv, rettv);
16023 else
16024 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016026 }
16027 else if (l != NULL)
16028 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 else
16030 {
16031 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 (varnumber_T)(regmatch.startp[0] - str);
16034 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016035 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016037 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 }
16039 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016040 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 }
16042
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016043 if (type == 4 && l == NULL)
16044 /* matchstrpos() without a list: drop the second item. */
16045 listitem_remove(rettv->vval.v_list,
16046 rettv->vval.v_list->lv_first->li_next);
16047
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016049 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050 p_cpo = save_cpo;
16051}
16052
16053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016054 * "match()" function
16055 */
16056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016057f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058{
16059 find_some_match(argvars, rettv, 1);
16060}
16061
16062/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016063 * "matchadd()" function
16064 */
16065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016066f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016067{
16068#ifdef FEAT_SEARCH_EXTRA
16069 char_u buf[NUMBUFLEN];
16070 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16071 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16072 int prio = 10; /* default priority */
16073 int id = -1;
16074 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016075 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016076
16077 rettv->vval.v_number = -1;
16078
16079 if (grp == NULL || pat == NULL)
16080 return;
16081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016082 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016083 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016084 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016085 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016086 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016087 if (argvars[4].v_type != VAR_UNKNOWN)
16088 {
16089 if (argvars[4].v_type != VAR_DICT)
16090 {
16091 EMSG(_(e_dictreq));
16092 return;
16093 }
16094 if (dict_find(argvars[4].vval.v_dict,
16095 (char_u *)"conceal", -1) != NULL)
16096 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16097 (char_u *)"conceal", FALSE);
16098 }
16099 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016100 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016101 if (error == TRUE)
16102 return;
16103 if (id >= 1 && id <= 3)
16104 {
16105 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16106 return;
16107 }
16108
Bram Moolenaar6561d522015-07-21 15:48:27 +020016109 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16110 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016111#endif
16112}
16113
16114/*
16115 * "matchaddpos()" function
16116 */
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016119{
16120#ifdef FEAT_SEARCH_EXTRA
16121 char_u buf[NUMBUFLEN];
16122 char_u *group;
16123 int prio = 10;
16124 int id = -1;
16125 int error = FALSE;
16126 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016127 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016128
16129 rettv->vval.v_number = -1;
16130
16131 group = get_tv_string_buf_chk(&argvars[0], buf);
16132 if (group == NULL)
16133 return;
16134
16135 if (argvars[1].v_type != VAR_LIST)
16136 {
16137 EMSG2(_(e_listarg), "matchaddpos()");
16138 return;
16139 }
16140 l = argvars[1].vval.v_list;
16141 if (l == NULL)
16142 return;
16143
16144 if (argvars[2].v_type != VAR_UNKNOWN)
16145 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016146 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016147 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016148 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016149 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016150 if (argvars[4].v_type != VAR_UNKNOWN)
16151 {
16152 if (argvars[4].v_type != VAR_DICT)
16153 {
16154 EMSG(_(e_dictreq));
16155 return;
16156 }
16157 if (dict_find(argvars[4].vval.v_dict,
16158 (char_u *)"conceal", -1) != NULL)
16159 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16160 (char_u *)"conceal", FALSE);
16161 }
16162 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016163 }
16164 if (error == TRUE)
16165 return;
16166
16167 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16168 if (id == 1 || id == 2)
16169 {
16170 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16171 return;
16172 }
16173
Bram Moolenaar6561d522015-07-21 15:48:27 +020016174 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16175 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016176#endif
16177}
16178
16179/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016180 * "matcharg()" function
16181 */
16182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016183f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016184{
16185 if (rettv_list_alloc(rettv) == OK)
16186 {
16187#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016188 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016189 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016190
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016191 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016192 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016193 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16194 {
16195 list_append_string(rettv->vval.v_list,
16196 syn_id2name(m->hlg_id), -1);
16197 list_append_string(rettv->vval.v_list, m->pattern, -1);
16198 }
16199 else
16200 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016201 list_append_string(rettv->vval.v_list, NULL, -1);
16202 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016203 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016204 }
16205#endif
16206 }
16207}
16208
16209/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016210 * "matchdelete()" function
16211 */
16212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016213f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016214{
16215#ifdef FEAT_SEARCH_EXTRA
16216 rettv->vval.v_number = match_delete(curwin,
16217 (int)get_tv_number(&argvars[0]), TRUE);
16218#endif
16219}
16220
16221/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222 * "matchend()" function
16223 */
16224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226{
16227 find_some_match(argvars, rettv, 0);
16228}
16229
16230/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231 * "matchlist()" function
16232 */
16233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016234f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235{
16236 find_some_match(argvars, rettv, 3);
16237}
16238
16239/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240 * "matchstr()" function
16241 */
16242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244{
16245 find_some_match(argvars, rettv, 2);
16246}
16247
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016248/*
16249 * "matchstrpos()" function
16250 */
16251 static void
16252f_matchstrpos(typval_T *argvars, typval_T *rettv)
16253{
16254 find_some_match(argvars, rettv, 4);
16255}
16256
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016257static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016258
16259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016260max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016261{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016262 varnumber_T n = 0;
16263 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016264 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016265
16266 if (argvars[0].v_type == VAR_LIST)
16267 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016268 list_T *l;
16269 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016270
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016271 l = argvars[0].vval.v_list;
16272 if (l != NULL)
16273 {
16274 li = l->lv_first;
16275 if (li != NULL)
16276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016277 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016278 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016279 {
16280 li = li->li_next;
16281 if (li == NULL)
16282 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016284 if (domax ? i > n : i < n)
16285 n = i;
16286 }
16287 }
16288 }
16289 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016290 else if (argvars[0].v_type == VAR_DICT)
16291 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016293 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016295 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016296
16297 d = argvars[0].vval.v_dict;
16298 if (d != NULL)
16299 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016300 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016303 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016305 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016306 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016307 if (first)
16308 {
16309 n = i;
16310 first = FALSE;
16311 }
16312 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016313 n = i;
16314 }
16315 }
16316 }
16317 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016318 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016319 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016320 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016321}
16322
16323/*
16324 * "max()" function
16325 */
16326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016327f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016328{
16329 max_min(argvars, rettv, TRUE);
16330}
16331
16332/*
16333 * "min()" function
16334 */
16335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016336f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016337{
16338 max_min(argvars, rettv, FALSE);
16339}
16340
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016341static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016342
16343/*
16344 * Create the directory in which "dir" is located, and higher levels when
16345 * needed.
16346 */
16347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016348mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016349{
16350 char_u *p;
16351 char_u *updir;
16352 int r = FAIL;
16353
16354 /* Get end of directory name in "dir".
16355 * We're done when it's "/" or "c:/". */
16356 p = gettail_sep(dir);
16357 if (p <= get_past_head(dir))
16358 return OK;
16359
16360 /* If the directory exists we're done. Otherwise: create it.*/
16361 updir = vim_strnsave(dir, (int)(p - dir));
16362 if (updir == NULL)
16363 return FAIL;
16364 if (mch_isdir(updir))
16365 r = OK;
16366 else if (mkdir_recurse(updir, prot) == OK)
16367 r = vim_mkdir_emsg(updir, prot);
16368 vim_free(updir);
16369 return r;
16370}
16371
16372#ifdef vim_mkdir
16373/*
16374 * "mkdir()" function
16375 */
16376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016377f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016378{
16379 char_u *dir;
16380 char_u buf[NUMBUFLEN];
16381 int prot = 0755;
16382
16383 rettv->vval.v_number = FAIL;
16384 if (check_restricted() || check_secure())
16385 return;
16386
16387 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016388 if (*dir == NUL)
16389 rettv->vval.v_number = FAIL;
16390 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016391 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016392 if (*gettail(dir) == NUL)
16393 /* remove trailing slashes */
16394 *gettail_sep(dir) = NUL;
16395
16396 if (argvars[1].v_type != VAR_UNKNOWN)
16397 {
16398 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016399 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016400 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16401 mkdir_recurse(dir, prot);
16402 }
16403 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016404 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016405}
16406#endif
16407
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 * "mode()" function
16410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016412f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016414 char_u buf[3];
16415
16416 buf[1] = NUL;
16417 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016419 if (time_for_testing == 93784)
16420 {
16421 /* Testing the two-character code. */
16422 buf[0] = 'x';
16423 buf[1] = '!';
16424 }
16425 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 {
16427 if (VIsual_select)
16428 buf[0] = VIsual_mode + 's' - 'v';
16429 else
16430 buf[0] = VIsual_mode;
16431 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016432 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016433 || State == CONFIRM)
16434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016436 if (State == ASKMORE)
16437 buf[1] = 'm';
16438 else if (State == CONFIRM)
16439 buf[1] = '?';
16440 }
16441 else if (State == EXTERNCMD)
16442 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016443 else if (State & INSERT)
16444 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016445#ifdef FEAT_VREPLACE
16446 if (State & VREPLACE_FLAG)
16447 {
16448 buf[0] = 'R';
16449 buf[1] = 'v';
16450 }
16451 else
16452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 if (State & REPLACE_FLAG)
16454 buf[0] = 'R';
16455 else
16456 buf[0] = 'i';
16457 }
16458 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016460 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016461 if (exmode_active)
16462 buf[1] = 'v';
16463 }
16464 else if (exmode_active)
16465 {
16466 buf[0] = 'c';
16467 buf[1] = 'e';
16468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016469 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016472 if (finish_op)
16473 buf[1] = 'o';
16474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016476 /* Clear out the minor mode when the argument is not a non-zero number or
16477 * non-empty string. */
16478 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016479 buf[1] = NUL;
16480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016481 rettv->vval.v_string = vim_strsave(buf);
16482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483}
16484
Bram Moolenaar429fa852013-04-15 12:27:36 +020016485#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016486/*
16487 * "mzeval()" function
16488 */
16489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016490f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016491{
16492 char_u *str;
16493 char_u buf[NUMBUFLEN];
16494
16495 str = get_tv_string_buf(&argvars[0], buf);
16496 do_mzeval(str, rettv);
16497}
Bram Moolenaar75676462013-01-30 14:55:42 +010016498
16499 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016500mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016501{
16502 typval_T argvars[3];
16503
16504 argvars[0].v_type = VAR_STRING;
16505 argvars[0].vval.v_string = name;
16506 copy_tv(args, &argvars[1]);
16507 argvars[2].v_type = VAR_UNKNOWN;
16508 f_call(argvars, rettv);
16509 clear_tv(&argvars[1]);
16510}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016511#endif
16512
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514 * "nextnonblank()" function
16515 */
16516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016517f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518{
16519 linenr_T lnum;
16520
16521 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016523 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524 {
16525 lnum = 0;
16526 break;
16527 }
16528 if (*skipwhite(ml_get(lnum)) != NUL)
16529 break;
16530 }
16531 rettv->vval.v_number = lnum;
16532}
16533
16534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016535 * "nr2char()" function
16536 */
16537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539{
16540 char_u buf[NUMBUFLEN];
16541
16542#ifdef FEAT_MBYTE
16543 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016544 {
16545 int utf8 = 0;
16546
16547 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016548 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016549 if (utf8)
16550 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16551 else
16552 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 else
16555#endif
16556 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016557 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 buf[1] = NUL;
16559 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016560 rettv->v_type = VAR_STRING;
16561 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016562}
16563
16564/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016565 * "or(expr, expr)" function
16566 */
16567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016568f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016569{
16570 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16571 | get_tv_number_chk(&argvars[1], NULL);
16572}
16573
16574/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016575 * "pathshorten()" function
16576 */
16577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016578f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016579{
16580 char_u *p;
16581
16582 rettv->v_type = VAR_STRING;
16583 p = get_tv_string_chk(&argvars[0]);
16584 if (p == NULL)
16585 rettv->vval.v_string = NULL;
16586 else
16587 {
16588 p = vim_strsave(p);
16589 rettv->vval.v_string = p;
16590 if (p != NULL)
16591 shorten_dir(p);
16592 }
16593}
16594
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016595#ifdef FEAT_PERL
16596/*
16597 * "perleval()" function
16598 */
16599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016600f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016601{
16602 char_u *str;
16603 char_u buf[NUMBUFLEN];
16604
16605 str = get_tv_string_buf(&argvars[0], buf);
16606 do_perleval(str, rettv);
16607}
16608#endif
16609
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016610#ifdef FEAT_FLOAT
16611/*
16612 * "pow()" function
16613 */
16614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016615f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016616{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016617 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016618
16619 rettv->v_type = VAR_FLOAT;
16620 if (get_float_arg(argvars, &fx) == OK
16621 && get_float_arg(&argvars[1], &fy) == OK)
16622 rettv->vval.v_float = pow(fx, fy);
16623 else
16624 rettv->vval.v_float = 0.0;
16625}
16626#endif
16627
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016628/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629 * "prevnonblank()" function
16630 */
16631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016632f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016633{
16634 linenr_T lnum;
16635
16636 lnum = get_tv_lnum(argvars);
16637 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16638 lnum = 0;
16639 else
16640 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16641 --lnum;
16642 rettv->vval.v_number = lnum;
16643}
16644
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016645/* This dummy va_list is here because:
16646 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16647 * - locally in the function results in a "used before set" warning
16648 * - using va_start() to initialize it gives "function with fixed args" error */
16649static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016650
Bram Moolenaar8c711452005-01-14 21:53:12 +000016651/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016652 * "printf()" function
16653 */
16654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016655f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016656{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016657 char_u buf[NUMBUFLEN];
16658 int len;
16659 char_u *s;
16660 int saved_did_emsg = did_emsg;
16661 char *fmt;
16662
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016663 rettv->v_type = VAR_STRING;
16664 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016665
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016666 /* Get the required length, allocate the buffer and do it for real. */
16667 did_emsg = FALSE;
16668 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16669 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16670 if (!did_emsg)
16671 {
16672 s = alloc(len + 1);
16673 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016674 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016675 rettv->vval.v_string = s;
16676 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016677 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016678 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016679 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016680}
16681
16682/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016683 * "pumvisible()" function
16684 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016686f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016687{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016688#ifdef FEAT_INS_EXPAND
16689 if (pum_visible())
16690 rettv->vval.v_number = 1;
16691#endif
16692}
16693
Bram Moolenaardb913952012-06-29 12:54:53 +020016694#ifdef FEAT_PYTHON3
16695/*
16696 * "py3eval()" function
16697 */
16698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016699f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016700{
16701 char_u *str;
16702 char_u buf[NUMBUFLEN];
16703
16704 str = get_tv_string_buf(&argvars[0], buf);
16705 do_py3eval(str, rettv);
16706}
16707#endif
16708
16709#ifdef FEAT_PYTHON
16710/*
16711 * "pyeval()" function
16712 */
16713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016714f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016715{
16716 char_u *str;
16717 char_u buf[NUMBUFLEN];
16718
16719 str = get_tv_string_buf(&argvars[0], buf);
16720 do_pyeval(str, rettv);
16721}
16722#endif
16723
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016724/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016725 * "range()" function
16726 */
16727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016728f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016729{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016730 varnumber_T start;
16731 varnumber_T end;
16732 varnumber_T stride = 1;
16733 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016734 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016737 if (argvars[1].v_type == VAR_UNKNOWN)
16738 {
16739 end = start - 1;
16740 start = 0;
16741 }
16742 else
16743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016744 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016745 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016746 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016747 }
16748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016749 if (error)
16750 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016751 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016752 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016753 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016754 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016755 else
16756 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016757 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016758 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016759 if (list_append_number(rettv->vval.v_list,
16760 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016761 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016762 }
16763}
16764
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016765/*
16766 * "readfile()" function
16767 */
16768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016769f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016770{
16771 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016772 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016773 char_u *fname;
16774 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016775 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16776 int io_size = sizeof(buf);
16777 int readlen; /* size of last fread() */
16778 char_u *prev = NULL; /* previously read bytes, if any */
16779 long prevlen = 0; /* length of data in prev */
16780 long prevsize = 0; /* size of prev buffer */
16781 long maxline = MAXLNUM;
16782 long cnt = 0;
16783 char_u *p; /* position in buf */
16784 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016785
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016786 if (argvars[1].v_type != VAR_UNKNOWN)
16787 {
16788 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16789 binary = TRUE;
16790 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016791 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016792 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016793
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016794 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016795 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016796
16797 /* Always open the file in binary mode, library functions have a mind of
16798 * their own about CR-LF conversion. */
16799 fname = get_tv_string(&argvars[0]);
16800 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16801 {
16802 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16803 return;
16804 }
16805
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016806 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016807 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016808 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016809
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016810 /* This for loop processes what was read, but is also entered at end
16811 * of file so that either:
16812 * - an incomplete line gets written
16813 * - a "binary" file gets an empty line at the end if it ends in a
16814 * newline. */
16815 for (p = buf, start = buf;
16816 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16817 ++p)
16818 {
16819 if (*p == '\n' || readlen <= 0)
16820 {
16821 listitem_T *li;
16822 char_u *s = NULL;
16823 long_u len = p - start;
16824
16825 /* Finished a line. Remove CRs before NL. */
16826 if (readlen > 0 && !binary)
16827 {
16828 while (len > 0 && start[len - 1] == '\r')
16829 --len;
16830 /* removal may cross back to the "prev" string */
16831 if (len == 0)
16832 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16833 --prevlen;
16834 }
16835 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016836 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016837 else
16838 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016839 /* Change "prev" buffer to be the right size. This way
16840 * the bytes are only copied once, and very long lines are
16841 * allocated only once. */
16842 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016843 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016844 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016845 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016846 prev = NULL; /* the list will own the string */
16847 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016848 }
16849 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850 if (s == NULL)
16851 {
16852 do_outofmem_msg((long_u) prevlen + len + 1);
16853 failed = TRUE;
16854 break;
16855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016856
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016857 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016858 {
16859 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016860 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016861 break;
16862 }
16863 li->li_tv.v_type = VAR_STRING;
16864 li->li_tv.v_lock = 0;
16865 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016866 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016867
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016868 start = p + 1; /* step over newline */
16869 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016870 break;
16871 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016872 else if (*p == NUL)
16873 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016874#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016875 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16876 * when finding the BF and check the previous two bytes. */
16877 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016878 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016879 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16880 * + 1, these may be in the "prev" string. */
16881 char_u back1 = p >= buf + 1 ? p[-1]
16882 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16883 char_u back2 = p >= buf + 2 ? p[-2]
16884 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16885 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016886
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016887 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016888 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016889 char_u *dest = p - 2;
16890
16891 /* Usually a BOM is at the beginning of a file, and so at
16892 * the beginning of a line; then we can just step over it.
16893 */
16894 if (start == dest)
16895 start = p + 1;
16896 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016897 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016898 /* have to shuffle buf to close gap */
16899 int adjust_prevlen = 0;
16900
16901 if (dest < buf)
16902 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016903 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016904 dest = buf;
16905 }
16906 if (readlen > p - buf + 1)
16907 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16908 readlen -= 3 - adjust_prevlen;
16909 prevlen -= adjust_prevlen;
16910 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016911 }
16912 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016913 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016914#endif
16915 } /* for */
16916
16917 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16918 break;
16919 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016920 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016921 /* There's part of a line in buf, store it in "prev". */
16922 if (p - start + prevlen >= prevsize)
16923 {
16924 /* need bigger "prev" buffer */
16925 char_u *newprev;
16926
16927 /* A common use case is ordinary text files and "prev" gets a
16928 * fragment of a line, so the first allocation is made
16929 * small, to avoid repeatedly 'allocing' large and
16930 * 'reallocing' small. */
16931 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016932 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016933 else
16934 {
16935 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016936 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016937 prevsize = grow50pc > growmin ? grow50pc : growmin;
16938 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016939 newprev = prev == NULL ? alloc(prevsize)
16940 : vim_realloc(prev, prevsize);
16941 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016942 {
16943 do_outofmem_msg((long_u)prevsize);
16944 failed = TRUE;
16945 break;
16946 }
16947 prev = newprev;
16948 }
16949 /* Add the line part to end of "prev". */
16950 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016951 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016952 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016953 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016954
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016955 /*
16956 * For a negative line count use only the lines at the end of the file,
16957 * free the rest.
16958 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016959 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016960 while (cnt > -maxline)
16961 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016962 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016963 --cnt;
16964 }
16965
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016966 if (failed)
16967 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016968 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016969 /* readfile doc says an empty list is returned on error */
16970 rettv->vval.v_list = list_alloc();
16971 }
16972
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016973 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016974 fclose(fd);
16975}
16976
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016977#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016978static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016979
16980/*
16981 * Convert a List to proftime_T.
16982 * Return FAIL when there is something wrong.
16983 */
16984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016985list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016986{
16987 long n1, n2;
16988 int error = FALSE;
16989
16990 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16991 || arg->vval.v_list->lv_len != 2)
16992 return FAIL;
16993 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16994 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16995# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016996 tm->HighPart = n1;
16997 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016998# else
16999 tm->tv_sec = n1;
17000 tm->tv_usec = n2;
17001# endif
17002 return error ? FAIL : OK;
17003}
17004#endif /* FEAT_RELTIME */
17005
17006/*
17007 * "reltime()" function
17008 */
17009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017010f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017011{
17012#ifdef FEAT_RELTIME
17013 proftime_T res;
17014 proftime_T start;
17015
17016 if (argvars[0].v_type == VAR_UNKNOWN)
17017 {
17018 /* No arguments: get current time. */
17019 profile_start(&res);
17020 }
17021 else if (argvars[1].v_type == VAR_UNKNOWN)
17022 {
17023 if (list2proftime(&argvars[0], &res) == FAIL)
17024 return;
17025 profile_end(&res);
17026 }
17027 else
17028 {
17029 /* Two arguments: compute the difference. */
17030 if (list2proftime(&argvars[0], &start) == FAIL
17031 || list2proftime(&argvars[1], &res) == FAIL)
17032 return;
17033 profile_sub(&res, &start);
17034 }
17035
17036 if (rettv_list_alloc(rettv) == OK)
17037 {
17038 long n1, n2;
17039
17040# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017041 n1 = res.HighPart;
17042 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017043# else
17044 n1 = res.tv_sec;
17045 n2 = res.tv_usec;
17046# endif
17047 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17048 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17049 }
17050#endif
17051}
17052
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017053#ifdef FEAT_FLOAT
17054/*
17055 * "reltimefloat()" function
17056 */
17057 static void
17058f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17059{
17060# ifdef FEAT_RELTIME
17061 proftime_T tm;
17062# endif
17063
17064 rettv->v_type = VAR_FLOAT;
17065 rettv->vval.v_float = 0;
17066# ifdef FEAT_RELTIME
17067 if (list2proftime(&argvars[0], &tm) == OK)
17068 rettv->vval.v_float = profile_float(&tm);
17069# endif
17070}
17071#endif
17072
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017073/*
17074 * "reltimestr()" function
17075 */
17076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017077f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017078{
17079#ifdef FEAT_RELTIME
17080 proftime_T tm;
17081#endif
17082
17083 rettv->v_type = VAR_STRING;
17084 rettv->vval.v_string = NULL;
17085#ifdef FEAT_RELTIME
17086 if (list2proftime(&argvars[0], &tm) == OK)
17087 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17088#endif
17089}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017090
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017092static void make_connection(void);
17093static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094
17095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017096make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097{
17098 if (X_DISPLAY == NULL
17099# ifdef FEAT_GUI
17100 && !gui.in_use
17101# endif
17102 )
17103 {
17104 x_force_connect = TRUE;
17105 setup_term_clip();
17106 x_force_connect = FALSE;
17107 }
17108}
17109
17110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017111check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112{
17113 make_connection();
17114 if (X_DISPLAY == NULL)
17115 {
17116 EMSG(_("E240: No connection to Vim server"));
17117 return FAIL;
17118 }
17119 return OK;
17120}
17121#endif
17122
17123#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017125remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126{
17127 char_u *server_name;
17128 char_u *keys;
17129 char_u *r = NULL;
17130 char_u buf[NUMBUFLEN];
17131# ifdef WIN32
17132 HWND w;
17133# else
17134 Window w;
17135# endif
17136
17137 if (check_restricted() || check_secure())
17138 return;
17139
17140# ifdef FEAT_X11
17141 if (check_connection() == FAIL)
17142 return;
17143# endif
17144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017145 server_name = get_tv_string_chk(&argvars[0]);
17146 if (server_name == NULL)
17147 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148 keys = get_tv_string_buf(&argvars[1], buf);
17149# ifdef WIN32
17150 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17151# else
17152 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17153 < 0)
17154# endif
17155 {
17156 if (r != NULL)
17157 EMSG(r); /* sending worked but evaluation failed */
17158 else
17159 EMSG2(_("E241: Unable to send to %s"), server_name);
17160 return;
17161 }
17162
17163 rettv->vval.v_string = r;
17164
17165 if (argvars[2].v_type != VAR_UNKNOWN)
17166 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017167 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017168 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017171 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017172 v.di_tv.v_type = VAR_STRING;
17173 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017174 idvar = get_tv_string_chk(&argvars[2]);
17175 if (idvar != NULL)
17176 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178 }
17179}
17180#endif
17181
17182/*
17183 * "remote_expr()" function
17184 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187{
17188 rettv->v_type = VAR_STRING;
17189 rettv->vval.v_string = NULL;
17190#ifdef FEAT_CLIENTSERVER
17191 remote_common(argvars, rettv, TRUE);
17192#endif
17193}
17194
17195/*
17196 * "remote_foreground()" function
17197 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017199f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017200{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201#ifdef FEAT_CLIENTSERVER
17202# ifdef WIN32
17203 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017204 {
17205 char_u *server_name = get_tv_string_chk(&argvars[0]);
17206
17207 if (server_name != NULL)
17208 serverForeground(server_name);
17209 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017210# else
17211 /* Send a foreground() expression to the server. */
17212 argvars[1].v_type = VAR_STRING;
17213 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17214 argvars[2].v_type = VAR_UNKNOWN;
17215 remote_common(argvars, rettv, TRUE);
17216 vim_free(argvars[1].vval.v_string);
17217# endif
17218#endif
17219}
17220
Bram Moolenaar0d660222005-01-07 21:51:51 +000017221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017222f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223{
17224#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017225 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226 char_u *s = NULL;
17227# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017228 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017230 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017231
17232 if (check_restricted() || check_secure())
17233 {
17234 rettv->vval.v_number = -1;
17235 return;
17236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017237 serverid = get_tv_string_chk(&argvars[0]);
17238 if (serverid == NULL)
17239 {
17240 rettv->vval.v_number = -1;
17241 return; /* type error; errmsg already given */
17242 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017243# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017244 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245 if (n == 0)
17246 rettv->vval.v_number = -1;
17247 else
17248 {
17249 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17250 rettv->vval.v_number = (s != NULL);
17251 }
17252# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 if (check_connection() == FAIL)
17254 return;
17255
17256 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258# endif
17259
17260 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017262 char_u *retvar;
17263
Bram Moolenaar33570922005-01-25 22:26:29 +000017264 v.di_tv.v_type = VAR_STRING;
17265 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017266 retvar = get_tv_string_chk(&argvars[1]);
17267 if (retvar != NULL)
17268 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017269 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017270 }
17271#else
17272 rettv->vval.v_number = -1;
17273#endif
17274}
17275
Bram Moolenaar0d660222005-01-07 21:51:51 +000017276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017277f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278{
17279 char_u *r = NULL;
17280
17281#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017282 char_u *serverid = get_tv_string_chk(&argvars[0]);
17283
17284 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017285 {
17286# ifdef WIN32
17287 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017288 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017289
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017290 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017291 if (n != 0)
17292 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17293 if (r == NULL)
17294# else
17295 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017296 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017297# endif
17298 EMSG(_("E277: Unable to read a server reply"));
17299 }
17300#endif
17301 rettv->v_type = VAR_STRING;
17302 rettv->vval.v_string = r;
17303}
17304
17305/*
17306 * "remote_send()" function
17307 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017309f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310{
17311 rettv->v_type = VAR_STRING;
17312 rettv->vval.v_string = NULL;
17313#ifdef FEAT_CLIENTSERVER
17314 remote_common(argvars, rettv, FALSE);
17315#endif
17316}
17317
17318/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017319 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320 */
17321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017322f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017323{
Bram Moolenaar33570922005-01-25 22:26:29 +000017324 list_T *l;
17325 listitem_T *item, *item2;
17326 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017327 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017328 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017329 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017330 dict_T *d;
17331 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017332 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017333
Bram Moolenaar8c711452005-01-14 21:53:12 +000017334 if (argvars[0].v_type == VAR_DICT)
17335 {
17336 if (argvars[2].v_type != VAR_UNKNOWN)
17337 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017338 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017339 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017341 key = get_tv_string_chk(&argvars[1]);
17342 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017344 di = dict_find(d, key, -1);
17345 if (di == NULL)
17346 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017347 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17348 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 {
17350 *rettv = di->di_tv;
17351 init_tv(&di->di_tv);
17352 dictitem_remove(d, di);
17353 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017355 }
17356 }
17357 else if (argvars[0].v_type != VAR_LIST)
17358 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017359 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017360 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017362 int error = FALSE;
17363
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017364 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 if (error)
17366 ; /* type error: do nothing, errmsg already given */
17367 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017368 EMSGN(_(e_listidx), idx);
17369 else
17370 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017371 if (argvars[2].v_type == VAR_UNKNOWN)
17372 {
17373 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017374 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017375 *rettv = item->li_tv;
17376 vim_free(item);
17377 }
17378 else
17379 {
17380 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017381 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017382 if (error)
17383 ; /* type error: do nothing */
17384 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017385 EMSGN(_(e_listidx), end);
17386 else
17387 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017388 int cnt = 0;
17389
17390 for (li = item; li != NULL; li = li->li_next)
17391 {
17392 ++cnt;
17393 if (li == item2)
17394 break;
17395 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017396 if (li == NULL) /* didn't find "item2" after "item" */
17397 EMSG(_(e_invrange));
17398 else
17399 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017400 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017401 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017402 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017403 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017404 l->lv_first = item;
17405 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017406 item->li_prev = NULL;
17407 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017408 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017409 }
17410 }
17411 }
17412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017413 }
17414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415}
17416
17417/*
17418 * "rename({from}, {to})" function
17419 */
17420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017421f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422{
17423 char_u buf[NUMBUFLEN];
17424
17425 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017426 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17429 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430}
17431
17432/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017433 * "repeat()" function
17434 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017436f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017437{
17438 char_u *p;
17439 int n;
17440 int slen;
17441 int len;
17442 char_u *r;
17443 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017444
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017445 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017446 if (argvars[0].v_type == VAR_LIST)
17447 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017448 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017449 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017450 if (list_extend(rettv->vval.v_list,
17451 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017452 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017453 }
17454 else
17455 {
17456 p = get_tv_string(&argvars[0]);
17457 rettv->v_type = VAR_STRING;
17458 rettv->vval.v_string = NULL;
17459
17460 slen = (int)STRLEN(p);
17461 len = slen * n;
17462 if (len <= 0)
17463 return;
17464
17465 r = alloc(len + 1);
17466 if (r != NULL)
17467 {
17468 for (i = 0; i < n; i++)
17469 mch_memmove(r + i * slen, p, (size_t)slen);
17470 r[len] = NUL;
17471 }
17472
17473 rettv->vval.v_string = r;
17474 }
17475}
17476
17477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 * "resolve()" function
17479 */
17480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017481f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482{
17483 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017484#ifdef HAVE_READLINK
17485 char_u *buf = NULL;
17486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489#ifdef FEAT_SHORTCUT
17490 {
17491 char_u *v = NULL;
17492
17493 v = mch_resolve_shortcut(p);
17494 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017497 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498 }
17499#else
17500# ifdef HAVE_READLINK
17501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 char_u *cpy;
17503 int len;
17504 char_u *remain = NULL;
17505 char_u *q;
17506 int is_relative_to_current = FALSE;
17507 int has_trailing_pathsep = FALSE;
17508 int limit = 100;
17509
17510 p = vim_strsave(p);
17511
17512 if (p[0] == '.' && (vim_ispathsep(p[1])
17513 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17514 is_relative_to_current = TRUE;
17515
17516 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017517 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017520 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522
17523 q = getnextcomp(p);
17524 if (*q != NUL)
17525 {
17526 /* Separate the first path component in "p", and keep the
17527 * remainder (beginning with the path separator). */
17528 remain = vim_strsave(q - 1);
17529 q[-1] = NUL;
17530 }
17531
Bram Moolenaard9462e32011-04-11 21:35:11 +020017532 buf = alloc(MAXPATHL + 1);
17533 if (buf == NULL)
17534 goto fail;
17535
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 for (;;)
17537 {
17538 for (;;)
17539 {
17540 len = readlink((char *)p, (char *)buf, MAXPATHL);
17541 if (len <= 0)
17542 break;
17543 buf[len] = NUL;
17544
17545 if (limit-- == 0)
17546 {
17547 vim_free(p);
17548 vim_free(remain);
17549 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 goto fail;
17552 }
17553
17554 /* Ensure that the result will have a trailing path separator
17555 * if the argument has one. */
17556 if (remain == NULL && has_trailing_pathsep)
17557 add_pathsep(buf);
17558
17559 /* Separate the first path component in the link value and
17560 * concatenate the remainders. */
17561 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17562 if (*q != NUL)
17563 {
17564 if (remain == NULL)
17565 remain = vim_strsave(q - 1);
17566 else
17567 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017568 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017569 if (cpy != NULL)
17570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 vim_free(remain);
17572 remain = cpy;
17573 }
17574 }
17575 q[-1] = NUL;
17576 }
17577
17578 q = gettail(p);
17579 if (q > p && *q == NUL)
17580 {
17581 /* Ignore trailing path separator. */
17582 q[-1] = NUL;
17583 q = gettail(p);
17584 }
17585 if (q > p && !mch_isFullName(buf))
17586 {
17587 /* symlink is relative to directory of argument */
17588 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17589 if (cpy != NULL)
17590 {
17591 STRCPY(cpy, p);
17592 STRCPY(gettail(cpy), buf);
17593 vim_free(p);
17594 p = cpy;
17595 }
17596 }
17597 else
17598 {
17599 vim_free(p);
17600 p = vim_strsave(buf);
17601 }
17602 }
17603
17604 if (remain == NULL)
17605 break;
17606
17607 /* Append the first path component of "remain" to "p". */
17608 q = getnextcomp(remain + 1);
17609 len = q - remain - (*q != NUL);
17610 cpy = vim_strnsave(p, STRLEN(p) + len);
17611 if (cpy != NULL)
17612 {
17613 STRNCAT(cpy, remain, len);
17614 vim_free(p);
17615 p = cpy;
17616 }
17617 /* Shorten "remain". */
17618 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017619 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017620 else
17621 {
17622 vim_free(remain);
17623 remain = NULL;
17624 }
17625 }
17626
17627 /* If the result is a relative path name, make it explicitly relative to
17628 * the current directory if and only if the argument had this form. */
17629 if (!vim_ispathsep(*p))
17630 {
17631 if (is_relative_to_current
17632 && *p != NUL
17633 && !(p[0] == '.'
17634 && (p[1] == NUL
17635 || vim_ispathsep(p[1])
17636 || (p[1] == '.'
17637 && (p[2] == NUL
17638 || vim_ispathsep(p[2]))))))
17639 {
17640 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017641 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 if (cpy != NULL)
17643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 vim_free(p);
17645 p = cpy;
17646 }
17647 }
17648 else if (!is_relative_to_current)
17649 {
17650 /* Strip leading "./". */
17651 q = p;
17652 while (q[0] == '.' && vim_ispathsep(q[1]))
17653 q += 2;
17654 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017655 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 }
17657 }
17658
17659 /* Ensure that the result will have no trailing path separator
17660 * if the argument had none. But keep "/" or "//". */
17661 if (!has_trailing_pathsep)
17662 {
17663 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017664 if (after_pathsep(p, q))
17665 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 }
17667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017668 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017669 }
17670# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672# endif
17673#endif
17674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017675 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676
17677#ifdef HAVE_READLINK
17678fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017679 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017681 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682}
17683
17684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 */
17687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017688f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689{
Bram Moolenaar33570922005-01-25 22:26:29 +000017690 list_T *l;
17691 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692
Bram Moolenaar0d660222005-01-07 21:51:51 +000017693 if (argvars[0].v_type != VAR_LIST)
17694 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017695 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017696 && !tv_check_lock(l->lv_lock,
17697 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698 {
17699 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017700 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017701 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702 while (li != NULL)
17703 {
17704 ni = li->li_prev;
17705 list_append(l, li);
17706 li = ni;
17707 }
17708 rettv->vval.v_list = l;
17709 rettv->v_type = VAR_LIST;
17710 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017711 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713}
17714
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017715#define SP_NOMOVE 0x01 /* don't move cursor */
17716#define SP_REPEAT 0x02 /* repeat to find outer pair */
17717#define SP_RETCOUNT 0x04 /* return matchcount */
17718#define SP_SETPCMARK 0x08 /* set previous context mark */
17719#define SP_START 0x10 /* accept match at start position */
17720#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17721#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017722#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017723
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017724static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017725
17726/*
17727 * Get flags for a search function.
17728 * Possibly sets "p_ws".
17729 * Returns BACKWARD, FORWARD or zero (for an error).
17730 */
17731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017732get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733{
17734 int dir = FORWARD;
17735 char_u *flags;
17736 char_u nbuf[NUMBUFLEN];
17737 int mask;
17738
17739 if (varp->v_type != VAR_UNKNOWN)
17740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017741 flags = get_tv_string_buf_chk(varp, nbuf);
17742 if (flags == NULL)
17743 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017744 while (*flags != NUL)
17745 {
17746 switch (*flags)
17747 {
17748 case 'b': dir = BACKWARD; break;
17749 case 'w': p_ws = TRUE; break;
17750 case 'W': p_ws = FALSE; break;
17751 default: mask = 0;
17752 if (flagsp != NULL)
17753 switch (*flags)
17754 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017755 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017756 case 'e': mask = SP_END; break;
17757 case 'm': mask = SP_RETCOUNT; break;
17758 case 'n': mask = SP_NOMOVE; break;
17759 case 'p': mask = SP_SUBPAT; break;
17760 case 'r': mask = SP_REPEAT; break;
17761 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017762 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763 }
17764 if (mask == 0)
17765 {
17766 EMSG2(_(e_invarg2), flags);
17767 dir = 0;
17768 }
17769 else
17770 *flagsp |= mask;
17771 }
17772 if (dir == 0)
17773 break;
17774 ++flags;
17775 }
17776 }
17777 return dir;
17778}
17779
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017781 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017784search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017786 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 char_u *pat;
17788 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017789 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017790 int save_p_ws = p_ws;
17791 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017792 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017793 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017794 proftime_T tm;
17795#ifdef FEAT_RELTIME
17796 long time_limit = 0;
17797#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017798 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017799 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017801 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017802 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017803 if (dir == 0)
17804 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017805 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017806 if (flags & SP_START)
17807 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017808 if (flags & SP_END)
17809 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017810 if (flags & SP_COLUMN)
17811 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017812
Bram Moolenaar76929292008-01-06 19:07:36 +000017813 /* Optional arguments: line number to stop searching and timeout. */
17814 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017815 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017816 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017817 if (lnum_stop < 0)
17818 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017819#ifdef FEAT_RELTIME
17820 if (argvars[3].v_type != VAR_UNKNOWN)
17821 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017822 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017823 if (time_limit < 0)
17824 goto theend;
17825 }
17826#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017827 }
17828
Bram Moolenaar76929292008-01-06 19:07:36 +000017829#ifdef FEAT_RELTIME
17830 /* Set the time limit, if there is one. */
17831 profile_setlimit(time_limit, &tm);
17832#endif
17833
Bram Moolenaar231334e2005-07-25 20:46:57 +000017834 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017835 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017836 * Check to make sure only those flags are set.
17837 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17838 * flags cannot be set. Check for that condition also.
17839 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017840 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017841 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017843 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017844 goto theend;
17845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017846
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017847 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017848 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017849 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017850 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017852 if (flags & SP_SUBPAT)
17853 retval = subpatnum;
17854 else
17855 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017856 if (flags & SP_SETPCMARK)
17857 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017859 if (match_pos != NULL)
17860 {
17861 /* Store the match cursor position */
17862 match_pos->lnum = pos.lnum;
17863 match_pos->col = pos.col + 1;
17864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 /* "/$" will put the cursor after the end of the line, may need to
17866 * correct that here */
17867 check_cursor();
17868 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017869
17870 /* If 'n' flag is used: restore cursor position. */
17871 if (flags & SP_NOMOVE)
17872 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017873 else
17874 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017875theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017876 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017877
17878 return retval;
17879}
17880
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017881#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017882
17883/*
17884 * round() is not in C90, use ceil() or floor() instead.
17885 */
17886 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017887vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017888{
17889 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17890}
17891
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017892/*
17893 * "round({float})" function
17894 */
17895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017896f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017897{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017898 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017899
17900 rettv->v_type = VAR_FLOAT;
17901 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017902 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017903 else
17904 rettv->vval.v_float = 0.0;
17905}
17906#endif
17907
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017908/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017909 * "screenattr()" function
17910 */
17911 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017912f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017913{
17914 int row;
17915 int col;
17916 int c;
17917
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017918 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17919 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017920 if (row < 0 || row >= screen_Rows
17921 || col < 0 || col >= screen_Columns)
17922 c = -1;
17923 else
17924 c = ScreenAttrs[LineOffset[row] + col];
17925 rettv->vval.v_number = c;
17926}
17927
17928/*
17929 * "screenchar()" function
17930 */
17931 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017932f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017933{
17934 int row;
17935 int col;
17936 int off;
17937 int c;
17938
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017939 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17940 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017941 if (row < 0 || row >= screen_Rows
17942 || col < 0 || col >= screen_Columns)
17943 c = -1;
17944 else
17945 {
17946 off = LineOffset[row] + col;
17947#ifdef FEAT_MBYTE
17948 if (enc_utf8 && ScreenLinesUC[off] != 0)
17949 c = ScreenLinesUC[off];
17950 else
17951#endif
17952 c = ScreenLines[off];
17953 }
17954 rettv->vval.v_number = c;
17955}
17956
17957/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017958 * "screencol()" function
17959 *
17960 * First column is 1 to be consistent with virtcol().
17961 */
17962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017963f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017964{
17965 rettv->vval.v_number = screen_screencol() + 1;
17966}
17967
17968/*
17969 * "screenrow()" function
17970 */
17971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017972f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017973{
17974 rettv->vval.v_number = screen_screenrow() + 1;
17975}
17976
17977/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017978 * "search()" function
17979 */
17980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017981f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017983 int flags = 0;
17984
17985 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986}
17987
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017989 * "searchdecl()" function
17990 */
17991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017992f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017993{
17994 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017995 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017996 int error = FALSE;
17997 char_u *name;
17998
17999 rettv->vval.v_number = 1; /* default: FAIL */
18000
18001 name = get_tv_string_chk(&argvars[0]);
18002 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018003 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018004 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018005 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018006 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018007 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018008 if (!error && name != NULL)
18009 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018010 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018011}
18012
18013/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018014 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018017searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018{
18019 char_u *spat, *mpat, *epat;
18020 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 int dir;
18023 int flags = 0;
18024 char_u nbuf1[NUMBUFLEN];
18025 char_u nbuf2[NUMBUFLEN];
18026 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018027 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018028 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018029 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018032 spat = get_tv_string_chk(&argvars[0]);
18033 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18034 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18035 if (spat == NULL || mpat == NULL || epat == NULL)
18036 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 /* Handle the optional fourth argument: flags */
18039 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018040 if (dir == 0)
18041 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018042
18043 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018044 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18045 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018046 if ((flags & (SP_END | SP_SUBPAT)) != 0
18047 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018048 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018049 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018050 goto theend;
18051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018053 /* Using 'r' implies 'W', otherwise it doesn't work. */
18054 if (flags & SP_REPEAT)
18055 p_ws = FALSE;
18056
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018057 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018058 if (argvars[3].v_type == VAR_UNKNOWN
18059 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 skip = (char_u *)"";
18061 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018063 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018064 if (argvars[5].v_type != VAR_UNKNOWN)
18065 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018066 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018067 if (lnum_stop < 0)
18068 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018069#ifdef FEAT_RELTIME
18070 if (argvars[6].v_type != VAR_UNKNOWN)
18071 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018072 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018073 if (time_limit < 0)
18074 goto theend;
18075 }
18076#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018077 }
18078 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018079 if (skip == NULL)
18080 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018082 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018083 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018084
18085theend:
18086 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018087
18088 return retval;
18089}
18090
18091/*
18092 * "searchpair()" function
18093 */
18094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018096{
18097 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18098}
18099
18100/*
18101 * "searchpairpos()" function
18102 */
18103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018104f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018105{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018106 pos_T match_pos;
18107 int lnum = 0;
18108 int col = 0;
18109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018111 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018112
18113 if (searchpair_cmn(argvars, &match_pos) > 0)
18114 {
18115 lnum = match_pos.lnum;
18116 col = match_pos.col;
18117 }
18118
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018119 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18120 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018121}
18122
18123/*
18124 * Search for a start/middle/end thing.
18125 * Used by searchpair(), see its documentation for the details.
18126 * Returns 0 or -1 for no match,
18127 */
18128 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018129do_searchpair(
18130 char_u *spat, /* start pattern */
18131 char_u *mpat, /* middle pattern */
18132 char_u *epat, /* end pattern */
18133 int dir, /* BACKWARD or FORWARD */
18134 char_u *skip, /* skip expression */
18135 int flags, /* SP_SETPCMARK and other SP_ values */
18136 pos_T *match_pos,
18137 linenr_T lnum_stop, /* stop at this line if not zero */
18138 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018139{
18140 char_u *save_cpo;
18141 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18142 long retval = 0;
18143 pos_T pos;
18144 pos_T firstpos;
18145 pos_T foundpos;
18146 pos_T save_cursor;
18147 pos_T save_pos;
18148 int n;
18149 int r;
18150 int nest = 1;
18151 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018152 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018153 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018154
18155 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18156 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018157 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018158
Bram Moolenaar76929292008-01-06 19:07:36 +000018159#ifdef FEAT_RELTIME
18160 /* Set the time limit, if there is one. */
18161 profile_setlimit(time_limit, &tm);
18162#endif
18163
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018164 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18165 * start/middle/end (pat3, for the top pair). */
18166 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18167 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18168 if (pat2 == NULL || pat3 == NULL)
18169 goto theend;
18170 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18171 if (*mpat == NUL)
18172 STRCPY(pat3, pat2);
18173 else
18174 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18175 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018176 if (flags & SP_START)
18177 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018178
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 save_cursor = curwin->w_cursor;
18180 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018181 clearpos(&firstpos);
18182 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 pat = pat3;
18184 for (;;)
18185 {
18186 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018187 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18189 /* didn't find it or found the first match again: FAIL */
18190 break;
18191
18192 if (firstpos.lnum == 0)
18193 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018194 if (equalpos(pos, foundpos))
18195 {
18196 /* Found the same position again. Can happen with a pattern that
18197 * has "\zs" at the end and searching backwards. Advance one
18198 * character and try again. */
18199 if (dir == BACKWARD)
18200 decl(&pos);
18201 else
18202 incl(&pos);
18203 }
18204 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018206 /* clear the start flag to avoid getting stuck here */
18207 options &= ~SEARCH_START;
18208
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 /* If the skip pattern matches, ignore this match. */
18210 if (*skip != NUL)
18211 {
18212 save_pos = curwin->w_cursor;
18213 curwin->w_cursor = pos;
18214 r = eval_to_bool(skip, &err, NULL, FALSE);
18215 curwin->w_cursor = save_pos;
18216 if (err)
18217 {
18218 /* Evaluating {skip} caused an error, break here. */
18219 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018220 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 break;
18222 }
18223 if (r)
18224 continue;
18225 }
18226
18227 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18228 {
18229 /* Found end when searching backwards or start when searching
18230 * forward: nested pair. */
18231 ++nest;
18232 pat = pat2; /* nested, don't search for middle */
18233 }
18234 else
18235 {
18236 /* Found end when searching forward or start when searching
18237 * backward: end of (nested) pair; or found middle in outer pair. */
18238 if (--nest == 1)
18239 pat = pat3; /* outer level, search for middle */
18240 }
18241
18242 if (nest == 0)
18243 {
18244 /* Found the match: return matchcount or line number. */
18245 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018246 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018248 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018249 if (flags & SP_SETPCMARK)
18250 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 curwin->w_cursor = pos;
18252 if (!(flags & SP_REPEAT))
18253 break;
18254 nest = 1; /* search for next unmatched */
18255 }
18256 }
18257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018258 if (match_pos != NULL)
18259 {
18260 /* Store the match cursor position */
18261 match_pos->lnum = curwin->w_cursor.lnum;
18262 match_pos->col = curwin->w_cursor.col + 1;
18263 }
18264
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018266 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 curwin->w_cursor = save_cursor;
18268
18269theend:
18270 vim_free(pat2);
18271 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018272 if (p_cpo == empty_option)
18273 p_cpo = save_cpo;
18274 else
18275 /* Darn, evaluating the {skip} expression changed the value. */
18276 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018277
18278 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279}
18280
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018281/*
18282 * "searchpos()" function
18283 */
18284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018285f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018286{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018287 pos_T match_pos;
18288 int lnum = 0;
18289 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018290 int n;
18291 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018292
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018293 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018294 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018295
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018296 n = search_cmn(argvars, &match_pos, &flags);
18297 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018298 {
18299 lnum = match_pos.lnum;
18300 col = match_pos.col;
18301 }
18302
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018303 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18304 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018305 if (flags & SP_SUBPAT)
18306 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018307}
18308
Bram Moolenaar0d660222005-01-07 21:51:51 +000018309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018310f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018312#ifdef FEAT_CLIENTSERVER
18313 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018314 char_u *server = get_tv_string_chk(&argvars[0]);
18315 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018318 if (server == NULL || reply == NULL)
18319 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018320 if (check_restricted() || check_secure())
18321 return;
18322# ifdef FEAT_X11
18323 if (check_connection() == FAIL)
18324 return;
18325# endif
18326
18327 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018329 EMSG(_("E258: Unable to send to client"));
18330 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018332 rettv->vval.v_number = 0;
18333#else
18334 rettv->vval.v_number = -1;
18335#endif
18336}
18337
Bram Moolenaar0d660222005-01-07 21:51:51 +000018338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018339f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018340{
18341 char_u *r = NULL;
18342
18343#ifdef FEAT_CLIENTSERVER
18344# ifdef WIN32
18345 r = serverGetVimNames();
18346# else
18347 make_connection();
18348 if (X_DISPLAY != NULL)
18349 r = serverGetVimNames(X_DISPLAY);
18350# endif
18351#endif
18352 rettv->v_type = VAR_STRING;
18353 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354}
18355
18356/*
18357 * "setbufvar()" function
18358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018360f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361{
18362 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018365 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 char_u nbuf[NUMBUFLEN];
18367
18368 if (check_restricted() || check_secure())
18369 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018370 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18371 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018372 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 varp = &argvars[2];
18374
18375 if (buf != NULL && varname != NULL && varp != NULL)
18376 {
18377 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379
18380 if (*varname == '&')
18381 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018382 long numval;
18383 char_u *strval;
18384 int error = FALSE;
18385
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018387 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018388 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018389 if (!error && strval != NULL)
18390 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 }
18392 else
18393 {
18394 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18395 if (bufvarname != NULL)
18396 {
18397 STRCPY(bufvarname, "b:");
18398 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018399 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 vim_free(bufvarname);
18401 }
18402 }
18403
18404 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407}
18408
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018410f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018411{
18412 dict_T *d;
18413 dictitem_T *di;
18414 char_u *csearch;
18415
18416 if (argvars[0].v_type != VAR_DICT)
18417 {
18418 EMSG(_(e_dictreq));
18419 return;
18420 }
18421
18422 if ((d = argvars[0].vval.v_dict) != NULL)
18423 {
18424 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18425 if (csearch != NULL)
18426 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018427#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018428 if (enc_utf8)
18429 {
18430 int pcc[MAX_MCO];
18431 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018432
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018433 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18434 }
18435 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018436#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018437 set_last_csearch(PTR2CHAR(csearch),
18438 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018439 }
18440
18441 di = dict_find(d, (char_u *)"forward", -1);
18442 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018443 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018444 ? FORWARD : BACKWARD);
18445
18446 di = dict_find(d, (char_u *)"until", -1);
18447 if (di != NULL)
18448 set_csearch_until(!!get_tv_number(&di->di_tv));
18449 }
18450}
18451
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452/*
18453 * "setcmdpos()" function
18454 */
18455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018456f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018458 int pos = (int)get_tv_number(&argvars[0]) - 1;
18459
18460 if (pos >= 0)
18461 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462}
18463
18464/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018465 * "setfperm({fname}, {mode})" function
18466 */
18467 static void
18468f_setfperm(typval_T *argvars, typval_T *rettv)
18469{
18470 char_u *fname;
18471 char_u modebuf[NUMBUFLEN];
18472 char_u *mode_str;
18473 int i;
18474 int mask;
18475 int mode = 0;
18476
18477 rettv->vval.v_number = 0;
18478 fname = get_tv_string_chk(&argvars[0]);
18479 if (fname == NULL)
18480 return;
18481 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18482 if (mode_str == NULL)
18483 return;
18484 if (STRLEN(mode_str) != 9)
18485 {
18486 EMSG2(_(e_invarg2), mode_str);
18487 return;
18488 }
18489
18490 mask = 1;
18491 for (i = 8; i >= 0; --i)
18492 {
18493 if (mode_str[i] != '-')
18494 mode |= mask;
18495 mask = mask << 1;
18496 }
18497 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18498}
18499
18500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 * "setline()" function
18502 */
18503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018504f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505{
18506 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018507 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018508 list_T *l = NULL;
18509 listitem_T *li = NULL;
18510 long added = 0;
18511 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018513 lnum = get_tv_lnum(&argvars[0]);
18514 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018516 l = argvars[1].vval.v_list;
18517 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018519 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018520 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018521
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018522 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018523 for (;;)
18524 {
18525 if (l != NULL)
18526 {
18527 /* list argument, get next string */
18528 if (li == NULL)
18529 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018530 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018531 li = li->li_next;
18532 }
18533
18534 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018535 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018536 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018537
18538 /* When coming here from Insert mode, sync undo, so that this can be
18539 * undone separately from what was previously inserted. */
18540 if (u_sync_once == 2)
18541 {
18542 u_sync_once = 1; /* notify that u_sync() was called */
18543 u_sync(TRUE);
18544 }
18545
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018546 if (lnum <= curbuf->b_ml.ml_line_count)
18547 {
18548 /* existing line, replace it */
18549 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18550 {
18551 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018552 if (lnum == curwin->w_cursor.lnum)
18553 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018554 rettv->vval.v_number = 0; /* OK */
18555 }
18556 }
18557 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18558 {
18559 /* lnum is one past the last line, append the line */
18560 ++added;
18561 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18562 rettv->vval.v_number = 0; /* OK */
18563 }
18564
18565 if (l == NULL) /* only one string argument */
18566 break;
18567 ++lnum;
18568 }
18569
18570 if (added > 0)
18571 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572}
18573
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018574static 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 +000018575
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018577 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018578 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018580set_qf_ll_list(
18581 win_T *wp UNUSED,
18582 typval_T *list_arg UNUSED,
18583 typval_T *action_arg UNUSED,
18584 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018585{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018586#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018587 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018588 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018589 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018590#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018591
Bram Moolenaar2641f772005-03-25 21:58:17 +000018592 rettv->vval.v_number = -1;
18593
18594#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018595 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018596 EMSG(_(e_listreq));
18597 else
18598 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018599 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018600
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018601 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018602 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018603 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 if (act == NULL)
18605 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018606 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018607 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018608 else
18609 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018610 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018611 else if (action_arg->v_type == VAR_UNKNOWN)
18612 action = ' ';
18613 else
18614 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018615
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018616 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018617 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018618 rettv->vval.v_number = 0;
18619 }
18620#endif
18621}
18622
18623/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018624 * "setloclist()" function
18625 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018627f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018628{
18629 win_T *win;
18630
18631 rettv->vval.v_number = -1;
18632
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018633 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018634 if (win != NULL)
18635 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18636}
18637
18638/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018639 * "setmatches()" function
18640 */
18641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018642f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018643{
18644#ifdef FEAT_SEARCH_EXTRA
18645 list_T *l;
18646 listitem_T *li;
18647 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018648 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018649
18650 rettv->vval.v_number = -1;
18651 if (argvars[0].v_type != VAR_LIST)
18652 {
18653 EMSG(_(e_listreq));
18654 return;
18655 }
18656 if ((l = argvars[0].vval.v_list) != NULL)
18657 {
18658
18659 /* To some extent make sure that we are dealing with a list from
18660 * "getmatches()". */
18661 li = l->lv_first;
18662 while (li != NULL)
18663 {
18664 if (li->li_tv.v_type != VAR_DICT
18665 || (d = li->li_tv.vval.v_dict) == NULL)
18666 {
18667 EMSG(_(e_invarg));
18668 return;
18669 }
18670 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018671 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18672 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018673 && dict_find(d, (char_u *)"priority", -1) != NULL
18674 && dict_find(d, (char_u *)"id", -1) != NULL))
18675 {
18676 EMSG(_(e_invarg));
18677 return;
18678 }
18679 li = li->li_next;
18680 }
18681
18682 clear_matches(curwin);
18683 li = l->lv_first;
18684 while (li != NULL)
18685 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018686 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018687 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018688 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018689 char_u *group;
18690 int priority;
18691 int id;
18692 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018693
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018694 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018695 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18696 {
18697 if (s == NULL)
18698 {
18699 s = list_alloc();
18700 if (s == NULL)
18701 return;
18702 }
18703
18704 /* match from matchaddpos() */
18705 for (i = 1; i < 9; i++)
18706 {
18707 sprintf((char *)buf, (char *)"pos%d", i);
18708 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18709 {
18710 if (di->di_tv.v_type != VAR_LIST)
18711 return;
18712
18713 list_append_tv(s, &di->di_tv);
18714 s->lv_refcount++;
18715 }
18716 else
18717 break;
18718 }
18719 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018720
18721 group = get_dict_string(d, (char_u *)"group", FALSE);
18722 priority = (int)get_dict_number(d, (char_u *)"priority");
18723 id = (int)get_dict_number(d, (char_u *)"id");
18724 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18725 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18726 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018727 if (i == 0)
18728 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018729 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018730 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018731 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018732 }
18733 else
18734 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018735 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018736 list_unref(s);
18737 s = NULL;
18738 }
18739
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018740 li = li->li_next;
18741 }
18742 rettv->vval.v_number = 0;
18743 }
18744#endif
18745}
18746
18747/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018748 * "setpos()" function
18749 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018751f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018752{
18753 pos_T pos;
18754 int fnum;
18755 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018756 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018757
Bram Moolenaar08250432008-02-13 11:42:46 +000018758 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018759 name = get_tv_string_chk(argvars);
18760 if (name != NULL)
18761 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018762 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018763 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018764 if (--pos.col < 0)
18765 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018766 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018767 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018768 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018769 if (fnum == curbuf->b_fnum)
18770 {
18771 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018772 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018773 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018774 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018775 curwin->w_set_curswant = FALSE;
18776 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018777 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018778 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018779 }
18780 else
18781 EMSG(_(e_invarg));
18782 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018783 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18784 {
18785 /* set mark */
18786 if (setmark_pos(name[1], &pos, fnum) == OK)
18787 rettv->vval.v_number = 0;
18788 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018789 else
18790 EMSG(_(e_invarg));
18791 }
18792 }
18793}
18794
18795/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018796 * "setqflist()" function
18797 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018799f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018800{
18801 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18802}
18803
18804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 * "setreg()" function
18806 */
18807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018808f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809{
18810 int regname;
18811 char_u *strregname;
18812 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018813 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 int append;
18815 char_u yank_type;
18816 long block_len;
18817
18818 block_len = -1;
18819 yank_type = MAUTO;
18820 append = FALSE;
18821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018822 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018823 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018825 if (strregname == NULL)
18826 return; /* type error; errmsg already given */
18827 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 if (regname == 0 || regname == '@')
18829 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018831 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018833 stropt = get_tv_string_chk(&argvars[2]);
18834 if (stropt == NULL)
18835 return; /* type error */
18836 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 switch (*stropt)
18838 {
18839 case 'a': case 'A': /* append */
18840 append = TRUE;
18841 break;
18842 case 'v': case 'c': /* character-wise selection */
18843 yank_type = MCHAR;
18844 break;
18845 case 'V': case 'l': /* line-wise selection */
18846 yank_type = MLINE;
18847 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848 case 'b': case Ctrl_V: /* block-wise selection */
18849 yank_type = MBLOCK;
18850 if (VIM_ISDIGIT(stropt[1]))
18851 {
18852 ++stropt;
18853 block_len = getdigits(&stropt) - 1;
18854 --stropt;
18855 }
18856 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 }
18858 }
18859
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018860 if (argvars[1].v_type == VAR_LIST)
18861 {
18862 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018863 char_u **allocval;
18864 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018865 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018866 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018867 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018868 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018869 int len;
18870
18871 /* If the list is NULL handle like an empty list. */
18872 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018873
Bram Moolenaar7d647822014-04-05 21:28:56 +020018874 /* First half: use for pointers to result lines; second half: use for
18875 * pointers to allocated copies. */
18876 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018877 if (lstval == NULL)
18878 return;
18879 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018880 allocval = lstval + len + 2;
18881 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018882
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018883 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018884 li = li->li_next)
18885 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018886 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018887 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018888 goto free_lstval;
18889 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018890 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018891 /* Need to make a copy, next get_tv_string_buf_chk() will
18892 * overwrite the string. */
18893 strval = vim_strsave(buf);
18894 if (strval == NULL)
18895 goto free_lstval;
18896 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018897 }
18898 *curval++ = strval;
18899 }
18900 *curval++ = NULL;
18901
18902 write_reg_contents_lst(regname, lstval, -1,
18903 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018904free_lstval:
18905 while (curallocval > allocval)
18906 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018907 vim_free(lstval);
18908 }
18909 else
18910 {
18911 strval = get_tv_string_chk(&argvars[1]);
18912 if (strval == NULL)
18913 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018914 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018917 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918}
18919
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018920/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018921 * "settabvar()" function
18922 */
18923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018924f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018925{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018926#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018927 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018928 tabpage_T *tp;
18929#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018930 char_u *varname, *tabvarname;
18931 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018932
18933 rettv->vval.v_number = 0;
18934
18935 if (check_restricted() || check_secure())
18936 return;
18937
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018938#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018939 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018940#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018941 varname = get_tv_string_chk(&argvars[1]);
18942 varp = &argvars[2];
18943
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018944 if (varname != NULL && varp != NULL
18945#ifdef FEAT_WINDOWS
18946 && tp != NULL
18947#endif
18948 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018949 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018950#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018951 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018952 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018953#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018954
18955 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18956 if (tabvarname != NULL)
18957 {
18958 STRCPY(tabvarname, "t:");
18959 STRCPY(tabvarname + 2, varname);
18960 set_var(tabvarname, varp, TRUE);
18961 vim_free(tabvarname);
18962 }
18963
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018964#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018965 /* Restore current tabpage */
18966 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018967 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018968#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018969 }
18970}
18971
18972/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018973 * "settabwinvar()" function
18974 */
18975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018976f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018977{
18978 setwinvar(argvars, rettv, 1);
18979}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980
18981/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018982 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018985f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018987 setwinvar(argvars, rettv, 0);
18988}
18989
18990/*
18991 * "setwinvar()" and "settabwinvar()" functions
18992 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018993
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018995setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018996{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 win_T *win;
18998#ifdef FEAT_WINDOWS
18999 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019000 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019001 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002#endif
19003 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019004 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019006 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007
19008 if (check_restricted() || check_secure())
19009 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019010
19011#ifdef FEAT_WINDOWS
19012 if (off == 1)
19013 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19014 else
19015 tp = curtab;
19016#endif
19017 win = find_win_by_nr(&argvars[off], tp);
19018 varname = get_tv_string_chk(&argvars[off + 1]);
19019 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
19021 if (win != NULL && varname != NULL && varp != NULL)
19022 {
19023#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019024 need_switch_win = !(tp == curtab && win == curwin);
19025 if (!need_switch_win
19026 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019029 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019031 long numval;
19032 char_u *strval;
19033 int error = FALSE;
19034
19035 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019036 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019037 strval = get_tv_string_buf_chk(varp, nbuf);
19038 if (!error && strval != NULL)
19039 set_option_value(varname, numval, strval, OPT_LOCAL);
19040 }
19041 else
19042 {
19043 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19044 if (winvarname != NULL)
19045 {
19046 STRCPY(winvarname, "w:");
19047 STRCPY(winvarname + 2, varname);
19048 set_var(winvarname, varp, TRUE);
19049 vim_free(winvarname);
19050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
19052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019054 if (need_switch_win)
19055 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056#endif
19057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058}
19059
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019060#ifdef FEAT_CRYPT
19061/*
19062 * "sha256({string})" function
19063 */
19064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019065f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019066{
19067 char_u *p;
19068
19069 p = get_tv_string(&argvars[0]);
19070 rettv->vval.v_string = vim_strsave(
19071 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19072 rettv->v_type = VAR_STRING;
19073}
19074#endif /* FEAT_CRYPT */
19075
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019077 * "shellescape({string})" function
19078 */
19079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019080f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019081{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019082 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019083 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019084 rettv->v_type = VAR_STRING;
19085}
19086
19087/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019088 * shiftwidth() function
19089 */
19090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019091f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019092{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019093 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019094}
19095
19096/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019097 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098 */
19099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019100f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019102 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103
Bram Moolenaar0d660222005-01-07 21:51:51 +000019104 p = get_tv_string(&argvars[0]);
19105 rettv->vval.v_string = vim_strsave(p);
19106 simplify_filename(rettv->vval.v_string); /* simplify in place */
19107 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108}
19109
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019110#ifdef FEAT_FLOAT
19111/*
19112 * "sin()" function
19113 */
19114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019115f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019116{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019117 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019118
19119 rettv->v_type = VAR_FLOAT;
19120 if (get_float_arg(argvars, &f) == OK)
19121 rettv->vval.v_float = sin(f);
19122 else
19123 rettv->vval.v_float = 0.0;
19124}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019125
19126/*
19127 * "sinh()" function
19128 */
19129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019130f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019131{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019132 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019133
19134 rettv->v_type = VAR_FLOAT;
19135 if (get_float_arg(argvars, &f) == OK)
19136 rettv->vval.v_float = sinh(f);
19137 else
19138 rettv->vval.v_float = 0.0;
19139}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019140#endif
19141
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142static int
19143#ifdef __BORLANDC__
19144 _RTLENTRYF
19145#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019146 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019147static int
19148#ifdef __BORLANDC__
19149 _RTLENTRYF
19150#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019151 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019153/* struct used in the array that's given to qsort() */
19154typedef struct
19155{
19156 listitem_T *item;
19157 int idx;
19158} sortItem_T;
19159
Bram Moolenaar0b962472016-02-22 22:51:33 +010019160/* struct storing information about current sort */
19161typedef struct
19162{
19163 int item_compare_ic;
19164 int item_compare_numeric;
19165 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019166#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019168#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019170 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019171 dict_T *item_compare_selfdict;
19172 int item_compare_func_err;
19173 int item_compare_keep_zero;
19174} sortinfo_T;
19175static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019176static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019177#define ITEM_COMPARE_FAIL 999
19178
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019180 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019182 static int
19183#ifdef __BORLANDC__
19184_RTLENTRYF
19185#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019186item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019188 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019189 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019190 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019191 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019192 int res;
19193 char_u numbuf1[NUMBUFLEN];
19194 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019196 si1 = (sortItem_T *)s1;
19197 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019198 tv1 = &si1->item->li_tv;
19199 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019200
Bram Moolenaar0b962472016-02-22 22:51:33 +010019201 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019202 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019203 varnumber_T v1 = get_tv_number(tv1);
19204 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019205
19206 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19207 }
19208
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019209#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019210 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019211 {
19212 float_T v1 = get_tv_float(tv1);
19213 float_T v2 = get_tv_float(tv2);
19214
19215 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19216 }
19217#endif
19218
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019219 /* tv2string() puts quotes around a string and allocates memory. Don't do
19220 * that for string variables. Use a single quote when comparing with a
19221 * non-string to do what the docs promise. */
19222 if (tv1->v_type == VAR_STRING)
19223 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019224 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019225 p1 = (char_u *)"'";
19226 else
19227 p1 = tv1->vval.v_string;
19228 }
19229 else
19230 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19231 if (tv2->v_type == VAR_STRING)
19232 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019233 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019234 p2 = (char_u *)"'";
19235 else
19236 p2 = tv2->vval.v_string;
19237 }
19238 else
19239 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019240 if (p1 == NULL)
19241 p1 = (char_u *)"";
19242 if (p2 == NULL)
19243 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019245 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019246 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019247 res = STRICMP(p1, p2);
19248 else
19249 res = STRCMP(p1, p2);
19250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019252 {
19253 double n1, n2;
19254 n1 = strtod((char *)p1, (char **)&p1);
19255 n2 = strtod((char *)p2, (char **)&p2);
19256 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19257 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019258
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019259 /* When the result would be zero, compare the item indexes. Makes the
19260 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019261 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019262 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019263
Bram Moolenaar0d660222005-01-07 21:51:51 +000019264 vim_free(tofree1);
19265 vim_free(tofree2);
19266 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267}
19268
19269 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019270#ifdef __BORLANDC__
19271_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019273item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019274{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019275 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019276 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019277 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019278 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019280 char_u *func_name;
19281 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019283 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019284 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019285 return 0;
19286
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019287 si1 = (sortItem_T *)s1;
19288 si2 = (sortItem_T *)s2;
19289
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019290 if (partial == NULL)
19291 func_name = sortinfo->item_compare_func;
19292 else
19293 func_name = partial->pt_name;
19294
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019295 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019296 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019297 copy_tv(&si1->item->li_tv, &argv[0]);
19298 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019299
19300 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019301 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019302 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019303 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304 clear_tv(&argv[0]);
19305 clear_tv(&argv[1]);
19306
19307 if (res == FAIL)
19308 res = ITEM_COMPARE_FAIL;
19309 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019310 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019311 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019312 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019313 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019314
19315 /* When the result would be zero, compare the pointers themselves. Makes
19316 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019317 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019318 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019319
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 return res;
19321}
19322
19323/*
19324 * "sort({list})" function
19325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019327do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328{
Bram Moolenaar33570922005-01-25 22:26:29 +000019329 list_T *l;
19330 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019331 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019332 sortinfo_T *old_sortinfo;
19333 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019334 long len;
19335 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336
Bram Moolenaar0b962472016-02-22 22:51:33 +010019337 /* Pointer to current info struct used in compare function. Save and
19338 * restore the current one for nested calls. */
19339 old_sortinfo = sortinfo;
19340 sortinfo = &info;
19341
Bram Moolenaar0d660222005-01-07 21:51:51 +000019342 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019343 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344 else
19345 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019346 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019347 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019348 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19349 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019350 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019351 rettv->vval.v_list = l;
19352 rettv->v_type = VAR_LIST;
19353 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354
Bram Moolenaar0d660222005-01-07 21:51:51 +000019355 len = list_len(l);
19356 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019357 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
Bram Moolenaar0b962472016-02-22 22:51:33 +010019359 info.item_compare_ic = FALSE;
19360 info.item_compare_numeric = FALSE;
19361 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019362#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019363 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019364#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019366 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019367 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019368 if (argvars[1].v_type != VAR_UNKNOWN)
19369 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019370 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019372 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019373 else if (argvars[1].v_type == VAR_PARTIAL)
19374 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375 else
19376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019377 int error = FALSE;
19378
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019379 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019380 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019381 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019382 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019383 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019384 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019385 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019386 else if (i != 0)
19387 {
19388 EMSG(_(e_invarg));
19389 goto theend;
19390 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019391 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019392 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019393 if (*info.item_compare_func == NUL)
19394 {
19395 /* empty string means default sort */
19396 info.item_compare_func = NULL;
19397 }
19398 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019399 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019400 info.item_compare_func = NULL;
19401 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019402 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019403 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019404 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019405 info.item_compare_func = NULL;
19406 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019407 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019408#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019409 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019410 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019411 info.item_compare_func = NULL;
19412 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019413 }
19414#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019415 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019416 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019417 info.item_compare_func = NULL;
19418 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019419 }
19420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019421 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019422
19423 if (argvars[2].v_type != VAR_UNKNOWN)
19424 {
19425 /* optional third argument: {dict} */
19426 if (argvars[2].v_type != VAR_DICT)
19427 {
19428 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019429 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019430 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019431 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019432 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434
Bram Moolenaar0d660222005-01-07 21:51:51 +000019435 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019436 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019438 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439
Bram Moolenaar327aa022014-03-25 18:24:23 +010019440 i = 0;
19441 if (sort)
19442 {
19443 /* sort(): ptrs will be the list to sort */
19444 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019445 {
19446 ptrs[i].item = li;
19447 ptrs[i].idx = i;
19448 ++i;
19449 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019450
Bram Moolenaar0b962472016-02-22 22:51:33 +010019451 info.item_compare_func_err = FALSE;
19452 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019453 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019454 if ((info.item_compare_func != NULL
19455 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019456 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019457 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019458 EMSG(_("E702: Sort compare function failed"));
19459 else
19460 {
19461 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019462 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019463 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019464 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019465 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019466
Bram Moolenaar0b962472016-02-22 22:51:33 +010019467 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019468 {
19469 /* Clear the List and append the items in sorted order. */
19470 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19471 l->lv_len = 0;
19472 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019473 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019474 }
19475 }
19476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019478 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019479 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019480
19481 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019482 info.item_compare_func_err = FALSE;
19483 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019484 item_compare_func_ptr = info.item_compare_func != NULL
19485 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019486 ? item_compare2 : item_compare;
19487
19488 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19489 li = li->li_next)
19490 {
19491 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19492 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019493 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019494 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019495 {
19496 EMSG(_("E882: Uniq compare function failed"));
19497 break;
19498 }
19499 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019500
Bram Moolenaar0b962472016-02-22 22:51:33 +010019501 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019502 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019503 while (--i >= 0)
19504 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019505 li = ptrs[i].item->li_next;
19506 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019507 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019508 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019509 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019510 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019511 list_fix_watch(l, li);
19512 listitem_free(li);
19513 l->lv_len--;
19514 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019515 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019516 }
19517
19518 vim_free(ptrs);
19519 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019520theend:
19521 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019522}
19523
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019524/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019525 * "sort({list})" function
19526 */
19527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019528f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019529{
19530 do_sort_uniq(argvars, rettv, TRUE);
19531}
19532
19533/*
19534 * "uniq({list})" function
19535 */
19536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019537f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019538{
19539 do_sort_uniq(argvars, rettv, FALSE);
19540}
19541
19542/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019543 * "soundfold({word})" function
19544 */
19545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019546f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019547{
19548 char_u *s;
19549
19550 rettv->v_type = VAR_STRING;
19551 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019552#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019553 rettv->vval.v_string = eval_soundfold(s);
19554#else
19555 rettv->vval.v_string = vim_strsave(s);
19556#endif
19557}
19558
19559/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019560 * "spellbadword()" function
19561 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019563f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019564{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019565 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019566 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019567 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019568
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019569 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019570 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019571
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019572#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019573 if (argvars[0].v_type == VAR_UNKNOWN)
19574 {
19575 /* Find the start and length of the badly spelled word. */
19576 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19577 if (len != 0)
19578 word = ml_get_cursor();
19579 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019580 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019581 {
19582 char_u *str = get_tv_string_chk(&argvars[0]);
19583 int capcol = -1;
19584
19585 if (str != NULL)
19586 {
19587 /* Check the argument for spelling. */
19588 while (*str != NUL)
19589 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019590 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019591 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019592 {
19593 word = str;
19594 break;
19595 }
19596 str += len;
19597 }
19598 }
19599 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019600#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019602 list_append_string(rettv->vval.v_list, word, len);
19603 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019604 attr == HLF_SPB ? "bad" :
19605 attr == HLF_SPR ? "rare" :
19606 attr == HLF_SPL ? "local" :
19607 attr == HLF_SPC ? "caps" :
19608 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019609}
19610
19611/*
19612 * "spellsuggest()" function
19613 */
19614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019615f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019616{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019617#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019618 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019619 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019620 int maxcount;
19621 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019622 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019623 listitem_T *li;
19624 int need_capital = FALSE;
19625#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019626
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019627 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019628 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019629
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019630#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019631 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019632 {
19633 str = get_tv_string(&argvars[0]);
19634 if (argvars[1].v_type != VAR_UNKNOWN)
19635 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019636 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019637 if (maxcount <= 0)
19638 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019639 if (argvars[2].v_type != VAR_UNKNOWN)
19640 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019641 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019642 if (typeerr)
19643 return;
19644 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019645 }
19646 else
19647 maxcount = 25;
19648
Bram Moolenaar4770d092006-01-12 23:22:24 +000019649 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019650
19651 for (i = 0; i < ga.ga_len; ++i)
19652 {
19653 str = ((char_u **)ga.ga_data)[i];
19654
19655 li = listitem_alloc();
19656 if (li == NULL)
19657 vim_free(str);
19658 else
19659 {
19660 li->li_tv.v_type = VAR_STRING;
19661 li->li_tv.v_lock = 0;
19662 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019663 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019664 }
19665 }
19666 ga_clear(&ga);
19667 }
19668#endif
19669}
19670
Bram Moolenaar0d660222005-01-07 21:51:51 +000019671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019672f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019673{
19674 char_u *str;
19675 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019676 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019677 regmatch_T regmatch;
19678 char_u patbuf[NUMBUFLEN];
19679 char_u *save_cpo;
19680 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019681 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019682 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019683 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019684
19685 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19686 save_cpo = p_cpo;
19687 p_cpo = (char_u *)"";
19688
19689 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019690 if (argvars[1].v_type != VAR_UNKNOWN)
19691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019692 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19693 if (pat == NULL)
19694 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019695 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019696 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019697 }
19698 if (pat == NULL || *pat == NUL)
19699 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019703 if (typeerr)
19704 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705
Bram Moolenaar0d660222005-01-07 21:51:51 +000019706 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19707 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019709 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019710 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019711 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019712 if (*str == NUL)
19713 match = FALSE; /* empty item at the end */
19714 else
19715 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019716 if (match)
19717 end = regmatch.startp[0];
19718 else
19719 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019720 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19721 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019722 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019723 if (list_append_string(rettv->vval.v_list, str,
19724 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019725 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019726 }
19727 if (!match)
19728 break;
19729 /* Advance to just after the match. */
19730 if (regmatch.endp[0] > str)
19731 col = 0;
19732 else
19733 {
19734 /* Don't get stuck at the same match. */
19735#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019736 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019737#else
19738 col = 1;
19739#endif
19740 }
19741 str = regmatch.endp[0];
19742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743
Bram Moolenaar473de612013-06-08 18:19:48 +020019744 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746
Bram Moolenaar0d660222005-01-07 21:51:51 +000019747 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748}
19749
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019750#ifdef FEAT_FLOAT
19751/*
19752 * "sqrt()" function
19753 */
19754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019755f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019756{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019757 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019758
19759 rettv->v_type = VAR_FLOAT;
19760 if (get_float_arg(argvars, &f) == OK)
19761 rettv->vval.v_float = sqrt(f);
19762 else
19763 rettv->vval.v_float = 0.0;
19764}
19765
19766/*
19767 * "str2float()" function
19768 */
19769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019770f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019771{
19772 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19773
19774 if (*p == '+')
19775 p = skipwhite(p + 1);
19776 (void)string2float(p, &rettv->vval.v_float);
19777 rettv->v_type = VAR_FLOAT;
19778}
19779#endif
19780
Bram Moolenaar2c932302006-03-18 21:42:09 +000019781/*
19782 * "str2nr()" function
19783 */
19784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019785f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019786{
19787 int base = 10;
19788 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019789 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019790 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019791
19792 if (argvars[1].v_type != VAR_UNKNOWN)
19793 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019794 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019795 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019796 {
19797 EMSG(_(e_invarg));
19798 return;
19799 }
19800 }
19801
19802 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019803 if (*p == '+')
19804 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019805 switch (base)
19806 {
19807 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19808 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19809 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19810 default: what = 0;
19811 }
19812 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019813 rettv->vval.v_number = n;
19814}
19815
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816#ifdef HAVE_STRFTIME
19817/*
19818 * "strftime({format}[, {time}])" function
19819 */
19820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019821f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822{
19823 char_u result_buf[256];
19824 struct tm *curtime;
19825 time_t seconds;
19826 char_u *p;
19827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019828 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019830 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019831 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 seconds = time(NULL);
19833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 curtime = localtime(&seconds);
19836 /* MSVC returns NULL for an invalid value of seconds. */
19837 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 else
19840 {
19841# ifdef FEAT_MBYTE
19842 vimconv_T conv;
19843 char_u *enc;
19844
19845 conv.vc_type = CONV_NONE;
19846 enc = enc_locale();
19847 convert_setup(&conv, p_enc, enc);
19848 if (conv.vc_type != CONV_NONE)
19849 p = string_convert(&conv, p, NULL);
19850# endif
19851 if (p != NULL)
19852 (void)strftime((char *)result_buf, sizeof(result_buf),
19853 (char *)p, curtime);
19854 else
19855 result_buf[0] = NUL;
19856
19857# ifdef FEAT_MBYTE
19858 if (conv.vc_type != CONV_NONE)
19859 vim_free(p);
19860 convert_setup(&conv, enc, p_enc);
19861 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 else
19864# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019865 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866
19867# ifdef FEAT_MBYTE
19868 /* Release conversion descriptors */
19869 convert_setup(&conv, NULL, NULL);
19870 vim_free(enc);
19871# endif
19872 }
19873}
19874#endif
19875
19876/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019877 * "strgetchar()" function
19878 */
19879 static void
19880f_strgetchar(typval_T *argvars, typval_T *rettv)
19881{
19882 char_u *str;
19883 int len;
19884 int error = FALSE;
19885 int charidx;
19886
19887 rettv->vval.v_number = -1;
19888 str = get_tv_string_chk(&argvars[0]);
19889 if (str == NULL)
19890 return;
19891 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019892 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019893 if (error)
19894 return;
19895#ifdef FEAT_MBYTE
19896 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019897 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019898
19899 while (charidx >= 0 && byteidx < len)
19900 {
19901 if (charidx == 0)
19902 {
19903 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19904 break;
19905 }
19906 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019907 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019908 }
19909 }
19910#else
19911 if (charidx < len)
19912 rettv->vval.v_number = str[charidx];
19913#endif
19914}
19915
19916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917 * "stridx()" function
19918 */
19919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019920f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921{
19922 char_u buf[NUMBUFLEN];
19923 char_u *needle;
19924 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019925 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019929 needle = get_tv_string_chk(&argvars[1]);
19930 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019932 if (needle == NULL || haystack == NULL)
19933 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 if (argvars[2].v_type != VAR_UNKNOWN)
19936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019937 int error = FALSE;
19938
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019939 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019940 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019941 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019942 if (start_idx >= 0)
19943 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019944 }
19945
19946 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19947 if (pos != NULL)
19948 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949}
19950
19951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019952 * "string()" function
19953 */
19954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019955f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956{
19957 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019958 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019960 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019961 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19962 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019963 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019964 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966}
19967
19968/*
19969 * "strlen()" function
19970 */
19971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019972f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019974 rettv->vval.v_number = (varnumber_T)(STRLEN(
19975 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976}
19977
19978/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019979 * "strchars()" function
19980 */
19981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019982f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019983{
19984 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019985 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019986#ifdef FEAT_MBYTE
19987 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019988 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019989#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019990
19991 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019992 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019993 if (skipcc < 0 || skipcc > 1)
19994 EMSG(_(e_invarg));
19995 else
19996 {
19997#ifdef FEAT_MBYTE
19998 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19999 while (*s != NUL)
20000 {
20001 func_mb_ptr2char_adv(&s);
20002 ++len;
20003 }
20004 rettv->vval.v_number = len;
20005#else
20006 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20007#endif
20008 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020009}
20010
20011/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020012 * "strdisplaywidth()" function
20013 */
20014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020015f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020016{
20017 char_u *s = get_tv_string(&argvars[0]);
20018 int col = 0;
20019
20020 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020021 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020022
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020023 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020024}
20025
20026/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020027 * "strwidth()" function
20028 */
20029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020030f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020031{
20032 char_u *s = get_tv_string(&argvars[0]);
20033
20034 rettv->vval.v_number = (varnumber_T)(
20035#ifdef FEAT_MBYTE
20036 mb_string2cells(s, -1)
20037#else
20038 STRLEN(s)
20039#endif
20040 );
20041}
20042
20043/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020044 * "strcharpart()" function
20045 */
20046 static void
20047f_strcharpart(typval_T *argvars, typval_T *rettv)
20048{
20049#ifdef FEAT_MBYTE
20050 char_u *p;
20051 int nchar;
20052 int nbyte = 0;
20053 int charlen;
20054 int len = 0;
20055 int slen;
20056 int error = FALSE;
20057
20058 p = get_tv_string(&argvars[0]);
20059 slen = (int)STRLEN(p);
20060
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020061 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020062 if (!error)
20063 {
20064 if (nchar > 0)
20065 while (nchar > 0 && nbyte < slen)
20066 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020067 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020068 --nchar;
20069 }
20070 else
20071 nbyte = nchar;
20072 if (argvars[2].v_type != VAR_UNKNOWN)
20073 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020074 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020075 while (charlen > 0 && nbyte + len < slen)
20076 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020077 int off = nbyte + len;
20078
20079 if (off < 0)
20080 len += 1;
20081 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020082 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020083 --charlen;
20084 }
20085 }
20086 else
20087 len = slen - nbyte; /* default: all bytes that are available. */
20088 }
20089
20090 /*
20091 * Only return the overlap between the specified part and the actual
20092 * string.
20093 */
20094 if (nbyte < 0)
20095 {
20096 len += nbyte;
20097 nbyte = 0;
20098 }
20099 else if (nbyte > slen)
20100 nbyte = slen;
20101 if (len < 0)
20102 len = 0;
20103 else if (nbyte + len > slen)
20104 len = slen - nbyte;
20105
20106 rettv->v_type = VAR_STRING;
20107 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20108#else
20109 f_strpart(argvars, rettv);
20110#endif
20111}
20112
20113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 * "strpart()" function
20115 */
20116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020117f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118{
20119 char_u *p;
20120 int n;
20121 int len;
20122 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020123 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020125 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 slen = (int)STRLEN(p);
20127
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020128 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020129 if (error)
20130 len = 0;
20131 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020132 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 else
20134 len = slen - n; /* default len: all bytes that are available. */
20135
20136 /*
20137 * Only return the overlap between the specified part and the actual
20138 * string.
20139 */
20140 if (n < 0)
20141 {
20142 len += n;
20143 n = 0;
20144 }
20145 else if (n > slen)
20146 n = slen;
20147 if (len < 0)
20148 len = 0;
20149 else if (n + len > slen)
20150 len = slen - n;
20151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020152 rettv->v_type = VAR_STRING;
20153 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154}
20155
20156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020157 * "strridx()" function
20158 */
20159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020160f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020161{
20162 char_u buf[NUMBUFLEN];
20163 char_u *needle;
20164 char_u *haystack;
20165 char_u *rest;
20166 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020167 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020169 needle = get_tv_string_chk(&argvars[1]);
20170 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020171
20172 rettv->vval.v_number = -1;
20173 if (needle == NULL || haystack == NULL)
20174 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020175
20176 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020177 if (argvars[2].v_type != VAR_UNKNOWN)
20178 {
20179 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020180 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020181 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020182 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020183 }
20184 else
20185 end_idx = haystack_len;
20186
Bram Moolenaar0d660222005-01-07 21:51:51 +000020187 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020188 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020189 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020190 lastmatch = haystack + end_idx;
20191 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020192 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020193 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020194 for (rest = haystack; *rest != '\0'; ++rest)
20195 {
20196 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020197 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020198 break;
20199 lastmatch = rest;
20200 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020202
20203 if (lastmatch == NULL)
20204 rettv->vval.v_number = -1;
20205 else
20206 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20207}
20208
20209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210 * "strtrans()" function
20211 */
20212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020213f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020215 rettv->v_type = VAR_STRING;
20216 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217}
20218
20219/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020220 * "submatch()" function
20221 */
20222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020223f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020224{
Bram Moolenaar41571762014-04-02 19:00:58 +020020225 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020226 int no;
20227 int retList = 0;
20228
20229 no = (int)get_tv_number_chk(&argvars[0], &error);
20230 if (error)
20231 return;
20232 error = FALSE;
20233 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020234 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020235 if (error)
20236 return;
20237
20238 if (retList == 0)
20239 {
20240 rettv->v_type = VAR_STRING;
20241 rettv->vval.v_string = reg_submatch(no);
20242 }
20243 else
20244 {
20245 rettv->v_type = VAR_LIST;
20246 rettv->vval.v_list = reg_submatch_list(no);
20247 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020248}
20249
20250/*
20251 * "substitute()" function
20252 */
20253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020254f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020255{
20256 char_u patbuf[NUMBUFLEN];
20257 char_u subbuf[NUMBUFLEN];
20258 char_u flagsbuf[NUMBUFLEN];
20259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020260 char_u *str = get_tv_string_chk(&argvars[0]);
20261 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20262 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20263 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20264
Bram Moolenaar0d660222005-01-07 21:51:51 +000020265 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020266 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20267 rettv->vval.v_string = NULL;
20268 else
20269 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020270}
20271
20272/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020273 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020276f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277{
20278 int id = 0;
20279#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020280 linenr_T lnum;
20281 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020283 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020285 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020286 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20287 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020289 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020290 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020291 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292#endif
20293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020294 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295}
20296
20297/*
20298 * "synIDattr(id, what [, mode])" function
20299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020301f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302{
20303 char_u *p = NULL;
20304#ifdef FEAT_SYN_HL
20305 int id;
20306 char_u *what;
20307 char_u *mode;
20308 char_u modebuf[NUMBUFLEN];
20309 int modec;
20310
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020311 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020312 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020313 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020315 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020317 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 modec = 0; /* replace invalid with current */
20319 }
20320 else
20321 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020322#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020323 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020324 modec = 'g';
20325 else
20326#endif
20327 if (t_colors > 1)
20328 modec = 'c';
20329 else
20330 modec = 't';
20331 }
20332
20333
20334 switch (TOLOWER_ASC(what[0]))
20335 {
20336 case 'b':
20337 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20338 p = highlight_color(id, what, modec);
20339 else /* bold */
20340 p = highlight_has_attr(id, HL_BOLD, modec);
20341 break;
20342
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020343 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 p = highlight_color(id, what, modec);
20345 break;
20346
20347 case 'i':
20348 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20349 p = highlight_has_attr(id, HL_INVERSE, modec);
20350 else /* italic */
20351 p = highlight_has_attr(id, HL_ITALIC, modec);
20352 break;
20353
20354 case 'n': /* name */
20355 p = get_highlight_name(NULL, id - 1);
20356 break;
20357
20358 case 'r': /* reverse */
20359 p = highlight_has_attr(id, HL_INVERSE, modec);
20360 break;
20361
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020362 case 's':
20363 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20364 p = highlight_color(id, what, modec);
20365 else /* standout */
20366 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367 break;
20368
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020369 case 'u':
20370 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20371 /* underline */
20372 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20373 else
20374 /* undercurl */
20375 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 break;
20377 }
20378
20379 if (p != NULL)
20380 p = vim_strsave(p);
20381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020382 rettv->v_type = VAR_STRING;
20383 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384}
20385
20386/*
20387 * "synIDtrans(id)" function
20388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020390f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391{
20392 int id;
20393
20394#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020395 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396
20397 if (id > 0)
20398 id = syn_get_final_id(id);
20399 else
20400#endif
20401 id = 0;
20402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020403 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404}
20405
20406/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020407 * "synconcealed(lnum, col)" function
20408 */
20409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020410f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020411{
20412#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020413 linenr_T lnum;
20414 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020415 int syntax_flags = 0;
20416 int cchar;
20417 int matchid = 0;
20418 char_u str[NUMBUFLEN];
20419#endif
20420
20421 rettv->v_type = VAR_LIST;
20422 rettv->vval.v_list = NULL;
20423
20424#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20425 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020426 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020427
20428 vim_memset(str, NUL, sizeof(str));
20429
20430 if (rettv_list_alloc(rettv) != FAIL)
20431 {
20432 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20433 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20434 && curwin->w_p_cole > 0)
20435 {
20436 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20437 syntax_flags = get_syntax_info(&matchid);
20438
20439 /* get the conceal character */
20440 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20441 {
20442 cchar = syn_get_sub_char();
20443 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20444 cchar = lcs_conceal;
20445 if (cchar != NUL)
20446 {
20447# ifdef FEAT_MBYTE
20448 if (has_mbyte)
20449 (*mb_char2bytes)(cchar, str);
20450 else
20451# endif
20452 str[0] = cchar;
20453 }
20454 }
20455 }
20456
20457 list_append_number(rettv->vval.v_list,
20458 (syntax_flags & HL_CONCEAL) != 0);
20459 /* -1 to auto-determine strlen */
20460 list_append_string(rettv->vval.v_list, str, -1);
20461 list_append_number(rettv->vval.v_list, matchid);
20462 }
20463#endif
20464}
20465
20466/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020467 * "synstack(lnum, col)" function
20468 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020470f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020471{
20472#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020473 linenr_T lnum;
20474 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020475 int i;
20476 int id;
20477#endif
20478
20479 rettv->v_type = VAR_LIST;
20480 rettv->vval.v_list = NULL;
20481
20482#ifdef FEAT_SYN_HL
20483 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020484 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020485
20486 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020487 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020488 && rettv_list_alloc(rettv) != FAIL)
20489 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020490 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020491 for (i = 0; ; ++i)
20492 {
20493 id = syn_get_stack_item(i);
20494 if (id < 0)
20495 break;
20496 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20497 break;
20498 }
20499 }
20500#endif
20501}
20502
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020504get_cmd_output_as_rettv(
20505 typval_T *argvars,
20506 typval_T *rettv,
20507 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020509 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020511 char_u *infile = NULL;
20512 char_u buf[NUMBUFLEN];
20513 int err = FALSE;
20514 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020515 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020516 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020518 rettv->v_type = VAR_STRING;
20519 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020520 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020521 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020522
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020523 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020524 {
20525 /*
20526 * Write the string to a temp file, to be used for input of the shell
20527 * command.
20528 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020529 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020530 {
20531 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020532 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020533 }
20534
20535 fd = mch_fopen((char *)infile, WRITEBIN);
20536 if (fd == NULL)
20537 {
20538 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020539 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020540 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020541 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020542 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020543 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20544 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020545 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020546 else
20547 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020548 size_t len;
20549
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020550 p = get_tv_string_buf_chk(&argvars[1], buf);
20551 if (p == NULL)
20552 {
20553 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020554 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020555 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020556 len = STRLEN(p);
20557 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020558 err = TRUE;
20559 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020560 if (fclose(fd) != 0)
20561 err = TRUE;
20562 if (err)
20563 {
20564 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020565 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020566 }
20567 }
20568
Bram Moolenaar52a72462014-08-29 15:53:52 +020020569 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20570 * echoes typeahead, that messes up the display. */
20571 if (!msg_silent)
20572 flags += SHELL_COOKED;
20573
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020574 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020576 int len;
20577 listitem_T *li;
20578 char_u *s = NULL;
20579 char_u *start;
20580 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020581 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582
Bram Moolenaar52a72462014-08-29 15:53:52 +020020583 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020584 if (res == NULL)
20585 goto errret;
20586
20587 list = list_alloc();
20588 if (list == NULL)
20589 goto errret;
20590
20591 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020593 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020594 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020595 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020596 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020597
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020598 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020599 if (s == NULL)
20600 goto errret;
20601
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020602 for (p = s; start < end; ++p, ++start)
20603 *p = *start == NUL ? NL : *start;
20604 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020605
20606 li = listitem_alloc();
20607 if (li == NULL)
20608 {
20609 vim_free(s);
20610 goto errret;
20611 }
20612 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020613 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020614 li->li_tv.vval.v_string = s;
20615 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020617
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020618 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020619 rettv->v_type = VAR_LIST;
20620 rettv->vval.v_list = list;
20621 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020623 else
20624 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020625 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020626#ifdef USE_CR
20627 /* translate <CR> into <NL> */
20628 if (res != NULL)
20629 {
20630 char_u *s;
20631
20632 for (s = res; *s; ++s)
20633 {
20634 if (*s == CAR)
20635 *s = NL;
20636 }
20637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638#else
20639# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020640 /* translate <CR><NL> into <NL> */
20641 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020643 char_u *s, *d;
20644
20645 d = res;
20646 for (s = res; *s; ++s)
20647 {
20648 if (s[0] == CAR && s[1] == NL)
20649 ++s;
20650 *d++ = *s;
20651 }
20652 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654# endif
20655#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020656 rettv->vval.v_string = res;
20657 res = NULL;
20658 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020659
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020660errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020661 if (infile != NULL)
20662 {
20663 mch_remove(infile);
20664 vim_free(infile);
20665 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020666 if (res != NULL)
20667 vim_free(res);
20668 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020669 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020670}
20671
20672/*
20673 * "system()" function
20674 */
20675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020676f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020677{
20678 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20679}
20680
20681/*
20682 * "systemlist()" function
20683 */
20684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020685f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020686{
20687 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688}
20689
20690/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020691 * "tabpagebuflist()" function
20692 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020694f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020695{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020696#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020697 tabpage_T *tp;
20698 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020699
20700 if (argvars[0].v_type == VAR_UNKNOWN)
20701 wp = firstwin;
20702 else
20703 {
20704 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20705 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020706 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020707 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020708 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020709 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020710 for (; wp != NULL; wp = wp->w_next)
20711 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020712 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020713 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020714 }
20715#endif
20716}
20717
20718
20719/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020720 * "tabpagenr()" function
20721 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020723f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020724{
20725 int nr = 1;
20726#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020727 char_u *arg;
20728
20729 if (argvars[0].v_type != VAR_UNKNOWN)
20730 {
20731 arg = get_tv_string_chk(&argvars[0]);
20732 nr = 0;
20733 if (arg != NULL)
20734 {
20735 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020736 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020737 else
20738 EMSG2(_(e_invexpr2), arg);
20739 }
20740 }
20741 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020742 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020743#endif
20744 rettv->vval.v_number = nr;
20745}
20746
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020747
20748#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020749static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020750
20751/*
20752 * Common code for tabpagewinnr() and winnr().
20753 */
20754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020755get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020756{
20757 win_T *twin;
20758 int nr = 1;
20759 win_T *wp;
20760 char_u *arg;
20761
20762 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20763 if (argvar->v_type != VAR_UNKNOWN)
20764 {
20765 arg = get_tv_string_chk(argvar);
20766 if (arg == NULL)
20767 nr = 0; /* type error; errmsg already given */
20768 else if (STRCMP(arg, "$") == 0)
20769 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20770 else if (STRCMP(arg, "#") == 0)
20771 {
20772 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20773 if (twin == NULL)
20774 nr = 0;
20775 }
20776 else
20777 {
20778 EMSG2(_(e_invexpr2), arg);
20779 nr = 0;
20780 }
20781 }
20782
20783 if (nr > 0)
20784 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20785 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020786 {
20787 if (wp == NULL)
20788 {
20789 /* didn't find it in this tabpage */
20790 nr = 0;
20791 break;
20792 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020793 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020794 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020795 return nr;
20796}
20797#endif
20798
20799/*
20800 * "tabpagewinnr()" function
20801 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020803f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020804{
20805 int nr = 1;
20806#ifdef FEAT_WINDOWS
20807 tabpage_T *tp;
20808
20809 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20810 if (tp == NULL)
20811 nr = 0;
20812 else
20813 nr = get_winnr(tp, &argvars[1]);
20814#endif
20815 rettv->vval.v_number = nr;
20816}
20817
20818
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020819/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020820 * "tagfiles()" function
20821 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020823f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020824{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020825 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020826 tagname_T tn;
20827 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020828
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020829 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020830 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020831 fname = alloc(MAXPATHL);
20832 if (fname == NULL)
20833 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020834
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020835 for (first = TRUE; ; first = FALSE)
20836 if (get_tagfname(&tn, first, fname) == FAIL
20837 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020838 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020839 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020840 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020841}
20842
20843/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020844 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020845 */
20846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020847f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020848{
20849 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020850
20851 tag_pattern = get_tv_string(&argvars[0]);
20852
20853 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020854 if (*tag_pattern == NUL)
20855 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020856
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020857 if (rettv_list_alloc(rettv) == OK)
20858 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020859}
20860
20861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 * "tempname()" function
20863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020865f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866{
20867 static int x = 'A';
20868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020869 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020870 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871
20872 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20873 * names. Skip 'I' and 'O', they are used for shell redirection. */
20874 do
20875 {
20876 if (x == 'Z')
20877 x = '0';
20878 else if (x == '9')
20879 x = 'A';
20880 else
20881 {
20882#ifdef EBCDIC
20883 if (x == 'I')
20884 x = 'J';
20885 else if (x == 'R')
20886 x = 'S';
20887 else
20888#endif
20889 ++x;
20890 }
20891 } while (x == 'I' || x == 'O');
20892}
20893
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020894#ifdef FEAT_FLOAT
20895/*
20896 * "tan()" function
20897 */
20898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020899f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020900{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020901 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020902
20903 rettv->v_type = VAR_FLOAT;
20904 if (get_float_arg(argvars, &f) == OK)
20905 rettv->vval.v_float = tan(f);
20906 else
20907 rettv->vval.v_float = 0.0;
20908}
20909
20910/*
20911 * "tanh()" function
20912 */
20913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020914f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020915{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020916 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020917
20918 rettv->v_type = VAR_FLOAT;
20919 if (get_float_arg(argvars, &f) == OK)
20920 rettv->vval.v_float = tanh(f);
20921 else
20922 rettv->vval.v_float = 0.0;
20923}
20924#endif
20925
Bram Moolenaar574860b2016-05-24 17:33:34 +020020926/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020927 * "test_alloc_fail(id, countdown, repeat)" function
20928 */
20929 static void
20930f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20931{
20932 if (argvars[0].v_type != VAR_NUMBER
20933 || argvars[0].vval.v_number <= 0
20934 || argvars[1].v_type != VAR_NUMBER
20935 || argvars[1].vval.v_number < 0
20936 || argvars[2].v_type != VAR_NUMBER)
20937 EMSG(_(e_invarg));
20938 else
20939 {
20940 alloc_fail_id = argvars[0].vval.v_number;
20941 if (alloc_fail_id >= aid_last)
20942 EMSG(_(e_invarg));
20943 alloc_fail_countdown = argvars[1].vval.v_number;
20944 alloc_fail_repeat = argvars[2].vval.v_number;
20945 did_outofmem_msg = FALSE;
20946 }
20947}
20948
20949/*
20950 * "test_disable_char_avail({expr})" function
20951 */
20952 static void
20953f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20954{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020955 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020956}
20957
20958/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020959 * "test_garbagecollect_now()" function
20960 */
20961 static void
20962f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20963{
20964 /* This is dangerous, any Lists and Dicts used internally may be freed
20965 * while still in use. */
20966 garbage_collect(TRUE);
20967}
20968
20969#ifdef FEAT_JOB_CHANNEL
20970 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020971f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020972{
20973 rettv->v_type = VAR_CHANNEL;
20974 rettv->vval.v_channel = NULL;
20975}
20976#endif
20977
20978 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020979f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020980{
20981 rettv->v_type = VAR_DICT;
20982 rettv->vval.v_dict = NULL;
20983}
20984
20985#ifdef FEAT_JOB_CHANNEL
20986 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020987f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020988{
20989 rettv->v_type = VAR_JOB;
20990 rettv->vval.v_job = NULL;
20991}
20992#endif
20993
20994 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020995f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020996{
20997 rettv->v_type = VAR_LIST;
20998 rettv->vval.v_list = NULL;
20999}
21000
21001 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021002f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021003{
21004 rettv->v_type = VAR_PARTIAL;
21005 rettv->vval.v_partial = NULL;
21006}
21007
21008 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021009f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021010{
21011 rettv->v_type = VAR_STRING;
21012 rettv->vval.v_string = NULL;
21013}
21014
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021015 static void
21016f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21017{
21018 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21019}
21020
Bram Moolenaar975b5272016-03-15 23:10:59 +010021021#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21022/*
21023 * Get a callback from "arg". It can be a Funcref or a function name.
21024 * When "arg" is zero return an empty string.
21025 * Return NULL for an invalid argument.
21026 */
21027 char_u *
21028get_callback(typval_T *arg, partial_T **pp)
21029{
21030 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21031 {
21032 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021033 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021034 return (*pp)->pt_name;
21035 }
21036 *pp = NULL;
21037 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21038 return arg->vval.v_string;
21039 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21040 return (char_u *)"";
21041 EMSG(_("E921: Invalid callback argument"));
21042 return NULL;
21043}
21044#endif
21045
21046#ifdef FEAT_TIMERS
21047/*
21048 * "timer_start(time, callback [, options])" function
21049 */
21050 static void
21051f_timer_start(typval_T *argvars, typval_T *rettv)
21052{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021053 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021054 timer_T *timer;
21055 int repeat = 0;
21056 char_u *callback;
21057 dict_T *dict;
21058
Bram Moolenaar38499922016-04-22 20:46:52 +020021059 if (check_secure())
21060 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021061 if (argvars[2].v_type != VAR_UNKNOWN)
21062 {
21063 if (argvars[2].v_type != VAR_DICT
21064 || (dict = argvars[2].vval.v_dict) == NULL)
21065 {
21066 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21067 return;
21068 }
21069 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21070 repeat = get_dict_number(dict, (char_u *)"repeat");
21071 }
21072
21073 timer = create_timer(msec, repeat);
21074 callback = get_callback(&argvars[1], &timer->tr_partial);
21075 if (callback == NULL)
21076 {
21077 stop_timer(timer);
21078 rettv->vval.v_number = -1;
21079 }
21080 else
21081 {
21082 timer->tr_callback = vim_strsave(callback);
21083 rettv->vval.v_number = timer->tr_id;
21084 }
21085}
21086
21087/*
21088 * "timer_stop(timer)" function
21089 */
21090 static void
21091f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21092{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021093 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021094
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021095 if (argvars[0].v_type != VAR_NUMBER)
21096 {
21097 EMSG(_(e_number_exp));
21098 return;
21099 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021100 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021101 if (timer != NULL)
21102 stop_timer(timer);
21103}
21104#endif
21105
Bram Moolenaard52d9742005-08-21 22:20:28 +000021106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 * "tolower(string)" function
21108 */
21109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021110f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111{
21112 char_u *p;
21113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021114 p = vim_strsave(get_tv_string(&argvars[0]));
21115 rettv->v_type = VAR_STRING;
21116 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021117
21118 if (p != NULL)
21119 while (*p != NUL)
21120 {
21121#ifdef FEAT_MBYTE
21122 int l;
21123
21124 if (enc_utf8)
21125 {
21126 int c, lc;
21127
21128 c = utf_ptr2char(p);
21129 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021130 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 /* TODO: reallocate string when byte count changes. */
21132 if (utf_char2len(lc) == l)
21133 utf_char2bytes(lc, p);
21134 p += l;
21135 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021136 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 p += l; /* skip multi-byte character */
21138 else
21139#endif
21140 {
21141 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21142 ++p;
21143 }
21144 }
21145}
21146
21147/*
21148 * "toupper(string)" function
21149 */
21150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021153 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021154 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155}
21156
21157/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021158 * "tr(string, fromstr, tostr)" function
21159 */
21160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021161f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021162{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021163 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021164 char_u *fromstr;
21165 char_u *tostr;
21166 char_u *p;
21167#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021168 int inlen;
21169 int fromlen;
21170 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021171 int idx;
21172 char_u *cpstr;
21173 int cplen;
21174 int first = TRUE;
21175#endif
21176 char_u buf[NUMBUFLEN];
21177 char_u buf2[NUMBUFLEN];
21178 garray_T ga;
21179
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021180 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021181 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21182 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021183
21184 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021185 rettv->v_type = VAR_STRING;
21186 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021187 if (fromstr == NULL || tostr == NULL)
21188 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021189 ga_init2(&ga, (int)sizeof(char), 80);
21190
21191#ifdef FEAT_MBYTE
21192 if (!has_mbyte)
21193#endif
21194 /* not multi-byte: fromstr and tostr must be the same length */
21195 if (STRLEN(fromstr) != STRLEN(tostr))
21196 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021197#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021198error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021199#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021200 EMSG2(_(e_invarg2), fromstr);
21201 ga_clear(&ga);
21202 return;
21203 }
21204
21205 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021206 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021207 {
21208#ifdef FEAT_MBYTE
21209 if (has_mbyte)
21210 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021211 inlen = (*mb_ptr2len)(in_str);
21212 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021213 cplen = inlen;
21214 idx = 0;
21215 for (p = fromstr; *p != NUL; p += fromlen)
21216 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021217 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021218 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021219 {
21220 for (p = tostr; *p != NUL; p += tolen)
21221 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021222 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021223 if (idx-- == 0)
21224 {
21225 cplen = tolen;
21226 cpstr = p;
21227 break;
21228 }
21229 }
21230 if (*p == NUL) /* tostr is shorter than fromstr */
21231 goto error;
21232 break;
21233 }
21234 ++idx;
21235 }
21236
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021237 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021238 {
21239 /* Check that fromstr and tostr have the same number of
21240 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021241 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021242 first = FALSE;
21243 for (p = tostr; *p != NUL; p += tolen)
21244 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021245 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021246 --idx;
21247 }
21248 if (idx != 0)
21249 goto error;
21250 }
21251
Bram Moolenaarcde88542015-08-11 19:14:00 +020021252 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021253 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021254 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021255
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021256 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021257 }
21258 else
21259#endif
21260 {
21261 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021262 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021263 if (p != NULL)
21264 ga_append(&ga, tostr[p - fromstr]);
21265 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021266 ga_append(&ga, *in_str);
21267 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021268 }
21269 }
21270
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021271 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021272 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021273 ga_append(&ga, NUL);
21274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021275 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021276}
21277
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021278#ifdef FEAT_FLOAT
21279/*
21280 * "trunc({float})" function
21281 */
21282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021283f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021285 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021286
21287 rettv->v_type = VAR_FLOAT;
21288 if (get_float_arg(argvars, &f) == OK)
21289 /* trunc() is not in C90, use floor() or ceil() instead. */
21290 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21291 else
21292 rettv->vval.v_float = 0.0;
21293}
21294#endif
21295
Bram Moolenaar8299df92004-07-10 09:47:34 +000021296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 * "type(expr)" function
21298 */
21299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021302 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021303
21304 switch (argvars[0].v_type)
21305 {
21306 case VAR_NUMBER: n = 0; break;
21307 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021308 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021309 case VAR_FUNC: n = 2; break;
21310 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021311 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021312 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021313 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021314 if (argvars[0].vval.v_number == VVAL_FALSE
21315 || argvars[0].vval.v_number == VVAL_TRUE)
21316 n = 6;
21317 else
21318 n = 7;
21319 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021320 case VAR_JOB: n = 8; break;
21321 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021322 case VAR_UNKNOWN:
21323 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21324 n = -1;
21325 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021326 }
21327 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328}
21329
21330/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021331 * "undofile(name)" function
21332 */
21333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021334f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021335{
21336 rettv->v_type = VAR_STRING;
21337#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021338 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021339 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021340
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021341 if (*fname == NUL)
21342 {
21343 /* If there is no file name there will be no undo file. */
21344 rettv->vval.v_string = NULL;
21345 }
21346 else
21347 {
21348 char_u *ffname = FullName_save(fname, FALSE);
21349
21350 if (ffname != NULL)
21351 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21352 vim_free(ffname);
21353 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021354 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021355#else
21356 rettv->vval.v_string = NULL;
21357#endif
21358}
21359
21360/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021361 * "undotree()" function
21362 */
21363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021364f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021365{
21366 if (rettv_dict_alloc(rettv) == OK)
21367 {
21368 dict_T *dict = rettv->vval.v_dict;
21369 list_T *list;
21370
Bram Moolenaar730cde92010-06-27 05:18:54 +020021371 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021372 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021373 dict_add_nr_str(dict, "save_last",
21374 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021375 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21376 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021377 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021378
21379 list = list_alloc();
21380 if (list != NULL)
21381 {
21382 u_eval_tree(curbuf->b_u_oldhead, list);
21383 dict_add_list(dict, "entries", list);
21384 }
21385 }
21386}
21387
21388/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021389 * "values(dict)" function
21390 */
21391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021393{
21394 dict_list(argvars, rettv, 1);
21395}
21396
21397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 * "virtcol(string)" function
21399 */
21400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021401f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402{
21403 colnr_T vcol = 0;
21404 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021405 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021407 fp = var2fpos(&argvars[0], FALSE, &fnum);
21408 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21409 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410 {
21411 getvvcol(curwin, fp, NULL, NULL, &vcol);
21412 ++vcol;
21413 }
21414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021415 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416}
21417
21418/*
21419 * "visualmode()" function
21420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021422f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 char_u str[2];
21425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021426 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 str[0] = curbuf->b_visual_mode_eval;
21428 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021429 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430
21431 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021432 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434}
21435
21436/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021437 * "wildmenumode()" function
21438 */
21439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021440f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021441{
21442#ifdef FEAT_WILDMENU
21443 if (wild_menu_showing)
21444 rettv->vval.v_number = 1;
21445#endif
21446}
21447
21448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 * "winbufnr(nr)" function
21450 */
21451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
21454 win_T *wp;
21455
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021456 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021458 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461}
21462
21463/*
21464 * "wincol()" function
21465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021467f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468{
21469 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021470 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471}
21472
21473/*
21474 * "winheight(nr)" function
21475 */
21476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021477f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478{
21479 win_T *wp;
21480
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021481 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021483 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021485 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486}
21487
21488/*
21489 * "winline()" function
21490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021492f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493{
21494 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021495 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496}
21497
21498/*
21499 * "winnr()" function
21500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021502f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503{
21504 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021505
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021507 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021509 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510}
21511
21512/*
21513 * "winrestcmd()" function
21514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021516f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517{
21518#ifdef FEAT_WINDOWS
21519 win_T *wp;
21520 int winnr = 1;
21521 garray_T ga;
21522 char_u buf[50];
21523
21524 ga_init2(&ga, (int)sizeof(char), 70);
21525 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21526 {
21527 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21528 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21530 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 ++winnr;
21532 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021533 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540}
21541
21542/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021543 * "winrestview()" function
21544 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021547{
21548 dict_T *dict;
21549
21550 if (argvars[0].v_type != VAR_DICT
21551 || (dict = argvars[0].vval.v_dict) == NULL)
21552 EMSG(_(e_invarg));
21553 else
21554 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021555 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021556 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021557 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021558 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021559#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021560 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021561 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021562#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021563 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21564 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021565 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021566 curwin->w_set_curswant = FALSE;
21567 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021568
Bram Moolenaar82c25852014-05-28 16:47:16 +020021569 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021570 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021571#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021572 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021573 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021574#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021575 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021576 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021577 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021578 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021579
21580 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021581 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021582# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021583 win_new_width(curwin, W_WIDTH(curwin));
21584# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021585 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021586
Bram Moolenaarb851a962014-10-31 15:45:52 +010021587 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021588 curwin->w_topline = 1;
21589 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21590 curwin->w_topline = curbuf->b_ml.ml_line_count;
21591#ifdef FEAT_DIFF
21592 check_topfill(curwin, TRUE);
21593#endif
21594 }
21595}
21596
21597/*
21598 * "winsaveview()" function
21599 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021601f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021602{
21603 dict_T *dict;
21604
Bram Moolenaara800b422010-06-27 01:15:55 +020021605 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021606 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021607 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021608
21609 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21610 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21611#ifdef FEAT_VIRTUALEDIT
21612 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21613#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021614 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021615 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21616
21617 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21618#ifdef FEAT_DIFF
21619 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21620#endif
21621 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21622 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21623}
21624
21625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * "winwidth(nr)" function
21627 */
21628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021629f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630{
21631 win_T *wp;
21632
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021633 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021637#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021638 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021640 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641#endif
21642}
21643
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021645 * "wordcount()" function
21646 */
21647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021648f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021649{
21650 if (rettv_dict_alloc(rettv) == FAIL)
21651 return;
21652 cursor_pos_info(rettv->vval.v_dict);
21653}
21654
21655/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021656 * Write list of strings to file
21657 */
21658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021659write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021660{
21661 listitem_T *li;
21662 int c;
21663 int ret = OK;
21664 char_u *s;
21665
21666 for (li = list->lv_first; li != NULL; li = li->li_next)
21667 {
21668 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21669 {
21670 if (*s == '\n')
21671 c = putc(NUL, fd);
21672 else
21673 c = putc(*s, fd);
21674 if (c == EOF)
21675 {
21676 ret = FAIL;
21677 break;
21678 }
21679 }
21680 if (!binary || li->li_next != NULL)
21681 if (putc('\n', fd) == EOF)
21682 {
21683 ret = FAIL;
21684 break;
21685 }
21686 if (ret == FAIL)
21687 {
21688 EMSG(_(e_write));
21689 break;
21690 }
21691 }
21692 return ret;
21693}
21694
21695/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021696 * "writefile()" function
21697 */
21698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021699f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021700{
21701 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021702 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021703 char_u *fname;
21704 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021705 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021706
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021707 if (check_restricted() || check_secure())
21708 return;
21709
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021710 if (argvars[0].v_type != VAR_LIST)
21711 {
21712 EMSG2(_(e_listarg), "writefile()");
21713 return;
21714 }
21715 if (argvars[0].vval.v_list == NULL)
21716 return;
21717
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021718 if (argvars[2].v_type != VAR_UNKNOWN)
21719 {
21720 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21721 binary = TRUE;
21722 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21723 append = TRUE;
21724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021725
21726 /* Always open the file in binary mode, library functions have a mind of
21727 * their own about CR-LF conversion. */
21728 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021729 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21730 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021731 {
21732 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21733 ret = -1;
21734 }
21735 else
21736 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021737 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21738 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021739 fclose(fd);
21740 }
21741
21742 rettv->vval.v_number = ret;
21743}
21744
21745/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021746 * "xor(expr, expr)" function
21747 */
21748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021749f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021750{
21751 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21752 ^ get_tv_number_chk(&argvars[1], NULL);
21753}
21754
21755
21756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021758 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 */
21760 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021761var2fpos(
21762 typval_T *varp,
21763 int dollar_lnum, /* TRUE when $ is last line */
21764 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021766 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021768 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769
Bram Moolenaara5525202006-03-02 22:52:09 +000021770 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021771 if (varp->v_type == VAR_LIST)
21772 {
21773 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021774 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021775 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021776 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021777
21778 l = varp->vval.v_list;
21779 if (l == NULL)
21780 return NULL;
21781
21782 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021783 pos.lnum = list_find_nr(l, 0L, &error);
21784 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021785 return NULL; /* invalid line number */
21786
21787 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021788 pos.col = list_find_nr(l, 1L, &error);
21789 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021790 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021791 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021792
21793 /* We accept "$" for the column number: last column. */
21794 li = list_find(l, 1L);
21795 if (li != NULL && li->li_tv.v_type == VAR_STRING
21796 && li->li_tv.vval.v_string != NULL
21797 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21798 pos.col = len + 1;
21799
Bram Moolenaara5525202006-03-02 22:52:09 +000021800 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021801 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021802 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021803 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021804
Bram Moolenaara5525202006-03-02 22:52:09 +000021805#ifdef FEAT_VIRTUALEDIT
21806 /* Get the virtual offset. Defaults to zero. */
21807 pos.coladd = list_find_nr(l, 2L, &error);
21808 if (error)
21809 pos.coladd = 0;
21810#endif
21811
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021812 return &pos;
21813 }
21814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021815 name = get_tv_string_chk(varp);
21816 if (name == NULL)
21817 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021818 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021820 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21821 {
21822 if (VIsual_active)
21823 return &VIsual;
21824 return &curwin->w_cursor;
21825 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021826 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021828 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21830 return NULL;
21831 return pp;
21832 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021833
21834#ifdef FEAT_VIRTUALEDIT
21835 pos.coladd = 0;
21836#endif
21837
Bram Moolenaar477933c2007-07-17 14:32:23 +000021838 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021839 {
21840 pos.col = 0;
21841 if (name[1] == '0') /* "w0": first visible line */
21842 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021843 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021844 pos.lnum = curwin->w_topline;
21845 return &pos;
21846 }
21847 else if (name[1] == '$') /* "w$": last visible line */
21848 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021849 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021850 pos.lnum = curwin->w_botline - 1;
21851 return &pos;
21852 }
21853 }
21854 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021856 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 {
21858 pos.lnum = curbuf->b_ml.ml_line_count;
21859 pos.col = 0;
21860 }
21861 else
21862 {
21863 pos.lnum = curwin->w_cursor.lnum;
21864 pos.col = (colnr_T)STRLEN(ml_get_curline());
21865 }
21866 return &pos;
21867 }
21868 return NULL;
21869}
21870
21871/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021872 * Convert list in "arg" into a position and optional file number.
21873 * When "fnump" is NULL there is no file number, only 3 items.
21874 * Note that the column is passed on as-is, the caller may want to decrement
21875 * it to use 1 for the first column.
21876 * Return FAIL when conversion is not possible, doesn't check the position for
21877 * validity.
21878 */
21879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021880list2fpos(
21881 typval_T *arg,
21882 pos_T *posp,
21883 int *fnump,
21884 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021885{
21886 list_T *l = arg->vval.v_list;
21887 long i = 0;
21888 long n;
21889
Bram Moolenaar493c1782014-05-28 14:34:46 +020021890 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21891 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021892 if (arg->v_type != VAR_LIST
21893 || l == NULL
21894 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021895 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021896 return FAIL;
21897
21898 if (fnump != NULL)
21899 {
21900 n = list_find_nr(l, i++, NULL); /* fnum */
21901 if (n < 0)
21902 return FAIL;
21903 if (n == 0)
21904 n = curbuf->b_fnum; /* current buffer */
21905 *fnump = n;
21906 }
21907
21908 n = list_find_nr(l, i++, NULL); /* lnum */
21909 if (n < 0)
21910 return FAIL;
21911 posp->lnum = n;
21912
21913 n = list_find_nr(l, i++, NULL); /* col */
21914 if (n < 0)
21915 return FAIL;
21916 posp->col = n;
21917
21918#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021919 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021920 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021921 posp->coladd = 0;
21922 else
21923 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021924#endif
21925
Bram Moolenaar493c1782014-05-28 14:34:46 +020021926 if (curswantp != NULL)
21927 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21928
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021929 return OK;
21930}
21931
21932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 * Get the length of an environment variable name.
21934 * Advance "arg" to the first character after the name.
21935 * Return 0 for error.
21936 */
21937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021938get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939{
21940 char_u *p;
21941 int len;
21942
21943 for (p = *arg; vim_isIDc(*p); ++p)
21944 ;
21945 if (p == *arg) /* no name found */
21946 return 0;
21947
21948 len = (int)(p - *arg);
21949 *arg = p;
21950 return len;
21951}
21952
21953/*
21954 * Get the length of the name of a function or internal variable.
21955 * "arg" is advanced to the first non-white character after the name.
21956 * Return 0 if something is wrong.
21957 */
21958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021959get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960{
21961 char_u *p;
21962 int len;
21963
21964 /* Find the end of the name. */
21965 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021966 {
21967 if (*p == ':')
21968 {
21969 /* "s:" is start of "s:var", but "n:" is not and can be used in
21970 * slice "[n:]". Also "xx:" is not a namespace. */
21971 len = (int)(p - *arg);
21972 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21973 || len > 1)
21974 break;
21975 }
21976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 if (p == *arg) /* no name found */
21978 return 0;
21979
21980 len = (int)(p - *arg);
21981 *arg = skipwhite(p);
21982
21983 return len;
21984}
21985
21986/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021987 * Get the length of the name of a variable or function.
21988 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021990 * Return -1 if curly braces expansion failed.
21991 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 * If the name contains 'magic' {}'s, expand them and return the
21993 * expanded name in an allocated string via 'alias' - caller must free.
21994 */
21995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021996get_name_len(
21997 char_u **arg,
21998 char_u **alias,
21999 int evaluate,
22000 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001{
22002 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 char_u *p;
22004 char_u *expr_start;
22005 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006
22007 *alias = NULL; /* default to no alias */
22008
22009 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22010 && (*arg)[2] == (int)KE_SNR)
22011 {
22012 /* hard coded <SNR>, already translated */
22013 *arg += 3;
22014 return get_id_len(arg) + 3;
22015 }
22016 len = eval_fname_script(*arg);
22017 if (len > 0)
22018 {
22019 /* literal "<SID>", "s:" or "<SNR>" */
22020 *arg += len;
22021 }
22022
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022024 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022026 p = find_name_end(*arg, &expr_start, &expr_end,
22027 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 if (expr_start != NULL)
22029 {
22030 char_u *temp_string;
22031
22032 if (!evaluate)
22033 {
22034 len += (int)(p - *arg);
22035 *arg = skipwhite(p);
22036 return len;
22037 }
22038
22039 /*
22040 * Include any <SID> etc in the expanded string:
22041 * Thus the -len here.
22042 */
22043 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22044 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022045 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 *alias = temp_string;
22047 *arg = skipwhite(p);
22048 return (int)STRLEN(temp_string);
22049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050
22051 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022052 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 EMSG2(_(e_invexpr2), *arg);
22054
22055 return len;
22056}
22057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022058/*
22059 * Find the end of a variable or function name, taking care of magic braces.
22060 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22061 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022062 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022063 * Return a pointer to just after the name. Equal to "arg" if there is no
22064 * valid name.
22065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022067find_name_end(
22068 char_u *arg,
22069 char_u **expr_start,
22070 char_u **expr_end,
22071 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022073 int mb_nest = 0;
22074 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022076 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022078 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022080 *expr_start = NULL;
22081 *expr_end = NULL;
22082 }
22083
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022084 /* Quick check for valid starting character. */
22085 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22086 return arg;
22087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022088 for (p = arg; *p != NUL
22089 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022090 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022091 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022092 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022093 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022094 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022095 if (*p == '\'')
22096 {
22097 /* skip over 'string' to avoid counting [ and ] inside it. */
22098 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22099 ;
22100 if (*p == NUL)
22101 break;
22102 }
22103 else if (*p == '"')
22104 {
22105 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22106 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22107 if (*p == '\\' && p[1] != NUL)
22108 ++p;
22109 if (*p == NUL)
22110 break;
22111 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022112 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22113 {
22114 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022115 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022116 len = (int)(p - arg);
22117 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022118 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022119 break;
22120 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022122 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022124 if (*p == '[')
22125 ++br_nest;
22126 else if (*p == ']')
22127 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022130 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022132 if (*p == '{')
22133 {
22134 mb_nest++;
22135 if (expr_start != NULL && *expr_start == NULL)
22136 *expr_start = p;
22137 }
22138 else if (*p == '}')
22139 {
22140 mb_nest--;
22141 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22142 *expr_end = p;
22143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 }
22146
22147 return p;
22148}
22149
22150/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022151 * Expands out the 'magic' {}'s in a variable/function name.
22152 * Note that this can call itself recursively, to deal with
22153 * constructs like foo{bar}{baz}{bam}
22154 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22155 * "in_start" ^
22156 * "expr_start" ^
22157 * "expr_end" ^
22158 * "in_end" ^
22159 *
22160 * Returns a new allocated string, which the caller must free.
22161 * Returns NULL for failure.
22162 */
22163 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022164make_expanded_name(
22165 char_u *in_start,
22166 char_u *expr_start,
22167 char_u *expr_end,
22168 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022169{
22170 char_u c1;
22171 char_u *retval = NULL;
22172 char_u *temp_result;
22173 char_u *nextcmd = NULL;
22174
22175 if (expr_end == NULL || in_end == NULL)
22176 return NULL;
22177 *expr_start = NUL;
22178 *expr_end = NUL;
22179 c1 = *in_end;
22180 *in_end = NUL;
22181
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022182 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022183 if (temp_result != NULL && nextcmd == NULL)
22184 {
22185 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22186 + (in_end - expr_end) + 1));
22187 if (retval != NULL)
22188 {
22189 STRCPY(retval, in_start);
22190 STRCAT(retval, temp_result);
22191 STRCAT(retval, expr_end + 1);
22192 }
22193 }
22194 vim_free(temp_result);
22195
22196 *in_end = c1; /* put char back for error messages */
22197 *expr_start = '{';
22198 *expr_end = '}';
22199
22200 if (retval != NULL)
22201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022202 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022203 if (expr_start != NULL)
22204 {
22205 /* Further expansion! */
22206 temp_result = make_expanded_name(retval, expr_start,
22207 expr_end, temp_result);
22208 vim_free(retval);
22209 retval = temp_result;
22210 }
22211 }
22212
22213 return retval;
22214}
22215
22216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022218 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 */
22220 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022221eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022223 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22224}
22225
22226/*
22227 * Return TRUE if character "c" can be used as the first character in a
22228 * variable or function name (excluding '{' and '}').
22229 */
22230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022231eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022232{
22233 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234}
22235
22236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022237 * Set number v: variable to "val".
22238 */
22239 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022240set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022242 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243}
22244
22245/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022246 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022248 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022249get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022251 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252}
22253
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022254/*
22255 * Get string v: variable value. Uses a static buffer, can only be used once.
22256 */
22257 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022258get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022259{
22260 return get_tv_string(&vimvars[idx].vv_tv);
22261}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022262
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022264 * Get List v: variable value. Caller must take care of reference count when
22265 * needed.
22266 */
22267 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022268get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022269{
22270 return vimvars[idx].vv_list;
22271}
22272
22273/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022274 * Set v:char to character "c".
22275 */
22276 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022278{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022279 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022280
22281#ifdef FEAT_MBYTE
22282 if (has_mbyte)
22283 buf[(*mb_char2bytes)(c, buf)] = NUL;
22284 else
22285#endif
22286 {
22287 buf[0] = c;
22288 buf[1] = NUL;
22289 }
22290 set_vim_var_string(VV_CHAR, buf, -1);
22291}
22292
22293/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022294 * Set v:count to "count" and v:count1 to "count1".
22295 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 */
22297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022298set_vcount(
22299 long count,
22300 long count1,
22301 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022303 if (set_prevcount)
22304 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022305 vimvars[VV_COUNT].vv_nr = count;
22306 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307}
22308
22309/*
22310 * Set string v: variable to a copy of "val".
22311 */
22312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022313set_vim_var_string(
22314 int idx,
22315 char_u *val,
22316 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317{
Bram Moolenaara542c682016-01-31 16:28:04 +010022318 clear_tv(&vimvars[idx].vv_di.di_tv);
22319 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022320 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022321 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022323 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022325 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326}
22327
22328/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022329 * Set List v: variable to "val".
22330 */
22331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022332set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022333{
Bram Moolenaara542c682016-01-31 16:28:04 +010022334 clear_tv(&vimvars[idx].vv_di.di_tv);
22335 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022336 vimvars[idx].vv_list = val;
22337 if (val != NULL)
22338 ++val->lv_refcount;
22339}
22340
22341/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022342 * Set Dictionary v: variable to "val".
22343 */
22344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022345set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022346{
22347 int todo;
22348 hashitem_T *hi;
22349
Bram Moolenaara542c682016-01-31 16:28:04 +010022350 clear_tv(&vimvars[idx].vv_di.di_tv);
22351 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022352 vimvars[idx].vv_dict = val;
22353 if (val != NULL)
22354 {
22355 ++val->dv_refcount;
22356
22357 /* Set readonly */
22358 todo = (int)val->dv_hashtab.ht_used;
22359 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22360 {
22361 if (HASHITEM_EMPTY(hi))
22362 continue;
22363 --todo;
22364 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22365 }
22366 }
22367}
22368
22369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 * Set v:register if needed.
22371 */
22372 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022373set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374{
22375 char_u regname;
22376
22377 if (c == 0 || c == ' ')
22378 regname = '"';
22379 else
22380 regname = c;
22381 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022382 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 set_vim_var_string(VV_REG, &regname, 1);
22384}
22385
22386/*
22387 * Get or set v:exception. If "oldval" == NULL, return the current value.
22388 * Otherwise, restore the value to "oldval" and return NULL.
22389 * Must always be called in pairs to save and restore v:exception! Does not
22390 * take care of memory allocations.
22391 */
22392 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022393v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394{
22395 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022396 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397
Bram Moolenaare9a41262005-01-15 22:18:47 +000022398 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 return NULL;
22400}
22401
22402/*
22403 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22404 * Otherwise, restore the value to "oldval" and return NULL.
22405 * Must always be called in pairs to save and restore v:throwpoint! Does not
22406 * take care of memory allocations.
22407 */
22408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022409v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410{
22411 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022412 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413
Bram Moolenaare9a41262005-01-15 22:18:47 +000022414 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 return NULL;
22416}
22417
22418#if defined(FEAT_AUTOCMD) || defined(PROTO)
22419/*
22420 * Set v:cmdarg.
22421 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22422 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22423 * Must always be called in pairs!
22424 */
22425 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022426set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427{
22428 char_u *oldval;
22429 char_u *newval;
22430 unsigned len;
22431
Bram Moolenaare9a41262005-01-15 22:18:47 +000022432 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022433 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022435 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022436 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022437 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 }
22439
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022440 if (eap->force_bin == FORCE_BIN)
22441 len = 6;
22442 else if (eap->force_bin == FORCE_NOBIN)
22443 len = 8;
22444 else
22445 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022446
22447 if (eap->read_edit)
22448 len += 7;
22449
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022450 if (eap->force_ff != 0)
22451 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22452# ifdef FEAT_MBYTE
22453 if (eap->force_enc != 0)
22454 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022455 if (eap->bad_char != 0)
22456 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022457# endif
22458
22459 newval = alloc(len + 1);
22460 if (newval == NULL)
22461 return NULL;
22462
22463 if (eap->force_bin == FORCE_BIN)
22464 sprintf((char *)newval, " ++bin");
22465 else if (eap->force_bin == FORCE_NOBIN)
22466 sprintf((char *)newval, " ++nobin");
22467 else
22468 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022469
22470 if (eap->read_edit)
22471 STRCAT(newval, " ++edit");
22472
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022473 if (eap->force_ff != 0)
22474 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22475 eap->cmd + eap->force_ff);
22476# ifdef FEAT_MBYTE
22477 if (eap->force_enc != 0)
22478 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22479 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022480 if (eap->bad_char == BAD_KEEP)
22481 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22482 else if (eap->bad_char == BAD_DROP)
22483 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22484 else if (eap->bad_char != 0)
22485 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022486# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022487 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022488 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489}
22490#endif
22491
22492/*
22493 * Get the value of internal variable "name".
22494 * Return OK or FAIL.
22495 */
22496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022497get_var_tv(
22498 char_u *name,
22499 int len, /* length of "name" */
22500 typval_T *rettv, /* NULL when only checking existence */
22501 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22502 int verbose, /* may give error message */
22503 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504{
22505 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 typval_T *tv = NULL;
22507 typval_T atv;
22508 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510
22511 /* truncate the name, so that we can use strcmp() */
22512 cc = name[len];
22513 name[len] = NUL;
22514
22515 /*
22516 * Check for "b:changedtick".
22517 */
22518 if (STRCMP(name, "b:changedtick") == 0)
22519 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022520 atv.v_type = VAR_NUMBER;
22521 atv.vval.v_number = curbuf->b_changedtick;
22522 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 }
22524
22525 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 * Check for user-defined variables.
22527 */
22528 else
22529 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022530 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022532 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022534 if (dip != NULL)
22535 *dip = v;
22536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 }
22538
Bram Moolenaare9a41262005-01-15 22:18:47 +000022539 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022541 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022542 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 ret = FAIL;
22544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022545 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022546 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547
22548 name[len] = cc;
22549
22550 return ret;
22551}
22552
22553/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022554 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22555 * Also handle function call with Funcref variable: func(expr)
22556 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22557 */
22558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022559handle_subscript(
22560 char_u **arg,
22561 typval_T *rettv,
22562 int evaluate, /* do more than finding the end */
22563 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022564{
22565 int ret = OK;
22566 dict_T *selfdict = NULL;
22567 char_u *s;
22568 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022569 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022570
22571 while (ret == OK
22572 && (**arg == '['
22573 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022574 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22575 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022576 && !vim_iswhite(*(*arg - 1)))
22577 {
22578 if (**arg == '(')
22579 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022580 partial_T *pt = NULL;
22581
Bram Moolenaard9fba312005-06-26 22:34:35 +000022582 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022583 if (evaluate)
22584 {
22585 functv = *rettv;
22586 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022587
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022588 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022589 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022590 {
22591 pt = functv.vval.v_partial;
22592 s = pt->pt_name;
22593 }
22594 else
22595 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022596 }
22597 else
22598 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022599 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022600 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022601 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022602
22603 /* Clear the funcref afterwards, so that deleting it while
22604 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022605 if (evaluate)
22606 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022607
22608 /* Stop the expression evaluation when immediately aborting on
22609 * error, or when an interrupt occurred or an exception was thrown
22610 * but not caught. */
22611 if (aborting())
22612 {
22613 if (ret == OK)
22614 clear_tv(rettv);
22615 ret = FAIL;
22616 }
22617 dict_unref(selfdict);
22618 selfdict = NULL;
22619 }
22620 else /* **arg == '[' || **arg == '.' */
22621 {
22622 dict_unref(selfdict);
22623 if (rettv->v_type == VAR_DICT)
22624 {
22625 selfdict = rettv->vval.v_dict;
22626 if (selfdict != NULL)
22627 ++selfdict->dv_refcount;
22628 }
22629 else
22630 selfdict = NULL;
22631 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22632 {
22633 clear_tv(rettv);
22634 ret = FAIL;
22635 }
22636 }
22637 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022638
Bram Moolenaar1d429612016-05-24 15:44:17 +020022639 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22640 * Don't do this when "Func" is already a partial that was bound
22641 * explicitly (pt_auto is FALSE). */
22642 if (selfdict != NULL
22643 && (rettv->v_type == VAR_FUNC
22644 || (rettv->v_type == VAR_PARTIAL
22645 && (rettv->vval.v_partial->pt_auto
22646 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022647 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022648 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22649 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022650 char_u *tofree = NULL;
22651 ufunc_T *fp;
22652 char_u fname_buf[FLEN_FIXED + 1];
22653 int error;
22654
22655 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022656 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022657 fp = find_func(fname);
22658 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022659
Bram Moolenaar65639032016-03-16 21:40:30 +010022660 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022661 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022662 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22663
22664 if (pt != NULL)
22665 {
22666 pt->pt_refcount = 1;
22667 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022668 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022669 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022670 if (rettv->v_type == VAR_FUNC)
22671 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022672 /* Just a function: Take over the function name and use
22673 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022674 pt->pt_name = rettv->vval.v_string;
22675 }
22676 else
22677 {
22678 partial_T *ret_pt = rettv->vval.v_partial;
22679 int i;
22680
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022681 /* Partial: copy the function name, use selfdict and copy
22682 * args. Can't take over name or args, the partial might
22683 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022684 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022685 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022686 if (ret_pt->pt_argc > 0)
22687 {
22688 pt->pt_argv = (typval_T *)alloc(
22689 sizeof(typval_T) * ret_pt->pt_argc);
22690 if (pt->pt_argv == NULL)
22691 /* out of memory: drop the arguments */
22692 pt->pt_argc = 0;
22693 else
22694 {
22695 pt->pt_argc = ret_pt->pt_argc;
22696 for (i = 0; i < pt->pt_argc; i++)
22697 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22698 }
22699 }
22700 partial_unref(ret_pt);
22701 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022702 rettv->v_type = VAR_PARTIAL;
22703 rettv->vval.v_partial = pt;
22704 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022705 }
22706 }
22707
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022708 dict_unref(selfdict);
22709 return ret;
22710}
22711
22712/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022713 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022714 * value).
22715 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022716 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022717alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022718{
Bram Moolenaar33570922005-01-25 22:26:29 +000022719 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022720}
22721
22722/*
22723 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 * The string "s" must have been allocated, it is consumed.
22725 * Return NULL for out of memory, the variable otherwise.
22726 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022728alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729{
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022732 rettv = alloc_tv();
22733 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022735 rettv->v_type = VAR_STRING;
22736 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 }
22738 else
22739 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022740 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741}
22742
22743/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022744 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022747free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748{
22749 if (varp != NULL)
22750 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022751 switch (varp->v_type)
22752 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022753 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022754 func_unref(varp->vval.v_string);
22755 /*FALLTHROUGH*/
22756 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022757 vim_free(varp->vval.v_string);
22758 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022759 case VAR_PARTIAL:
22760 partial_unref(varp->vval.v_partial);
22761 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022762 case VAR_LIST:
22763 list_unref(varp->vval.v_list);
22764 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022765 case VAR_DICT:
22766 dict_unref(varp->vval.v_dict);
22767 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022768 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022770 job_unref(varp->vval.v_job);
22771 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022772#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022773 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022774#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022775 channel_unref(varp->vval.v_channel);
22776 break;
22777#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022778 case VAR_NUMBER:
22779 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022780 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022781 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022782 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 vim_free(varp);
22785 }
22786}
22787
22788/*
22789 * Free the memory for a variable value and set the value to NULL or 0.
22790 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022792clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793{
22794 if (varp != NULL)
22795 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022796 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022798 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022799 func_unref(varp->vval.v_string);
22800 /*FALLTHROUGH*/
22801 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022802 vim_free(varp->vval.v_string);
22803 varp->vval.v_string = NULL;
22804 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022805 case VAR_PARTIAL:
22806 partial_unref(varp->vval.v_partial);
22807 varp->vval.v_partial = NULL;
22808 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022809 case VAR_LIST:
22810 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022811 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022812 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022813 case VAR_DICT:
22814 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022815 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022816 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022817 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022818 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022819 varp->vval.v_number = 0;
22820 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022821 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022822#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022823 varp->vval.v_float = 0.0;
22824 break;
22825#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022826 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022827#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022828 job_unref(varp->vval.v_job);
22829 varp->vval.v_job = NULL;
22830#endif
22831 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022832 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022833#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022834 channel_unref(varp->vval.v_channel);
22835 varp->vval.v_channel = NULL;
22836#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022837 case VAR_UNKNOWN:
22838 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022840 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 }
22842}
22843
22844/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022845 * Set the value of a variable to NULL without freeing items.
22846 */
22847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022848init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022849{
22850 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022852}
22853
22854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 * Get the number value of a variable.
22856 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022857 * For incompatible types, return 0.
22858 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22859 * caller of incompatible types: it sets *denote to TRUE if "denote"
22860 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022862 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022863get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022865 int error = FALSE;
22866
22867 return get_tv_number_chk(varp, &error); /* return 0L on error */
22868}
22869
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022870 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022871get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022872{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022873 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022875 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022877 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022878 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022879 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022880#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022881 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022882 break;
22883#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022885 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022886 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022887 break;
22888 case VAR_STRING:
22889 if (varp->vval.v_string != NULL)
22890 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022891 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022892 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022893 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022894 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022895 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022897 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022899 case VAR_SPECIAL:
22900 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22901 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022902 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022904 EMSG(_("E910: Using a Job as a Number"));
22905 break;
22906#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022907 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022908#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022909 EMSG(_("E913: Using a Channel as a Number"));
22910 break;
22911#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022912 case VAR_UNKNOWN:
22913 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022914 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022916 if (denote == NULL) /* useful for values that must be unsigned */
22917 n = -1;
22918 else
22919 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022920 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921}
22922
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022923#ifdef FEAT_FLOAT
22924 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022925get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022926{
22927 switch (varp->v_type)
22928 {
22929 case VAR_NUMBER:
22930 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022931 case VAR_FLOAT:
22932 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022933 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022934 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022935 EMSG(_("E891: Using a Funcref as a Float"));
22936 break;
22937 case VAR_STRING:
22938 EMSG(_("E892: Using a String as a Float"));
22939 break;
22940 case VAR_LIST:
22941 EMSG(_("E893: Using a List as a Float"));
22942 break;
22943 case VAR_DICT:
22944 EMSG(_("E894: Using a Dictionary as a Float"));
22945 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022946 case VAR_SPECIAL:
22947 EMSG(_("E907: Using a special value as a Float"));
22948 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022949 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022950# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022951 EMSG(_("E911: Using a Job as a Float"));
22952 break;
22953# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022954 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022955# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022956 EMSG(_("E914: Using a Channel as a Float"));
22957 break;
22958# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022959 case VAR_UNKNOWN:
22960 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022961 break;
22962 }
22963 return 0;
22964}
22965#endif
22966
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022968 * Get the lnum from the first argument.
22969 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022970 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 */
22972 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022973get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974{
Bram Moolenaar33570922005-01-25 22:26:29 +000022975 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 linenr_T lnum;
22977
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022978 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 if (lnum == 0) /* no valid number, try using line() */
22980 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022981 rettv.v_type = VAR_NUMBER;
22982 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022983 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022984 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 }
22986 return lnum;
22987}
22988
22989/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022990 * Get the lnum from the first argument.
22991 * Also accepts "$", then "buf" is used.
22992 * Returns 0 on error.
22993 */
22994 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022995get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022996{
22997 if (argvars[0].v_type == VAR_STRING
22998 && argvars[0].vval.v_string != NULL
22999 && argvars[0].vval.v_string[0] == '$'
23000 && buf != NULL)
23001 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023002 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023003}
23004
23005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 * Get the string value of a variable.
23007 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023008 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23009 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 * If the String variable has never been set, return an empty string.
23011 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023012 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23013 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023015 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023016get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017{
23018 static char_u mybuf[NUMBUFLEN];
23019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023020 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021}
23022
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023023 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023024get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023026 char_u *res = get_tv_string_buf_chk(varp, buf);
23027
23028 return res != NULL ? res : (char_u *)"";
23029}
23030
Bram Moolenaar7d647822014-04-05 21:28:56 +020023031/*
23032 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23033 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023034 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023036{
23037 static char_u mybuf[NUMBUFLEN];
23038
23039 return get_tv_string_buf_chk(varp, mybuf);
23040}
23041
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023042 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023043get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023044{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023045 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023047 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023048 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23049 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023050 return buf;
23051 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023052 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023053 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023054 break;
23055 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023056 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023057 break;
23058 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023059 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023060 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023061 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023062#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023063 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023064 break;
23065#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023066 case VAR_STRING:
23067 if (varp->vval.v_string != NULL)
23068 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023069 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023070 case VAR_SPECIAL:
23071 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23072 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023073 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023075 {
23076 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023077 char *status;
23078
23079 if (job == NULL)
23080 return (char_u *)"no process";
23081 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023082 : job->jv_status == JOB_ENDED ? "dead"
23083 : "run";
23084# ifdef UNIX
23085 vim_snprintf((char *)buf, NUMBUFLEN,
23086 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023087# elif defined(WIN32)
23088 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023089 "process %ld %s",
23090 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023091 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023092# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023093 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023094 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23095# endif
23096 return buf;
23097 }
23098#endif
23099 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023100 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023101#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023102 {
23103 channel_T *channel = varp->vval.v_channel;
23104 char *status = channel_status(channel);
23105
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023106 if (channel == NULL)
23107 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23108 else
23109 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023110 "channel %d %s", channel->ch_id, status);
23111 return buf;
23112 }
23113#endif
23114 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023115 case VAR_UNKNOWN:
23116 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023119 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120}
23121
23122/*
23123 * Find variable "name" in the list of variables.
23124 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023125 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023126 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023127 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023130find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134
Bram Moolenaara7043832005-01-21 11:56:39 +000023135 ht = find_var_ht(name, &varname);
23136 if (htp != NULL)
23137 *htp = ht;
23138 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023140 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141}
23142
23143/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023144 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023145 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023148find_var_in_ht(
23149 hashtab_T *ht,
23150 int htname,
23151 char_u *varname,
23152 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023153{
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 hashitem_T *hi;
23155
23156 if (*varname == NUL)
23157 {
23158 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023159 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023161 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 case 'g': return &globvars_var;
23163 case 'v': return &vimvars_var;
23164 case 'b': return &curbuf->b_bufvar;
23165 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023166#ifdef FEAT_WINDOWS
23167 case 't': return &curtab->tp_winvar;
23168#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023169 case 'l': return current_funccal == NULL
23170 ? NULL : &current_funccal->l_vars_var;
23171 case 'a': return current_funccal == NULL
23172 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023173 }
23174 return NULL;
23175 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023176
23177 hi = hash_find(ht, varname);
23178 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023179 {
23180 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023181 * worked find the variable again. Don't auto-load a script if it was
23182 * loaded already, otherwise it would be loaded every time when
23183 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023184 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023185 {
23186 /* Note: script_autoload() may make "hi" invalid. It must either
23187 * be obtained again or not used. */
23188 if (!script_autoload(varname, FALSE) || aborting())
23189 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023190 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023191 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023192 if (HASHITEM_EMPTY(hi))
23193 return NULL;
23194 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023195 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023196}
23197
23198/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023199 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023200 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023201 * Set "varname" to the start of name without ':'.
23202 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023204find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023206 hashitem_T *hi;
23207
Bram Moolenaar73627d02015-08-11 15:46:09 +020023208 if (name[0] == NUL)
23209 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 if (name[1] != ':')
23211 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023212 /* The name must not start with a colon or #. */
23213 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 return NULL;
23215 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023216
23217 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023218 hi = hash_find(&compat_hashtab, name);
23219 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023220 return &compat_hashtab;
23221
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023223 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023224 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 }
23226 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023227 if (*name == 'g') /* global variable */
23228 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023229 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23230 */
23231 if (vim_strchr(name + 2, ':') != NULL
23232 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023233 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023235 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023237 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023238#ifdef FEAT_WINDOWS
23239 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023240 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023241#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023242 if (*name == 'v') /* v: variable */
23243 return &vimvarht;
23244 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023245 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023247 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 if (*name == 's' /* script variable */
23249 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23250 return &SCRIPT_VARS(current_SID);
23251 return NULL;
23252}
23253
23254/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023255 * Get function call environment based on bactrace debug level
23256 */
23257 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023258get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023259{
23260 int i;
23261 funccall_T *funccal;
23262 funccall_T *temp_funccal;
23263
23264 funccal = current_funccal;
23265 if (debug_backtrace_level > 0)
23266 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023267 for (i = 0; i < debug_backtrace_level; i++)
23268 {
23269 temp_funccal = funccal->caller;
23270 if (temp_funccal)
23271 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023272 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023273 /* backtrace level overflow. reset to max */
23274 debug_backtrace_level = i;
23275 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023276 }
23277 return funccal;
23278}
23279
23280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023282 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 * Returns NULL when it doesn't exist.
23284 */
23285 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023286get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287{
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023290 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 if (v == NULL)
23292 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294}
23295
23296/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 * sourcing this script and when executing functions defined in the script.
23299 */
23300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023301new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302{
Bram Moolenaara7043832005-01-21 11:56:39 +000023303 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023304 hashtab_T *ht;
23305 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023306
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23308 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023309 /* Re-allocating ga_data means that an ht_array pointing to
23310 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023312 for (i = 1; i <= ga_scripts.ga_len; ++i)
23313 {
23314 ht = &SCRIPT_VARS(i);
23315 if (ht->ht_mask == HT_INIT_SIZE - 1)
23316 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023317 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023318 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023319 }
23320
Bram Moolenaar071d4272004-06-13 20:20:40 +000023321 while (ga_scripts.ga_len < id)
23322 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023323 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023324 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023325 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 }
23328 }
23329}
23330
23331/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023332 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23333 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 */
23335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023336init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337{
Bram Moolenaar33570922005-01-25 22:26:29 +000023338 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023339 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023340 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023341 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023342 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023343 dict_var->di_tv.vval.v_dict = dict;
23344 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023345 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23347 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348}
23349
23350/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023351 * Unreference a dictionary initialized by init_var_dict().
23352 */
23353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023354unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023355{
23356 /* Now the dict needs to be freed if no one else is using it, go back to
23357 * normal reference counting. */
23358 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23359 dict_unref(dict);
23360}
23361
23362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023364 * Frees all allocated variables and the value they contain.
23365 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 */
23367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023368vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023369{
23370 vars_clear_ext(ht, TRUE);
23371}
23372
23373/*
23374 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23375 */
23376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023377vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378{
Bram Moolenaara7043832005-01-21 11:56:39 +000023379 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023380 hashitem_T *hi;
23381 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382
Bram Moolenaar33570922005-01-25 22:26:29 +000023383 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023384 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023385 for (hi = ht->ht_array; todo > 0; ++hi)
23386 {
23387 if (!HASHITEM_EMPTY(hi))
23388 {
23389 --todo;
23390
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023392 * ht_array might change then. hash_clear() takes care of it
23393 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 v = HI2DI(hi);
23395 if (free_val)
23396 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023397 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023398 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023399 }
23400 }
23401 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023402 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403}
23404
Bram Moolenaara7043832005-01-21 11:56:39 +000023405/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023406 * Delete a variable from hashtab "ht" at item "hi".
23407 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023410delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411{
Bram Moolenaar33570922005-01-25 22:26:29 +000023412 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023413
23414 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023415 clear_tv(&di->di_tv);
23416 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417}
23418
23419/*
23420 * List the value of one internal variable.
23421 */
23422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023423list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023425 char_u *tofree;
23426 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023427 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023428
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023429 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023430 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023431 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023432 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433}
23434
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023436list_one_var_a(
23437 char_u *prefix,
23438 char_u *name,
23439 int type,
23440 char_u *string,
23441 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442{
Bram Moolenaar31859182007-08-14 20:41:13 +000023443 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23444 msg_start();
23445 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 if (name != NULL) /* "a:" vars don't have a name stored */
23447 msg_puts(name);
23448 msg_putchar(' ');
23449 msg_advance(22);
23450 if (type == VAR_NUMBER)
23451 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023452 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023453 msg_putchar('*');
23454 else if (type == VAR_LIST)
23455 {
23456 msg_putchar('[');
23457 if (*string == '[')
23458 ++string;
23459 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023460 else if (type == VAR_DICT)
23461 {
23462 msg_putchar('{');
23463 if (*string == '{')
23464 ++string;
23465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023466 else
23467 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023468
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023470
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023471 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023472 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023473 if (*first)
23474 {
23475 msg_clr_eos();
23476 *first = FALSE;
23477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478}
23479
23480/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023481 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 * If the variable already exists, the value is updated.
23483 * Otherwise the variable is created.
23484 */
23485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023486set_var(
23487 char_u *name,
23488 typval_T *tv,
23489 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490{
Bram Moolenaar33570922005-01-25 22:26:29 +000023491 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023493 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023495 ht = find_var_ht(name, &varname);
23496 if (ht == NULL || *varname == NUL)
23497 {
23498 EMSG2(_(e_illvar), name);
23499 return;
23500 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023501 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023502
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023503 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23504 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023505 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023506
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023509 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023510 if (var_check_ro(v->di_flags, name, FALSE)
23511 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023512 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023513
23514 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023515 * Handle setting internal v: variables separately where needed to
23516 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023517 */
23518 if (ht == &vimvarht)
23519 {
23520 if (v->di_tv.v_type == VAR_STRING)
23521 {
23522 vim_free(v->di_tv.vval.v_string);
23523 if (copy || tv->v_type != VAR_STRING)
23524 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23525 else
23526 {
23527 /* Take over the string to avoid an extra alloc/free. */
23528 v->di_tv.vval.v_string = tv->vval.v_string;
23529 tv->vval.v_string = NULL;
23530 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023531 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023532 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023533 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023534 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023535 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023536 if (STRCMP(varname, "searchforward") == 0)
23537 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023538#ifdef FEAT_SEARCH_EXTRA
23539 else if (STRCMP(varname, "hlsearch") == 0)
23540 {
23541 no_hlsearch = !v->di_tv.vval.v_number;
23542 redraw_all_later(SOME_VALID);
23543 }
23544#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023545 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023546 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023547 else if (v->di_tv.v_type != tv->v_type)
23548 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023549 }
23550
23551 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 }
23553 else /* add a new variable */
23554 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023555 /* Can't add "v:" variable. */
23556 if (ht == &vimvarht)
23557 {
23558 EMSG2(_(e_illvar), name);
23559 return;
23560 }
23561
Bram Moolenaar92124a32005-06-17 22:03:40 +000023562 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023563 if (!valid_varname(varname))
23564 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023565
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023566 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23567 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023568 if (v == NULL)
23569 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023570 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023571 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023573 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574 return;
23575 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023576 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023578
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023579 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023580 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023581 else
23582 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023583 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023584 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023585 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587}
23588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023589/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023590 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023591 * Also give an error message.
23592 */
23593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023594var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023595{
23596 if (flags & DI_FLAGS_RO)
23597 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023598 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023599 return TRUE;
23600 }
23601 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23602 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023603 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023604 return TRUE;
23605 }
23606 return FALSE;
23607}
23608
23609/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023610 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23611 * Also give an error message.
23612 */
23613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023614var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023615{
23616 if (flags & DI_FLAGS_FIX)
23617 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023618 EMSG2(_("E795: Cannot delete variable %s"),
23619 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023620 return TRUE;
23621 }
23622 return FALSE;
23623}
23624
23625/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023626 * Check if a funcref is assigned to a valid variable name.
23627 * Return TRUE and give an error if not.
23628 */
23629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023630var_check_func_name(
23631 char_u *name, /* points to start of variable name */
23632 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023633{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023634 /* Allow for w: b: s: and t:. */
23635 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023636 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23637 ? name[2] : name[0]))
23638 {
23639 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23640 name);
23641 return TRUE;
23642 }
23643 /* Don't allow hiding a function. When "v" is not NULL we might be
23644 * assigning another function to the same var, the type is checked
23645 * below. */
23646 if (new_var && function_exists(name))
23647 {
23648 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23649 name);
23650 return TRUE;
23651 }
23652 return FALSE;
23653}
23654
23655/*
23656 * Check if a variable name is valid.
23657 * Return FALSE and give an error if not.
23658 */
23659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023660valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023661{
23662 char_u *p;
23663
23664 for (p = varname; *p != NUL; ++p)
23665 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23666 && *p != AUTOLOAD_CHAR)
23667 {
23668 EMSG2(_(e_illvar), varname);
23669 return FALSE;
23670 }
23671 return TRUE;
23672}
23673
23674/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023675 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023676 * Also give an error message, using "name" or _("name") when use_gettext is
23677 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023678 */
23679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023680tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023681{
23682 if (lock & VAR_LOCKED)
23683 {
23684 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023685 name == NULL ? (char_u *)_("Unknown")
23686 : use_gettext ? (char_u *)_(name)
23687 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023688 return TRUE;
23689 }
23690 if (lock & VAR_FIXED)
23691 {
23692 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023693 name == NULL ? (char_u *)_("Unknown")
23694 : use_gettext ? (char_u *)_(name)
23695 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023696 return TRUE;
23697 }
23698 return FALSE;
23699}
23700
23701/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023702 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023703 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023704 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023705 * It is OK for "from" and "to" to point to the same item. This is used to
23706 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023707 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023709copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023711 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023712 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023713 switch (from->v_type)
23714 {
23715 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023716 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023717 to->vval.v_number = from->vval.v_number;
23718 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023719 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023720#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023721 to->vval.v_float = from->vval.v_float;
23722 break;
23723#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023724 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023725#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023726 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023727 if (to->vval.v_job != NULL)
23728 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023729 break;
23730#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023731 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023732#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023733 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023734 if (to->vval.v_channel != NULL)
23735 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023736 break;
23737#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023738 case VAR_STRING:
23739 case VAR_FUNC:
23740 if (from->vval.v_string == NULL)
23741 to->vval.v_string = NULL;
23742 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023743 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023744 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023745 if (from->v_type == VAR_FUNC)
23746 func_ref(to->vval.v_string);
23747 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023748 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023749 case VAR_PARTIAL:
23750 if (from->vval.v_partial == NULL)
23751 to->vval.v_partial = NULL;
23752 else
23753 {
23754 to->vval.v_partial = from->vval.v_partial;
23755 ++to->vval.v_partial->pt_refcount;
23756 }
23757 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023758 case VAR_LIST:
23759 if (from->vval.v_list == NULL)
23760 to->vval.v_list = NULL;
23761 else
23762 {
23763 to->vval.v_list = from->vval.v_list;
23764 ++to->vval.v_list->lv_refcount;
23765 }
23766 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023767 case VAR_DICT:
23768 if (from->vval.v_dict == NULL)
23769 to->vval.v_dict = NULL;
23770 else
23771 {
23772 to->vval.v_dict = from->vval.v_dict;
23773 ++to->vval.v_dict->dv_refcount;
23774 }
23775 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023776 case VAR_UNKNOWN:
23777 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023778 break;
23779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780}
23781
23782/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023783 * Make a copy of an item.
23784 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023785 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23786 * reference to an already copied list/dict can be used.
23787 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023788 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023790item_copy(
23791 typval_T *from,
23792 typval_T *to,
23793 int deep,
23794 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023795{
23796 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023797 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023798
Bram Moolenaar33570922005-01-25 22:26:29 +000023799 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023800 {
23801 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023802 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023803 }
23804 ++recurse;
23805
23806 switch (from->v_type)
23807 {
23808 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023809 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023810 case VAR_STRING:
23811 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023812 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023813 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023814 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023815 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023816 copy_tv(from, to);
23817 break;
23818 case VAR_LIST:
23819 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023820 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023821 if (from->vval.v_list == NULL)
23822 to->vval.v_list = NULL;
23823 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23824 {
23825 /* use the copy made earlier */
23826 to->vval.v_list = from->vval.v_list->lv_copylist;
23827 ++to->vval.v_list->lv_refcount;
23828 }
23829 else
23830 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23831 if (to->vval.v_list == NULL)
23832 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023833 break;
23834 case VAR_DICT:
23835 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023836 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023837 if (from->vval.v_dict == NULL)
23838 to->vval.v_dict = NULL;
23839 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23840 {
23841 /* use the copy made earlier */
23842 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23843 ++to->vval.v_dict->dv_refcount;
23844 }
23845 else
23846 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23847 if (to->vval.v_dict == NULL)
23848 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023849 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023850 case VAR_UNKNOWN:
23851 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023852 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023853 }
23854 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023855 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023856}
23857
23858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 * ":echo expr1 ..." print each argument separated with a space, add a
23860 * newline at the end.
23861 * ":echon expr1 ..." print each argument plain.
23862 */
23863 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023864ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865{
23866 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023867 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023868 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 char_u *p;
23870 int needclr = TRUE;
23871 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023872 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873
23874 if (eap->skip)
23875 ++emsg_skip;
23876 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23877 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023878 /* If eval1() causes an error message the text from the command may
23879 * still need to be cleared. E.g., "echo 22,44". */
23880 need_clr_eos = needclr;
23881
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023883 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884 {
23885 /*
23886 * Report the invalid expression unless the expression evaluation
23887 * has been cancelled due to an aborting error, an interrupt, or an
23888 * exception.
23889 */
23890 if (!aborting())
23891 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023892 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 break;
23894 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023895 need_clr_eos = FALSE;
23896
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 if (!eap->skip)
23898 {
23899 if (atstart)
23900 {
23901 atstart = FALSE;
23902 /* Call msg_start() after eval1(), evaluating the expression
23903 * may cause a message to appear. */
23904 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023905 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023906 /* Mark the saved text as finishing the line, so that what
23907 * follows is displayed on a new line when scrolling back
23908 * at the more prompt. */
23909 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 }
23913 else if (eap->cmdidx == CMD_echo)
23914 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023915 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023916 if (p != NULL)
23917 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023919 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023921 if (*p != TAB && needclr)
23922 {
23923 /* remove any text still there from the command */
23924 msg_clr_eos();
23925 needclr = FALSE;
23926 }
23927 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 }
23929 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023930 {
23931#ifdef FEAT_MBYTE
23932 if (has_mbyte)
23933 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023934 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023935
23936 (void)msg_outtrans_len_attr(p, i, echo_attr);
23937 p += i - 1;
23938 }
23939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023941 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023944 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023946 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 arg = skipwhite(arg);
23948 }
23949 eap->nextcmd = check_nextcmd(arg);
23950
23951 if (eap->skip)
23952 --emsg_skip;
23953 else
23954 {
23955 /* remove text that may still be there from the command */
23956 if (needclr)
23957 msg_clr_eos();
23958 if (eap->cmdidx == CMD_echo)
23959 msg_end();
23960 }
23961}
23962
23963/*
23964 * ":echohl {name}".
23965 */
23966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023967ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968{
23969 int id;
23970
23971 id = syn_name2id(eap->arg);
23972 if (id == 0)
23973 echo_attr = 0;
23974 else
23975 echo_attr = syn_id2attr(id);
23976}
23977
23978/*
23979 * ":execute expr1 ..." execute the result of an expression.
23980 * ":echomsg expr1 ..." Print a message
23981 * ":echoerr expr1 ..." Print an error
23982 * Each gets spaces around each argument and a newline at the end for
23983 * echo commands
23984 */
23985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023986ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987{
23988 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023989 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 int ret = OK;
23991 char_u *p;
23992 garray_T ga;
23993 int len;
23994 int save_did_emsg;
23995
23996 ga_init2(&ga, 1, 80);
23997
23998 if (eap->skip)
23999 ++emsg_skip;
24000 while (*arg != NUL && *arg != '|' && *arg != '\n')
24001 {
24002 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024003 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 {
24005 /*
24006 * Report the invalid expression unless the expression evaluation
24007 * has been cancelled due to an aborting error, an interrupt, or an
24008 * exception.
24009 */
24010 if (!aborting())
24011 EMSG2(_(e_invexpr2), p);
24012 ret = FAIL;
24013 break;
24014 }
24015
24016 if (!eap->skip)
24017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024018 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 len = (int)STRLEN(p);
24020 if (ga_grow(&ga, len + 2) == FAIL)
24021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024022 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 ret = FAIL;
24024 break;
24025 }
24026 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 ga.ga_len += len;
24030 }
24031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024032 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 arg = skipwhite(arg);
24034 }
24035
24036 if (ret != FAIL && ga.ga_data != NULL)
24037 {
24038 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024041 out_flush();
24042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 else if (eap->cmdidx == CMD_echoerr)
24044 {
24045 /* We don't want to abort following commands, restore did_emsg. */
24046 save_did_emsg = did_emsg;
24047 EMSG((char_u *)ga.ga_data);
24048 if (!force_abort)
24049 did_emsg = save_did_emsg;
24050 }
24051 else if (eap->cmdidx == CMD_execute)
24052 do_cmdline((char_u *)ga.ga_data,
24053 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24054 }
24055
24056 ga_clear(&ga);
24057
24058 if (eap->skip)
24059 --emsg_skip;
24060
24061 eap->nextcmd = check_nextcmd(arg);
24062}
24063
24064/*
24065 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24066 * "arg" points to the "&" or '+' when called, to "option" when returning.
24067 * Returns NULL when no option name found. Otherwise pointer to the char
24068 * after the option name.
24069 */
24070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024071find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072{
24073 char_u *p = *arg;
24074
24075 ++p;
24076 if (*p == 'g' && p[1] == ':')
24077 {
24078 *opt_flags = OPT_GLOBAL;
24079 p += 2;
24080 }
24081 else if (*p == 'l' && p[1] == ':')
24082 {
24083 *opt_flags = OPT_LOCAL;
24084 p += 2;
24085 }
24086 else
24087 *opt_flags = 0;
24088
24089 if (!ASCII_ISALPHA(*p))
24090 return NULL;
24091 *arg = p;
24092
24093 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24094 p += 4; /* termcap option */
24095 else
24096 while (ASCII_ISALPHA(*p))
24097 ++p;
24098 return p;
24099}
24100
24101/*
24102 * ":function"
24103 */
24104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024105ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106{
24107 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 int j;
24110 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024112 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 char_u *name = NULL;
24114 char_u *p;
24115 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024116 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 garray_T newargs;
24118 garray_T newlines;
24119 int varargs = FALSE;
24120 int mustend = FALSE;
24121 int flags = 0;
24122 ufunc_T *fp;
24123 int indent;
24124 int nesting;
24125 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024126 dictitem_T *v;
24127 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024128 static int func_nr = 0; /* number for nameless function */
24129 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024130 hashtab_T *ht;
24131 int todo;
24132 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024133 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134
24135 /*
24136 * ":function" without argument: list functions.
24137 */
24138 if (ends_excmd(*eap->arg))
24139 {
24140 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024141 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024142 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024143 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024144 {
24145 if (!HASHITEM_EMPTY(hi))
24146 {
24147 --todo;
24148 fp = HI2UF(hi);
24149 if (!isdigit(*fp->uf_name))
24150 list_func_head(fp, FALSE);
24151 }
24152 }
24153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 eap->nextcmd = check_nextcmd(eap->arg);
24155 return;
24156 }
24157
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024159 * ":function /pat": list functions matching pattern.
24160 */
24161 if (*eap->arg == '/')
24162 {
24163 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24164 if (!eap->skip)
24165 {
24166 regmatch_T regmatch;
24167
24168 c = *p;
24169 *p = NUL;
24170 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24171 *p = c;
24172 if (regmatch.regprog != NULL)
24173 {
24174 regmatch.rm_ic = p_ic;
24175
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024176 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024177 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24178 {
24179 if (!HASHITEM_EMPTY(hi))
24180 {
24181 --todo;
24182 fp = HI2UF(hi);
24183 if (!isdigit(*fp->uf_name)
24184 && vim_regexec(&regmatch, fp->uf_name, 0))
24185 list_func_head(fp, FALSE);
24186 }
24187 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024188 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024189 }
24190 }
24191 if (*p == '/')
24192 ++p;
24193 eap->nextcmd = check_nextcmd(p);
24194 return;
24195 }
24196
24197 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024198 * Get the function name. There are these situations:
24199 * func normal function name
24200 * "name" == func, "fudi.fd_dict" == NULL
24201 * dict.func new dictionary entry
24202 * "name" == NULL, "fudi.fd_dict" set,
24203 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24204 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024205 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024206 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24207 * dict.func existing dict entry that's not a Funcref
24208 * "name" == NULL, "fudi.fd_dict" set,
24209 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024210 * s:func script-local function name
24211 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024214 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024215 paren = (vim_strchr(p, '(') != NULL);
24216 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 {
24218 /*
24219 * Return on an invalid expression in braces, unless the expression
24220 * evaluation has been cancelled due to an aborting error, an
24221 * interrupt, or an exception.
24222 */
24223 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024224 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024225 if (!eap->skip && fudi.fd_newkey != NULL)
24226 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024227 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024230 else
24231 eap->skip = TRUE;
24232 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024233
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 /* An error in a function call during evaluation of an expression in magic
24235 * braces should not cause the function not to be defined. */
24236 saved_did_emsg = did_emsg;
24237 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238
24239 /*
24240 * ":function func" with only function name: list function.
24241 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024242 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024243 {
24244 if (!ends_excmd(*skipwhite(p)))
24245 {
24246 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248 }
24249 eap->nextcmd = check_nextcmd(p);
24250 if (eap->nextcmd != NULL)
24251 *p = NUL;
24252 if (!eap->skip && !got_int)
24253 {
24254 fp = find_func(name);
24255 if (fp != NULL)
24256 {
24257 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024258 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024259 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024260 if (FUNCLINE(fp, j) == NULL)
24261 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 msg_putchar('\n');
24263 msg_outnum((long)(j + 1));
24264 if (j < 9)
24265 msg_putchar(' ');
24266 if (j < 99)
24267 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024268 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 out_flush(); /* show a line at a time */
24270 ui_breakcheck();
24271 }
24272 if (!got_int)
24273 {
24274 msg_putchar('\n');
24275 msg_puts((char_u *)" endfunction");
24276 }
24277 }
24278 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024279 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024281 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 }
24283
24284 /*
24285 * ":function name(arg1, arg2)" Define function.
24286 */
24287 p = skipwhite(p);
24288 if (*p != '(')
24289 {
24290 if (!eap->skip)
24291 {
24292 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024293 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 }
24295 /* attempt to continue by skipping some text */
24296 if (vim_strchr(p, '(') != NULL)
24297 p = vim_strchr(p, '(');
24298 }
24299 p = skipwhite(p + 1);
24300
24301 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24302 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24303
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024304 if (!eap->skip)
24305 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024306 /* Check the name of the function. Unless it's a dictionary function
24307 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024308 if (name != NULL)
24309 arg = name;
24310 else
24311 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024312 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024313 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24314 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024315 {
24316 if (*arg == K_SPECIAL)
24317 j = 3;
24318 else
24319 j = 0;
24320 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24321 : eval_isnamec(arg[j])))
24322 ++j;
24323 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024324 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024325 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024326 /* Disallow using the g: dict. */
24327 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24328 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024329 }
24330
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331 /*
24332 * Isolate the arguments: "arg1, arg2, ...)"
24333 */
24334 while (*p != ')')
24335 {
24336 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24337 {
24338 varargs = TRUE;
24339 p += 3;
24340 mustend = TRUE;
24341 }
24342 else
24343 {
24344 arg = p;
24345 while (ASCII_ISALNUM(*p) || *p == '_')
24346 ++p;
24347 if (arg == p || isdigit(*arg)
24348 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24349 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24350 {
24351 if (!eap->skip)
24352 EMSG2(_("E125: Illegal argument: %s"), arg);
24353 break;
24354 }
24355 if (ga_grow(&newargs, 1) == FAIL)
24356 goto erret;
24357 c = *p;
24358 *p = NUL;
24359 arg = vim_strsave(arg);
24360 if (arg == NULL)
24361 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024362
24363 /* Check for duplicate argument name. */
24364 for (i = 0; i < newargs.ga_len; ++i)
24365 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24366 {
24367 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024368 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024369 goto erret;
24370 }
24371
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24373 *p = c;
24374 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 if (*p == ',')
24376 ++p;
24377 else
24378 mustend = TRUE;
24379 }
24380 p = skipwhite(p);
24381 if (mustend && *p != ')')
24382 {
24383 if (!eap->skip)
24384 EMSG2(_(e_invarg2), eap->arg);
24385 break;
24386 }
24387 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024388 if (*p != ')')
24389 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024390 ++p; /* skip the ')' */
24391
Bram Moolenaare9a41262005-01-15 22:18:47 +000024392 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 for (;;)
24394 {
24395 p = skipwhite(p);
24396 if (STRNCMP(p, "range", 5) == 0)
24397 {
24398 flags |= FC_RANGE;
24399 p += 5;
24400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024401 else if (STRNCMP(p, "dict", 4) == 0)
24402 {
24403 flags |= FC_DICT;
24404 p += 4;
24405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 else if (STRNCMP(p, "abort", 5) == 0)
24407 {
24408 flags |= FC_ABORT;
24409 p += 5;
24410 }
24411 else
24412 break;
24413 }
24414
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024415 /* When there is a line break use what follows for the function body.
24416 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24417 if (*p == '\n')
24418 line_arg = p + 1;
24419 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 EMSG(_(e_trailing));
24421
24422 /*
24423 * Read the body of the function, until ":endfunction" is found.
24424 */
24425 if (KeyTyped)
24426 {
24427 /* Check if the function already exists, don't let the user type the
24428 * whole function before telling him it doesn't work! For a script we
24429 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024430 if (!eap->skip && !eap->forceit)
24431 {
24432 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24433 EMSG(_(e_funcdict));
24434 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024435 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024438 if (!eap->skip && did_emsg)
24439 goto erret;
24440
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441 msg_putchar('\n'); /* don't overwrite the function name */
24442 cmdline_row = msg_row;
24443 }
24444
24445 indent = 2;
24446 nesting = 0;
24447 for (;;)
24448 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024449 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024450 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024451 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024452 saved_wait_return = FALSE;
24453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024455 sourcing_lnum_off = sourcing_lnum;
24456
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024457 if (line_arg != NULL)
24458 {
24459 /* Use eap->arg, split up in parts by line breaks. */
24460 theline = line_arg;
24461 p = vim_strchr(theline, '\n');
24462 if (p == NULL)
24463 line_arg += STRLEN(line_arg);
24464 else
24465 {
24466 *p = NUL;
24467 line_arg = p + 1;
24468 }
24469 }
24470 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471 theline = getcmdline(':', 0L, indent);
24472 else
24473 theline = eap->getline(':', eap->cookie, indent);
24474 if (KeyTyped)
24475 lines_left = Rows - 1;
24476 if (theline == NULL)
24477 {
24478 EMSG(_("E126: Missing :endfunction"));
24479 goto erret;
24480 }
24481
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024482 /* Detect line continuation: sourcing_lnum increased more than one. */
24483 if (sourcing_lnum > sourcing_lnum_off + 1)
24484 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24485 else
24486 sourcing_lnum_off = 0;
24487
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 if (skip_until != NULL)
24489 {
24490 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24491 * don't check for ":endfunc". */
24492 if (STRCMP(theline, skip_until) == 0)
24493 {
24494 vim_free(skip_until);
24495 skip_until = NULL;
24496 }
24497 }
24498 else
24499 {
24500 /* skip ':' and blanks*/
24501 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24502 ;
24503
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024504 /* Check for "endfunction". */
24505 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024507 if (line_arg == NULL)
24508 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024509 break;
24510 }
24511
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024512 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 * at "end". */
24514 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24515 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024516 else if (STRNCMP(p, "if", 2) == 0
24517 || STRNCMP(p, "wh", 2) == 0
24518 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519 || STRNCMP(p, "try", 3) == 0)
24520 indent += 2;
24521
24522 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024523 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024525 if (*p == '!')
24526 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024528 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024529 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024531 ++nesting;
24532 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 }
24534 }
24535
24536 /* Check for ":append" or ":insert". */
24537 p = skip_range(p, NULL);
24538 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24539 || (p[0] == 'i'
24540 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24541 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24542 skip_until = vim_strsave((char_u *)".");
24543
24544 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24545 arg = skipwhite(skiptowhite(p));
24546 if (arg[0] == '<' && arg[1] =='<'
24547 && ((p[0] == 'p' && p[1] == 'y'
24548 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24549 || (p[0] == 'p' && p[1] == 'e'
24550 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24551 || (p[0] == 't' && p[1] == 'c'
24552 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024553 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24554 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24556 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024557 || (p[0] == 'm' && p[1] == 'z'
24558 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 ))
24560 {
24561 /* ":python <<" continues until a dot, like ":append" */
24562 p = skipwhite(arg + 2);
24563 if (*p == NUL)
24564 skip_until = vim_strsave((char_u *)".");
24565 else
24566 skip_until = vim_strsave(p);
24567 }
24568 }
24569
24570 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024571 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024572 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024573 if (line_arg == NULL)
24574 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024576 }
24577
24578 /* Copy the line to newly allocated memory. get_one_sourceline()
24579 * allocates 250 bytes per line, this saves 80% on average. The cost
24580 * is an extra alloc/free. */
24581 p = vim_strsave(theline);
24582 if (p != NULL)
24583 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024584 if (line_arg == NULL)
24585 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024586 theline = p;
24587 }
24588
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024589 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24590
24591 /* Add NULL lines for continuation lines, so that the line count is
24592 * equal to the index in the growarray. */
24593 while (sourcing_lnum_off-- > 0)
24594 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024595
24596 /* Check for end of eap->arg. */
24597 if (line_arg != NULL && *line_arg == NUL)
24598 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 }
24600
24601 /* Don't define the function when skipping commands or when an error was
24602 * detected. */
24603 if (eap->skip || did_emsg)
24604 goto erret;
24605
24606 /*
24607 * If there are no errors, add the function
24608 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024609 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024610 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024611 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024612 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024613 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024614 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024615 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024616 goto erret;
24617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024618
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024619 fp = find_func(name);
24620 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024622 if (!eap->forceit)
24623 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024624 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024625 goto erret;
24626 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024627 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024628 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024629 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024630 name);
24631 goto erret;
24632 }
24633 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024634 ga_clear_strings(&(fp->uf_args));
24635 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024636 vim_free(name);
24637 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 }
24640 else
24641 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024642 char numbuf[20];
24643
24644 fp = NULL;
24645 if (fudi.fd_newkey == NULL && !eap->forceit)
24646 {
24647 EMSG(_(e_funcdict));
24648 goto erret;
24649 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024650 if (fudi.fd_di == NULL)
24651 {
24652 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024653 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024654 goto erret;
24655 }
24656 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024657 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024658 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024659
24660 /* Give the function a sequential number. Can only be used with a
24661 * Funcref! */
24662 vim_free(name);
24663 sprintf(numbuf, "%d", ++func_nr);
24664 name = vim_strsave((char_u *)numbuf);
24665 if (name == NULL)
24666 goto erret;
24667 }
24668
24669 if (fp == NULL)
24670 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024671 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024672 {
24673 int slen, plen;
24674 char_u *scriptname;
24675
24676 /* Check that the autoload name matches the script name. */
24677 j = FAIL;
24678 if (sourcing_name != NULL)
24679 {
24680 scriptname = autoload_name(name);
24681 if (scriptname != NULL)
24682 {
24683 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024684 plen = (int)STRLEN(p);
24685 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024686 if (slen > plen && fnamecmp(p,
24687 sourcing_name + slen - plen) == 0)
24688 j = OK;
24689 vim_free(scriptname);
24690 }
24691 }
24692 if (j == FAIL)
24693 {
24694 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24695 goto erret;
24696 }
24697 }
24698
24699 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700 if (fp == NULL)
24701 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024702
24703 if (fudi.fd_dict != NULL)
24704 {
24705 if (fudi.fd_di == NULL)
24706 {
24707 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024708 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024709 if (fudi.fd_di == NULL)
24710 {
24711 vim_free(fp);
24712 goto erret;
24713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024714 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24715 {
24716 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024717 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024718 goto erret;
24719 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024720 }
24721 else
24722 /* overwrite existing dict entry */
24723 clear_tv(&fudi.fd_di->di_tv);
24724 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024725 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024726 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024728
24729 /* behave like "dict" was used */
24730 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024731 }
24732
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024734 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024735 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24736 {
24737 vim_free(fp);
24738 goto erret;
24739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024741 fp->uf_args = newargs;
24742 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024743#ifdef FEAT_PROFILE
24744 fp->uf_tml_count = NULL;
24745 fp->uf_tml_total = NULL;
24746 fp->uf_tml_self = NULL;
24747 fp->uf_profiling = FALSE;
24748 if (prof_def_func())
24749 func_do_profile(fp);
24750#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024751 fp->uf_varargs = varargs;
24752 fp->uf_flags = flags;
24753 fp->uf_calls = 0;
24754 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024755 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756
24757erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 ga_clear_strings(&newargs);
24759 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024760ret_free:
24761 vim_free(skip_until);
24762 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024765 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766}
24767
24768/*
24769 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024770 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024772 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024773 * TFN_INT: internal function name OK
24774 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024775 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776 * Advances "pp" to just after the function name (if no error).
24777 */
24778 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024779trans_function_name(
24780 char_u **pp,
24781 int skip, /* only find the end, don't evaluate */
24782 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024783 funcdict_T *fdp, /* return: info about dictionary used */
24784 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024786 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 char_u *start;
24788 char_u *end;
24789 int lead;
24790 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024792 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024793
24794 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024795 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024797
24798 /* Check for hard coded <SNR>: already translated function ID (from a user
24799 * command). */
24800 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24801 && (*pp)[2] == (int)KE_SNR)
24802 {
24803 *pp += 3;
24804 len = get_id_len(pp) + 3;
24805 return vim_strnsave(start, len);
24806 }
24807
24808 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24809 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024811 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024813
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024814 /* Note that TFN_ flags use the same values as GLV_ flags. */
24815 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024816 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024817 if (end == start)
24818 {
24819 if (!skip)
24820 EMSG(_("E129: Function name required"));
24821 goto theend;
24822 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024823 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024824 {
24825 /*
24826 * Report an invalid expression in braces, unless the expression
24827 * evaluation has been cancelled due to an aborting error, an
24828 * interrupt, or an exception.
24829 */
24830 if (!aborting())
24831 {
24832 if (end != NULL)
24833 EMSG2(_(e_invarg2), start);
24834 }
24835 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024836 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024837 goto theend;
24838 }
24839
24840 if (lv.ll_tv != NULL)
24841 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024842 if (fdp != NULL)
24843 {
24844 fdp->fd_dict = lv.ll_dict;
24845 fdp->fd_newkey = lv.ll_newkey;
24846 lv.ll_newkey = NULL;
24847 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024848 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024849 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24850 {
24851 name = vim_strsave(lv.ll_tv->vval.v_string);
24852 *pp = end;
24853 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024854 else if (lv.ll_tv->v_type == VAR_PARTIAL
24855 && lv.ll_tv->vval.v_partial != NULL)
24856 {
24857 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24858 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024859 if (partial != NULL)
24860 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024861 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024862 else
24863 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024864 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24865 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024866 EMSG(_(e_funcref));
24867 else
24868 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024869 name = NULL;
24870 }
24871 goto theend;
24872 }
24873
24874 if (lv.ll_name == NULL)
24875 {
24876 /* Error found, but continue after the function name. */
24877 *pp = end;
24878 goto theend;
24879 }
24880
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024881 /* Check if the name is a Funcref. If so, use the value. */
24882 if (lv.ll_exp_name != NULL)
24883 {
24884 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024885 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024886 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024887 if (name == lv.ll_exp_name)
24888 name = NULL;
24889 }
24890 else
24891 {
24892 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024893 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024894 if (name == *pp)
24895 name = NULL;
24896 }
24897 if (name != NULL)
24898 {
24899 name = vim_strsave(name);
24900 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024901 if (STRNCMP(name, "<SNR>", 5) == 0)
24902 {
24903 /* Change "<SNR>" to the byte sequence. */
24904 name[0] = K_SPECIAL;
24905 name[1] = KS_EXTRA;
24906 name[2] = (int)KE_SNR;
24907 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24908 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024909 goto theend;
24910 }
24911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024912 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024913 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024914 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024915 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24916 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24917 {
24918 /* When there was "s:" already or the name expanded to get a
24919 * leading "s:" then remove it. */
24920 lv.ll_name += 2;
24921 len -= 2;
24922 lead = 2;
24923 }
24924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024925 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024926 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024927 /* skip over "s:" and "g:" */
24928 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024929 lv.ll_name += 2;
24930 len = (int)(end - lv.ll_name);
24931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024932
24933 /*
24934 * Copy the function name to allocated memory.
24935 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24936 * Accept <SNR>123_name() outside a script.
24937 */
24938 if (skip)
24939 lead = 0; /* do nothing */
24940 else if (lead > 0)
24941 {
24942 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024943 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24944 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024945 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024946 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024947 if (current_SID <= 0)
24948 {
24949 EMSG(_(e_usingsid));
24950 goto theend;
24951 }
24952 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24953 lead += (int)STRLEN(sid_buf);
24954 }
24955 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024956 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024957 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024958 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024959 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024960 goto theend;
24961 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024962 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024963 {
24964 char_u *cp = vim_strchr(lv.ll_name, ':');
24965
24966 if (cp != NULL && cp < end)
24967 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024968 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024969 goto theend;
24970 }
24971 }
24972
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024973 name = alloc((unsigned)(len + lead + 1));
24974 if (name != NULL)
24975 {
24976 if (lead > 0)
24977 {
24978 name[0] = K_SPECIAL;
24979 name[1] = KS_EXTRA;
24980 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024981 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024982 STRCPY(name + 3, sid_buf);
24983 }
24984 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024985 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024986 }
24987 *pp = end;
24988
24989theend:
24990 clear_lval(&lv);
24991 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992}
24993
24994/*
24995 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24996 * Return 2 if "p" starts with "s:".
24997 * Return 0 otherwise.
24998 */
24999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025000eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025002 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25003 * the standard library function. */
25004 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25005 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 return 5;
25007 if (p[0] == 's' && p[1] == ':')
25008 return 2;
25009 return 0;
25010}
25011
25012/*
25013 * Return TRUE if "p" starts with "<SID>" or "s:".
25014 * Only works if eval_fname_script() returned non-zero for "p"!
25015 */
25016 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025017eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018{
25019 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25020}
25021
25022/*
25023 * List the head of the function: "name(arg1, arg2)".
25024 */
25025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025026list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027{
25028 int j;
25029
25030 msg_start();
25031 if (indent)
25032 MSG_PUTS(" ");
25033 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025034 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 {
25036 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025037 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038 }
25039 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025040 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025042 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 {
25044 if (j)
25045 MSG_PUTS(", ");
25046 msg_puts(FUNCARG(fp, j));
25047 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025048 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 {
25050 if (j)
25051 MSG_PUTS(", ");
25052 MSG_PUTS("...");
25053 }
25054 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025055 if (fp->uf_flags & FC_ABORT)
25056 MSG_PUTS(" abort");
25057 if (fp->uf_flags & FC_RANGE)
25058 MSG_PUTS(" range");
25059 if (fp->uf_flags & FC_DICT)
25060 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025061 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025062 if (p_verbose > 0)
25063 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025064}
25065
25066/*
25067 * Find a function by name, return pointer to it in ufuncs.
25068 * Return NULL for unknown function.
25069 */
25070 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025071find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025073 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025075 hi = hash_find(&func_hashtab, name);
25076 if (!HASHITEM_EMPTY(hi))
25077 return HI2UF(hi);
25078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079}
25080
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025081#if defined(EXITFREE) || defined(PROTO)
25082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025083free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025084{
25085 hashitem_T *hi;
25086
25087 /* Need to start all over every time, because func_free() may change the
25088 * hash table. */
25089 while (func_hashtab.ht_used > 0)
25090 for (hi = func_hashtab.ht_array; ; ++hi)
25091 if (!HASHITEM_EMPTY(hi))
25092 {
25093 func_free(HI2UF(hi));
25094 break;
25095 }
25096}
25097#endif
25098
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025100translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025101{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025102 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025103 return find_internal_func(name) >= 0;
25104 return find_func(name) != NULL;
25105}
25106
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025107/*
25108 * Return TRUE if a function "name" exists.
25109 */
25110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025111function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025112{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025113 char_u *nm = name;
25114 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025115 int n = FALSE;
25116
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025117 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025118 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025119 nm = skipwhite(nm);
25120
25121 /* Only accept "funcname", "funcname ", "funcname (..." and
25122 * "funcname(...", not "funcname!...". */
25123 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025124 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025125 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025126 return n;
25127}
25128
Bram Moolenaara1544c02013-05-30 12:35:52 +020025129 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025130get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025131{
25132 char_u *nm = name;
25133 char_u *p;
25134
Bram Moolenaar65639032016-03-16 21:40:30 +010025135 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025136
25137 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025138 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025139 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025140
Bram Moolenaara1544c02013-05-30 12:35:52 +020025141 vim_free(p);
25142 return NULL;
25143}
25144
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025145/*
25146 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025147 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25148 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025149 */
25150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025151builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025152{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025153 char_u *p;
25154
25155 if (!ASCII_ISLOWER(name[0]))
25156 return FALSE;
25157 p = vim_strchr(name, AUTOLOAD_CHAR);
25158 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025159}
25160
Bram Moolenaar05159a02005-02-26 23:04:13 +000025161#if defined(FEAT_PROFILE) || defined(PROTO)
25162/*
25163 * Start profiling function "fp".
25164 */
25165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025166func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025167{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025168 int len = fp->uf_lines.ga_len;
25169
25170 if (len == 0)
25171 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025172 fp->uf_tm_count = 0;
25173 profile_zero(&fp->uf_tm_self);
25174 profile_zero(&fp->uf_tm_total);
25175 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025176 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025177 if (fp->uf_tml_total == NULL)
25178 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025179 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025180 if (fp->uf_tml_self == NULL)
25181 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025182 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025183 fp->uf_tml_idx = -1;
25184 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25185 || fp->uf_tml_self == NULL)
25186 return; /* out of memory */
25187
25188 fp->uf_profiling = TRUE;
25189}
25190
25191/*
25192 * Dump the profiling results for all functions in file "fd".
25193 */
25194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025195func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025196{
25197 hashitem_T *hi;
25198 int todo;
25199 ufunc_T *fp;
25200 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025201 ufunc_T **sorttab;
25202 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025204 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025205 if (todo == 0)
25206 return; /* nothing to dump */
25207
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025208 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025209
Bram Moolenaar05159a02005-02-26 23:04:13 +000025210 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25211 {
25212 if (!HASHITEM_EMPTY(hi))
25213 {
25214 --todo;
25215 fp = HI2UF(hi);
25216 if (fp->uf_profiling)
25217 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025218 if (sorttab != NULL)
25219 sorttab[st_len++] = fp;
25220
Bram Moolenaar05159a02005-02-26 23:04:13 +000025221 if (fp->uf_name[0] == K_SPECIAL)
25222 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25223 else
25224 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25225 if (fp->uf_tm_count == 1)
25226 fprintf(fd, "Called 1 time\n");
25227 else
25228 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25229 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25230 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25231 fprintf(fd, "\n");
25232 fprintf(fd, "count total (s) self (s)\n");
25233
25234 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25235 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025236 if (FUNCLINE(fp, i) == NULL)
25237 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025238 prof_func_line(fd, fp->uf_tml_count[i],
25239 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025240 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25241 }
25242 fprintf(fd, "\n");
25243 }
25244 }
25245 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025246
25247 if (sorttab != NULL && st_len > 0)
25248 {
25249 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25250 prof_total_cmp);
25251 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25252 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25253 prof_self_cmp);
25254 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25255 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025256
25257 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025258}
Bram Moolenaar73830342005-02-28 22:48:19 +000025259
25260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025261prof_sort_list(
25262 FILE *fd,
25263 ufunc_T **sorttab,
25264 int st_len,
25265 char *title,
25266 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025267{
25268 int i;
25269 ufunc_T *fp;
25270
25271 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25272 fprintf(fd, "count total (s) self (s) function\n");
25273 for (i = 0; i < 20 && i < st_len; ++i)
25274 {
25275 fp = sorttab[i];
25276 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25277 prefer_self);
25278 if (fp->uf_name[0] == K_SPECIAL)
25279 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25280 else
25281 fprintf(fd, " %s()\n", fp->uf_name);
25282 }
25283 fprintf(fd, "\n");
25284}
25285
25286/*
25287 * Print the count and times for one function or function line.
25288 */
25289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025290prof_func_line(
25291 FILE *fd,
25292 int count,
25293 proftime_T *total,
25294 proftime_T *self,
25295 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025296{
25297 if (count > 0)
25298 {
25299 fprintf(fd, "%5d ", count);
25300 if (prefer_self && profile_equal(total, self))
25301 fprintf(fd, " ");
25302 else
25303 fprintf(fd, "%s ", profile_msg(total));
25304 if (!prefer_self && profile_equal(total, self))
25305 fprintf(fd, " ");
25306 else
25307 fprintf(fd, "%s ", profile_msg(self));
25308 }
25309 else
25310 fprintf(fd, " ");
25311}
25312
25313/*
25314 * Compare function for total time sorting.
25315 */
25316 static int
25317#ifdef __BORLANDC__
25318_RTLENTRYF
25319#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025320prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025321{
25322 ufunc_T *p1, *p2;
25323
25324 p1 = *(ufunc_T **)s1;
25325 p2 = *(ufunc_T **)s2;
25326 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25327}
25328
25329/*
25330 * Compare function for self time sorting.
25331 */
25332 static int
25333#ifdef __BORLANDC__
25334_RTLENTRYF
25335#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025336prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025337{
25338 ufunc_T *p1, *p2;
25339
25340 p1 = *(ufunc_T **)s1;
25341 p2 = *(ufunc_T **)s2;
25342 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25343}
25344
Bram Moolenaar05159a02005-02-26 23:04:13 +000025345#endif
25346
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025347/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025348 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025349 * Return TRUE if a package was loaded.
25350 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025352script_autoload(
25353 char_u *name,
25354 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025355{
25356 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025357 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025358 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025359 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025360
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025361 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025362 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025363 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025364 return FALSE;
25365
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025366 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025367
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025368 /* Find the name in the list of previously loaded package names. Skip
25369 * "autoload/", it's always the same. */
25370 for (i = 0; i < ga_loaded.ga_len; ++i)
25371 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25372 break;
25373 if (!reload && i < ga_loaded.ga_len)
25374 ret = FALSE; /* was loaded already */
25375 else
25376 {
25377 /* Remember the name if it wasn't loaded already. */
25378 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25379 {
25380 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25381 tofree = NULL;
25382 }
25383
25384 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025385 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025386 ret = TRUE;
25387 }
25388
25389 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025390 return ret;
25391}
25392
25393/*
25394 * Return the autoload script name for a function or variable name.
25395 * Returns NULL when out of memory.
25396 */
25397 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025398autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025399{
25400 char_u *p;
25401 char_u *scriptname;
25402
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025403 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025404 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25405 if (scriptname == NULL)
25406 return FALSE;
25407 STRCPY(scriptname, "autoload/");
25408 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025409 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025410 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025411 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025412 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025413 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025414}
25415
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25417
25418/*
25419 * Function given to ExpandGeneric() to obtain the list of user defined
25420 * function names.
25421 */
25422 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025423get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025425 static long_u done;
25426 static hashitem_T *hi;
25427 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428
25429 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025431 done = 0;
25432 hi = func_hashtab.ht_array;
25433 }
25434 if (done < func_hashtab.ht_used)
25435 {
25436 if (done++ > 0)
25437 ++hi;
25438 while (HASHITEM_EMPTY(hi))
25439 ++hi;
25440 fp = HI2UF(hi);
25441
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025442 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025443 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025444
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025445 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25446 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447
25448 cat_func_name(IObuff, fp);
25449 if (xp->xp_context != EXPAND_USER_FUNC)
25450 {
25451 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025452 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 STRCAT(IObuff, ")");
25454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 return IObuff;
25456 }
25457 return NULL;
25458}
25459
25460#endif /* FEAT_CMDL_COMPL */
25461
25462/*
25463 * Copy the function name of "fp" to buffer "buf".
25464 * "buf" must be able to hold the function name plus three bytes.
25465 * Takes care of script-local function names.
25466 */
25467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025468cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025470 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 {
25472 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025473 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 }
25475 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025476 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477}
25478
25479/*
25480 * ":delfunction {name}"
25481 */
25482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025483ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025485 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 char_u *p;
25487 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025488 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489
25490 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025491 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025492 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025494 {
25495 if (fudi.fd_dict != NULL && !eap->skip)
25496 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 if (!ends_excmd(*skipwhite(p)))
25500 {
25501 vim_free(name);
25502 EMSG(_(e_trailing));
25503 return;
25504 }
25505 eap->nextcmd = check_nextcmd(p);
25506 if (eap->nextcmd != NULL)
25507 *p = NUL;
25508
25509 if (!eap->skip)
25510 fp = find_func(name);
25511 vim_free(name);
25512
25513 if (!eap->skip)
25514 {
25515 if (fp == NULL)
25516 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025517 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 return;
25519 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025520 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 {
25522 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25523 return;
25524 }
25525
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025526 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025528 /* Delete the dict item that refers to the function, it will
25529 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025530 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025532 else
25533 func_free(fp);
25534 }
25535}
25536
25537/*
25538 * Free a function and remove it from the list of functions.
25539 */
25540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025541func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025542{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025543 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025544
25545 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025546 ga_clear_strings(&(fp->uf_args));
25547 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025548#ifdef FEAT_PROFILE
25549 vim_free(fp->uf_tml_count);
25550 vim_free(fp->uf_tml_total);
25551 vim_free(fp->uf_tml_self);
25552#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025553
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025554 /* remove the function from the function hashtable */
25555 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25556 if (HASHITEM_EMPTY(hi))
25557 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025558 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025559 hash_remove(&func_hashtab, hi);
25560
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025561 vim_free(fp);
25562}
25563
25564/*
25565 * Unreference a Function: decrement the reference count and free it when it
25566 * becomes zero. Only for numbered functions.
25567 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025569func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025570{
25571 ufunc_T *fp;
25572
25573 if (name != NULL && isdigit(*name))
25574 {
25575 fp = find_func(name);
25576 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025577 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025578#ifdef EXITFREE
25579 if (!entered_free_all_mem)
25580#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025581 EMSG2(_(e_intern2), "func_unref()");
25582 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025583 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025584 {
25585 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025586 * when "uf_calls" becomes zero. */
25587 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025588 func_free(fp);
25589 }
25590 }
25591}
25592
25593/*
25594 * Count a reference to a Function.
25595 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025597func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025598{
25599 ufunc_T *fp;
25600
25601 if (name != NULL && isdigit(*name))
25602 {
25603 fp = find_func(name);
25604 if (fp == NULL)
25605 EMSG2(_(e_intern2), "func_ref()");
25606 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025607 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 }
25609}
25610
25611/*
25612 * Call a user function.
25613 */
25614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025615call_user_func(
25616 ufunc_T *fp, /* pointer to function */
25617 int argcount, /* nr of args */
25618 typval_T *argvars, /* arguments */
25619 typval_T *rettv, /* return value */
25620 linenr_T firstline, /* first line of range */
25621 linenr_T lastline, /* last line of range */
25622 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623{
Bram Moolenaar33570922005-01-25 22:26:29 +000025624 char_u *save_sourcing_name;
25625 linenr_T save_sourcing_lnum;
25626 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025627 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025628 int save_did_emsg;
25629 static int depth = 0;
25630 dictitem_T *v;
25631 int fixvar_idx = 0; /* index in fixvar[] */
25632 int i;
25633 int ai;
25634 char_u numbuf[NUMBUFLEN];
25635 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025636 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025637#ifdef FEAT_PROFILE
25638 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025639 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641
25642 /* If depth of calling is getting too high, don't execute the function */
25643 if (depth >= p_mfd)
25644 {
25645 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025646 rettv->v_type = VAR_NUMBER;
25647 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 return;
25649 }
25650 ++depth;
25651
25652 line_breakcheck(); /* check for CTRL-C hit */
25653
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025654 fc = (funccall_T *)alloc(sizeof(funccall_T));
25655 fc->caller = current_funccal;
25656 current_funccal = fc;
25657 fc->func = fp;
25658 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025659 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025660 fc->linenr = 0;
25661 fc->returned = FALSE;
25662 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025664 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25665 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666
Bram Moolenaar33570922005-01-25 22:26:29 +000025667 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025668 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025669 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25670 * each argument variable and saves a lot of time.
25671 */
25672 /*
25673 * Init l: variables.
25674 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025675 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025676 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025677 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025678 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25679 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025680 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025681 name = v->di_key;
25682 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025683 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025684 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025685 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025686 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025687 v->di_tv.vval.v_dict = selfdict;
25688 ++selfdict->dv_refcount;
25689 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025690
Bram Moolenaar33570922005-01-25 22:26:29 +000025691 /*
25692 * Init a: variables.
25693 * Set a:0 to "argcount".
25694 * Set a:000 to a list with room for the "..." arguments.
25695 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025696 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025697 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025698 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025699 /* Use "name" to avoid a warning from some compiler that checks the
25700 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025701 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025702 name = v->di_key;
25703 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025704 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025705 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025706 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025707 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025708 v->di_tv.vval.v_list = &fc->l_varlist;
25709 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25710 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25711 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025712
25713 /*
25714 * Set a:firstline to "firstline" and a:lastline to "lastline".
25715 * Set a:name to named arguments.
25716 * Set a:N to the "..." arguments.
25717 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025718 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025719 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025720 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025721 (varnumber_T)lastline);
25722 for (i = 0; i < argcount; ++i)
25723 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025724 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025725 if (ai < 0)
25726 /* named argument a:name */
25727 name = FUNCARG(fp, i);
25728 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025729 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025730 /* "..." argument a:1, a:2, etc. */
25731 sprintf((char *)numbuf, "%d", ai + 1);
25732 name = numbuf;
25733 }
25734 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25735 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025736 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025737 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25738 }
25739 else
25740 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025741 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25742 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025743 if (v == NULL)
25744 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025745 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025746 }
25747 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025748 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025749
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025750 /* Note: the values are copied directly to avoid alloc/free.
25751 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025752 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025753 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025754
25755 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25756 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025757 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25758 fc->l_listitems[ai].li_tv = argvars[i];
25759 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025760 }
25761 }
25762
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763 /* Don't redraw while executing the function. */
25764 ++RedrawingDisabled;
25765 save_sourcing_name = sourcing_name;
25766 save_sourcing_lnum = sourcing_lnum;
25767 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025768 /* need space for function name + ("function " + 3) or "[number]" */
25769 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25770 + STRLEN(fp->uf_name) + 20;
25771 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025772 if (sourcing_name != NULL)
25773 {
25774 if (save_sourcing_name != NULL
25775 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025776 sprintf((char *)sourcing_name, "%s[%d]..",
25777 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 else
25779 STRCPY(sourcing_name, "function ");
25780 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25781
25782 if (p_verbose >= 12)
25783 {
25784 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025785 verbose_enter_scroll();
25786
Bram Moolenaar555b2802005-05-19 21:08:39 +000025787 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 if (p_verbose >= 14)
25789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025790 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025791 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025792 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025793 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794
25795 msg_puts((char_u *)"(");
25796 for (i = 0; i < argcount; ++i)
25797 {
25798 if (i > 0)
25799 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025800 if (argvars[i].v_type == VAR_NUMBER)
25801 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 else
25803 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025804 /* Do not want errors such as E724 here. */
25805 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025806 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025807 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025808 if (s != NULL)
25809 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025810 if (vim_strsize(s) > MSG_BUF_CLEN)
25811 {
25812 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25813 s = buf;
25814 }
25815 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025816 vim_free(tofree);
25817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818 }
25819 }
25820 msg_puts((char_u *)")");
25821 }
25822 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025823
25824 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 --no_wait_return;
25826 }
25827 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025828#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025829 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025830 {
25831 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25832 func_do_profile(fp);
25833 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025834 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025835 {
25836 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025837 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025838 profile_zero(&fp->uf_tm_children);
25839 }
25840 script_prof_save(&wait_start);
25841 }
25842#endif
25843
Bram Moolenaar071d4272004-06-13 20:20:40 +000025844 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025845 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025846 save_did_emsg = did_emsg;
25847 did_emsg = FALSE;
25848
25849 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025850 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025851 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25852
25853 --RedrawingDisabled;
25854
25855 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025856 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025858 clear_tv(rettv);
25859 rettv->v_type = VAR_NUMBER;
25860 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025861 }
25862
Bram Moolenaar05159a02005-02-26 23:04:13 +000025863#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025864 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025865 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025866 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025867 profile_end(&call_start);
25868 profile_sub_wait(&wait_start, &call_start);
25869 profile_add(&fp->uf_tm_total, &call_start);
25870 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025871 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025872 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025873 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25874 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025875 }
25876 }
25877#endif
25878
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 /* when being verbose, mention the return value */
25880 if (p_verbose >= 12)
25881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025883 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025886 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025887 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025888 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025889 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025890 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025892 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025893 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025894 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025895 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025896
Bram Moolenaar555b2802005-05-19 21:08:39 +000025897 /* The value may be very long. Skip the middle part, so that we
25898 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025899 * truncate it at the end. Don't want errors such as E724 here. */
25900 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025901 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025902 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025903 if (s != NULL)
25904 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025905 if (vim_strsize(s) > MSG_BUF_CLEN)
25906 {
25907 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25908 s = buf;
25909 }
25910 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025911 vim_free(tofree);
25912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 }
25914 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025915
25916 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917 --no_wait_return;
25918 }
25919
25920 vim_free(sourcing_name);
25921 sourcing_name = save_sourcing_name;
25922 sourcing_lnum = save_sourcing_lnum;
25923 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025924#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025925 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025926 script_prof_restore(&wait_start);
25927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928
25929 if (p_verbose >= 12 && sourcing_name != NULL)
25930 {
25931 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025932 verbose_enter_scroll();
25933
Bram Moolenaar555b2802005-05-19 21:08:39 +000025934 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025936
25937 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938 --no_wait_return;
25939 }
25940
25941 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025942 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025944
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025945 /* If the a:000 list and the l: and a: dicts are not referenced we can
25946 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025947 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25948 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25949 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25950 {
25951 free_funccal(fc, FALSE);
25952 }
25953 else
25954 {
25955 hashitem_T *hi;
25956 listitem_T *li;
25957 int todo;
25958
25959 /* "fc" is still in use. This can happen when returning "a:000" or
25960 * assigning "l:" to a global variable.
25961 * Link "fc" in the list for garbage collection later. */
25962 fc->caller = previous_funccal;
25963 previous_funccal = fc;
25964
25965 /* Make a copy of the a: variables, since we didn't do that above. */
25966 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25967 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25968 {
25969 if (!HASHITEM_EMPTY(hi))
25970 {
25971 --todo;
25972 v = HI2DI(hi);
25973 copy_tv(&v->di_tv, &v->di_tv);
25974 }
25975 }
25976
25977 /* Make a copy of the a:000 items, since we didn't do that above. */
25978 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25979 copy_tv(&li->li_tv, &li->li_tv);
25980 }
25981}
25982
25983/*
25984 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025985 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025986 */
25987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025988can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025989{
25990 return (fc->l_varlist.lv_copyID != copyID
25991 && fc->l_vars.dv_copyID != copyID
25992 && fc->l_avars.dv_copyID != copyID);
25993}
25994
25995/*
25996 * Free "fc" and what it contains.
25997 */
25998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025999free_funccal(
26000 funccall_T *fc,
26001 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026002{
26003 listitem_T *li;
26004
26005 /* The a: variables typevals may not have been allocated, only free the
26006 * allocated variables. */
26007 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26008
26009 /* free all l: variables */
26010 vars_clear(&fc->l_vars.dv_hashtab);
26011
26012 /* Free the a:000 variables if they were allocated. */
26013 if (free_val)
26014 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26015 clear_tv(&li->li_tv);
26016
26017 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018}
26019
26020/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026021 * Add a number variable "name" to dict "dp" with value "nr".
26022 */
26023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026024add_nr_var(
26025 dict_T *dp,
26026 dictitem_T *v,
26027 char *name,
26028 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026029{
26030 STRCPY(v->di_key, name);
26031 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26032 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26033 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026034 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026035 v->di_tv.vval.v_number = nr;
26036}
26037
26038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039 * ":return [expr]"
26040 */
26041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026042ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043{
26044 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026045 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046 int returning = FALSE;
26047
26048 if (current_funccal == NULL)
26049 {
26050 EMSG(_("E133: :return not inside a function"));
26051 return;
26052 }
26053
26054 if (eap->skip)
26055 ++emsg_skip;
26056
26057 eap->nextcmd = NULL;
26058 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026059 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 {
26061 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026062 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026064 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 }
26066 /* It's safer to return also on error. */
26067 else if (!eap->skip)
26068 {
26069 /*
26070 * Return unless the expression evaluation has been cancelled due to an
26071 * aborting error, an interrupt, or an exception.
26072 */
26073 if (!aborting())
26074 returning = do_return(eap, FALSE, TRUE, NULL);
26075 }
26076
26077 /* When skipping or the return gets pending, advance to the next command
26078 * in this line (!returning). Otherwise, ignore the rest of the line.
26079 * Following lines will be ignored by get_func_line(). */
26080 if (returning)
26081 eap->nextcmd = NULL;
26082 else if (eap->nextcmd == NULL) /* no argument */
26083 eap->nextcmd = check_nextcmd(arg);
26084
26085 if (eap->skip)
26086 --emsg_skip;
26087}
26088
26089/*
26090 * Return from a function. Possibly makes the return pending. Also called
26091 * for a pending return at the ":endtry" or after returning from an extra
26092 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026093 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026094 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095 * FALSE when the return gets pending.
26096 */
26097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026098do_return(
26099 exarg_T *eap,
26100 int reanimate,
26101 int is_cmd,
26102 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103{
26104 int idx;
26105 struct condstack *cstack = eap->cstack;
26106
26107 if (reanimate)
26108 /* Undo the return. */
26109 current_funccal->returned = FALSE;
26110
26111 /*
26112 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26113 * not in its finally clause (which then is to be executed next) is found.
26114 * In this case, make the ":return" pending for execution at the ":endtry".
26115 * Otherwise, return normally.
26116 */
26117 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26118 if (idx >= 0)
26119 {
26120 cstack->cs_pending[idx] = CSTP_RETURN;
26121
26122 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026123 /* A pending return again gets pending. "rettv" points to an
26124 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026126 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127 else
26128 {
26129 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026130 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026132 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026134 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 {
26136 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026137 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026138 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 else
26140 EMSG(_(e_outofmem));
26141 }
26142 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026143 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144
26145 if (reanimate)
26146 {
26147 /* The pending return value could be overwritten by a ":return"
26148 * without argument in a finally clause; reset the default
26149 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026150 current_funccal->rettv->v_type = VAR_NUMBER;
26151 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152 }
26153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026154 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 }
26156 else
26157 {
26158 current_funccal->returned = TRUE;
26159
26160 /* If the return is carried out now, store the return value. For
26161 * a return immediately after reanimation, the value is already
26162 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026163 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026165 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026166 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026167 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026168 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 }
26170 }
26171
26172 return idx < 0;
26173}
26174
26175/*
26176 * Free the variable with a pending return value.
26177 */
26178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026179discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180{
Bram Moolenaar33570922005-01-25 22:26:29 +000026181 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182}
26183
26184/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026185 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026186 * is an allocated string. Used by report_pending() for verbose messages.
26187 */
26188 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026189get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026191 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026192 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026193 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026195 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026196 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026197 if (s == NULL)
26198 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026199
26200 STRCPY(IObuff, ":return ");
26201 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26202 if (STRLEN(s) + 8 >= IOSIZE)
26203 STRCPY(IObuff + IOSIZE - 4, "...");
26204 vim_free(tofree);
26205 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206}
26207
26208/*
26209 * Get next function line.
26210 * Called by do_cmdline() to get the next line.
26211 * Returns allocated string, or NULL for end of function.
26212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026213 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026214get_func_line(
26215 int c UNUSED,
26216 void *cookie,
26217 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026218{
Bram Moolenaar33570922005-01-25 22:26:29 +000026219 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026220 ufunc_T *fp = fcp->func;
26221 char_u *retval;
26222 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026223
26224 /* If breakpoints have been added/deleted need to check for it. */
26225 if (fcp->dbg_tick != debug_tick)
26226 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026227 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228 sourcing_lnum);
26229 fcp->dbg_tick = debug_tick;
26230 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026231#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026232 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026233 func_line_end(cookie);
26234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235
Bram Moolenaar05159a02005-02-26 23:04:13 +000026236 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026237 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26238 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026239 retval = NULL;
26240 else
26241 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026242 /* Skip NULL lines (continuation lines). */
26243 while (fcp->linenr < gap->ga_len
26244 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26245 ++fcp->linenr;
26246 if (fcp->linenr >= gap->ga_len)
26247 retval = NULL;
26248 else
26249 {
26250 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26251 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026252#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026253 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026254 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026255#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257 }
26258
26259 /* Did we encounter a breakpoint? */
26260 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26261 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026262 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026263 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026264 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265 sourcing_lnum);
26266 fcp->dbg_tick = debug_tick;
26267 }
26268
26269 return retval;
26270}
26271
Bram Moolenaar05159a02005-02-26 23:04:13 +000026272#if defined(FEAT_PROFILE) || defined(PROTO)
26273/*
26274 * Called when starting to read a function line.
26275 * "sourcing_lnum" must be correct!
26276 * When skipping lines it may not actually be executed, but we won't find out
26277 * until later and we need to store the time now.
26278 */
26279 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026280func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026281{
26282 funccall_T *fcp = (funccall_T *)cookie;
26283 ufunc_T *fp = fcp->func;
26284
26285 if (fp->uf_profiling && sourcing_lnum >= 1
26286 && sourcing_lnum <= fp->uf_lines.ga_len)
26287 {
26288 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026289 /* Skip continuation lines. */
26290 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26291 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026292 fp->uf_tml_execed = FALSE;
26293 profile_start(&fp->uf_tml_start);
26294 profile_zero(&fp->uf_tml_children);
26295 profile_get_wait(&fp->uf_tml_wait);
26296 }
26297}
26298
26299/*
26300 * Called when actually executing a function line.
26301 */
26302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026303func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026304{
26305 funccall_T *fcp = (funccall_T *)cookie;
26306 ufunc_T *fp = fcp->func;
26307
26308 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26309 fp->uf_tml_execed = TRUE;
26310}
26311
26312/*
26313 * Called when done with a function line.
26314 */
26315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026316func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026317{
26318 funccall_T *fcp = (funccall_T *)cookie;
26319 ufunc_T *fp = fcp->func;
26320
26321 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26322 {
26323 if (fp->uf_tml_execed)
26324 {
26325 ++fp->uf_tml_count[fp->uf_tml_idx];
26326 profile_end(&fp->uf_tml_start);
26327 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026328 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026329 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26330 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026331 }
26332 fp->uf_tml_idx = -1;
26333 }
26334}
26335#endif
26336
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337/*
26338 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026339 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026340 */
26341 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026342func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343{
Bram Moolenaar33570922005-01-25 22:26:29 +000026344 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345
26346 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26347 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026348 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349 || fcp->returned);
26350}
26351
26352/*
26353 * return TRUE if cookie indicates a function which "abort"s on errors.
26354 */
26355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026356func_has_abort(
26357 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026359 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360}
26361
26362#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26363typedef enum
26364{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026365 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26366 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26367 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368} var_flavour_T;
26369
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026370static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026371
26372 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026373var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026374{
26375 char_u *p = varname;
26376
26377 if (ASCII_ISUPPER(*p))
26378 {
26379 while (*(++p))
26380 if (ASCII_ISLOWER(*p))
26381 return VAR_FLAVOUR_SESSION;
26382 return VAR_FLAVOUR_VIMINFO;
26383 }
26384 else
26385 return VAR_FLAVOUR_DEFAULT;
26386}
26387#endif
26388
26389#if defined(FEAT_VIMINFO) || defined(PROTO)
26390/*
26391 * Restore global vars that start with a capital from the viminfo file
26392 */
26393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026394read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395{
26396 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026397 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026398 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026399 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400
26401 if (!writing && (find_viminfo_parameter('!') != NULL))
26402 {
26403 tab = vim_strchr(virp->vir_line + 1, '\t');
26404 if (tab != NULL)
26405 {
26406 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026407 switch (*tab)
26408 {
26409 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026410#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026411 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026412#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026413 case 'D': type = VAR_DICT; break;
26414 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026415 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417
26418 tab = vim_strchr(tab, '\t');
26419 if (tab != NULL)
26420 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026421 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026422 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026423 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026425#ifdef FEAT_FLOAT
26426 else if (type == VAR_FLOAT)
26427 (void)string2float(tab + 1, &tv.vval.v_float);
26428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026430 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026431 if (type == VAR_DICT || type == VAR_LIST)
26432 {
26433 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26434
26435 if (etv == NULL)
26436 /* Failed to parse back the dict or list, use it as a
26437 * string. */
26438 tv.v_type = VAR_STRING;
26439 else
26440 {
26441 vim_free(tv.vval.v_string);
26442 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026443 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026444 }
26445 }
26446
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026447 /* when in a function use global variables */
26448 save_funccal = current_funccal;
26449 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026450 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026451 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026452
26453 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026454 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026455 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26456 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457 }
26458 }
26459 }
26460
26461 return viminfo_readline(virp);
26462}
26463
26464/*
26465 * Write global vars that start with a capital to the viminfo file
26466 */
26467 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026468write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469{
Bram Moolenaar33570922005-01-25 22:26:29 +000026470 hashitem_T *hi;
26471 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026472 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026473 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026474 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026475 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026476 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477
26478 if (find_viminfo_parameter('!') == NULL)
26479 return;
26480
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026481 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026482
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026483 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026484 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026485 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026486 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026488 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026489 this_var = HI2DI(hi);
26490 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026491 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026492 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026493 {
26494 case VAR_STRING: s = "STR"; break;
26495 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026496 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026497 case VAR_DICT: s = "DIC"; break;
26498 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026499 case VAR_SPECIAL: s = "XPL"; break;
26500
26501 case VAR_UNKNOWN:
26502 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026503 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026504 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026505 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026506 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026507 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026508 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026509 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026510 if (p != NULL)
26511 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026512 vim_free(tofree);
26513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026514 }
26515 }
26516}
26517#endif
26518
26519#if defined(FEAT_SESSION) || defined(PROTO)
26520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026521store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522{
Bram Moolenaar33570922005-01-25 22:26:29 +000026523 hashitem_T *hi;
26524 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026525 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526 char_u *p, *t;
26527
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026528 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026529 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026531 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026533 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026534 this_var = HI2DI(hi);
26535 if ((this_var->di_tv.v_type == VAR_NUMBER
26536 || this_var->di_tv.v_type == VAR_STRING)
26537 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026538 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026539 /* Escape special characters with a backslash. Turn a LF and
26540 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026541 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026542 (char_u *)"\\\"\n\r");
26543 if (p == NULL) /* out of memory */
26544 break;
26545 for (t = p; *t != NUL; ++t)
26546 if (*t == '\n')
26547 *t = 'n';
26548 else if (*t == '\r')
26549 *t = 'r';
26550 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026551 this_var->di_key,
26552 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26553 : ' ',
26554 p,
26555 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26556 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026557 || put_eol(fd) == FAIL)
26558 {
26559 vim_free(p);
26560 return FAIL;
26561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 vim_free(p);
26563 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026564#ifdef FEAT_FLOAT
26565 else if (this_var->di_tv.v_type == VAR_FLOAT
26566 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26567 {
26568 float_T f = this_var->di_tv.vval.v_float;
26569 int sign = ' ';
26570
26571 if (f < 0)
26572 {
26573 f = -f;
26574 sign = '-';
26575 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026576 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026577 this_var->di_key, sign, f) < 0)
26578 || put_eol(fd) == FAIL)
26579 return FAIL;
26580 }
26581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 }
26583 }
26584 return OK;
26585}
26586#endif
26587
Bram Moolenaar661b1822005-07-28 22:36:45 +000026588/*
26589 * Display script name where an item was last set.
26590 * Should only be invoked when 'verbose' is non-zero.
26591 */
26592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026593last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026594{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026595 char_u *p;
26596
Bram Moolenaar661b1822005-07-28 22:36:45 +000026597 if (scriptID != 0)
26598 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026599 p = home_replace_save(NULL, get_scriptname(scriptID));
26600 if (p != NULL)
26601 {
26602 verbose_enter();
26603 MSG_PUTS(_("\n\tLast set from "));
26604 MSG_PUTS(p);
26605 vim_free(p);
26606 verbose_leave();
26607 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026608 }
26609}
26610
Bram Moolenaard812df62008-11-09 12:46:09 +000026611/*
26612 * List v:oldfiles in a nice way.
26613 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026615ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026616{
26617 list_T *l = vimvars[VV_OLDFILES].vv_list;
26618 listitem_T *li;
26619 int nr = 0;
26620
26621 if (l == NULL)
26622 msg((char_u *)_("No old files"));
26623 else
26624 {
26625 msg_start();
26626 msg_scroll = TRUE;
26627 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26628 {
26629 msg_outnum((long)++nr);
26630 MSG_PUTS(": ");
26631 msg_outtrans(get_tv_string(&li->li_tv));
26632 msg_putchar('\n');
26633 out_flush(); /* output one line at a time */
26634 ui_breakcheck();
26635 }
26636 /* Assume "got_int" was set to truncate the listing. */
26637 got_int = FALSE;
26638
26639#ifdef FEAT_BROWSE_CMD
26640 if (cmdmod.browse)
26641 {
26642 quit_more = FALSE;
26643 nr = prompt_for_number(FALSE);
26644 msg_starthere();
26645 if (nr > 0)
26646 {
26647 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26648 (long)nr);
26649
26650 if (p != NULL)
26651 {
26652 p = expand_env_save(p);
26653 eap->arg = p;
26654 eap->cmdidx = CMD_edit;
26655 cmdmod.browse = FALSE;
26656 do_exedit(eap, NULL);
26657 vim_free(p);
26658 }
26659 }
26660 }
26661#endif
26662 }
26663}
26664
Bram Moolenaar53744302015-07-17 17:38:22 +020026665/* reset v:option_new, v:option_old and v:option_type */
26666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026667reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026668{
26669 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26670 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26671 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26672}
26673
26674
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675#endif /* FEAT_EVAL */
26676
Bram Moolenaar071d4272004-06-13 20:20:40 +000026677
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026678#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679
26680#ifdef WIN3264
26681/*
26682 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26683 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026684static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26685static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26686static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026687
26688/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026689 * Get the short path (8.3) for the filename in "fnamep".
26690 * Only works for a valid file name.
26691 * When the path gets longer "fnamep" is changed and the allocated buffer
26692 * is put in "bufp".
26693 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26694 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026695 */
26696 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026697get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026698{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026699 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026700 char_u *newbuf;
26701
26702 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026703 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026704 if (l > len - 1)
26705 {
26706 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026707 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026708 newbuf = vim_strnsave(*fnamep, l);
26709 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026710 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026711
26712 vim_free(*bufp);
26713 *fnamep = *bufp = newbuf;
26714
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026715 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026716 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026717 }
26718
26719 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026720 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026721}
26722
26723/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026724 * Get the short path (8.3) for the filename in "fname". The converted
26725 * path is returned in "bufp".
26726 *
26727 * Some of the directories specified in "fname" may not exist. This function
26728 * will shorten the existing directories at the beginning of the path and then
26729 * append the remaining non-existing path.
26730 *
26731 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026732 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026733 * bufp - Pointer to an allocated buffer for the filename.
26734 * fnamelen - Length of the filename pointed to by fname
26735 *
26736 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026737 */
26738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026739shortpath_for_invalid_fname(
26740 char_u **fname,
26741 char_u **bufp,
26742 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026743{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026744 char_u *short_fname, *save_fname, *pbuf_unused;
26745 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026746 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026747 int old_len, len;
26748 int new_len, sfx_len;
26749 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026750
26751 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026752 old_len = *fnamelen;
26753 save_fname = vim_strnsave(*fname, old_len);
26754 pbuf_unused = NULL;
26755 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026756
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026757 endp = save_fname + old_len - 1; /* Find the end of the copy */
26758 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026759
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026760 /*
26761 * Try shortening the supplied path till it succeeds by removing one
26762 * directory at a time from the tail of the path.
26763 */
26764 len = 0;
26765 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026766 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026767 /* go back one path-separator */
26768 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26769 --endp;
26770 if (endp <= save_fname)
26771 break; /* processed the complete path */
26772
26773 /*
26774 * Replace the path separator with a NUL and try to shorten the
26775 * resulting path.
26776 */
26777 ch = *endp;
26778 *endp = 0;
26779 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026780 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026781 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26782 {
26783 retval = FAIL;
26784 goto theend;
26785 }
26786 *endp = ch; /* preserve the string */
26787
26788 if (len > 0)
26789 break; /* successfully shortened the path */
26790
26791 /* failed to shorten the path. Skip the path separator */
26792 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026793 }
26794
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026795 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026796 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026797 /*
26798 * Succeeded in shortening the path. Now concatenate the shortened
26799 * path with the remaining path at the tail.
26800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026802 /* Compute the length of the new path. */
26803 sfx_len = (int)(save_endp - endp) + 1;
26804 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026805
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026806 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026807 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026808 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026810 /* There is not enough space in the currently allocated string,
26811 * copy it to a buffer big enough. */
26812 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026813 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026814 {
26815 retval = FAIL;
26816 goto theend;
26817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026818 }
26819 else
26820 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026821 /* Transfer short_fname to the main buffer (it's big enough),
26822 * unless get_short_pathname() did its work in-place. */
26823 *fname = *bufp = save_fname;
26824 if (short_fname != save_fname)
26825 vim_strncpy(save_fname, short_fname, len);
26826 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026827 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026828
26829 /* concat the not-shortened part of the path */
26830 vim_strncpy(*fname + len, endp, sfx_len);
26831 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026832 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026833
26834theend:
26835 vim_free(pbuf_unused);
26836 vim_free(save_fname);
26837
26838 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026839}
26840
26841/*
26842 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026843 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026844 */
26845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026846shortpath_for_partial(
26847 char_u **fnamep,
26848 char_u **bufp,
26849 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026850{
26851 int sepcount, len, tflen;
26852 char_u *p;
26853 char_u *pbuf, *tfname;
26854 int hasTilde;
26855
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026856 /* Count up the path separators from the RHS.. so we know which part
26857 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026858 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026859 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026860 if (vim_ispathsep(*p))
26861 ++sepcount;
26862
26863 /* Need full path first (use expand_env() to remove a "~/") */
26864 hasTilde = (**fnamep == '~');
26865 if (hasTilde)
26866 pbuf = tfname = expand_env_save(*fnamep);
26867 else
26868 pbuf = tfname = FullName_save(*fnamep, FALSE);
26869
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026870 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026871
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026872 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26873 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026874
26875 if (len == 0)
26876 {
26877 /* Don't have a valid filename, so shorten the rest of the
26878 * path if we can. This CAN give us invalid 8.3 filenames, but
26879 * there's not a lot of point in guessing what it might be.
26880 */
26881 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026882 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26883 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026884 }
26885
26886 /* Count the paths backward to find the beginning of the desired string. */
26887 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026888 {
26889#ifdef FEAT_MBYTE
26890 if (has_mbyte)
26891 p -= mb_head_off(tfname, p);
26892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026893 if (vim_ispathsep(*p))
26894 {
26895 if (sepcount == 0 || (hasTilde && sepcount == 1))
26896 break;
26897 else
26898 sepcount --;
26899 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 if (hasTilde)
26902 {
26903 --p;
26904 if (p >= tfname)
26905 *p = '~';
26906 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026907 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026908 }
26909 else
26910 ++p;
26911
26912 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26913 vim_free(*bufp);
26914 *fnamelen = (int)STRLEN(p);
26915 *bufp = pbuf;
26916 *fnamep = p;
26917
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026918 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026919}
26920#endif /* WIN3264 */
26921
26922/*
26923 * Adjust a filename, according to a string of modifiers.
26924 * *fnamep must be NUL terminated when called. When returning, the length is
26925 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026926 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026927 * When there is an error, *fnamep is set to NULL.
26928 */
26929 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026930modify_fname(
26931 char_u *src, /* string with modifiers */
26932 int *usedlen, /* characters after src that are used */
26933 char_u **fnamep, /* file name so far */
26934 char_u **bufp, /* buffer for allocated file name or NULL */
26935 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026936{
26937 int valid = 0;
26938 char_u *tail;
26939 char_u *s, *p, *pbuf;
26940 char_u dirname[MAXPATHL];
26941 int c;
26942 int has_fullname = 0;
26943#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026944 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026945 int has_shortname = 0;
26946#endif
26947
26948repeat:
26949 /* ":p" - full path/file_name */
26950 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26951 {
26952 has_fullname = 1;
26953
26954 valid |= VALID_PATH;
26955 *usedlen += 2;
26956
26957 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26958 if ((*fnamep)[0] == '~'
26959#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26960 && ((*fnamep)[1] == '/'
26961# ifdef BACKSLASH_IN_FILENAME
26962 || (*fnamep)[1] == '\\'
26963# endif
26964 || (*fnamep)[1] == NUL)
26965
26966#endif
26967 )
26968 {
26969 *fnamep = expand_env_save(*fnamep);
26970 vim_free(*bufp); /* free any allocated file name */
26971 *bufp = *fnamep;
26972 if (*fnamep == NULL)
26973 return -1;
26974 }
26975
26976 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026977 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026978 {
26979 if (vim_ispathsep(*p)
26980 && p[1] == '.'
26981 && (p[2] == NUL
26982 || vim_ispathsep(p[2])
26983 || (p[2] == '.'
26984 && (p[3] == NUL || vim_ispathsep(p[3])))))
26985 break;
26986 }
26987
26988 /* FullName_save() is slow, don't use it when not needed. */
26989 if (*p != NUL || !vim_isAbsName(*fnamep))
26990 {
26991 *fnamep = FullName_save(*fnamep, *p != NUL);
26992 vim_free(*bufp); /* free any allocated file name */
26993 *bufp = *fnamep;
26994 if (*fnamep == NULL)
26995 return -1;
26996 }
26997
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026998#ifdef WIN3264
26999# if _WIN32_WINNT >= 0x0500
27000 if (vim_strchr(*fnamep, '~') != NULL)
27001 {
27002 /* Expand 8.3 filename to full path. Needed to make sure the same
27003 * file does not have two different names.
27004 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27005 p = alloc(_MAX_PATH + 1);
27006 if (p != NULL)
27007 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027008 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027009 {
27010 vim_free(*bufp);
27011 *bufp = *fnamep = p;
27012 }
27013 else
27014 vim_free(p);
27015 }
27016 }
27017# endif
27018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027019 /* Append a path separator to a directory. */
27020 if (mch_isdir(*fnamep))
27021 {
27022 /* Make room for one or two extra characters. */
27023 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27024 vim_free(*bufp); /* free any allocated file name */
27025 *bufp = *fnamep;
27026 if (*fnamep == NULL)
27027 return -1;
27028 add_pathsep(*fnamep);
27029 }
27030 }
27031
27032 /* ":." - path relative to the current directory */
27033 /* ":~" - path relative to the home directory */
27034 /* ":8" - shortname path - postponed till after */
27035 while (src[*usedlen] == ':'
27036 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27037 {
27038 *usedlen += 2;
27039 if (c == '8')
27040 {
27041#ifdef WIN3264
27042 has_shortname = 1; /* Postpone this. */
27043#endif
27044 continue;
27045 }
27046 pbuf = NULL;
27047 /* Need full path first (use expand_env() to remove a "~/") */
27048 if (!has_fullname)
27049 {
27050 if (c == '.' && **fnamep == '~')
27051 p = pbuf = expand_env_save(*fnamep);
27052 else
27053 p = pbuf = FullName_save(*fnamep, FALSE);
27054 }
27055 else
27056 p = *fnamep;
27057
27058 has_fullname = 0;
27059
27060 if (p != NULL)
27061 {
27062 if (c == '.')
27063 {
27064 mch_dirname(dirname, MAXPATHL);
27065 s = shorten_fname(p, dirname);
27066 if (s != NULL)
27067 {
27068 *fnamep = s;
27069 if (pbuf != NULL)
27070 {
27071 vim_free(*bufp); /* free any allocated file name */
27072 *bufp = pbuf;
27073 pbuf = NULL;
27074 }
27075 }
27076 }
27077 else
27078 {
27079 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27080 /* Only replace it when it starts with '~' */
27081 if (*dirname == '~')
27082 {
27083 s = vim_strsave(dirname);
27084 if (s != NULL)
27085 {
27086 *fnamep = s;
27087 vim_free(*bufp);
27088 *bufp = s;
27089 }
27090 }
27091 }
27092 vim_free(pbuf);
27093 }
27094 }
27095
27096 tail = gettail(*fnamep);
27097 *fnamelen = (int)STRLEN(*fnamep);
27098
27099 /* ":h" - head, remove "/file_name", can be repeated */
27100 /* Don't remove the first "/" or "c:\" */
27101 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27102 {
27103 valid |= VALID_HEAD;
27104 *usedlen += 2;
27105 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027106 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027107 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027108 *fnamelen = (int)(tail - *fnamep);
27109#ifdef VMS
27110 if (*fnamelen > 0)
27111 *fnamelen += 1; /* the path separator is part of the path */
27112#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027113 if (*fnamelen == 0)
27114 {
27115 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27116 p = vim_strsave((char_u *)".");
27117 if (p == NULL)
27118 return -1;
27119 vim_free(*bufp);
27120 *bufp = *fnamep = tail = p;
27121 *fnamelen = 1;
27122 }
27123 else
27124 {
27125 while (tail > s && !after_pathsep(s, tail))
27126 mb_ptr_back(*fnamep, tail);
27127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027128 }
27129
27130 /* ":8" - shortname */
27131 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27132 {
27133 *usedlen += 2;
27134#ifdef WIN3264
27135 has_shortname = 1;
27136#endif
27137 }
27138
27139#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027140 /*
27141 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027142 */
27143 if (has_shortname)
27144 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027145 /* Copy the string if it is shortened by :h and when it wasn't copied
27146 * yet, because we are going to change it in place. Avoids changing
27147 * the buffer name for "%:8". */
27148 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027149 {
27150 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027151 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027152 return -1;
27153 vim_free(*bufp);
27154 *bufp = *fnamep = p;
27155 }
27156
27157 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027158 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027159 if (!has_fullname && !vim_isAbsName(*fnamep))
27160 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027161 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027162 return -1;
27163 }
27164 else
27165 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027166 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027167
Bram Moolenaardc935552011-08-17 15:23:23 +020027168 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027169 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027170 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027171 return -1;
27172
27173 if (l == 0)
27174 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027175 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027176 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027177 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027178 return -1;
27179 }
27180 *fnamelen = l;
27181 }
27182 }
27183#endif /* WIN3264 */
27184
27185 /* ":t" - tail, just the basename */
27186 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27187 {
27188 *usedlen += 2;
27189 *fnamelen -= (int)(tail - *fnamep);
27190 *fnamep = tail;
27191 }
27192
27193 /* ":e" - extension, can be repeated */
27194 /* ":r" - root, without extension, can be repeated */
27195 while (src[*usedlen] == ':'
27196 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27197 {
27198 /* find a '.' in the tail:
27199 * - for second :e: before the current fname
27200 * - otherwise: The last '.'
27201 */
27202 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27203 s = *fnamep - 2;
27204 else
27205 s = *fnamep + *fnamelen - 1;
27206 for ( ; s > tail; --s)
27207 if (s[0] == '.')
27208 break;
27209 if (src[*usedlen + 1] == 'e') /* :e */
27210 {
27211 if (s > tail)
27212 {
27213 *fnamelen += (int)(*fnamep - (s + 1));
27214 *fnamep = s + 1;
27215#ifdef VMS
27216 /* cut version from the extension */
27217 s = *fnamep + *fnamelen - 1;
27218 for ( ; s > *fnamep; --s)
27219 if (s[0] == ';')
27220 break;
27221 if (s > *fnamep)
27222 *fnamelen = s - *fnamep;
27223#endif
27224 }
27225 else if (*fnamep <= tail)
27226 *fnamelen = 0;
27227 }
27228 else /* :r */
27229 {
27230 if (s > tail) /* remove one extension */
27231 *fnamelen = (int)(s - *fnamep);
27232 }
27233 *usedlen += 2;
27234 }
27235
27236 /* ":s?pat?foo?" - substitute */
27237 /* ":gs?pat?foo?" - global substitute */
27238 if (src[*usedlen] == ':'
27239 && (src[*usedlen + 1] == 's'
27240 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27241 {
27242 char_u *str;
27243 char_u *pat;
27244 char_u *sub;
27245 int sep;
27246 char_u *flags;
27247 int didit = FALSE;
27248
27249 flags = (char_u *)"";
27250 s = src + *usedlen + 2;
27251 if (src[*usedlen + 1] == 'g')
27252 {
27253 flags = (char_u *)"g";
27254 ++s;
27255 }
27256
27257 sep = *s++;
27258 if (sep)
27259 {
27260 /* find end of pattern */
27261 p = vim_strchr(s, sep);
27262 if (p != NULL)
27263 {
27264 pat = vim_strnsave(s, (int)(p - s));
27265 if (pat != NULL)
27266 {
27267 s = p + 1;
27268 /* find end of substitution */
27269 p = vim_strchr(s, sep);
27270 if (p != NULL)
27271 {
27272 sub = vim_strnsave(s, (int)(p - s));
27273 str = vim_strnsave(*fnamep, *fnamelen);
27274 if (sub != NULL && str != NULL)
27275 {
27276 *usedlen = (int)(p + 1 - src);
27277 s = do_string_sub(str, pat, sub, flags);
27278 if (s != NULL)
27279 {
27280 *fnamep = s;
27281 *fnamelen = (int)STRLEN(s);
27282 vim_free(*bufp);
27283 *bufp = s;
27284 didit = TRUE;
27285 }
27286 }
27287 vim_free(sub);
27288 vim_free(str);
27289 }
27290 vim_free(pat);
27291 }
27292 }
27293 /* after using ":s", repeat all the modifiers */
27294 if (didit)
27295 goto repeat;
27296 }
27297 }
27298
Bram Moolenaar26df0922014-02-23 23:39:13 +010027299 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27300 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027301 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027302 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027303 if (c != NUL)
27304 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027305 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027306 if (c != NUL)
27307 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027308 if (p == NULL)
27309 return -1;
27310 vim_free(*bufp);
27311 *bufp = *fnamep = p;
27312 *fnamelen = (int)STRLEN(p);
27313 *usedlen += 2;
27314 }
27315
Bram Moolenaar071d4272004-06-13 20:20:40 +000027316 return valid;
27317}
27318
27319/*
27320 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27321 * "flags" can be "g" to do a global substitute.
27322 * Returns an allocated string, NULL for error.
27323 */
27324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027325do_string_sub(
27326 char_u *str,
27327 char_u *pat,
27328 char_u *sub,
27329 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027330{
27331 int sublen;
27332 regmatch_T regmatch;
27333 int i;
27334 int do_all;
27335 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027336 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027337 garray_T ga;
27338 char_u *ret;
27339 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027340 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027341
27342 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27343 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027344 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027345
27346 ga_init2(&ga, 1, 200);
27347
27348 do_all = (flags[0] == 'g');
27349
27350 regmatch.rm_ic = p_ic;
27351 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27352 if (regmatch.regprog != NULL)
27353 {
27354 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027355 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027356 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27357 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027358 /* Skip empty match except for first match. */
27359 if (regmatch.startp[0] == regmatch.endp[0])
27360 {
27361 if (zero_width == regmatch.startp[0])
27362 {
27363 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027364 i = MB_PTR2LEN(tail);
27365 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27366 (size_t)i);
27367 ga.ga_len += i;
27368 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027369 continue;
27370 }
27371 zero_width = regmatch.startp[0];
27372 }
27373
Bram Moolenaar071d4272004-06-13 20:20:40 +000027374 /*
27375 * Get some space for a temporary buffer to do the substitution
27376 * into. It will contain:
27377 * - The text up to where the match is.
27378 * - The substituted text.
27379 * - The text after the match.
27380 */
27381 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027382 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027383 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27384 {
27385 ga_clear(&ga);
27386 break;
27387 }
27388
27389 /* copy the text up to where the match is */
27390 i = (int)(regmatch.startp[0] - tail);
27391 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27392 /* add the substituted text */
27393 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27394 + ga.ga_len + i, TRUE, TRUE, FALSE);
27395 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027396 tail = regmatch.endp[0];
27397 if (*tail == NUL)
27398 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027399 if (!do_all)
27400 break;
27401 }
27402
27403 if (ga.ga_data != NULL)
27404 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27405
Bram Moolenaar473de612013-06-08 18:19:48 +020027406 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027407 }
27408
27409 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27410 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027411 if (p_cpo == empty_option)
27412 p_cpo = save_cpo;
27413 else
27414 /* Darn, evaluating {sub} expression changed the value. */
27415 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027416
27417 return ret;
27418}
27419
27420#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */