blob: 8b67604471eabcb49e81affcafd2468cd4974603 [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);
558static void f_eventhandler(typval_T *argvars, typval_T *rettv);
559static void f_executable(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200560static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static 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);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200596#if defined(FEAT_CMDL_COMPL)
597static void f_getcompletion(typval_T *argvars, typval_T *rettv);
598#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100599static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
600static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
601static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
602static void f_getcwd(typval_T *argvars, typval_T *rettv);
603static void f_getfontname(typval_T *argvars, typval_T *rettv);
604static void f_getfperm(typval_T *argvars, typval_T *rettv);
605static void f_getfsize(typval_T *argvars, typval_T *rettv);
606static void f_getftime(typval_T *argvars, typval_T *rettv);
607static void f_getftype(typval_T *argvars, typval_T *rettv);
608static void f_getline(typval_T *argvars, typval_T *rettv);
609static void f_getmatches(typval_T *argvars, typval_T *rettv);
610static void f_getpid(typval_T *argvars, typval_T *rettv);
611static void f_getcurpos(typval_T *argvars, typval_T *rettv);
612static void f_getpos(typval_T *argvars, typval_T *rettv);
613static void f_getqflist(typval_T *argvars, typval_T *rettv);
614static void f_getreg(typval_T *argvars, typval_T *rettv);
615static void f_getregtype(typval_T *argvars, typval_T *rettv);
616static void f_gettabvar(typval_T *argvars, typval_T *rettv);
617static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
618static void f_getwinposx(typval_T *argvars, typval_T *rettv);
619static void f_getwinposy(typval_T *argvars, typval_T *rettv);
620static void f_getwinvar(typval_T *argvars, typval_T *rettv);
621static void f_glob(typval_T *argvars, typval_T *rettv);
622static void f_globpath(typval_T *argvars, typval_T *rettv);
623static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
624static void f_has(typval_T *argvars, typval_T *rettv);
625static void f_has_key(typval_T *argvars, typval_T *rettv);
626static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
627static void f_hasmapto(typval_T *argvars, typval_T *rettv);
628static void f_histadd(typval_T *argvars, typval_T *rettv);
629static void f_histdel(typval_T *argvars, typval_T *rettv);
630static void f_histget(typval_T *argvars, typval_T *rettv);
631static void f_histnr(typval_T *argvars, typval_T *rettv);
632static void f_hlID(typval_T *argvars, typval_T *rettv);
633static void f_hlexists(typval_T *argvars, typval_T *rettv);
634static void f_hostname(typval_T *argvars, typval_T *rettv);
635static void f_iconv(typval_T *argvars, typval_T *rettv);
636static void f_indent(typval_T *argvars, typval_T *rettv);
637static void f_index(typval_T *argvars, typval_T *rettv);
638static void f_input(typval_T *argvars, typval_T *rettv);
639static void f_inputdialog(typval_T *argvars, typval_T *rettv);
640static void f_inputlist(typval_T *argvars, typval_T *rettv);
641static void f_inputrestore(typval_T *argvars, typval_T *rettv);
642static void f_inputsave(typval_T *argvars, typval_T *rettv);
643static void f_inputsecret(typval_T *argvars, typval_T *rettv);
644static void f_insert(typval_T *argvars, typval_T *rettv);
645static void f_invert(typval_T *argvars, typval_T *rettv);
646static void f_isdirectory(typval_T *argvars, typval_T *rettv);
647static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100648#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
649static void f_isnan(typval_T *argvars, typval_T *rettv);
650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100652#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100653static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100654static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100655static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100656static void f_job_start(typval_T *argvars, typval_T *rettv);
657static void f_job_stop(typval_T *argvars, typval_T *rettv);
658static void f_job_status(typval_T *argvars, typval_T *rettv);
659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100661static void f_js_decode(typval_T *argvars, typval_T *rettv);
662static void f_js_encode(typval_T *argvars, typval_T *rettv);
663static void f_json_decode(typval_T *argvars, typval_T *rettv);
664static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_keys(typval_T *argvars, typval_T *rettv);
666static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
667static void f_len(typval_T *argvars, typval_T *rettv);
668static void f_libcall(typval_T *argvars, typval_T *rettv);
669static void f_libcallnr(typval_T *argvars, typval_T *rettv);
670static void f_line(typval_T *argvars, typval_T *rettv);
671static void f_line2byte(typval_T *argvars, typval_T *rettv);
672static void f_lispindent(typval_T *argvars, typval_T *rettv);
673static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_log(typval_T *argvars, typval_T *rettv);
676static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200678#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_map(typval_T *argvars, typval_T *rettv);
682static void f_maparg(typval_T *argvars, typval_T *rettv);
683static void f_mapcheck(typval_T *argvars, typval_T *rettv);
684static void f_match(typval_T *argvars, typval_T *rettv);
685static void f_matchadd(typval_T *argvars, typval_T *rettv);
686static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
687static void f_matcharg(typval_T *argvars, typval_T *rettv);
688static void f_matchdelete(typval_T *argvars, typval_T *rettv);
689static void f_matchend(typval_T *argvars, typval_T *rettv);
690static void f_matchlist(typval_T *argvars, typval_T *rettv);
691static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200692static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_max(typval_T *argvars, typval_T *rettv);
694static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100699#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100701#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
703static void f_nr2char(typval_T *argvars, typval_T *rettv);
704static void f_or(typval_T *argvars, typval_T *rettv);
705static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100706#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100708#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
713static void f_printf(typval_T *argvars, typval_T *rettv);
714static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
718#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_range(typval_T *argvars, typval_T *rettv);
722static void f_readfile(typval_T *argvars, typval_T *rettv);
723static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100724#ifdef FEAT_FLOAT
725static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
726#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_reltimestr(typval_T *argvars, typval_T *rettv);
728static void f_remote_expr(typval_T *argvars, typval_T *rettv);
729static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
730static void f_remote_peek(typval_T *argvars, typval_T *rettv);
731static void f_remote_read(typval_T *argvars, typval_T *rettv);
732static void f_remote_send(typval_T *argvars, typval_T *rettv);
733static void f_remove(typval_T *argvars, typval_T *rettv);
734static void f_rename(typval_T *argvars, typval_T *rettv);
735static void f_repeat(typval_T *argvars, typval_T *rettv);
736static void f_resolve(typval_T *argvars, typval_T *rettv);
737static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_screenattr(typval_T *argvars, typval_T *rettv);
742static void f_screenchar(typval_T *argvars, typval_T *rettv);
743static void f_screencol(typval_T *argvars, typval_T *rettv);
744static void f_screenrow(typval_T *argvars, typval_T *rettv);
745static void f_search(typval_T *argvars, typval_T *rettv);
746static void f_searchdecl(typval_T *argvars, typval_T *rettv);
747static void f_searchpair(typval_T *argvars, typval_T *rettv);
748static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
749static void f_searchpos(typval_T *argvars, typval_T *rettv);
750static void f_server2client(typval_T *argvars, typval_T *rettv);
751static void f_serverlist(typval_T *argvars, typval_T *rettv);
752static void f_setbufvar(typval_T *argvars, typval_T *rettv);
753static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
754static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100755static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_setline(typval_T *argvars, typval_T *rettv);
757static void f_setloclist(typval_T *argvars, typval_T *rettv);
758static void f_setmatches(typval_T *argvars, typval_T *rettv);
759static void f_setpos(typval_T *argvars, typval_T *rettv);
760static void f_setqflist(typval_T *argvars, typval_T *rettv);
761static void f_setreg(typval_T *argvars, typval_T *rettv);
762static void f_settabvar(typval_T *argvars, typval_T *rettv);
763static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
764static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100765#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100767#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_shellescape(typval_T *argvars, typval_T *rettv);
769static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
770static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sin(typval_T *argvars, typval_T *rettv);
773static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sort(typval_T *argvars, typval_T *rettv);
776static void f_soundfold(typval_T *argvars, typval_T *rettv);
777static void f_spellbadword(typval_T *argvars, typval_T *rettv);
778static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
779static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_sqrt(typval_T *argvars, typval_T *rettv);
782static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_str2nr(typval_T *argvars, typval_T *rettv);
785static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000788#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200789static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_stridx(typval_T *argvars, typval_T *rettv);
791static void f_string(typval_T *argvars, typval_T *rettv);
792static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200793static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_strpart(typval_T *argvars, typval_T *rettv);
795static void f_strridx(typval_T *argvars, typval_T *rettv);
796static void f_strtrans(typval_T *argvars, typval_T *rettv);
797static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
798static void f_strwidth(typval_T *argvars, typval_T *rettv);
799static void f_submatch(typval_T *argvars, typval_T *rettv);
800static void f_substitute(typval_T *argvars, typval_T *rettv);
801static void f_synID(typval_T *argvars, typval_T *rettv);
802static void f_synIDattr(typval_T *argvars, typval_T *rettv);
803static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
804static void f_synstack(typval_T *argvars, typval_T *rettv);
805static void f_synconcealed(typval_T *argvars, typval_T *rettv);
806static void f_system(typval_T *argvars, typval_T *rettv);
807static void f_systemlist(typval_T *argvars, typval_T *rettv);
808static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
809static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
810static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
811static void f_taglist(typval_T *argvars, typval_T *rettv);
812static void f_tagfiles(typval_T *argvars, typval_T *rettv);
813static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200814static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5c719942016-07-09 23:40:45 +0200815static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200816static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200817static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
818#ifdef FEAT_JOB_CHANNEL
819static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
820#endif
821static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
822#ifdef FEAT_JOB_CHANNEL
823static void f_test_null_job(typval_T *argvars, typval_T *rettv);
824#endif
825static void f_test_null_list(typval_T *argvars, typval_T *rettv);
826static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
827static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200828static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200829#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static void f_tan(typval_T *argvars, typval_T *rettv);
831static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200832#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100833#ifdef FEAT_TIMERS
834static void f_timer_start(typval_T *argvars, typval_T *rettv);
835static void f_timer_stop(typval_T *argvars, typval_T *rettv);
836#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_tolower(typval_T *argvars, typval_T *rettv);
838static void f_toupper(typval_T *argvars, typval_T *rettv);
839static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000840#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000842#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static void f_type(typval_T *argvars, typval_T *rettv);
844static void f_undofile(typval_T *argvars, typval_T *rettv);
845static void f_undotree(typval_T *argvars, typval_T *rettv);
846static void f_uniq(typval_T *argvars, typval_T *rettv);
847static void f_values(typval_T *argvars, typval_T *rettv);
848static void f_virtcol(typval_T *argvars, typval_T *rettv);
849static void f_visualmode(typval_T *argvars, typval_T *rettv);
850static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100851static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100852static void f_win_getid(typval_T *argvars, typval_T *rettv);
853static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
854static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
855static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static void f_winbufnr(typval_T *argvars, typval_T *rettv);
857static void f_wincol(typval_T *argvars, typval_T *rettv);
858static void f_winheight(typval_T *argvars, typval_T *rettv);
859static void f_winline(typval_T *argvars, typval_T *rettv);
860static void f_winnr(typval_T *argvars, typval_T *rettv);
861static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
862static void f_winrestview(typval_T *argvars, typval_T *rettv);
863static void f_winsaveview(typval_T *argvars, typval_T *rettv);
864static void f_winwidth(typval_T *argvars, typval_T *rettv);
865static void f_writefile(typval_T *argvars, typval_T *rettv);
866static void f_wordcount(typval_T *argvars, typval_T *rettv);
867static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
870static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
871static int get_env_len(char_u **arg);
872static int get_id_len(char_u **arg);
873static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
874static 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 +0000875#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
876#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
877 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
879static int eval_isnamec(int c);
880static int eval_isnamec1(int c);
881static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
882static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static typval_T *alloc_string_tv(char_u *string);
884static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100885#ifdef FEAT_FLOAT
886static float_T get_tv_float(typval_T *varp);
887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static linenr_T get_tv_lnum(typval_T *argvars);
889static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
891static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
892static hashtab_T *find_var_ht(char_u *name, char_u **varname);
893static funccall_T *get_funccal(void);
894static void vars_clear_ext(hashtab_T *ht, int free_val);
895static void delete_var(hashtab_T *ht, hashitem_T *hi);
896static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
897static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
898static void set_var(char_u *name, typval_T *varp, int copy);
899static int var_check_ro(int flags, char_u *name, int use_gettext);
900static int var_check_fixed(int flags, char_u *name, int use_gettext);
901static int var_check_func_name(char_u *name, int new_var);
902static int valid_varname(char_u *varname);
903static int tv_check_lock(int lock, char_u *name, int use_gettext);
904static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
905static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100906static 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 +0100907static int eval_fname_script(char_u *p);
908static int eval_fname_sid(char_u *p);
909static void list_func_head(ufunc_T *fp, int indent);
910static ufunc_T *find_func(char_u *name);
911static int function_exists(char_u *name);
912static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100914static void func_do_profile(ufunc_T *fp);
915static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
916static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000917static int
918# ifdef __BORLANDC__
919 _RTLENTRYF
920# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100921 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000922static int
923# ifdef __BORLANDC__
924 _RTLENTRYF
925# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100926 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100928static int script_autoload(char_u *name, int reload);
929static char_u *autoload_name(char_u *name);
930static void cat_func_name(char_u *buf, ufunc_T *fp);
931static void func_free(ufunc_T *fp);
932static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
933static int can_free_funccal(funccall_T *fc, int copyID) ;
934static void free_funccal(funccall_T *fc, int free_val);
935static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
936static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
937static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
938static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
939static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
940static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
941static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
942static int write_list(FILE *fd, list_T *list, int binary);
943static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200945
946#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100947static int compare_func_name(const void *s1, const void *s2);
948static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200949#endif
950
Bram Moolenaar33570922005-01-25 22:26:29 +0000951/*
952 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000953 */
954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 int i;
958 struct vimvar *p;
959
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200960 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
961 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200962 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000963 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000964 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000965
966 for (i = 0; i < VV_LEN; ++i)
967 {
968 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200969 if (STRLEN(p->vv_name) > 16)
970 {
971 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
972 getout(1);
973 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000974 STRCPY(p->vv_di.di_key, p->vv_name);
975 if (p->vv_flags & VV_RO)
976 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
977 else if (p->vv_flags & VV_RO_SBX)
978 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
979 else
980 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000981
982 /* add to v: scope dict, unless the value is not always available */
983 if (p->vv_type != VAR_UNKNOWN)
984 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000986 /* add to compat scope dict */
987 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100989 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
990
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000991 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100992 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200993 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100994 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100995
996 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
997 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
998 set_vim_var_nr(VV_NONE, VVAL_NONE);
999 set_vim_var_nr(VV_NULL, VVAL_NULL);
1000
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001001 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001002
1003#ifdef EBCDIC
1004 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001005 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001006 */
1007 sortFunctions();
1008#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001009}
1010
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001011#if defined(EXITFREE) || defined(PROTO)
1012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001014{
1015 int i;
1016 struct vimvar *p;
1017
1018 for (i = 0; i < VV_LEN; ++i)
1019 {
1020 p = &vimvars[i];
1021 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001022 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001023 vim_free(p->vv_str);
1024 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001025 }
1026 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1027 {
1028 list_unref(p->vv_list);
1029 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001030 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001031 }
1032 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001033 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001034 hash_clear(&compat_hashtab);
1035
Bram Moolenaard9fba312005-06-26 22:34:35 +00001036 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001037# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001038 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001039# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001040
1041 /* global variables */
1042 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001043
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001044 /* autoloaded script names */
1045 ga_clear_strings(&ga_loaded);
1046
Bram Moolenaarcca74132013-09-25 21:00:28 +02001047 /* Script-local variables. First clear all the variables and in a second
1048 * loop free the scriptvar_T, because a variable in one script might hold
1049 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001051 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001052 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001053 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001054 ga_clear(&ga_scripts);
1055
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001056 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001057 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001058
1059 /* functions */
1060 free_all_functions();
1061 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001062}
1063#endif
1064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * Return the name of the executed function.
1067 */
1068 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001069func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
1074/*
1075 * Return the address holding the next breakpoint line for a funccall cookie.
1076 */
1077 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001078func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082
1083/*
1084 * Return the address holding the debug tick for a funccall cookie.
1085 */
1086 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/*
1093 * Return the nesting level for a funccall cookie.
1094 */
1095 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001096func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
Bram Moolenaar33570922005-01-25 22:26:29 +00001098 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099}
1100
1101/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001102funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001104/* pointer to list of previously used funccal, still around because some
1105 * item in it is still being used. */
1106funccall_T *previous_funccal = NULL;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
1109 * Return TRUE when a function was ended by a ":return" command.
1110 */
1111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 return current_funccal->returned;
1115}
1116
1117
1118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * Set an internal variable to a string value. Creates the variable if it does
1120 * not already exist.
1121 */
1122 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001123set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001126 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 val = vim_strsave(value);
1129 if (val != NULL)
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001132 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001134 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 }
1138}
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001141#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143static char_u *redir_endp = NULL;
1144static char_u *redir_varname = NULL;
1145
1146/*
1147 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001148 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 * Returns OK if successfully completed the setup. FAIL otherwise.
1150 */
1151 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001152var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153{
1154 int save_emsg;
1155 int err;
1156 typval_T tv;
1157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001159 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 {
1161 EMSG(_(e_invarg));
1162 return FAIL;
1163 }
1164
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 redir_varname = vim_strsave(name);
1167 if (redir_varname == NULL)
1168 return FAIL;
1169
1170 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1171 if (redir_lval == NULL)
1172 {
1173 var_redir_stop();
1174 return FAIL;
1175 }
1176
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 /* The output is stored in growarray "redir_ga" until redirection ends. */
1178 ga_init2(&redir_ga, (int)sizeof(char), 500);
1179
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001181 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001182 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1184 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_endp != NULL && *redir_endp != NUL)
1187 /* Trailing characters are present after the variable name */
1188 EMSG(_(e_trailing));
1189 else
1190 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
1193 return FAIL;
1194 }
1195
1196 /* check if we can write to the variable: set it to or append an empty
1197 * string */
1198 save_emsg = did_emsg;
1199 did_emsg = FALSE;
1200 tv.v_type = VAR_STRING;
1201 tv.vval.v_string = (char_u *)"";
1202 if (append)
1203 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1204 else
1205 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001206 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001208 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209 if (err)
1210 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 var_redir_stop();
1213 return FAIL;
1214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215
1216 return OK;
1217}
1218
1219/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001220 * Append "value[value_len]" to the variable set by var_redir_start().
1221 * The actual appending is postponed until redirection ends, because the value
1222 * appended may in fact be the string we write to, changing it may cause freed
1223 * memory to be used:
1224 * :redir => foo
1225 * :let foo
1226 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 */
1228 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001229var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232
1233 if (redir_lval == NULL)
1234 return;
1235
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001239 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001241 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001242 {
1243 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001244 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001245 }
1246 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248}
1249
1250/*
1251 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001252 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001253 */
1254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001255var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001256{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001257 typval_T tv;
1258
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001259 if (EVALCMD_BUSY)
1260 {
1261 redir_lval = NULL;
1262 return;
1263 }
1264
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001265 if (redir_lval != NULL)
1266 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001267 /* If there was no error: assign the text to the variable. */
1268 if (redir_endp != NULL)
1269 {
1270 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1271 tv.v_type = VAR_STRING;
1272 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001273 /* Call get_lval() again, if it's inside a Dict or List it may
1274 * have changed. */
1275 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001276 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001277 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1278 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1279 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001280 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001281
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001282 /* free the collected output */
1283 vim_free(redir_ga.ga_data);
1284 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001285
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001286 vim_free(redir_lval);
1287 redir_lval = NULL;
1288 }
1289 vim_free(redir_varname);
1290 redir_varname = NULL;
1291}
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293# if defined(FEAT_MBYTE) || defined(PROTO)
1294 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001295eval_charconvert(
1296 char_u *enc_from,
1297 char_u *enc_to,
1298 char_u *fname_from,
1299 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int err = FALSE;
1302
1303 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1304 set_vim_var_string(VV_CC_TO, enc_to, -1);
1305 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1306 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1307 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1308 err = TRUE;
1309 set_vim_var_string(VV_CC_FROM, NULL, -1);
1310 set_vim_var_string(VV_CC_TO, NULL, -1);
1311 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1312 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1313
1314 if (err)
1315 return FAIL;
1316 return OK;
1317}
1318# endif
1319
1320# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 int err = FALSE;
1325
1326 set_vim_var_string(VV_FNAME_IN, fname, -1);
1327 set_vim_var_string(VV_CMDARG, args, -1);
1328 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1329 err = TRUE;
1330 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1331 set_vim_var_string(VV_CMDARG, NULL, -1);
1332
1333 if (err)
1334 {
1335 mch_remove(fname);
1336 return FAIL;
1337 }
1338 return OK;
1339}
1340# endif
1341
1342# if defined(FEAT_DIFF) || defined(PROTO)
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_diff(
1345 char_u *origfile,
1346 char_u *newfile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err = FALSE;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359
1360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361eval_patch(
1362 char_u *origfile,
1363 char_u *difffile,
1364 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int err;
1367
1368 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1369 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1370 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1371 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1372 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1373 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1374 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1375}
1376# endif
1377
1378/*
1379 * Top level evaluation function, returning a boolean.
1380 * Sets "error" to TRUE if there was an error.
1381 * Return TRUE or FALSE.
1382 */
1383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001384eval_to_bool(
1385 char_u *arg,
1386 int *error,
1387 char_u **nextcmd,
1388 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001391 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 if (skip)
1394 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 else
1398 {
1399 *error = FALSE;
1400 if (!skip)
1401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001402 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 }
1406 if (skip)
1407 --emsg_skip;
1408
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001409 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410}
1411
1412/*
1413 * Top level evaluation function, returning a string. If "skip" is TRUE,
1414 * only parsing to "nextcmd" is done, without reporting errors. Return
1415 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1416 */
1417 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001418eval_to_string_skip(
1419 char_u *arg,
1420 char_u **nextcmd,
1421 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 char_u *retval;
1425
1426 if (skip)
1427 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 retval = NULL;
1430 else
1431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 retval = vim_strsave(get_tv_string(&tv));
1433 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 if (skip)
1436 --emsg_skip;
1437
1438 return retval;
1439}
1440
1441/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001442 * Skip over an expression at "*pp".
1443 * Return FAIL for an error, OK otherwise.
1444 */
1445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001447{
Bram Moolenaar33570922005-01-25 22:26:29 +00001448 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001449
1450 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001452}
1453
1454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001456 * When "convert" is TRUE convert a List into a sequence of lines and convert
1457 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 * Return pointer to allocated memory, or NULL for failure.
1459 */
1460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461eval_to_string(
1462 char_u *arg,
1463 char_u **nextcmd,
1464 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001469#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 retval = NULL;
1475 else
1476 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001477 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001478 {
1479 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001480 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001481 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001482 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001483 if (tv.vval.v_list->lv_len > 0)
1484 ga_append(&ga, NL);
1485 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001486 ga_append(&ga, NUL);
1487 retval = (char_u *)ga.ga_data;
1488 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001489#ifdef FEAT_FLOAT
1490 else if (convert && tv.v_type == VAR_FLOAT)
1491 {
1492 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1493 retval = vim_strsave(numbuf);
1494 }
1495#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001496 else
1497 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500
1501 return retval;
1502}
1503
1504/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 * Call eval_to_string() without using current local variables and using
1506 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
1508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509eval_to_string_safe(
1510 char_u *arg,
1511 char_u **nextcmd,
1512 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 char_u *retval;
1515 void *save_funccalp;
1516
1517 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001518 if (use_sandbox)
1519 ++sandbox;
1520 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001521 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001522 if (use_sandbox)
1523 --sandbox;
1524 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 restore_funccal(save_funccalp);
1526 return retval;
1527}
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529/*
1530 * Top level evaluation function, returning a number.
1531 * Evaluates "expr" silently.
1532 * Returns -1 for an error.
1533 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaar33570922005-01-25 22:26:29 +00001537 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001538 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001539 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 ++emsg_off;
1542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001543 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 retval = -1;
1545 else
1546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001547 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 --emsg_off;
1551
1552 return retval;
1553}
1554
Bram Moolenaara40058a2005-07-11 22:42:07 +00001555/*
1556 * Prepare v: variable "idx" to be used.
1557 * Save the current typeval in "save_tv".
1558 * When not used yet add the variable to the v: hashtable.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001562{
1563 *save_tv = vimvars[idx].vv_tv;
1564 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1565 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1566}
1567
1568/*
1569 * Restore v: variable "idx" to typeval "save_tv".
1570 * When no longer defined, remove the variable from the v: hashtable.
1571 */
1572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001573restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001574{
1575 hashitem_T *hi;
1576
Bram Moolenaara40058a2005-07-11 22:42:07 +00001577 vimvars[idx].vv_tv = *save_tv;
1578 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1579 {
1580 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1581 if (HASHITEM_EMPTY(hi))
1582 EMSG2(_(e_intern2), "restore_vimvar()");
1583 else
1584 hash_remove(&vimvarht, hi);
1585 }
1586}
1587
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001588#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589/*
1590 * Evaluate an expression to a list with suggestions.
1591 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001592 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593 */
1594 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001596{
1597 typval_T save_val;
1598 typval_T rettv;
1599 list_T *list = NULL;
1600 char_u *p = skipwhite(expr);
1601
1602 /* Set "v:val" to the bad word. */
1603 prepare_vimvar(VV_VAL, &save_val);
1604 vimvars[VV_VAL].vv_type = VAR_STRING;
1605 vimvars[VV_VAL].vv_str = badword;
1606 if (p_verbose == 0)
1607 ++emsg_off;
1608
1609 if (eval1(&p, &rettv, TRUE) == OK)
1610 {
1611 if (rettv.v_type != VAR_LIST)
1612 clear_tv(&rettv);
1613 else
1614 list = rettv.vval.v_list;
1615 }
1616
1617 if (p_verbose == 0)
1618 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001619 restore_vimvar(VV_VAL, &save_val);
1620
1621 return list;
1622}
1623
1624/*
1625 * "list" is supposed to contain two items: a word and a number. Return the
1626 * word in "pp" and the number as the return value.
1627 * Return -1 if anything isn't right.
1628 * Used to get the good word and score from the eval_spell_expr() result.
1629 */
1630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001631get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001632{
1633 listitem_T *li;
1634
1635 li = list->lv_first;
1636 if (li == NULL)
1637 return -1;
1638 *pp = get_tv_string(&li->li_tv);
1639
1640 li = li->li_next;
1641 if (li == NULL)
1642 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001643 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001644}
1645#endif
1646
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001648 * Top level evaluation function.
1649 * Returns an allocated typval_T with the result.
1650 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001651 */
1652 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001653eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001654{
1655 typval_T *tv;
1656
1657 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001658 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001659 {
1660 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001661 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001662 }
1663
1664 return tv;
1665}
1666
1667
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001670 * Uses argv[argc] for the function arguments. Only Number and String
1671 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675call_vim_function(
1676 char_u *func,
1677 int argc,
1678 char_u **argv,
1679 int safe, /* use the sandbox */
1680 int str_arg_only, /* all arguments are strings */
1681 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001684 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 int len;
1686 int i;
1687 int doesrange;
1688 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001691 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695 for (i = 0; i < argc; i++)
1696 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001697 /* Pass a NULL or empty argument as an empty string */
1698 if (argv[i] == NULL || *argv[i] == NUL)
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001702 continue;
1703 }
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 if (str_arg_only)
1706 len = 0;
1707 else
1708 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001709 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (len != 0 && len == (int)STRLEN(argv[i]))
1711 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001712 argvars[i].v_type = VAR_NUMBER;
1713 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 else
1716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001717 argvars[i].v_type = VAR_STRING;
1718 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721
1722 if (safe)
1723 {
1724 save_funccalp = save_funccal();
1725 ++sandbox;
1726 }
1727
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1729 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001731 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (safe)
1733 {
1734 --sandbox;
1735 restore_funccal(save_funccalp);
1736 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 vim_free(argvars);
1738
1739 if (ret == FAIL)
1740 clear_tv(rettv);
1741
1742 return ret;
1743}
1744
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001745/*
1746 * Call vimL function "func" and return the result as a number.
1747 * Returns -1 when calling the function fails.
1748 * Uses argv[argc] for the function arguments.
1749 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001750 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751call_func_retnr(
1752 char_u *func,
1753 int argc,
1754 char_u **argv,
1755 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001756{
1757 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001758 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759
1760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1762 return -1;
1763
1764 retval = get_tv_number_chk(&rettv, NULL);
1765 clear_tv(&rettv);
1766 return retval;
1767}
1768
1769#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1770 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1771
Bram Moolenaar4f688582007-07-24 12:34:30 +00001772# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001774 * Call vimL function "func" and return the result as a string.
1775 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 * Uses argv[argc] for the function arguments.
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779call_func_retstr(
1780 char_u *func,
1781 int argc,
1782 char_u **argv,
1783 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784{
1785 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001786 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001787
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001788 /* All arguments are passed as strings, no conversion to number. */
1789 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001790 return NULL;
1791
1792 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return retval;
1795}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001796# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001798/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001799 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001800 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001801 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001802 */
1803 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001804call_func_retlist(
1805 char_u *func,
1806 int argc,
1807 char_u **argv,
1808 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001809{
1810 typval_T rettv;
1811
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001812 /* All arguments are passed as strings, no conversion to number. */
1813 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001814 return NULL;
1815
1816 if (rettv.v_type != VAR_LIST)
1817 {
1818 clear_tv(&rettv);
1819 return NULL;
1820 }
1821
1822 return rettv.vval.v_list;
1823}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#endif
1825
1826/*
1827 * Save the current function call pointer, and set it to NULL.
1828 * Used when executing autocommands and for ":source".
1829 */
1830 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001833 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 current_funccal = NULL;
1836 return (void *)fc;
1837}
1838
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001842 funccall_T *fc = (funccall_T *)vfc;
1843
1844 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845}
1846
Bram Moolenaar05159a02005-02-26 23:04:13 +00001847#if defined(FEAT_PROFILE) || defined(PROTO)
1848/*
1849 * Prepare profiling for entering a child or something else that is not
1850 * counted for the script/function itself.
1851 * Should always be called in pair with prof_child_exit().
1852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854prof_child_enter(
1855 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001856{
1857 funccall_T *fc = current_funccal;
1858
1859 if (fc != NULL && fc->func->uf_profiling)
1860 profile_start(&fc->prof_child);
1861 script_prof_save(tm);
1862}
1863
1864/*
1865 * Take care of time spent in a child.
1866 * Should always be called after prof_child_enter().
1867 */
1868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869prof_child_exit(
1870 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001871{
1872 funccall_T *fc = current_funccal;
1873
1874 if (fc != NULL && fc->func->uf_profiling)
1875 {
1876 profile_end(&fc->prof_child);
1877 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1878 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1879 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1880 }
1881 script_prof_restore(tm);
1882}
1883#endif
1884
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#ifdef FEAT_FOLDING
1887/*
1888 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1889 * it in "*cp". Doesn't give error messages.
1890 */
1891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001895 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001897 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1898 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001901 if (use_sandbox)
1902 ++sandbox;
1903 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 retval = 0;
1907 else
1908 {
1909 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 if (tv.v_type == VAR_NUMBER)
1911 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001912 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 retval = 0;
1914 else
1915 {
1916 /* If the result is a string, check if there is a non-digit before
1917 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (!VIM_ISDIGIT(*s) && *s != '-')
1920 *cp = *s++;
1921 retval = atol((char *)s);
1922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001926 if (use_sandbox)
1927 --sandbox;
1928 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001930 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 * ":let" list all variable values
1936 * ":let var1 var2" list variable values
1937 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 * ":let var += expr" assignment command.
1939 * ":let var -= expr" assignment command.
1940 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001944ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 int var_count = 0;
1951 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001953 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaardb552d602006-03-23 22:59:57 +00001956 argend = skip_var_list(arg, &var_count, &semicolon);
1957 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001959 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1960 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001961 expr = skipwhite(argend);
1962 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1963 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001965 /*
1966 * ":let" without "=": list variables
1967 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 if (*arg == '[')
1969 EMSG(_(e_invarg));
1970 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976 list_glob_vars(&first);
1977 list_buf_vars(&first);
1978 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001979#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001980 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001981#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001982 list_script_vars(&first);
1983 list_func_vars(&first);
1984 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 eap->nextcmd = check_nextcmd(arg);
1987 }
1988 else
1989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 op[0] = '=';
1991 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001992 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001994 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1995 op[0] = *expr; /* +=, -= or .= */
1996 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001998 else
1999 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (eap->skip)
2002 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (eap->skip)
2005 {
2006 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 --emsg_skip;
2009 }
2010 else if (i != FAIL)
2011 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
2017}
2018
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019/*
2020 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2021 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 * When "nextchars" is not NULL it points to a string with characters that
2023 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2024 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 * Returns OK or FAIL;
2026 */
2027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028ex_let_vars(
2029 char_u *arg_start,
2030 typval_T *tv,
2031 int copy, /* copy values from "tv", don't move */
2032 int semicolon, /* from skip_var_list() */
2033 int var_count, /* from skip_var_list() */
2034 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035{
2036 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002037 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 listitem_T *item;
2040 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041
2042 if (*arg != '[')
2043 {
2044 /*
2045 * ":let var = expr" or ":for var in list"
2046 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 return OK;
2050 }
2051
2052 /*
2053 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2054 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002055 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 EMSG(_(e_listreq));
2058 return FAIL;
2059 }
2060
2061 i = list_len(l);
2062 if (semicolon == 0 && var_count < i)
2063 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002064 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 return FAIL;
2066 }
2067 if (var_count - semicolon > i)
2068 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002069 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 return FAIL;
2071 }
2072
2073 item = l->lv_first;
2074 while (*arg != ']')
2075 {
2076 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078 item = item->li_next;
2079 if (arg == NULL)
2080 return FAIL;
2081
2082 arg = skipwhite(arg);
2083 if (*arg == ';')
2084 {
2085 /* Put the rest of the list (may be empty) in the var after ';'.
2086 * Create a new list for this. */
2087 l = list_alloc();
2088 if (l == NULL)
2089 return FAIL;
2090 while (item != NULL)
2091 {
2092 list_append_tv(l, &item->li_tv);
2093 item = item->li_next;
2094 }
2095
2096 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002097 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002098 ltv.vval.v_list = l;
2099 l->lv_refcount = 1;
2100
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2102 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103 clear_tv(&ltv);
2104 if (arg == NULL)
2105 return FAIL;
2106 break;
2107 }
2108 else if (*arg != ',' && *arg != ']')
2109 {
2110 EMSG2(_(e_intern2), "ex_let_vars()");
2111 return FAIL;
2112 }
2113 }
2114
2115 return OK;
2116}
2117
2118/*
2119 * Skip over assignable variable "var" or list of variables "[var, var]".
2120 * Used for ":let varvar = expr" and ":for varvar in expr".
2121 * For "[var, var]" increment "*var_count" for each variable.
2122 * for "[var, var; var]" set "semicolon".
2123 * Return NULL for an error.
2124 */
2125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126skip_var_list(
2127 char_u *arg,
2128 int *var_count,
2129 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130{
2131 char_u *p, *s;
2132
2133 if (*arg == '[')
2134 {
2135 /* "[var, var]": find the matching ']'. */
2136 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002137 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002138 {
2139 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2140 s = skip_var_one(p);
2141 if (s == p)
2142 {
2143 EMSG2(_(e_invarg2), p);
2144 return NULL;
2145 }
2146 ++*var_count;
2147
2148 p = skipwhite(s);
2149 if (*p == ']')
2150 break;
2151 else if (*p == ';')
2152 {
2153 if (*semicolon == 1)
2154 {
2155 EMSG(_("Double ; in list of variables"));
2156 return NULL;
2157 }
2158 *semicolon = 1;
2159 }
2160 else if (*p != ',')
2161 {
2162 EMSG2(_(e_invarg2), p);
2163 return NULL;
2164 }
2165 }
2166 return p + 1;
2167 }
2168 else
2169 return skip_var_one(arg);
2170}
2171
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002173 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002174 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002179 if (*arg == '@' && arg[1] != NUL)
2180 return arg + 2;
2181 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2182 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183}
2184
Bram Moolenaara7043832005-01-21 11:56:39 +00002185/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 * List variables for hashtab "ht" with prefix "prefix".
2187 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_hashtable_vars(
2191 hashtab_T *ht,
2192 char_u *prefix,
2193 int empty,
2194 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002195{
Bram Moolenaar33570922005-01-25 22:26:29 +00002196 hashitem_T *hi;
2197 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 int todo;
2199
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002200 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002201 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2202 {
2203 if (!HASHITEM_EMPTY(hi))
2204 {
2205 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 di = HI2DI(hi);
2207 if (empty || di->di_tv.v_type != VAR_STRING
2208 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002210 }
2211 }
2212}
2213
2214/*
2215 * List global variables.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002219{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
2223/*
2224 * List buffer variables.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 char_u numbuf[NUMBUFLEN];
2230
Bram Moolenaar429fa852013-04-15 12:27:36 +02002231 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233
2234 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2236 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002237}
2238
2239/*
2240 * List window variables.
2241 */
2242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002245 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002247}
2248
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
2250/*
2251 * List tab page variables.
2252 */
2253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002254list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002256 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002258}
2259#endif
2260
Bram Moolenaara7043832005-01-21 11:56:39 +00002261/*
2262 * List Vim variables.
2263 */
2264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268}
2269
2270/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271 * List script-local variables, if there is a script.
2272 */
2273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002274list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275{
2276 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2278 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002279}
2280
2281/*
2282 * List function variables, if there is a function.
2283 */
2284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002285list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002286{
2287 if (current_funccal != NULL)
2288 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002290}
2291
2292/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 * List variables in "arg".
2294 */
2295 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002296list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297{
2298 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 char_u *name_start;
2302 char_u *arg_subsc;
2303 char_u *tofree;
2304 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305
2306 while (!ends_excmd(*arg) && !got_int)
2307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002310 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2312 {
2313 emsg_severe = TRUE;
2314 EMSG(_(e_trailing));
2315 break;
2316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 /* get_name_len() takes care of expanding curly braces */
2321 name_start = name = arg;
2322 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2323 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 /* This is mainly to keep test 49 working: when expanding
2326 * curly braces fails overrule the exception error message. */
2327 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 emsg_severe = TRUE;
2330 EMSG2(_(e_invarg2), arg);
2331 break;
2332 }
2333 error = TRUE;
2334 }
2335 else
2336 {
2337 if (tofree != NULL)
2338 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002339 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 else
2342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002343 /* handle d.key, l[idx], f(expr) */
2344 arg_subsc = arg;
2345 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002349 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002352 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002353 case 'g': list_glob_vars(first); break;
2354 case 'b': list_buf_vars(first); break;
2355 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002356#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002357 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002358#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002359 case 'v': list_vim_vars(first); break;
2360 case 's': list_script_vars(first); break;
2361 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362 default:
2363 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002364 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002365 }
2366 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002367 {
2368 char_u numbuf[NUMBUFLEN];
2369 char_u *tf;
2370 int c;
2371 char_u *s;
2372
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002373 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374 c = *arg;
2375 *arg = NUL;
2376 list_one_var_a((char_u *)"",
2377 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002378 tv.v_type,
2379 s == NULL ? (char_u *)"" : s,
2380 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002381 *arg = c;
2382 vim_free(tf);
2383 }
2384 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
2387 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002388
2389 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002391
2392 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 }
2394
2395 return arg;
2396}
2397
2398/*
2399 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2400 * Returns a pointer to the char just after the var name.
2401 * Returns NULL if there is an error.
2402 */
2403 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002404ex_let_one(
2405 char_u *arg, /* points to variable name */
2406 typval_T *tv, /* value to assign to variable */
2407 int copy, /* copy value from "tv" */
2408 char_u *endchars, /* valid chars after variable name or NULL */
2409 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410{
2411 int c1;
2412 char_u *name;
2413 char_u *p;
2414 char_u *arg_end = NULL;
2415 int len;
2416 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418
2419 /*
2420 * ":let $VAR = expr": Set environment variable.
2421 */
2422 if (*arg == '$')
2423 {
2424 /* Find the end of the name. */
2425 ++arg;
2426 name = arg;
2427 len = get_env_len(&arg);
2428 if (len == 0)
2429 EMSG2(_(e_invarg2), name - 1);
2430 else
2431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 if (op != NULL && (*op == '+' || *op == '-'))
2433 EMSG2(_(e_letwrong), op);
2434 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002437 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 {
2439 c1 = name[len];
2440 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 p = get_tv_string_chk(tv);
2442 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 int mustfree = FALSE;
2445 char_u *s = vim_getenv(name, &mustfree);
2446
2447 if (s != NULL)
2448 {
2449 p = tofree = concat_str(s, p);
2450 if (mustfree)
2451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (STRICMP(name, "HOME") == 0)
2458 init_homedir();
2459 else if (didset_vim && STRICMP(name, "VIM") == 0)
2460 didset_vim = FALSE;
2461 else if (didset_vimruntime
2462 && STRICMP(name, "VIMRUNTIME") == 0)
2463 didset_vimruntime = FALSE;
2464 arg_end = arg;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470 }
2471
2472 /*
2473 * ":let &option = expr": Set option value.
2474 * ":let &l:option = expr": Set local option value.
2475 * ":let &g:option = expr": Set global option value.
2476 */
2477 else if (*arg == '&')
2478 {
2479 /* Find the end of the name. */
2480 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481 if (p == NULL || (endchars != NULL
2482 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 long n;
2487 int opt_type;
2488 long numval;
2489 char_u *stringval = NULL;
2490 char_u *s;
2491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 c1 = *p;
2493 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002495 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 s = get_tv_string_chk(tv); /* != NULL if number or string */
2497 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 {
2499 opt_type = get_option_value(arg, &numval,
2500 &stringval, opt_flags);
2501 if ((opt_type == 1 && *op == '.')
2502 || (opt_type == 0 && *op != '.'))
2503 EMSG2(_(e_letwrong), op);
2504 else
2505 {
2506 if (opt_type == 1) /* number */
2507 {
2508 if (*op == '+')
2509 n = numval + n;
2510 else
2511 n = numval - n;
2512 }
2513 else if (opt_type == 0 && stringval != NULL) /* string */
2514 {
2515 s = concat_str(stringval, s);
2516 vim_free(stringval);
2517 stringval = s;
2518 }
2519 }
2520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 if (s != NULL)
2522 {
2523 set_option_value(arg, n, s, opt_flags);
2524 arg_end = p;
2525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 }
2529 }
2530
2531 /*
2532 * ":let @r = expr": Set register contents.
2533 */
2534 else if (*arg == '@')
2535 {
2536 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 if (op != NULL && (*op == '+' || *op == '-'))
2538 EMSG2(_(e_letwrong), op);
2539 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002540 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 EMSG(_(e_letunexp));
2542 else
2543 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002544 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 char_u *s;
2546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 p = get_tv_string_chk(tv);
2548 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002550 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551 if (s != NULL)
2552 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002553 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002554 vim_free(s);
2555 }
2556 }
2557 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 arg_end = arg + 1;
2561 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002562 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 }
2564 }
2565
2566 /*
2567 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002570 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002574 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2578 EMSG(_(e_letunexp));
2579 else
2580 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 arg_end = p;
2583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 }
2587
2588 else
2589 EMSG2(_(e_invarg2), arg);
2590
2591 return arg_end;
2592}
2593
2594/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2596 */
2597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002598check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599{
2600 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2601 {
2602 EMSG2(_(e_readonlyvar), arg);
2603 return TRUE;
2604 }
2605 return FALSE;
2606}
2607
2608/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * Get an lval: variable, Dict item or List item that can be assigned a value
2610 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2611 * "name.key", "name.key[expr]" etc.
2612 * Indexing only works if "name" is an existing List or Dictionary.
2613 * "name" points to the start of the name.
2614 * If "rettv" is not NULL it points to the value to be assigned.
2615 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2616 * wrong; must end in space or cmd separator.
2617 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002618 * flags:
2619 * GLV_QUIET: do not give error messages
2620 * GLV_NO_AUTOLOAD: do not use script autoloading
2621 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002623 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002625 */
2626 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002627get_lval(
2628 char_u *name,
2629 typval_T *rettv,
2630 lval_T *lp,
2631 int unlet,
2632 int skip,
2633 int flags, /* GLV_ values */
2634 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 char_u *expr_start, *expr_end;
2638 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 dictitem_T *v;
2640 typval_T var1;
2641 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002647 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651
2652 if (skip)
2653 {
2654 /* When skipping just find the end of the name. */
2655 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002656 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 }
2658
2659 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002660 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (expr_start != NULL)
2662 {
2663 /* Don't expand the name when we already know there is an error. */
2664 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2665 && *p != '[' && *p != '.')
2666 {
2667 EMSG(_(e_trailing));
2668 return NULL;
2669 }
2670
2671 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2672 if (lp->ll_exp_name == NULL)
2673 {
2674 /* Report an invalid expression in braces, unless the
2675 * expression evaluation has been cancelled due to an
2676 * aborting error, an interrupt, or an exception. */
2677 if (!aborting() && !quiet)
2678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002679 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 EMSG2(_(e_invarg2), name);
2681 return NULL;
2682 }
2683 }
2684 lp->ll_name = lp->ll_exp_name;
2685 }
2686 else
2687 lp->ll_name = name;
2688
2689 /* Without [idx] or .key we are done. */
2690 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2691 return p;
2692
2693 cc = *p;
2694 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002695 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (v == NULL && !quiet)
2697 EMSG2(_(e_undefvar), lp->ll_name);
2698 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 if (v == NULL)
2700 return NULL;
2701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 /*
2703 * Loop until no more [idx] or .key is following.
2704 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002705 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2709 && !(lp->ll_tv->v_type == VAR_DICT
2710 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_("E689: Can only index a List or Dictionary"));
2714 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E708: [:] must come last"));
2720 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 len = -1;
2724 if (*p == '.')
2725 {
2726 key = p + 1;
2727 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2728 ;
2729 if (len == 0)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
2733 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
2735 p = key + len;
2736 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 else
2738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (*p == ':')
2742 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743 else
2744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 empty1 = FALSE;
2746 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 if (get_tv_string_chk(&var1) == NULL)
2749 {
2750 /* not a number or string */
2751 clear_tv(&var1);
2752 return NULL;
2753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755
2756 /* Optionally get the second index [ :expr]. */
2757 if (*p == ':')
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002763 if (!empty1)
2764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002766 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (rettv != NULL && (rettv->v_type != VAR_LIST
2768 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
2771 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (!empty1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 p = skipwhite(p + 1);
2777 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 else
2780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (!empty1)
2785 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 if (get_tv_string_chk(&var2) == NULL)
2789 {
2790 /* not a number or string */
2791 if (!empty1)
2792 clear_tv(&var1);
2793 clear_tv(&var2);
2794 return NULL;
2795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002801
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (!quiet)
2805 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 if (!empty1)
2807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812
2813 /* Skip to past ']'. */
2814 ++p;
2815 }
2816
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
2819 if (len == -1)
2820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002822 key = get_tv_string_chk(&var1); /* is number or string */
2823 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_list = NULL;
2830 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002831 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002832
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002833 /* When assigning to a scope dictionary check that a function and
2834 * variable name is valid (only variable name unless it is l: or
2835 * g: dictionary). Disallow overwriting a builtin function. */
2836 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002838 int prevval;
2839 int wrong;
2840
2841 if (len != -1)
2842 {
2843 prevval = key[len];
2844 key[len] = NUL;
2845 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002846 else
2847 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002848 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2849 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002850 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002851 || !valid_varname(key);
2852 if (len != -1)
2853 key[len] = prevval;
2854 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002855 return NULL;
2856 }
2857
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 /* Can't add "v:" variable. */
2861 if (lp->ll_dict == &vimvardict)
2862 {
2863 EMSG2(_(e_illvar), name);
2864 return NULL;
2865 }
2866
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002867 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 if (len == -1)
2873 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 }
2876 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 if (len == -1)
2881 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 p = NULL;
2884 break;
2885 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002886 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002887 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002888 return NULL;
2889
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 if (len == -1)
2891 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 }
2894 else
2895 {
2896 /*
2897 * Get the number and item for the only or first index of the List.
2898 */
2899 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 else
2902 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002903 lp->ll_n1 = (long)get_tv_number(&var1);
2904 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 clear_tv(&var1);
2906 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_list = lp->ll_tv->vval.v_list;
2909 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2910 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002912 if (lp->ll_n1 < 0)
2913 {
2914 lp->ll_n1 = 0;
2915 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2916 }
2917 }
2918 if (lp->ll_li == NULL)
2919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002922 if (!quiet)
2923 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 }
2926
2927 /*
2928 * May need to find the item or absolute index for the second
2929 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 * When no index given: "lp->ll_empty2" is TRUE.
2931 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002934 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002935 lp->ll_n2 = (long)get_tv_number(&var2);
2936 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002941 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 {
2943 if (!quiet)
2944 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 }
2949
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2951 if (lp->ll_n1 < 0)
2952 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2953 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002954 {
2955 if (!quiet)
2956 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002958 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002959 }
2960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 return p;
2966}
2967
2968/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002969 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 */
2971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002972clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973{
2974 vim_free(lp->ll_exp_name);
2975 vim_free(lp->ll_newkey);
2976}
2977
2978/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 */
2983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002984set_var_lval(
2985 lval_T *lp,
2986 char_u *endp,
2987 typval_T *rettv,
2988 int copy,
2989 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990{
2991 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002992 listitem_T *ri;
2993 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994
2995 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 cc = *endp;
3000 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 if (op != NULL && *op != '=')
3002 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003006 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003007 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003008 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003010 if ((di == NULL
3011 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3012 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3013 FALSE)))
3014 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 set_var(lp->ll_name, &tv, FALSE);
3016 clear_tv(&tv);
3017 }
3018 }
3019 else
3020 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003024 else if (tv_check_lock(lp->ll_newkey == NULL
3025 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003026 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003027 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 else if (lp->ll_range)
3029 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003030 listitem_T *ll_li = lp->ll_li;
3031 int ll_n1 = lp->ll_n1;
3032
3033 /*
3034 * Check whether any of the list items is locked
3035 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003036 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003037 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003038 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003039 return;
3040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3042 break;
3043 ll_li = ll_li->li_next;
3044 ++ll_n1;
3045 }
3046
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /*
3048 * Assign the List values to the list items.
3049 */
3050 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 if (op != NULL && *op != '=')
3053 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3054 else
3055 {
3056 clear_tv(&lp->ll_li->li_tv);
3057 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 ri = ri->li_next;
3060 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3061 break;
3062 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003065 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 ri = NULL;
3068 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003069 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 lp->ll_li = lp->ll_li->li_next;
3072 ++lp->ll_n1;
3073 }
3074 if (ri != NULL)
3075 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 else if (lp->ll_empty2
3077 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 : lp->ll_n1 != lp->ll_n2)
3079 EMSG(_("E711: List value has not enough items"));
3080 }
3081 else
3082 {
3083 /*
3084 * Assign to a List or Dictionary item.
3085 */
3086 if (lp->ll_newkey != NULL)
3087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 if (op != NULL && *op != '=')
3089 {
3090 EMSG2(_(e_letwrong), op);
3091 return;
3092 }
3093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 if (di == NULL)
3097 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003098 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3099 {
3100 vim_free(di);
3101 return;
3102 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 else if (op != NULL && *op != '=')
3106 {
3107 tv_op(lp->ll_tv, rettv, op);
3108 return;
3109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003111 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003113 /*
3114 * Assign the value to the variable or list item.
3115 */
3116 if (copy)
3117 copy_tv(rettv, lp->ll_tv);
3118 else
3119 {
3120 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003121 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003122 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003123 }
3124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125}
3126
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3129 * Returns OK or FAIL.
3130 */
3131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003132tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003134 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 char_u numbuf[NUMBUFLEN];
3136 char_u *s;
3137
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003138 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3139 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3140 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 {
3142 switch (tv1->v_type)
3143 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003144 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 case VAR_DICT:
3146 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003147 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003148 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003149 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003150 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 break;
3152
3153 case VAR_LIST:
3154 if (*op != '+' || tv2->v_type != VAR_LIST)
3155 break;
3156 /* List += List */
3157 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3158 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3159 return OK;
3160
3161 case VAR_NUMBER:
3162 case VAR_STRING:
3163 if (tv2->v_type == VAR_LIST)
3164 break;
3165 if (*op == '+' || *op == '-')
3166 {
3167 /* nr += nr or nr -= nr*/
3168 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#ifdef FEAT_FLOAT
3170 if (tv2->v_type == VAR_FLOAT)
3171 {
3172 float_T f = n;
3173
3174 if (*op == '+')
3175 f += tv2->vval.v_float;
3176 else
3177 f -= tv2->vval.v_float;
3178 clear_tv(tv1);
3179 tv1->v_type = VAR_FLOAT;
3180 tv1->vval.v_float = f;
3181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003183#endif
3184 {
3185 if (*op == '+')
3186 n += get_tv_number(tv2);
3187 else
3188 n -= get_tv_number(tv2);
3189 clear_tv(tv1);
3190 tv1->v_type = VAR_NUMBER;
3191 tv1->vval.v_number = n;
3192 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003193 }
3194 else
3195 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 if (tv2->v_type == VAR_FLOAT)
3197 break;
3198
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003199 /* str .= str */
3200 s = get_tv_string(tv1);
3201 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3202 clear_tv(tv1);
3203 tv1->v_type = VAR_STRING;
3204 tv1->vval.v_string = s;
3205 }
3206 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003209#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 {
3211 float_T f;
3212
3213 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3214 && tv2->v_type != VAR_NUMBER
3215 && tv2->v_type != VAR_STRING))
3216 break;
3217 if (tv2->v_type == VAR_FLOAT)
3218 f = tv2->vval.v_float;
3219 else
3220 f = get_tv_number(tv2);
3221 if (*op == '+')
3222 tv1->vval.v_float += f;
3223 else
3224 tv1->vval.v_float -= f;
3225 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003227 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003228 }
3229 }
3230
3231 EMSG2(_(e_letwrong), op);
3232 return FAIL;
3233}
3234
3235/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 * Add a watcher to a list.
3237 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
3241 lw->lw_next = l->lv_watch;
3242 l->lv_watch = lw;
3243}
3244
3245/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003246 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 * No warning when it isn't found...
3248 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003250list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253
3254 lwp = &l->lv_watch;
3255 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3256 {
3257 if (lw == lwrem)
3258 {
3259 *lwp = lw->lw_next;
3260 break;
3261 }
3262 lwp = &lw->lw_next;
3263 }
3264}
3265
3266/*
3267 * Just before removing an item from a list: advance watchers to the next
3268 * item.
3269 */
3270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272{
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274
3275 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3276 if (lw->lw_item == item)
3277 lw->lw_item = item->li_next;
3278}
3279
3280/*
3281 * Evaluate the expression used in a ":for var in expr" command.
3282 * "arg" points to "var".
3283 * Set "*errp" to TRUE for an error, FALSE otherwise;
3284 * Return a pointer that holds the info. Null when there is an error.
3285 */
3286 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287eval_for_line(
3288 char_u *arg,
3289 int *errp,
3290 char_u **nextcmdp,
3291 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 typval_T tv;
3296 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
3298 *errp = TRUE; /* default: there is an error */
3299
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 if (fi == NULL)
3302 return NULL;
3303
3304 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3305 if (expr == NULL)
3306 return fi;
3307
3308 expr = skipwhite(expr);
3309 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3310 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003311 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 return fi;
3313 }
3314
3315 if (skip)
3316 ++emsg_skip;
3317 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3318 {
3319 *errp = FALSE;
3320 if (!skip)
3321 {
3322 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003323 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 clear_tv(&tv);
3327 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003328 else if (l == NULL)
3329 {
3330 /* a null list is like an empty list: do nothing */
3331 clear_tv(&tv);
3332 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 else
3334 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003335 /* No need to increment the refcount, it's already set for the
3336 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 fi->fi_list = l;
3338 list_add_watch(l, &fi->fi_lw);
3339 fi->fi_lw.lw_item = l->lv_first;
3340 }
3341 }
3342 }
3343 if (skip)
3344 --emsg_skip;
3345
3346 return fi;
3347}
3348
3349/*
3350 * Use the first item in a ":for" list. Advance to the next.
3351 * Assign the values to the variable (list). "arg" points to the first one.
3352 * Return TRUE when a valid item was found, FALSE when at end of list or
3353 * something wrong.
3354 */
3355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003358 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003360 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361
3362 item = fi->fi_lw.lw_item;
3363 if (item == NULL)
3364 result = FALSE;
3365 else
3366 {
3367 fi->fi_lw.lw_item = item->li_next;
3368 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3369 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3370 }
3371 return result;
3372}
3373
3374/*
3375 * Free the structure used to store info used by ":for".
3376 */
3377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003379{
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003382 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003383 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003385 list_unref(fi->fi_list);
3386 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 vim_free(fi);
3388}
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3391
3392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393set_context_for_expression(
3394 expand_T *xp,
3395 char_u *arg,
3396 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 int got_eq = FALSE;
3399 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003400 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 if (cmdidx == CMD_let)
3403 {
3404 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003405 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003406 {
3407 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003408 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003409 {
3410 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003411 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003412 if (vim_iswhite(*p))
3413 break;
3414 }
3415 return;
3416 }
3417 }
3418 else
3419 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3420 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 while ((xp->xp_pattern = vim_strpbrk(arg,
3422 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3423 {
3424 c = *xp->xp_pattern;
3425 if (c == '&')
3426 {
3427 c = xp->xp_pattern[1];
3428 if (c == '&')
3429 {
3430 ++xp->xp_pattern;
3431 xp->xp_context = cmdidx != CMD_let || got_eq
3432 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3433 }
3434 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003437 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3438 xp->xp_pattern += 2;
3439
3440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 else if (c == '$')
3443 {
3444 /* environment variable */
3445 xp->xp_context = EXPAND_ENV_VARS;
3446 }
3447 else if (c == '=')
3448 {
3449 got_eq = TRUE;
3450 xp->xp_context = EXPAND_EXPRESSION;
3451 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003452 else if (c == '#'
3453 && xp->xp_context == EXPAND_EXPRESSION)
3454 {
3455 /* Autoload function/variable contains '#'. */
3456 break;
3457 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003458 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 && xp->xp_context == EXPAND_FUNCTIONS
3460 && vim_strchr(xp->xp_pattern, '(') == NULL)
3461 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003462 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 break;
3464 }
3465 else if (cmdidx != CMD_let || got_eq)
3466 {
3467 if (c == '"') /* string */
3468 {
3469 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3470 if (c == '\\' && xp->xp_pattern[1] != NUL)
3471 ++xp->xp_pattern;
3472 xp->xp_context = EXPAND_NOTHING;
3473 }
3474 else if (c == '\'') /* literal string */
3475 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3478 /* skip */ ;
3479 xp->xp_context = EXPAND_NOTHING;
3480 }
3481 else if (c == '|')
3482 {
3483 if (xp->xp_pattern[1] == '|')
3484 {
3485 ++xp->xp_pattern;
3486 xp->xp_context = EXPAND_EXPRESSION;
3487 }
3488 else
3489 xp->xp_context = EXPAND_COMMANDS;
3490 }
3491 else
3492 xp->xp_context = EXPAND_EXPRESSION;
3493 }
3494 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003495 /* Doesn't look like something valid, expand as an expression
3496 * anyway. */
3497 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 arg = xp->xp_pattern;
3499 if (*arg != NUL)
3500 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3501 /* skip */ ;
3502 }
3503 xp->xp_pattern = arg;
3504}
3505
3506#endif /* FEAT_CMDL_COMPL */
3507
3508/*
3509 * ":1,25call func(arg1, arg2)" function call.
3510 */
3511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 char_u *arg = eap->arg;
3515 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003517 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 linenr_T lnum;
3521 int doesrange;
3522 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003524 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003526 if (eap->skip)
3527 {
3528 /* trans_function_name() doesn't work well when skipping, use eval0()
3529 * instead to skip to any following command, e.g. for:
3530 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003531 ++emsg_skip;
3532 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3533 clear_tv(&rettv);
3534 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003535 return;
3536 }
3537
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003539 if (fudi.fd_newkey != NULL)
3540 {
3541 /* Still need to give an error message for missing key. */
3542 EMSG2(_(e_dictkey), fudi.fd_newkey);
3543 vim_free(fudi.fd_newkey);
3544 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003545 if (tofree == NULL)
3546 return;
3547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003548 /* Increase refcount on dictionary, it could get deleted when evaluating
3549 * the arguments. */
3550 if (fudi.fd_dict != NULL)
3551 ++fudi.fd_dict->dv_refcount;
3552
Bram Moolenaar65639032016-03-16 21:40:30 +01003553 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3554 * contents. For VAR_PARTIAL get its partial, unless we already have one
3555 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003557 name = deref_func_name(tofree, &len,
3558 partial != NULL ? NULL : &partial, FALSE);
3559
Bram Moolenaar532c7802005-01-27 14:44:31 +00003560 /* Skip white space to allow ":call func ()". Not good, but required for
3561 * backward compatibility. */
3562 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003563 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 if (*startarg != '(')
3566 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003567 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 goto end;
3569 }
3570
3571 /*
3572 * When skipping, evaluate the function once, to find the end of the
3573 * arguments.
3574 * When the function takes a range, this is discovered after the first
3575 * call, and the loop is broken.
3576 */
3577 if (eap->skip)
3578 {
3579 ++emsg_skip;
3580 lnum = eap->line2; /* do it once, also with an invalid range */
3581 }
3582 else
3583 lnum = eap->line1;
3584 for ( ; lnum <= eap->line2; ++lnum)
3585 {
3586 if (!eap->skip && eap->addr_count > 0)
3587 {
3588 curwin->w_cursor.lnum = lnum;
3589 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003590#ifdef FEAT_VIRTUALEDIT
3591 curwin->w_cursor.coladd = 0;
3592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003595 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003596 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003597 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 failed = TRUE;
3600 break;
3601 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003602
3603 /* Handle a function returning a Funcref, Dictionary or List. */
3604 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3605 {
3606 failed = TRUE;
3607 break;
3608 }
3609
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (doesrange || eap->skip)
3612 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003615 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (aborting())
3619 break;
3620 }
3621 if (eap->skip)
3622 --emsg_skip;
3623
3624 if (!failed)
3625 {
3626 /* Check for trailing illegal characters and a following command. */
3627 if (!ends_excmd(*arg))
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 else
3633 eap->nextcmd = check_nextcmd(arg);
3634 }
3635
3636end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003637 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003638 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639}
3640
3641/*
3642 * ":unlet[!] var1 ... " command.
3643 */
3644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 ex_unletlock(eap, eap->arg, 0);
3648}
3649
3650/*
3651 * ":lockvar" and ":unlockvar" commands
3652 */
3653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 int deep = 2;
3658
3659 if (eap->forceit)
3660 deep = -1;
3661 else if (vim_isdigit(*arg))
3662 {
3663 deep = getdigits(&arg);
3664 arg = skipwhite(arg);
3665 }
3666
3667 ex_unletlock(eap, arg, deep);
3668}
3669
3670/*
3671 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674ex_unletlock(
3675 exarg_T *eap,
3676 char_u *argstart,
3677 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678{
3679 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 do
3685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003687 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003688 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 if (lv.ll_name == NULL)
3690 error = TRUE; /* error but continue parsing */
3691 if (name_end == NULL || (!vim_iswhite(*name_end)
3692 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 if (name_end != NULL)
3695 {
3696 emsg_severe = TRUE;
3697 EMSG(_(e_trailing));
3698 }
3699 if (!(eap->skip || error))
3700 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 break;
3702 }
3703
3704 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 {
3706 if (eap->cmdidx == CMD_unlet)
3707 {
3708 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3709 error = TRUE;
3710 }
3711 else
3712 {
3713 if (do_lock_var(&lv, name_end, deep,
3714 eap->cmdidx == CMD_lockvar) == FAIL)
3715 error = TRUE;
3716 }
3717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 if (!eap->skip)
3720 clear_lval(&lv);
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 arg = skipwhite(name_end);
3723 } while (!ends_excmd(*arg));
3724
3725 eap->nextcmd = check_nextcmd(arg);
3726}
3727
Bram Moolenaar8c711452005-01-14 21:53:12 +00003728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet_var(
3730 lval_T *lp,
3731 char_u *name_end,
3732 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003733{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003734 int ret = OK;
3735 int cc;
3736
3737 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 cc = *name_end;
3740 *name_end = NUL;
3741
3742 /* Normal name or expanded name. */
3743 if (check_changedtick(lp->ll_name))
3744 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003746 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003748 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003749 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003750 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003751 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003752 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003753 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003754 else if (lp->ll_range)
3755 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003757 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003758 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003759
3760 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3761 {
3762 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003764 return FAIL;
3765 ll_li = li;
3766 ++ll_n1;
3767 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768
3769 /* Delete a range of List items. */
3770 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3771 {
3772 li = lp->ll_li->li_next;
3773 listitem_remove(lp->ll_list, lp->ll_li);
3774 lp->ll_li = li;
3775 ++lp->ll_n1;
3776 }
3777 }
3778 else
3779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003781 /* unlet a List item. */
3782 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003783 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003784 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003785 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003786 }
3787
3788 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003789}
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791/*
3792 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003796do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaar33570922005-01-25 22:26:29 +00003798 hashtab_T *ht;
3799 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003800 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003801 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003802 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 ht = find_var_ht(name, &varname);
3805 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003807 if (ht == &globvarht)
3808 d = &globvardict;
3809 else if (current_funccal != NULL
3810 && ht == &current_funccal->l_vars.dv_hashtab)
3811 d = &current_funccal->l_vars;
3812 else if (ht == &compat_hashtab)
3813 d = &vimvardict;
3814 else
3815 {
3816 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3817 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3818 }
3819 if (d == NULL)
3820 {
3821 EMSG2(_(e_intern2), "do_unlet()");
3822 return FAIL;
3823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 hi = hash_find(ht, varname);
3825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003826 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003827 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003828 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003829 || var_check_ro(di->di_flags, name, FALSE)
3830 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003831 return FAIL;
3832
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 delete_var(ht, hi);
3834 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 if (forceit)
3838 return OK;
3839 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return FAIL;
3841}
3842
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843/*
3844 * Lock or unlock variable indicated by "lp".
3845 * "deep" is the levels to go (-1 for unlimited);
3846 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3847 */
3848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849do_lock_var(
3850 lval_T *lp,
3851 char_u *name_end,
3852 int deep,
3853 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854{
3855 int ret = OK;
3856 int cc;
3857 dictitem_T *di;
3858
3859 if (deep == 0) /* nothing to do */
3860 return OK;
3861
3862 if (lp->ll_tv == NULL)
3863 {
3864 cc = *name_end;
3865 *name_end = NUL;
3866
3867 /* Normal name or expanded name. */
3868 if (check_changedtick(lp->ll_name))
3869 ret = FAIL;
3870 else
3871 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003872 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 if (di == NULL)
3874 ret = FAIL;
3875 else
3876 {
3877 if (lock)
3878 di->di_flags |= DI_FLAGS_LOCK;
3879 else
3880 di->di_flags &= ~DI_FLAGS_LOCK;
3881 item_lock(&di->di_tv, deep, lock);
3882 }
3883 }
3884 *name_end = cc;
3885 }
3886 else if (lp->ll_range)
3887 {
3888 listitem_T *li = lp->ll_li;
3889
3890 /* (un)lock a range of List items. */
3891 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3892 {
3893 item_lock(&li->li_tv, deep, lock);
3894 li = li->li_next;
3895 ++lp->ll_n1;
3896 }
3897 }
3898 else if (lp->ll_list != NULL)
3899 /* (un)lock a List item. */
3900 item_lock(&lp->ll_li->li_tv, deep, lock);
3901 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003902 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003903 item_lock(&lp->ll_di->di_tv, deep, lock);
3904
3905 return ret;
3906}
3907
3908/*
3909 * Lock or unlock an item. "deep" is nr of levels to go.
3910 */
3911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003912item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913{
3914 static int recurse = 0;
3915 list_T *l;
3916 listitem_T *li;
3917 dict_T *d;
3918 hashitem_T *hi;
3919 int todo;
3920
3921 if (recurse >= DICT_MAXNEST)
3922 {
3923 EMSG(_("E743: variable nested too deep for (un)lock"));
3924 return;
3925 }
3926 if (deep == 0)
3927 return;
3928 ++recurse;
3929
3930 /* lock/unlock the item itself */
3931 if (lock)
3932 tv->v_lock |= VAR_LOCKED;
3933 else
3934 tv->v_lock &= ~VAR_LOCKED;
3935
3936 switch (tv->v_type)
3937 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003938 case VAR_UNKNOWN:
3939 case VAR_NUMBER:
3940 case VAR_STRING:
3941 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003942 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003943 case VAR_FLOAT:
3944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003946 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003947 break;
3948
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003949 case VAR_LIST:
3950 if ((l = tv->vval.v_list) != NULL)
3951 {
3952 if (lock)
3953 l->lv_lock |= VAR_LOCKED;
3954 else
3955 l->lv_lock &= ~VAR_LOCKED;
3956 if (deep < 0 || deep > 1)
3957 /* recursive: lock/unlock the items the List contains */
3958 for (li = l->lv_first; li != NULL; li = li->li_next)
3959 item_lock(&li->li_tv, deep - 1, lock);
3960 }
3961 break;
3962 case VAR_DICT:
3963 if ((d = tv->vval.v_dict) != NULL)
3964 {
3965 if (lock)
3966 d->dv_lock |= VAR_LOCKED;
3967 else
3968 d->dv_lock &= ~VAR_LOCKED;
3969 if (deep < 0 || deep > 1)
3970 {
3971 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003972 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003973 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3974 {
3975 if (!HASHITEM_EMPTY(hi))
3976 {
3977 --todo;
3978 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3979 }
3980 }
3981 }
3982 }
3983 }
3984 --recurse;
3985}
3986
Bram Moolenaara40058a2005-07-11 22:42:07 +00003987/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003988 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3989 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003990 */
3991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003993{
3994 return (tv->v_lock & VAR_LOCKED)
3995 || (tv->v_type == VAR_LIST
3996 && tv->vval.v_list != NULL
3997 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3998 || (tv->v_type == VAR_DICT
3999 && tv->vval.v_dict != NULL
4000 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
4001}
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4004/*
4005 * Delete all "menutrans_" variables.
4006 */
4007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004008del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004014 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 {
4017 if (!HASHITEM_EMPTY(hi))
4018 {
4019 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4021 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 }
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025}
4026#endif
4027
4028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4029
4030/*
4031 * Local string buffer for the next two functions to store a variable name
4032 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4033 * get_user_var_name().
4034 */
4035
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004036static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038static char_u *varnamebuf = NULL;
4039static int varnamebuflen = 0;
4040
4041/*
4042 * Function to concatenate a prefix and a variable name.
4043 */
4044 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004045cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 int len;
4048
4049 len = (int)STRLEN(name) + 3;
4050 if (len > varnamebuflen)
4051 {
4052 vim_free(varnamebuf);
4053 len += 10; /* some additional space */
4054 varnamebuf = alloc(len);
4055 if (varnamebuf == NULL)
4056 {
4057 varnamebuflen = 0;
4058 return NULL;
4059 }
4060 varnamebuflen = len;
4061 }
4062 *varnamebuf = prefix;
4063 varnamebuf[1] = ':';
4064 STRCPY(varnamebuf + 2, name);
4065 return varnamebuf;
4066}
4067
4068/*
4069 * Function given to ExpandGeneric() to obtain the list of user defined
4070 * (global/buffer/window/built-in) variable names.
4071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004073get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004075 static long_u gdone;
4076 static long_u bdone;
4077 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078#ifdef FEAT_WINDOWS
4079 static long_u tdone;
4080#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004081 static int vidx;
4082 static hashitem_T *hi;
4083 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004086 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004087 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088#ifdef FEAT_WINDOWS
4089 tdone = 0;
4090#endif
4091 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004092
4093 /* Global variables */
4094 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004098 else
4099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004100 while (HASHITEM_EMPTY(hi))
4101 ++hi;
4102 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4103 return cat_prefix_varname('g', hi->hi_key);
4104 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004106
4107 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004108 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004111 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004113 else
4114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004115 while (HASHITEM_EMPTY(hi))
4116 ++hi;
4117 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004121 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return (char_u *)"b:changedtick";
4123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004124
4125 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004126 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004127 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004129 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004131 else
4132 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004133 while (HASHITEM_EMPTY(hi))
4134 ++hi;
4135 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004137
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004138#ifdef FEAT_WINDOWS
4139 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004140 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 if (tdone < ht->ht_used)
4142 {
4143 if (tdone++ == 0)
4144 hi = ht->ht_array;
4145 else
4146 ++hi;
4147 while (HASHITEM_EMPTY(hi))
4148 ++hi;
4149 return cat_prefix_varname('t', hi->hi_key);
4150 }
4151#endif
4152
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 /* v: variables */
4154 if (vidx < VV_LEN)
4155 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 vim_free(varnamebuf);
4158 varnamebuf = NULL;
4159 varnamebuflen = 0;
4160 return NULL;
4161}
4162
4163#endif /* FEAT_CMDL_COMPL */
4164
4165/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004166 * Return TRUE if "pat" matches "text".
4167 * Does not use 'cpo' and always uses 'magic'.
4168 */
4169 static int
4170pattern_match(char_u *pat, char_u *text, int ic)
4171{
4172 int matches = FALSE;
4173 char_u *save_cpo;
4174 regmatch_T regmatch;
4175
4176 /* avoid 'l' flag in 'cpoptions' */
4177 save_cpo = p_cpo;
4178 p_cpo = (char_u *)"";
4179 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4180 if (regmatch.regprog != NULL)
4181 {
4182 regmatch.rm_ic = ic;
4183 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4184 vim_regfree(regmatch.regprog);
4185 }
4186 p_cpo = save_cpo;
4187 return matches;
4188}
4189
4190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * types for expressions.
4192 */
4193typedef enum
4194{
4195 TYPE_UNKNOWN = 0
4196 , TYPE_EQUAL /* == */
4197 , TYPE_NEQUAL /* != */
4198 , TYPE_GREATER /* > */
4199 , TYPE_GEQUAL /* >= */
4200 , TYPE_SMALLER /* < */
4201 , TYPE_SEQUAL /* <= */
4202 , TYPE_MATCH /* =~ */
4203 , TYPE_NOMATCH /* !~ */
4204} exptype_T;
4205
4206/*
4207 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4210 */
4211
4212/*
4213 * Handle zero level expression.
4214 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004216 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004220eval0(
4221 char_u *arg,
4222 typval_T *rettv,
4223 char_u **nextcmd,
4224 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int ret;
4227 char_u *p;
4228
4229 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (ret == FAIL || !ends_excmd(*p))
4232 {
4233 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 /*
4236 * Report the invalid expression unless the expression evaluation has
4237 * been cancelled due to an aborting error, an interrupt, or an
4238 * exception.
4239 */
4240 if (!aborting())
4241 EMSG2(_(e_invexpr2), arg);
4242 ret = FAIL;
4243 }
4244 if (nextcmd != NULL)
4245 *nextcmd = check_nextcmd(p);
4246
4247 return ret;
4248}
4249
4250/*
4251 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004252 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 *
4254 * "arg" must point to the first non-white of the expression.
4255 * "arg" is advanced to the next non-white after the recognized expression.
4256 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004257 * Note: "rettv.v_lock" is not set.
4258 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 * Return OK or FAIL.
4260 */
4261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004262eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 /*
4268 * Get the first variable.
4269 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 return FAIL;
4272
4273 if ((*arg)[0] == '?')
4274 {
4275 result = FALSE;
4276 if (evaluate)
4277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 int error = FALSE;
4279
4280 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286
4287 /*
4288 * Get the second variable.
4289 */
4290 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return FAIL;
4293
4294 /*
4295 * Check for the ":".
4296 */
4297 if ((*arg)[0] != ':')
4298 {
4299 EMSG(_("E109: Missing ':' after '?'"));
4300 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return FAIL;
4303 }
4304
4305 /*
4306 * Get the third variable.
4307 */
4308 *arg = skipwhite(*arg + 1);
4309 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4310 {
4311 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318
4319 return OK;
4320}
4321
4322/*
4323 * Handle first level expression:
4324 * expr2 || expr2 || expr2 logical OR
4325 *
4326 * "arg" must point to the first non-white of the expression.
4327 * "arg" is advanced to the next non-white after the recognized expression.
4328 *
4329 * Return OK or FAIL.
4330 */
4331 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004332eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaar33570922005-01-25 22:26:29 +00004334 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 long result;
4336 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 /*
4340 * Get the first variable.
4341 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return FAIL;
4344
4345 /*
4346 * Repeat until there is no following "||".
4347 */
4348 first = TRUE;
4349 result = FALSE;
4350 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4351 {
4352 if (evaluate && first)
4353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (error)
4358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 first = FALSE;
4360 }
4361
4362 /*
4363 * Get the second variable.
4364 */
4365 *arg = skipwhite(*arg + 2);
4366 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4367 return FAIL;
4368
4369 /*
4370 * Compute the result.
4371 */
4372 if (evaluate && !result)
4373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 if (error)
4378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 if (evaluate)
4381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 rettv->v_type = VAR_NUMBER;
4383 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 }
4386
4387 return OK;
4388}
4389
4390/*
4391 * Handle second level expression:
4392 * expr3 && expr3 && expr3 logical AND
4393 *
4394 * "arg" must point to the first non-white of the expression.
4395 * "arg" is advanced to the next non-white after the recognized expression.
4396 *
4397 * Return OK or FAIL.
4398 */
4399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004400eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
Bram Moolenaar33570922005-01-25 22:26:29 +00004402 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 long result;
4404 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004405 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407 /*
4408 * Get the first variable.
4409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FAIL;
4412
4413 /*
4414 * Repeat until there is no following "&&".
4415 */
4416 first = TRUE;
4417 result = TRUE;
4418 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4419 {
4420 if (evaluate && first)
4421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 if (error)
4426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 first = FALSE;
4428 }
4429
4430 /*
4431 * Get the second variable.
4432 */
4433 *arg = skipwhite(*arg + 2);
4434 if (eval4(arg, &var2, evaluate && result) == FAIL)
4435 return FAIL;
4436
4437 /*
4438 * Compute the result.
4439 */
4440 if (evaluate && result)
4441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 if (error)
4446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 if (evaluate)
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 rettv->v_type = VAR_NUMBER;
4451 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 }
4454
4455 return OK;
4456}
4457
4458/*
4459 * Handle third level expression:
4460 * var1 == var2
4461 * var1 =~ var2
4462 * var1 != var2
4463 * var1 !~ var2
4464 * var1 > var2
4465 * var1 >= var2
4466 * var1 < var2
4467 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 * var1 is var2
4469 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 *
4471 * "arg" must point to the first non-white of the expression.
4472 * "arg" is advanced to the next non-white after the recognized expression.
4473 *
4474 * Return OK or FAIL.
4475 */
4476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
Bram Moolenaar33570922005-01-25 22:26:29 +00004479 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 char_u *p;
4481 int i;
4482 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004485 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u *s1, *s2;
4487 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 /*
4491 * Get the first variable.
4492 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return FAIL;
4495
4496 p = *arg;
4497 switch (p[0])
4498 {
4499 case '=': if (p[1] == '=')
4500 type = TYPE_EQUAL;
4501 else if (p[1] == '~')
4502 type = TYPE_MATCH;
4503 break;
4504 case '!': if (p[1] == '=')
4505 type = TYPE_NEQUAL;
4506 else if (p[1] == '~')
4507 type = TYPE_NOMATCH;
4508 break;
4509 case '>': if (p[1] != '=')
4510 {
4511 type = TYPE_GREATER;
4512 len = 1;
4513 }
4514 else
4515 type = TYPE_GEQUAL;
4516 break;
4517 case '<': if (p[1] != '=')
4518 {
4519 type = TYPE_SMALLER;
4520 len = 1;
4521 }
4522 else
4523 type = TYPE_SEQUAL;
4524 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 case 'i': if (p[1] == 's')
4526 {
4527 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4528 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004529 i = p[len];
4530 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004531 {
4532 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4533 type_is = TRUE;
4534 }
4535 }
4536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
4539 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
4542 if (type != TYPE_UNKNOWN)
4543 {
4544 /* extra question mark appended: ignore case */
4545 if (p[len] == '?')
4546 {
4547 ic = TRUE;
4548 ++len;
4549 }
4550 /* extra '#' appended: match case */
4551 else if (p[len] == '#')
4552 {
4553 ic = FALSE;
4554 ++len;
4555 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
4558 ic = p_ic;
4559
4560 /*
4561 * Get the second variable.
4562 */
4563 *arg = skipwhite(p + len);
4564 if (eval5(arg, &var2, evaluate) == FAIL)
4565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return FAIL;
4568 }
4569
4570 if (evaluate)
4571 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004572 if (type_is && rettv->v_type != var2.v_type)
4573 {
4574 /* For "is" a different type always means FALSE, for "notis"
4575 * it means TRUE. */
4576 n1 = (type == TYPE_NEQUAL);
4577 }
4578 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4579 {
4580 if (type_is)
4581 {
4582 n1 = (rettv->v_type == var2.v_type
4583 && rettv->vval.v_list == var2.vval.v_list);
4584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 else if (rettv->v_type != var2.v_type
4588 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4589 {
4590 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004591 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004592 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004593 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 clear_tv(rettv);
4595 clear_tv(&var2);
4596 return FAIL;
4597 }
4598 else
4599 {
4600 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004601 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4602 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004603 if (type == TYPE_NEQUAL)
4604 n1 = !n1;
4605 }
4606 }
4607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004608 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4609 {
4610 if (type_is)
4611 {
4612 n1 = (rettv->v_type == var2.v_type
4613 && rettv->vval.v_dict == var2.vval.v_dict);
4614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 else if (rettv->v_type != var2.v_type
4618 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4619 {
4620 if (rettv->v_type != var2.v_type)
4621 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4622 else
4623 EMSG(_("E736: Invalid operation for Dictionary"));
4624 clear_tv(rettv);
4625 clear_tv(&var2);
4626 return FAIL;
4627 }
4628 else
4629 {
4630 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004631 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4632 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004633 if (type == TYPE_NEQUAL)
4634 n1 = !n1;
4635 }
4636 }
4637
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004638 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4639 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004640 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004641 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004642 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004643 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004644 clear_tv(rettv);
4645 clear_tv(&var2);
4646 return FAIL;
4647 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004648 if ((rettv->v_type == VAR_PARTIAL
4649 && rettv->vval.v_partial == NULL)
4650 || (var2.v_type == VAR_PARTIAL
4651 && var2.vval.v_partial == NULL))
4652 /* when a partial is NULL assume not equal */
4653 n1 = FALSE;
4654 else if (type_is)
4655 {
4656 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4657 /* strings are considered the same if their value is
4658 * the same */
4659 n1 = tv_equal(rettv, &var2, ic, FALSE);
4660 else if (rettv->v_type == VAR_PARTIAL
4661 && var2.v_type == VAR_PARTIAL)
4662 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4663 else
4664 n1 = FALSE;
4665 }
4666 else
4667 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004668 if (type == TYPE_NEQUAL)
4669 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004670 }
4671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004672#ifdef FEAT_FLOAT
4673 /*
4674 * If one of the two variables is a float, compare as a float.
4675 * When using "=~" or "!~", always compare as string.
4676 */
4677 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4678 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4679 {
4680 float_T f1, f2;
4681
4682 if (rettv->v_type == VAR_FLOAT)
4683 f1 = rettv->vval.v_float;
4684 else
4685 f1 = get_tv_number(rettv);
4686 if (var2.v_type == VAR_FLOAT)
4687 f2 = var2.vval.v_float;
4688 else
4689 f2 = get_tv_number(&var2);
4690 n1 = FALSE;
4691 switch (type)
4692 {
4693 case TYPE_EQUAL: n1 = (f1 == f2); break;
4694 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4695 case TYPE_GREATER: n1 = (f1 > f2); break;
4696 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4697 case TYPE_SMALLER: n1 = (f1 < f2); break;
4698 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4699 case TYPE_UNKNOWN:
4700 case TYPE_MATCH:
4701 case TYPE_NOMATCH: break; /* avoid gcc warning */
4702 }
4703 }
4704#endif
4705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 /*
4707 * If one of the two variables is a number, compare as a number.
4708 * When using "=~" or "!~", always compare as string.
4709 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004710 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 n1 = get_tv_number(rettv);
4714 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 switch (type)
4716 {
4717 case TYPE_EQUAL: n1 = (n1 == n2); break;
4718 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4719 case TYPE_GREATER: n1 = (n1 > n2); break;
4720 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4721 case TYPE_SMALLER: n1 = (n1 < n2); break;
4722 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4723 case TYPE_UNKNOWN:
4724 case TYPE_MATCH:
4725 case TYPE_NOMATCH: break; /* avoid gcc warning */
4726 }
4727 }
4728 else
4729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 s1 = get_tv_string_buf(rettv, buf1);
4731 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4733 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4734 else
4735 i = 0;
4736 n1 = FALSE;
4737 switch (type)
4738 {
4739 case TYPE_EQUAL: n1 = (i == 0); break;
4740 case TYPE_NEQUAL: n1 = (i != 0); break;
4741 case TYPE_GREATER: n1 = (i > 0); break;
4742 case TYPE_GEQUAL: n1 = (i >= 0); break;
4743 case TYPE_SMALLER: n1 = (i < 0); break;
4744 case TYPE_SEQUAL: n1 = (i <= 0); break;
4745
4746 case TYPE_MATCH:
4747 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004748 n1 = pattern_match(s2, s1, ic);
4749 if (type == TYPE_NOMATCH)
4750 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752
4753 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4754 }
4755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 rettv->v_type = VAR_NUMBER;
4759 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762
4763 return OK;
4764}
4765
4766/*
4767 * Handle fourth level expression:
4768 * + number addition
4769 * - number subtraction
4770 * . string concatenation
4771 *
4772 * "arg" must point to the first non-white of the expression.
4773 * "arg" is advanced to the next non-white after the recognized expression.
4774 *
4775 * Return OK or FAIL.
4776 */
4777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004778eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 typval_T var2;
4781 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004783 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784#ifdef FEAT_FLOAT
4785 float_T f1 = 0, f2 = 0;
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 char_u *s1, *s2;
4788 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4789 char_u *p;
4790
4791 /*
4792 * Get the first variable.
4793 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796
4797 /*
4798 * Repeat computing, until no '+', '-' or '.' is following.
4799 */
4800 for (;;)
4801 {
4802 op = **arg;
4803 if (op != '+' && op != '-' && op != '.')
4804 break;
4805
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806 if ((op != '+' || rettv->v_type != VAR_LIST)
4807#ifdef FEAT_FLOAT
4808 && (op == '.' || rettv->v_type != VAR_FLOAT)
4809#endif
4810 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 {
4812 /* For "list + ...", an illegal use of the first operand as
4813 * a number cannot be determined before evaluating the 2nd
4814 * operand: if this is also a list, all is ok.
4815 * For "something . ...", "something - ..." or "non-list + ...",
4816 * we know that the first operand needs to be a string or number
4817 * without evaluating the 2nd operand. So check before to avoid
4818 * side effects after an error. */
4819 if (evaluate && get_tv_string_chk(rettv) == NULL)
4820 {
4821 clear_tv(rettv);
4822 return FAIL;
4823 }
4824 }
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 /*
4827 * Get the second variable.
4828 */
4829 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004830 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
4834 }
4835
4836 if (evaluate)
4837 {
4838 /*
4839 * Compute the result.
4840 */
4841 if (op == '.')
4842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4844 s2 = get_tv_string_buf_chk(&var2, buf2);
4845 if (s2 == NULL) /* type error ? */
4846 {
4847 clear_tv(rettv);
4848 clear_tv(&var2);
4849 return FAIL;
4850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004851 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004856 else if (op == '+' && rettv->v_type == VAR_LIST
4857 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004858 {
4859 /* concatenate Lists */
4860 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4861 &var3) == FAIL)
4862 {
4863 clear_tv(rettv);
4864 clear_tv(&var2);
4865 return FAIL;
4866 }
4867 clear_tv(rettv);
4868 *rettv = var3;
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 else
4871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 int error = FALSE;
4873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 f1 = rettv->vval.v_float;
4878 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 else
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 n1 = get_tv_number_chk(rettv, &error);
4884 if (error)
4885 {
4886 /* This can only happen for "list + non-list". For
4887 * "non-list + ..." or "something - ...", we returned
4888 * before evaluating the 2nd operand. */
4889 clear_tv(rettv);
4890 return FAIL;
4891 }
4892#ifdef FEAT_FLOAT
4893 if (var2.v_type == VAR_FLOAT)
4894 f1 = n1;
4895#endif
4896 }
4897#ifdef FEAT_FLOAT
4898 if (var2.v_type == VAR_FLOAT)
4899 {
4900 f2 = var2.vval.v_float;
4901 n2 = 0;
4902 }
4903 else
4904#endif
4905 {
4906 n2 = get_tv_number_chk(&var2, &error);
4907 if (error)
4908 {
4909 clear_tv(rettv);
4910 clear_tv(&var2);
4911 return FAIL;
4912 }
4913#ifdef FEAT_FLOAT
4914 if (rettv->v_type == VAR_FLOAT)
4915 f2 = n2;
4916#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919
4920#ifdef FEAT_FLOAT
4921 /* If there is a float on either side the result is a float. */
4922 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4923 {
4924 if (op == '+')
4925 f1 = f1 + f2;
4926 else
4927 f1 = f1 - f2;
4928 rettv->v_type = VAR_FLOAT;
4929 rettv->vval.v_float = f1;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932#endif
4933 {
4934 if (op == '+')
4935 n1 = n1 + n2;
4936 else
4937 n1 = n1 - n2;
4938 rettv->v_type = VAR_NUMBER;
4939 rettv->vval.v_number = n1;
4940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 }
4945 return OK;
4946}
4947
4948/*
4949 * Handle fifth level expression:
4950 * * number multiplication
4951 * / number division
4952 * % number modulo
4953 *
4954 * "arg" must point to the first non-white of the expression.
4955 * "arg" is advanced to the next non-white after the recognized expression.
4956 *
4957 * Return OK or FAIL.
4958 */
4959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960eval6(
4961 char_u **arg,
4962 typval_T *rettv,
4963 int evaluate,
4964 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar33570922005-01-25 22:26:29 +00004966 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004968 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969#ifdef FEAT_FLOAT
4970 int use_float = FALSE;
4971 float_T f1 = 0, f2;
4972#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 /*
4976 * Get the first variable.
4977 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004978 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 return FAIL;
4980
4981 /*
4982 * Repeat computing, until no '*', '/' or '%' is following.
4983 */
4984 for (;;)
4985 {
4986 op = **arg;
4987 if (op != '*' && op != '/' && op != '%')
4988 break;
4989
4990 if (evaluate)
4991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#ifdef FEAT_FLOAT
4993 if (rettv->v_type == VAR_FLOAT)
4994 {
4995 f1 = rettv->vval.v_float;
4996 use_float = TRUE;
4997 n1 = 0;
4998 }
4999 else
5000#endif
5001 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 if (error)
5004 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 else
5007 n1 = 0;
5008
5009 /*
5010 * Get the second variable.
5011 */
5012 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005013 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 return FAIL;
5015
5016 if (evaluate)
5017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018#ifdef FEAT_FLOAT
5019 if (var2.v_type == VAR_FLOAT)
5020 {
5021 if (!use_float)
5022 {
5023 f1 = n1;
5024 use_float = TRUE;
5025 }
5026 f2 = var2.vval.v_float;
5027 n2 = 0;
5028 }
5029 else
5030#endif
5031 {
5032 n2 = get_tv_number_chk(&var2, &error);
5033 clear_tv(&var2);
5034 if (error)
5035 return FAIL;
5036#ifdef FEAT_FLOAT
5037 if (use_float)
5038 f2 = n2;
5039#endif
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 /*
5043 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046#ifdef FEAT_FLOAT
5047 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 if (op == '*')
5050 f1 = f1 * f2;
5051 else if (op == '/')
5052 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005053# ifdef VMS
5054 /* VMS crashes on divide by zero, work around it */
5055 if (f2 == 0.0)
5056 {
5057 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005058 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005059 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005060 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005061 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005062 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005063 }
5064 else
5065 f1 = f1 / f2;
5066# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 /* We rely on the floating point library to handle divide
5068 * by zero to result in "inf" and not a crash. */
5069 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005070# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005074 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 return FAIL;
5076 }
5077 rettv->v_type = VAR_FLOAT;
5078 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005083 if (op == '*')
5084 n1 = n1 * n2;
5085 else if (op == '/')
5086 {
5087 if (n2 == 0) /* give an error message? */
5088 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005089#ifdef FEAT_NUM64
5090 if (n1 == 0)
5091 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5092 else if (n1 < 0)
5093 n1 = -0x7fffffffffffffff;
5094 else
5095 n1 = 0x7fffffffffffffff;
5096#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 if (n1 == 0)
5098 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5099 else if (n1 < 0)
5100 n1 = -0x7fffffffL;
5101 else
5102 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005103#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 }
5105 else
5106 n1 = n1 / n2;
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 {
5110 if (n2 == 0) /* give an error message? */
5111 n1 = 0;
5112 else
5113 n1 = n1 % n2;
5114 }
5115 rettv->v_type = VAR_NUMBER;
5116 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120
5121 return OK;
5122}
5123
5124/*
5125 * Handle sixth level expression:
5126 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005127 * "string" string constant
5128 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 * &option-name option value
5130 * @r register contents
5131 * identifier variable value
5132 * function() function call
5133 * $VAR environment variable
5134 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005135 * [expr, expr] List
5136 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 *
5138 * Also handle:
5139 * ! in front logical NOT
5140 * - in front unary minus
5141 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 * trailing [] subscript in String or List
5143 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 *
5145 * "arg" must point to the first non-white of the expression.
5146 * "arg" is advanced to the next non-white after the recognized expression.
5147 *
5148 * Return OK or FAIL.
5149 */
5150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005151eval7(
5152 char_u **arg,
5153 typval_T *rettv,
5154 int evaluate,
5155 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005157 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 int len;
5159 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 char_u *start_leader, *end_leader;
5161 int ret = OK;
5162 char_u *alias;
5163
5164 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * Skip '!' and '-' characters. They are handled later.
5172 */
5173 start_leader = *arg;
5174 while (**arg == '!' || **arg == '-' || **arg == '+')
5175 *arg = skipwhite(*arg + 1);
5176 end_leader = *arg;
5177
5178 switch (**arg)
5179 {
5180 /*
5181 * Number constant.
5182 */
5183 case '0':
5184 case '1':
5185 case '2':
5186 case '3':
5187 case '4':
5188 case '5':
5189 case '6':
5190 case '7':
5191 case '8':
5192 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 {
5194#ifdef FEAT_FLOAT
5195 char_u *p = skipdigits(*arg + 1);
5196 int get_float = FALSE;
5197
5198 /* We accept a float when the format matches
5199 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005200 * strict to avoid backwards compatibility problems.
5201 * Don't look for a float after the "." operator, so that
5202 * ":let vers = 1.2.3" doesn't fail. */
5203 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005205 get_float = TRUE;
5206 p = skipdigits(p + 2);
5207 if (*p == 'e' || *p == 'E')
5208 {
5209 ++p;
5210 if (*p == '-' || *p == '+')
5211 ++p;
5212 if (!vim_isdigit(*p))
5213 get_float = FALSE;
5214 else
5215 p = skipdigits(p + 1);
5216 }
5217 if (ASCII_ISALPHA(*p) || *p == '.')
5218 get_float = FALSE;
5219 }
5220 if (get_float)
5221 {
5222 float_T f;
5223
5224 *arg += string2float(*arg, &f);
5225 if (evaluate)
5226 {
5227 rettv->v_type = VAR_FLOAT;
5228 rettv->vval.v_float = f;
5229 }
5230 }
5231 else
5232#endif
5233 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005234 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235 *arg += len;
5236 if (evaluate)
5237 {
5238 rettv->v_type = VAR_NUMBER;
5239 rettv->vval.v_number = n;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 /*
5246 * String constant: "string".
5247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005248 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250
5251 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 break;
5256
5257 /*
5258 * List: [expr, expr]
5259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 break;
5262
5263 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 * Dictionary: {key: val, key: val}
5265 */
5266 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5267 break;
5268
5269 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005270 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 break;
5274
5275 /*
5276 * Environment variable: $VAR.
5277 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 break;
5280
5281 /*
5282 * Register contents: @r.
5283 */
5284 case '@': ++*arg;
5285 if (evaluate)
5286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005288 rettv->vval.v_string = get_reg_contents(**arg,
5289 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 if (**arg != NUL)
5292 ++*arg;
5293 break;
5294
5295 /*
5296 * nested expression: (expression).
5297 */
5298 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (**arg == ')')
5301 ++*arg;
5302 else if (ret == OK)
5303 {
5304 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ret = FAIL;
5307 }
5308 break;
5309
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 break;
5312 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313
5314 if (ret == NOTDONE)
5315 {
5316 /*
5317 * Must be a variable or function name.
5318 * Can also be a curly-braces kind of name: {expr}.
5319 */
5320 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 if (alias != NULL)
5323 s = alias;
5324
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 ret = FAIL;
5327 else
5328 {
5329 if (**arg == '(') /* recursive! */
5330 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005331 partial_T *partial;
5332
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 /* If "s" is the name of a variable of type VAR_FUNC
5334 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005335 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336
5337 /* Invoke the function. */
5338 ret = get_func_tv(s, len, rettv, arg,
5339 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005340 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005341
5342 /* If evaluate is FALSE rettv->v_type was not set in
5343 * get_func_tv, but it's needed in handle_subscript() to parse
5344 * what follows. So set it here. */
5345 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5346 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005347 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005348 rettv->v_type = VAR_FUNC;
5349 }
5350
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 /* Stop the expression evaluation when immediately
5352 * aborting on error, or when an interrupt occurred or
5353 * an exception was thrown but not caught. */
5354 if (aborting())
5355 {
5356 if (ret == OK)
5357 clear_tv(rettv);
5358 ret = FAIL;
5359 }
5360 }
5361 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005362 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005363 else
5364 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005366 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 }
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 *arg = skipwhite(*arg);
5370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5372 * expr(expr). */
5373 if (ret == OK)
5374 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 /*
5377 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5378 */
5379 if (ret == OK && evaluate && end_leader > start_leader)
5380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005382 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005383#ifdef FEAT_FLOAT
5384 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005386 if (rettv->v_type == VAR_FLOAT)
5387 f = rettv->vval.v_float;
5388 else
5389#endif
5390 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005391 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005393 clear_tv(rettv);
5394 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 else
5397 {
5398 while (end_leader > start_leader)
5399 {
5400 --end_leader;
5401 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005402 {
5403#ifdef FEAT_FLOAT
5404 if (rettv->v_type == VAR_FLOAT)
5405 f = !f;
5406 else
5407#endif
5408 val = !val;
5409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005411 {
5412#ifdef FEAT_FLOAT
5413 if (rettv->v_type == VAR_FLOAT)
5414 f = -f;
5415 else
5416#endif
5417 val = -val;
5418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005420#ifdef FEAT_FLOAT
5421 if (rettv->v_type == VAR_FLOAT)
5422 {
5423 clear_tv(rettv);
5424 rettv->vval.v_float = f;
5425 }
5426 else
5427#endif
5428 {
5429 clear_tv(rettv);
5430 rettv->v_type = VAR_NUMBER;
5431 rettv->vval.v_number = val;
5432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435
5436 return ret;
5437}
5438
5439/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5441 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5443 */
5444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005445eval_index(
5446 char_u **arg,
5447 typval_T *rettv,
5448 int evaluate,
5449 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450{
5451 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 long len = -1;
5455 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458
Bram Moolenaara03f2332016-02-06 18:09:59 +01005459 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005461 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005462 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005463 if (verbose)
5464 EMSG(_("E695: Cannot index a Funcref"));
5465 return FAIL;
5466 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005467#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005468 if (verbose)
5469 EMSG(_(e_float_as_string));
5470 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005471#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005472 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005473 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005474 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005475 if (verbose)
5476 EMSG(_("E909: Cannot index a special variable"));
5477 return FAIL;
5478 case VAR_UNKNOWN:
5479 if (evaluate)
5480 return FAIL;
5481 /* FALLTHROUGH */
5482
5483 case VAR_STRING:
5484 case VAR_NUMBER:
5485 case VAR_LIST:
5486 case VAR_DICT:
5487 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005488 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005490 init_tv(&var1);
5491 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 /*
5495 * dict.name
5496 */
5497 key = *arg + 1;
5498 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5499 ;
5500 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 /*
5507 * something[idx]
5508 *
5509 * Get the (first) variable from inside the [].
5510 */
5511 *arg = skipwhite(*arg + 1);
5512 if (**arg == ':')
5513 empty1 = TRUE;
5514 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5515 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005516 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5517 {
5518 /* not a number or string */
5519 clear_tv(&var1);
5520 return FAIL;
5521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522
5523 /*
5524 * Get the second variable from inside the [:].
5525 */
5526 if (**arg == ':')
5527 {
5528 range = TRUE;
5529 *arg = skipwhite(*arg + 1);
5530 if (**arg == ']')
5531 empty2 = TRUE;
5532 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005534 if (!empty1)
5535 clear_tv(&var1);
5536 return FAIL;
5537 }
5538 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5539 {
5540 /* not a number or string */
5541 if (!empty1)
5542 clear_tv(&var1);
5543 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 return FAIL;
5545 }
5546 }
5547
5548 /* Check for the ']'. */
5549 if (**arg != ']')
5550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005551 if (verbose)
5552 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 clear_tv(&var1);
5554 if (range)
5555 clear_tv(&var2);
5556 return FAIL;
5557 }
5558 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 }
5560
5561 if (evaluate)
5562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 n1 = 0;
5564 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 n1 = get_tv_number(&var1);
5567 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 }
5569 if (range)
5570 {
5571 if (empty2)
5572 n2 = -1;
5573 else
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 n2 = get_tv_number(&var2);
5576 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 }
5578 }
5579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005583 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005584 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005586 case VAR_SPECIAL:
5587 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005588 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005589 break; /* not evaluating, skipping over subscript */
5590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 case VAR_NUMBER:
5592 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 len = (long)STRLEN(s);
5595 if (range)
5596 {
5597 /* The resulting variable is a substring. If the indexes
5598 * are out of range the result is empty. */
5599 if (n1 < 0)
5600 {
5601 n1 = len + n1;
5602 if (n1 < 0)
5603 n1 = 0;
5604 }
5605 if (n2 < 0)
5606 n2 = len + n2;
5607 else if (n2 >= len)
5608 n2 = len;
5609 if (n1 >= len || n2 < 0 || n1 > n2)
5610 s = NULL;
5611 else
5612 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5613 }
5614 else
5615 {
5616 /* The resulting variable is a string of a single
5617 * character. If the index is too big or negative the
5618 * result is empty. */
5619 if (n1 >= len || n1 < 0)
5620 s = NULL;
5621 else
5622 s = vim_strnsave(s + n1, 1);
5623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 clear_tv(rettv);
5625 rettv->v_type = VAR_STRING;
5626 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 break;
5628
5629 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 if (n1 < 0)
5632 n1 = len + n1;
5633 if (!empty1 && (n1 < 0 || n1 >= len))
5634 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005635 /* For a range we allow invalid values and return an empty
5636 * list. A list index out of range is an error. */
5637 if (!range)
5638 {
5639 if (verbose)
5640 EMSGN(_(e_listidx), n1);
5641 return FAIL;
5642 }
5643 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 }
5645 if (range)
5646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *l;
5648 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649
5650 if (n2 < 0)
5651 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005652 else if (n2 >= len)
5653 n2 = len - 1;
5654 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005655 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 l = list_alloc();
5657 if (l == NULL)
5658 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 n1 <= n2; ++n1)
5661 {
5662 if (list_append_tv(l, &item->li_tv) == FAIL)
5663 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005664 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return FAIL;
5666 }
5667 item = item->li_next;
5668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 clear_tv(rettv);
5670 rettv->v_type = VAR_LIST;
5671 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005672 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 }
5674 else
5675 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005676 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 clear_tv(rettv);
5678 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
5680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681
5682 case VAR_DICT:
5683 if (range)
5684 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 if (verbose)
5686 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 if (len == -1)
5688 clear_tv(&var1);
5689 return FAIL;
5690 }
5691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693
5694 if (len == -1)
5695 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005696 key = get_tv_string_chk(&var1);
5697 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 clear_tv(&var1);
5700 return FAIL;
5701 }
5702 }
5703
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005704 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005707 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 if (len == -1)
5709 clear_tv(&var1);
5710 if (item == NULL)
5711 return FAIL;
5712
5713 copy_tv(&item->di_tv, &var1);
5714 clear_tv(rettv);
5715 *rettv = var1;
5716 }
5717 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 }
5719 }
5720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 return OK;
5722}
5723
5724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Get an option value.
5726 * "arg" points to the '&' or '+' before the option name.
5727 * "arg" is advanced to character after the option name.
5728 * Return OK or FAIL.
5729 */
5730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005731get_option_tv(
5732 char_u **arg,
5733 typval_T *rettv, /* when NULL, only check if option exists */
5734 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 char_u *option_end;
5737 long numval;
5738 char_u *stringval;
5739 int opt_type;
5740 int c;
5741 int working = (**arg == '+'); /* has("+option") */
5742 int ret = OK;
5743 int opt_flags;
5744
5745 /*
5746 * Isolate the option name and find its value.
5747 */
5748 option_end = find_option_end(arg, &opt_flags);
5749 if (option_end == NULL)
5750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005751 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 EMSG2(_("E112: Option name missing: %s"), *arg);
5753 return FAIL;
5754 }
5755
5756 if (!evaluate)
5757 {
5758 *arg = option_end;
5759 return OK;
5760 }
5761
5762 c = *option_end;
5763 *option_end = NUL;
5764 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 if (opt_type == -3) /* invalid name */
5768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 EMSG2(_("E113: Unknown option: %s"), *arg);
5771 ret = FAIL;
5772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (opt_type == -2) /* hidden string option */
5776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 rettv->v_type = VAR_STRING;
5778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 else if (opt_type == -1) /* hidden number option */
5781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005782 rettv->v_type = VAR_NUMBER;
5783 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785 else if (opt_type == 1) /* number option */
5786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005787 rettv->v_type = VAR_NUMBER;
5788 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 else /* string option */
5791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005792 rettv->v_type = VAR_STRING;
5793 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
5795 }
5796 else if (working && (opt_type == -2 || opt_type == -1))
5797 ret = FAIL;
5798
5799 *option_end = c; /* put back for error messages */
5800 *arg = option_end;
5801
5802 return ret;
5803}
5804
5805/*
5806 * Allocate a variable for a string constant.
5807 * Return OK or FAIL.
5808 */
5809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 char_u *p;
5813 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 int extra = 0;
5815
5816 /*
5817 * Find the end of the string, skipping backslashed characters.
5818 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005819 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
5821 if (*p == '\\' && p[1] != NUL)
5822 {
5823 ++p;
5824 /* A "\<x>" form occupies at least 4 characters, and produces up
5825 * to 6 characters: reserve space for 2 extra */
5826 if (*p == '<')
5827 extra += 2;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830
5831 if (*p != '"')
5832 {
5833 EMSG2(_("E114: Missing quote: %s"), *arg);
5834 return FAIL;
5835 }
5836
5837 /* If only parsing, set *arg and return here */
5838 if (!evaluate)
5839 {
5840 *arg = p + 1;
5841 return OK;
5842 }
5843
5844 /*
5845 * Copy the string into allocated memory, handling backslashed
5846 * characters.
5847 */
5848 name = alloc((unsigned)(p - *arg + extra));
5849 if (name == NULL)
5850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 rettv->v_type = VAR_STRING;
5852 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
5856 if (*p == '\\')
5857 {
5858 switch (*++p)
5859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 case 'b': *name++ = BS; ++p; break;
5861 case 'e': *name++ = ESC; ++p; break;
5862 case 'f': *name++ = FF; ++p; break;
5863 case 'n': *name++ = NL; ++p; break;
5864 case 'r': *name++ = CAR; ++p; break;
5865 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 case 'X': /* hex: "\x1", "\x12" */
5868 case 'x':
5869 case 'u': /* Unicode: "\u0023" */
5870 case 'U':
5871 if (vim_isxdigit(p[1]))
5872 {
5873 int n, nr;
5874 int c = toupper(*p);
5875
5876 if (c == 'X')
5877 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005878 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005880 else
5881 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 nr = 0;
5883 while (--n >= 0 && vim_isxdigit(p[1]))
5884 {
5885 ++p;
5886 nr = (nr << 4) + hex2nr(*p);
5887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#ifdef FEAT_MBYTE
5890 /* For "\u" store the number according to
5891 * 'encoding'. */
5892 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 else
5895#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 break;
5899
5900 /* octal: "\1", "\12", "\123" */
5901 case '0':
5902 case '1':
5903 case '2':
5904 case '3':
5905 case '4':
5906 case '5':
5907 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 case '7': *name = *p++ - '0';
5909 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 *name = (*name << 3) + *p++ - '0';
5912 if (*p >= '0' && *p <= '7')
5913 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 break;
5917
5918 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (extra != 0)
5921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 break;
5924 }
5925 /* FALLTHROUGH */
5926
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 break;
5929 }
5930 }
5931 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 *arg = p + 1;
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 return OK;
5939}
5940
5941/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 * Return OK or FAIL.
5944 */
5945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005946get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
5948 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 int reduce = 0;
5951
5952 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 */
5955 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960 break;
5961 ++reduce;
5962 ++p;
5963 }
5964 }
5965
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 return FAIL;
5970 }
5971
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 if (!evaluate)
5974 {
5975 *arg = p + 1;
5976 return OK;
5977 }
5978
5979 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005981 */
5982 str = alloc((unsigned)((p - *arg) - reduce));
5983 if (str == NULL)
5984 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985 rettv->v_type = VAR_STRING;
5986 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005990 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 break;
5994 ++p;
5995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005998 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005999 *arg = p + 1;
6000
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 return OK;
6002}
6003
Bram Moolenaarddecc252016-04-06 22:59:37 +02006004 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006005partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006006{
6007 int i;
6008
6009 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006010 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006011 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006012 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006013 func_unref(pt->pt_name);
6014 vim_free(pt->pt_name);
6015 vim_free(pt);
6016}
6017
6018/*
6019 * Unreference a closure: decrement the reference count and free it when it
6020 * becomes zero.
6021 */
6022 void
6023partial_unref(partial_T *pt)
6024{
6025 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006026 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006027}
6028
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Allocate a variable for a List and fill it from "*arg".
6031 * Return OK or FAIL.
6032 */
6033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 list_T *l = NULL;
6037 typval_T tv;
6038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039
6040 if (evaluate)
6041 {
6042 l = list_alloc();
6043 if (l == NULL)
6044 return FAIL;
6045 }
6046
6047 *arg = skipwhite(*arg + 1);
6048 while (**arg != ']' && **arg != NUL)
6049 {
6050 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6051 goto failret;
6052 if (evaluate)
6053 {
6054 item = listitem_alloc();
6055 if (item != NULL)
6056 {
6057 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006058 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 list_append(l, item);
6060 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006061 else
6062 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 }
6064
6065 if (**arg == ']')
6066 break;
6067 if (**arg != ',')
6068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006069 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 goto failret;
6071 }
6072 *arg = skipwhite(*arg + 1);
6073 }
6074
6075 if (**arg != ']')
6076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078failret:
6079 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 return FAIL;
6082 }
6083
6084 *arg = skipwhite(*arg + 1);
6085 if (evaluate)
6086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006087 rettv->v_type = VAR_LIST;
6088 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 ++l->lv_refcount;
6090 }
6091
6092 return OK;
6093}
6094
6095/*
6096 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006097 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006099 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 list_T *l;
6103
6104 l = (list_T *)alloc_clear(sizeof(list_T));
6105 if (l != NULL)
6106 {
6107 /* Prepend the list to the list of lists for garbage collection. */
6108 if (first_list != NULL)
6109 first_list->lv_used_prev = l;
6110 l->lv_used_prev = NULL;
6111 l->lv_used_next = first_list;
6112 first_list = l;
6113 }
6114 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115}
6116
6117/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006118 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006119 * Returns OK or FAIL.
6120 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006123{
6124 list_T *l = list_alloc();
6125
6126 if (l == NULL)
6127 return FAIL;
6128
6129 rettv->vval.v_list = l;
6130 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006131 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006132 ++l->lv_refcount;
6133 return OK;
6134}
6135
6136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Unreference a list: decrement the reference count and free it when it
6138 * becomes zero.
6139 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006141list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006143 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006144 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145}
6146
6147/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006148 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 * Ignores the reference count.
6150 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006151 static void
6152list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153{
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006156 for (item = l->lv_first; item != NULL; item = l->lv_first)
6157 {
6158 /* Remove the item before deleting it. */
6159 l->lv_first = item->li_next;
6160 clear_tv(&item->li_tv);
6161 vim_free(item);
6162 }
6163}
6164
6165 static void
6166list_free_list(list_T *l)
6167{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006168 /* Remove the list from the list of lists for garbage collection. */
6169 if (l->lv_used_prev == NULL)
6170 first_list = l->lv_used_next;
6171 else
6172 l->lv_used_prev->lv_used_next = l->lv_used_next;
6173 if (l->lv_used_next != NULL)
6174 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 vim_free(l);
6177}
6178
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006179 void
6180list_free(list_T *l)
6181{
6182 if (!in_free_unref_items)
6183 {
6184 list_free_contents(l);
6185 list_free_list(l);
6186 }
6187}
6188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189/*
6190 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006191 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006193 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006194listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195{
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197}
6198
6199/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006200 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006205 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206 vim_free(item);
6207}
6208
6209/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006210 * Remove a list item from a List and free it. Also clears the value.
6211 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006214{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006215 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006216 listitem_free(item);
6217}
6218
6219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 * Get the number of items in a list.
6221 */
6222 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 if (l == NULL)
6226 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006227 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228}
6229
6230/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006231 * Return TRUE when two lists have exactly the same values.
6232 */
6233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234list_equal(
6235 list_T *l1,
6236 list_T *l2,
6237 int ic, /* ignore case for strings */
6238 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006242 if (l1 == NULL || l2 == NULL)
6243 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006244 if (l1 == l2)
6245 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006246 if (list_len(l1) != list_len(l2))
6247 return FALSE;
6248
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 for (item1 = l1->lv_first, item2 = l2->lv_first;
6250 item1 != NULL && item2 != NULL;
6251 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006252 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 return FALSE;
6254 return item1 == NULL && item2 == NULL;
6255}
6256
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006257/*
6258 * Return the dictitem that an entry in a hashtable points to.
6259 */
6260 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006262{
6263 return HI2DI(hi);
6264}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006265
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006267 * Return TRUE when two dictionaries have exactly the same key/values.
6268 */
6269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006270dict_equal(
6271 dict_T *d1,
6272 dict_T *d2,
6273 int ic, /* ignore case for strings */
6274 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275{
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 hashitem_T *hi;
6277 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006279
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006280 if (d1 == NULL && d2 == NULL)
6281 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006282 if (d1 == NULL || d2 == NULL)
6283 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006284 if (d1 == d2)
6285 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006286 if (dict_len(d1) != dict_len(d2))
6287 return FALSE;
6288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006289 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292 if (!HASHITEM_EMPTY(hi))
6293 {
6294 item2 = dict_find(d2, hi->hi_key, -1);
6295 if (item2 == NULL)
6296 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006297 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 return FALSE;
6299 --todo;
6300 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006301 }
6302 return TRUE;
6303}
6304
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305static int tv_equal_recurse_limit;
6306
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006307 static int
6308func_equal(
6309 typval_T *tv1,
6310 typval_T *tv2,
6311 int ic) /* ignore case */
6312{
6313 char_u *s1, *s2;
6314 dict_T *d1, *d2;
6315 int a1, a2;
6316 int i;
6317
6318 /* empty and NULL function name considered the same */
6319 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6320 : tv1->vval.v_partial->pt_name;
6321 if (s1 != NULL && *s1 == NUL)
6322 s1 = NULL;
6323 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6324 : tv2->vval.v_partial->pt_name;
6325 if (s2 != NULL && *s2 == NUL)
6326 s2 = NULL;
6327 if (s1 == NULL || s2 == NULL)
6328 {
6329 if (s1 != s2)
6330 return FALSE;
6331 }
6332 else if (STRCMP(s1, s2) != 0)
6333 return FALSE;
6334
6335 /* empty dict and NULL dict is different */
6336 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6337 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6338 if (d1 == NULL || d2 == NULL)
6339 {
6340 if (d1 != d2)
6341 return FALSE;
6342 }
6343 else if (!dict_equal(d1, d2, ic, TRUE))
6344 return FALSE;
6345
6346 /* empty list and no list considered the same */
6347 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6348 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6349 if (a1 != a2)
6350 return FALSE;
6351 for (i = 0; i < a1; ++i)
6352 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6353 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6354 return FALSE;
6355
6356 return TRUE;
6357}
6358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006359/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006361 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006362 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006363 */
6364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006365tv_equal(
6366 typval_T *tv1,
6367 typval_T *tv2,
6368 int ic, /* ignore case */
6369 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370{
6371 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006372 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006373 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006374 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006376 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006377 * recursiveness to a limit. We guess they are equal then.
6378 * A fixed limit has the problem of still taking an awful long time.
6379 * Reduce the limit every time running into it. That should work fine for
6380 * deeply linked structures that are not recursively linked and catch
6381 * recursiveness quickly. */
6382 if (!recursive)
6383 tv_equal_recurse_limit = 1000;
6384 if (recursive_cnt >= tv_equal_recurse_limit)
6385 {
6386 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006387 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006388 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006389
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006390 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6391 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006392 if ((tv1->v_type == VAR_FUNC
6393 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6394 && (tv2->v_type == VAR_FUNC
6395 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6396 {
6397 ++recursive_cnt;
6398 r = func_equal(tv1, tv2, ic);
6399 --recursive_cnt;
6400 return r;
6401 }
6402
6403 if (tv1->v_type != tv2->v_type)
6404 return FALSE;
6405
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006406 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006408 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006409 ++recursive_cnt;
6410 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6411 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006412 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006413
6414 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006415 ++recursive_cnt;
6416 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6417 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006418 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006419
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006420 case VAR_NUMBER:
6421 return tv1->vval.v_number == tv2->vval.v_number;
6422
6423 case VAR_STRING:
6424 s1 = get_tv_string_buf(tv1, buf1);
6425 s2 = get_tv_string_buf(tv2, buf2);
6426 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006427
6428 case VAR_SPECIAL:
6429 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006430
6431 case VAR_FLOAT:
6432#ifdef FEAT_FLOAT
6433 return tv1->vval.v_float == tv2->vval.v_float;
6434#endif
6435 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006437 return tv1->vval.v_job == tv2->vval.v_job;
6438#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006439 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006440#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006441 return tv1->vval.v_channel == tv2->vval.v_channel;
6442#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006443 case VAR_FUNC:
6444 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006445 case VAR_UNKNOWN:
6446 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006448
Bram Moolenaara03f2332016-02-06 18:09:59 +01006449 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6450 * does not equal anything, not even itself. */
6451 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452}
6453
6454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 * Locate item with index "n" in list "l" and return it.
6456 * A negative index is counted from the end; -1 is the last item.
6457 * Returns NULL when "n" is out of range.
6458 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006459 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 long idx;
6464
6465 if (l == NULL)
6466 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467
6468 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006470 n = l->lv_len + n;
6471
6472 /* Check for index out of range. */
6473 if (n < 0 || n >= l->lv_len)
6474 return NULL;
6475
6476 /* When there is a cached index may start search from there. */
6477 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006479 if (n < l->lv_idx / 2)
6480 {
6481 /* closest to the start of the list */
6482 item = l->lv_first;
6483 idx = 0;
6484 }
6485 else if (n > (l->lv_idx + l->lv_len) / 2)
6486 {
6487 /* closest to the end of the list */
6488 item = l->lv_last;
6489 idx = l->lv_len - 1;
6490 }
6491 else
6492 {
6493 /* closest to the cached index */
6494 item = l->lv_idx_item;
6495 idx = l->lv_idx;
6496 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 }
6498 else
6499 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006500 if (n < l->lv_len / 2)
6501 {
6502 /* closest to the start of the list */
6503 item = l->lv_first;
6504 idx = 0;
6505 }
6506 else
6507 {
6508 /* closest to the end of the list */
6509 item = l->lv_last;
6510 idx = l->lv_len - 1;
6511 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513
6514 while (n > idx)
6515 {
6516 /* search forward */
6517 item = item->li_next;
6518 ++idx;
6519 }
6520 while (n < idx)
6521 {
6522 /* search backward */
6523 item = item->li_prev;
6524 --idx;
6525 }
6526
6527 /* cache the used index */
6528 l->lv_idx = idx;
6529 l->lv_idx_item = item;
6530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 return item;
6532}
6533
6534/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006535 * Get list item "l[idx]" as a number.
6536 */
6537 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_find_nr(
6539 list_T *l,
6540 long idx,
6541 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006542{
6543 listitem_T *li;
6544
6545 li = list_find(l, idx);
6546 if (li == NULL)
6547 {
6548 if (errorp != NULL)
6549 *errorp = TRUE;
6550 return -1L;
6551 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006552 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006553}
6554
6555/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006556 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6557 */
6558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006560{
6561 listitem_T *li;
6562
6563 li = list_find(l, idx - 1);
6564 if (li == NULL)
6565 {
6566 EMSGN(_(e_listidx), idx);
6567 return NULL;
6568 }
6569 return get_tv_string(&li->li_tv);
6570}
6571
6572/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006573 * Locate "item" list "l" and return its index.
6574 * Returns -1 when "item" is not in the list.
6575 */
6576 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006578{
6579 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006581
6582 if (l == NULL)
6583 return -1;
6584 idx = 0;
6585 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6586 ++idx;
6587 if (li == NULL)
6588 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006589 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006590}
6591
6592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 * Append item "item" to the end of list "l".
6594 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006596list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597{
6598 if (l->lv_last == NULL)
6599 {
6600 /* empty list */
6601 l->lv_first = item;
6602 l->lv_last = item;
6603 item->li_prev = NULL;
6604 }
6605 else
6606 {
6607 l->lv_last->li_next = item;
6608 item->li_prev = l->lv_last;
6609 l->lv_last = item;
6610 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 item->li_next = NULL;
6613}
6614
6615/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623
Bram Moolenaar05159a02005-02-26 23:04:13 +00006624 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006626 copy_tv(tv, &li->li_tv);
6627 list_append(l, li);
6628 return OK;
6629}
6630
6631/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006632 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633 * Return FAIL when out of memory.
6634 */
6635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006637{
6638 listitem_T *li = listitem_alloc();
6639
6640 if (li == NULL)
6641 return FAIL;
6642 li->li_tv.v_type = VAR_DICT;
6643 li->li_tv.v_lock = 0;
6644 li->li_tv.vval.v_dict = dict;
6645 list_append(list, li);
6646 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 return OK;
6648}
6649
6650/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006651 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006652 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006653 * Returns FAIL when out of memory.
6654 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006656list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006657{
6658 listitem_T *li = listitem_alloc();
6659
6660 if (li == NULL)
6661 return FAIL;
6662 list_append(l, li);
6663 li->li_tv.v_type = VAR_STRING;
6664 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006665 if (str == NULL)
6666 li->li_tv.vval.v_string = NULL;
6667 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006668 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006669 return FAIL;
6670 return OK;
6671}
6672
6673/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006674 * Append "n" to list "l".
6675 * Returns FAIL when out of memory.
6676 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006679{
6680 listitem_T *li;
6681
6682 li = listitem_alloc();
6683 if (li == NULL)
6684 return FAIL;
6685 li->li_tv.v_type = VAR_NUMBER;
6686 li->li_tv.v_lock = 0;
6687 li->li_tv.vval.v_number = n;
6688 list_append(l, li);
6689 return OK;
6690}
6691
6692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006694 * If "item" is NULL append at the end.
6695 * Return FAIL when out of memory.
6696 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006699{
Bram Moolenaar33570922005-01-25 22:26:29 +00006700 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006701
6702 if (ni == NULL)
6703 return FAIL;
6704 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006705 list_insert(l, ni, item);
6706 return OK;
6707}
6708
6709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006711{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006712 if (item == NULL)
6713 /* Append new item at end of list. */
6714 list_append(l, ni);
6715 else
6716 {
6717 /* Insert new item before existing item. */
6718 ni->li_prev = item->li_prev;
6719 ni->li_next = item;
6720 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006721 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006722 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006723 ++l->lv_idx;
6724 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006726 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006727 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006728 l->lv_idx_item = NULL;
6729 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006731 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733}
6734
6735/*
6736 * Extend "l1" with "l2".
6737 * If "bef" is NULL append at the end, otherwise insert before this item.
6738 * Returns FAIL when out of memory.
6739 */
6740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742{
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006744 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006746 /* We also quit the loop when we have inserted the original item count of
6747 * the list, avoid a hang when we extend a list with itself. */
6748 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006749 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6750 return FAIL;
6751 return OK;
6752}
6753
6754/*
6755 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6756 * Return FAIL when out of memory.
6757 */
6758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006760{
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006763 if (l1 == NULL || l2 == NULL)
6764 return FAIL;
6765
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006766 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006768 if (l == NULL)
6769 return FAIL;
6770 tv->v_type = VAR_LIST;
6771 tv->vval.v_list = l;
6772
6773 /* append all items from the second list */
6774 return list_extend(l, l2, NULL);
6775}
6776
6777/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 * Returns NULL when out of memory.
6782 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006784list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785{
Bram Moolenaar33570922005-01-25 22:26:29 +00006786 list_T *copy;
6787 listitem_T *item;
6788 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789
6790 if (orig == NULL)
6791 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792
6793 copy = list_alloc();
6794 if (copy != NULL)
6795 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006796 if (copyID != 0)
6797 {
6798 /* Do this before adding the items, because one of the items may
6799 * refer back to this list. */
6800 orig->lv_copyID = copyID;
6801 orig->lv_copylist = copy;
6802 }
6803 for (item = orig->lv_first; item != NULL && !got_int;
6804 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 {
6806 ni = listitem_alloc();
6807 if (ni == NULL)
6808 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006810 {
6811 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6812 {
6813 vim_free(ni);
6814 break;
6815 }
6816 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006818 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 list_append(copy, ni);
6820 }
6821 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006822 if (item != NULL)
6823 {
6824 list_unref(copy);
6825 copy = NULL;
6826 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 }
6828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 return copy;
6830}
6831
6832/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006833 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006834 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006835 * This used to be called list_remove, but that conflicts with a Sun header
6836 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006840{
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006843 /* notify watchers */
6844 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006846 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847 list_fix_watch(l, ip);
6848 if (ip == item2)
6849 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006851
6852 if (item2->li_next == NULL)
6853 l->lv_last = item->li_prev;
6854 else
6855 item2->li_next->li_prev = item->li_prev;
6856 if (item->li_prev == NULL)
6857 l->lv_first = item2->li_next;
6858 else
6859 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006860 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861}
6862
6863/*
6864 * Return an allocated string with the string representation of a list.
6865 * May return NULL.
6866 */
6867 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006868list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006869{
6870 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006871
6872 if (tv->vval.v_list == NULL)
6873 return NULL;
6874 ga_init2(&ga, (int)sizeof(char), 80);
6875 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006876 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6877 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006878 {
6879 vim_free(ga.ga_data);
6880 return NULL;
6881 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 ga_append(&ga, ']');
6883 ga_append(&ga, NUL);
6884 return (char_u *)ga.ga_data;
6885}
6886
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006887typedef struct join_S {
6888 char_u *s;
6889 char_u *tofree;
6890} join_T;
6891
6892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006893list_join_inner(
6894 garray_T *gap, /* to store the result in */
6895 list_T *l,
6896 char_u *sep,
6897 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006898 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006899 int copyID,
6900 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006901{
6902 int i;
6903 join_T *p;
6904 int len;
6905 int sumlen = 0;
6906 int first = TRUE;
6907 char_u *tofree;
6908 char_u numbuf[NUMBUFLEN];
6909 listitem_T *item;
6910 char_u *s;
6911
6912 /* Stringify each item in the list. */
6913 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6914 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006915 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6916 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006917 if (s == NULL)
6918 return FAIL;
6919
6920 len = (int)STRLEN(s);
6921 sumlen += len;
6922
Bram Moolenaarcde88542015-08-11 19:14:00 +02006923 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006924 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6925 if (tofree != NULL || s != numbuf)
6926 {
6927 p->s = s;
6928 p->tofree = tofree;
6929 }
6930 else
6931 {
6932 p->s = vim_strnsave(s, len);
6933 p->tofree = p->s;
6934 }
6935
6936 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006937 if (did_echo_string_emsg) /* recursion error, bail out */
6938 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006939 }
6940
6941 /* Allocate result buffer with its total size, avoid re-allocation and
6942 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6943 if (join_gap->ga_len >= 2)
6944 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6945 if (ga_grow(gap, sumlen + 2) == FAIL)
6946 return FAIL;
6947
6948 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6949 {
6950 if (first)
6951 first = FALSE;
6952 else
6953 ga_concat(gap, sep);
6954 p = ((join_T *)join_gap->ga_data) + i;
6955
6956 if (p->s != NULL)
6957 ga_concat(gap, p->s);
6958 line_breakcheck();
6959 }
6960
6961 return OK;
6962}
6963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006964/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006966 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006968 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970list_join(
6971 garray_T *gap,
6972 list_T *l,
6973 char_u *sep,
6974 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006975 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006976 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006977{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006978 garray_T join_ga;
6979 int retval;
6980 join_T *p;
6981 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006982
Bram Moolenaard39a7512015-04-16 22:51:22 +02006983 if (l->lv_len < 1)
6984 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006985 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006986 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6987 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988
6989 /* Dispose each item in join_ga. */
6990 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006991 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006992 p = (join_T *)join_ga.ga_data;
6993 for (i = 0; i < join_ga.ga_len; ++i)
6994 {
6995 vim_free(p->tofree);
6996 ++p;
6997 }
6998 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01007000
7001 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002}
7003
7004/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007005 * Return the next (unique) copy ID.
7006 * Used for serializing nested structures.
7007 */
7008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007010{
7011 current_copyID += COPYID_INC;
7012 return current_copyID;
7013}
7014
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007015/* Used by get_func_tv() */
7016static garray_T funcargs = GA_EMPTY;
7017
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007018/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 * Garbage collection for lists and dictionaries.
7020 *
7021 * We use reference counts to be able to free most items right away when they
7022 * are no longer used. But for composite items it's possible that it becomes
7023 * unused while the reference count is > 0: When there is a recursive
7024 * reference. Example:
7025 * :let l = [1, 2, 3]
7026 * :let d = {9: l}
7027 * :let l[1] = d
7028 *
7029 * Since this is quite unusual we handle this with garbage collection: every
7030 * once in a while find out which lists and dicts are not referenced from any
7031 * variable.
7032 *
7033 * Here is a good reference text about garbage collection (refers to Python
7034 * but it applies to all reference-counting mechanisms):
7035 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007037
7038/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007040 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007042 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007044garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007045{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007046 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 buf_T *buf;
7049 win_T *wp;
7050 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007051 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007052 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007053 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007054#ifdef FEAT_WINDOWS
7055 tabpage_T *tp;
7056#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007058 if (!testing)
7059 {
7060 /* Only do this once. */
7061 want_garbage_collect = FALSE;
7062 may_garbage_collect = FALSE;
7063 garbage_collect_at_exit = FALSE;
7064 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007065
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007066 /* We advance by two because we add one for items referenced through
7067 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007068 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 /*
7071 * 1. Go through all accessible variables and mark all lists and dicts
7072 * with copyID.
7073 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074
7075 /* Don't free variables in the previous_funccal list unless they are only
7076 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007077 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007078 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7079 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7081 NULL);
7082 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7083 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007084 }
7085
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 /* script-local variables */
7087 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089
7090 /* buffer-local variables */
7091 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7093 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094
7095 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7098 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007099#ifdef FEAT_AUTOCMD
7100 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7102 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007103#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007105#ifdef FEAT_WINDOWS
7106 /* tabpage-local variables */
7107 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7109 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007110#endif
7111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114
7115 /* function-local variables */
7116 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7117 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7119 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 }
7121
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007122 /* function call arguments, if v:testing is set. */
7123 for (i = 0; i < funcargs.ga_len; ++i)
7124 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7125 copyID, NULL, NULL);
7126
Bram Moolenaard812df62008-11-09 12:46:09 +00007127 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007129
Bram Moolenaar1dced572012-04-05 16:54:08 +02007130#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007132#endif
7133
Bram Moolenaardb913952012-06-29 12:54:53 +02007134#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007136#endif
7137
7138#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007140#endif
7141
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007143 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007144 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007145#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007146#ifdef FEAT_NETBEANS_INTG
7147 abort = abort || set_ref_in_nb_channel(copyID);
7148#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007149
Bram Moolenaare3188e22016-05-31 21:13:04 +02007150#ifdef FEAT_TIMERS
7151 abort = abort || set_ref_in_timer(copyID);
7152#endif
7153
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 /*
7157 * 2. Free lists and dictionaries that are not referenced.
7158 */
7159 did_free = free_unref_items(copyID);
7160
7161 /*
7162 * 3. Check if any funccal can be freed now.
7163 */
7164 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007165 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 if (can_free_funccal(*pfc, copyID))
7167 {
7168 fc = *pfc;
7169 *pfc = fc->caller;
7170 free_funccal(fc, TRUE);
7171 did_free = TRUE;
7172 did_free_funccal = TRUE;
7173 }
7174 else
7175 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007176 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 if (did_free_funccal)
7178 /* When a funccal was freed some more items might be garbage
7179 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007180 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007181 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 else if (p_verbose > 0)
7183 {
7184 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7185 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007186
7187 return did_free;
7188}
7189
7190/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007191 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007192 */
7193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007195{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007196 dict_T *dd, *dd_next;
7197 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007198 int did_free = FALSE;
7199
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007200 /* Let all "free" functions know that we are here. This means no
7201 * dictionaries, lists, channels or jobs are to be freed, because we will
7202 * do that here. */
7203 in_free_unref_items = TRUE;
7204
7205 /*
7206 * PASS 1: free the contents of the items. We don't free the items
7207 * themselves yet, so that it is possible to decrement refcount counters
7208 */
7209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007211 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007213 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007214 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007216 /* Free the Dictionary and ordinary items it contains, but don't
7217 * recurse into Lists and Dictionaries, they will be in the list
7218 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007219 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
7223 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007224 * Go through the list of lists and free items without the copyID.
7225 * But don't free a list that has a watcher (used in a for loop), these
7226 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007228 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7229 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7230 && ll->lv_watch == NULL)
7231 {
7232 /* Free the List and ordinary items it contains, but don't recurse
7233 * into Lists and Dictionaries, they will be in the list of dicts
7234 * or list of lists. */
7235 list_free_contents(ll);
7236 did_free = TRUE;
7237 }
7238
7239#ifdef FEAT_JOB_CHANNEL
7240 /* Go through the list of jobs and free items without the copyID. This
7241 * must happen before doing channels, because jobs refer to channels, but
7242 * the reference from the channel to the job isn't tracked. */
7243 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7244
7245 /* Go through the list of channels and free items without the copyID. */
7246 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7247#endif
7248
7249 /*
7250 * PASS 2: free the items themselves.
7251 */
7252 for (dd = first_dict; dd != NULL; dd = dd_next)
7253 {
7254 dd_next = dd->dv_used_next;
7255 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7256 dict_free_dict(dd);
7257 }
7258
7259 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007260 {
7261 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007262 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7263 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007264 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265 /* Free the List and ordinary items it contains, but don't recurse
7266 * into Lists and Dictionaries, they will be in the list of dicts
7267 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007270 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007271
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007272#ifdef FEAT_JOB_CHANNEL
7273 /* Go through the list of jobs and free items without the copyID. This
7274 * must happen before doing channels, because jobs refer to channels, but
7275 * the reference from the channel to the job isn't tracked. */
7276 free_unused_jobs(copyID, COPYID_MASK);
7277
7278 /* Go through the list of channels and free items without the copyID. */
7279 free_unused_channels(copyID, COPYID_MASK);
7280#endif
7281
7282 in_free_unref_items = FALSE;
7283
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007284 return did_free;
7285}
7286
7287/*
7288 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 * "list_stack" is used to add lists to be marked. Can be NULL.
7290 *
7291 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007292 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007295{
7296 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007297 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007298 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007299 hashtab_T *cur_ht;
7300 ht_stack_T *ht_stack = NULL;
7301 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007302
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007303 cur_ht = ht;
7304 for (;;)
7305 {
7306 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007307 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308 /* Mark each item in the hashtab. If the item contains a hashtab
7309 * it is added to ht_stack, if it contains a list it is added to
7310 * list_stack. */
7311 todo = (int)cur_ht->ht_used;
7312 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7313 if (!HASHITEM_EMPTY(hi))
7314 {
7315 --todo;
7316 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7317 &ht_stack, list_stack);
7318 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007319 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007320
7321 if (ht_stack == NULL)
7322 break;
7323
7324 /* take an item from the stack */
7325 cur_ht = ht_stack->ht;
7326 tempitem = ht_stack;
7327 ht_stack = ht_stack->prev;
7328 free(tempitem);
7329 }
7330
7331 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007332}
7333
7334/*
7335 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7337 *
7338 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007339 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007342{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007343 listitem_T *li;
7344 int abort = FALSE;
7345 list_T *cur_l;
7346 list_stack_T *list_stack = NULL;
7347 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007348
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007349 cur_l = l;
7350 for (;;)
7351 {
7352 if (!abort)
7353 /* Mark each item in the list. If the item contains a hashtab
7354 * it is added to ht_stack, if it contains a list it is added to
7355 * list_stack. */
7356 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7357 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7358 ht_stack, &list_stack);
7359 if (list_stack == NULL)
7360 break;
7361
7362 /* take an item from the stack */
7363 cur_l = list_stack->list;
7364 tempitem = list_stack;
7365 list_stack = list_stack->prev;
7366 free(tempitem);
7367 }
7368
7369 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007370}
7371
7372/*
7373 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007374 * "list_stack" is used to add lists to be marked. Can be NULL.
7375 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7376 *
7377 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007378 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007380set_ref_in_item(
7381 typval_T *tv,
7382 int copyID,
7383 ht_stack_T **ht_stack,
7384 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007385{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007386 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007388 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007389 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007390 dict_T *dd = tv->vval.v_dict;
7391
Bram Moolenaara03f2332016-02-06 18:09:59 +01007392 if (dd != NULL && dd->dv_copyID != copyID)
7393 {
7394 /* Didn't see this dict yet. */
7395 dd->dv_copyID = copyID;
7396 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007397 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007398 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7399 }
7400 else
7401 {
7402 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7403 if (newitem == NULL)
7404 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007405 else
7406 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007407 newitem->ht = &dd->dv_hashtab;
7408 newitem->prev = *ht_stack;
7409 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007410 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007411 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007412 }
7413 }
7414 else if (tv->v_type == VAR_LIST)
7415 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007416 list_T *ll = tv->vval.v_list;
7417
Bram Moolenaara03f2332016-02-06 18:09:59 +01007418 if (ll != NULL && ll->lv_copyID != copyID)
7419 {
7420 /* Didn't see this list yet. */
7421 ll->lv_copyID = copyID;
7422 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007423 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007424 abort = set_ref_in_list(ll, copyID, ht_stack);
7425 }
7426 else
7427 {
7428 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007429 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007430 if (newitem == NULL)
7431 abort = TRUE;
7432 else
7433 {
7434 newitem->list = ll;
7435 newitem->prev = *list_stack;
7436 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007437 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007438 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007439 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007440 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007441 else if (tv->v_type == VAR_PARTIAL)
7442 {
7443 partial_T *pt = tv->vval.v_partial;
7444 int i;
7445
7446 /* A partial does not have a copyID, because it cannot contain itself.
7447 */
7448 if (pt != NULL)
7449 {
7450 if (pt->pt_dict != NULL)
7451 {
7452 typval_T dtv;
7453
7454 dtv.v_type = VAR_DICT;
7455 dtv.vval.v_dict = pt->pt_dict;
7456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7457 }
7458
7459 for (i = 0; i < pt->pt_argc; ++i)
7460 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7461 ht_stack, list_stack);
7462 }
7463 }
7464#ifdef FEAT_JOB_CHANNEL
7465 else if (tv->v_type == VAR_JOB)
7466 {
7467 job_T *job = tv->vval.v_job;
7468 typval_T dtv;
7469
7470 if (job != NULL && job->jv_copyID != copyID)
7471 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007472 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 if (job->jv_channel != NULL)
7474 {
7475 dtv.v_type = VAR_CHANNEL;
7476 dtv.vval.v_channel = job->jv_channel;
7477 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7478 }
7479 if (job->jv_exit_partial != NULL)
7480 {
7481 dtv.v_type = VAR_PARTIAL;
7482 dtv.vval.v_partial = job->jv_exit_partial;
7483 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7484 }
7485 }
7486 }
7487 else if (tv->v_type == VAR_CHANNEL)
7488 {
7489 channel_T *ch =tv->vval.v_channel;
7490 int part;
7491 typval_T dtv;
7492 jsonq_T *jq;
7493 cbq_T *cq;
7494
7495 if (ch != NULL && ch->ch_copyID != copyID)
7496 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007497 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007498 for (part = PART_SOCK; part <= PART_IN; ++part)
7499 {
7500 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7501 jq = jq->jq_next)
7502 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7503 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7504 cq = cq->cq_next)
7505 if (cq->cq_partial != NULL)
7506 {
7507 dtv.v_type = VAR_PARTIAL;
7508 dtv.vval.v_partial = cq->cq_partial;
7509 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7510 }
7511 if (ch->ch_part[part].ch_partial != NULL)
7512 {
7513 dtv.v_type = VAR_PARTIAL;
7514 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7515 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7516 }
7517 }
7518 if (ch->ch_partial != NULL)
7519 {
7520 dtv.v_type = VAR_PARTIAL;
7521 dtv.vval.v_partial = ch->ch_partial;
7522 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7523 }
7524 if (ch->ch_close_partial != NULL)
7525 {
7526 dtv.v_type = VAR_PARTIAL;
7527 dtv.vval.v_partial = ch->ch_close_partial;
7528 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7529 }
7530 }
7531 }
7532#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007533 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007534}
7535
7536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * Allocate an empty header for a dictionary.
7538 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007539 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541{
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007546 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007547 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007548 if (first_dict != NULL)
7549 first_dict->dv_used_prev = d;
7550 d->dv_used_next = first_dict;
7551 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007553
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007556 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007557 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007558 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561}
7562
7563/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007564 * Allocate an empty dict for a return value.
7565 * Returns OK or FAIL.
7566 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007569{
7570 dict_T *d = dict_alloc();
7571
7572 if (d == NULL)
7573 return FAIL;
7574
7575 rettv->vval.v_dict = d;
7576 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007577 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007578 ++d->dv_refcount;
7579 return OK;
7580}
7581
7582
7583/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 * Unreference a Dictionary: decrement the reference count and free it when it
7585 * becomes zero.
7586 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007588dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007590 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007591 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592}
7593
7594/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007595 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 * Ignores the reference count.
7597 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007598 static void
7599dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007603 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007605 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007606 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007607 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007610 if (!HASHITEM_EMPTY(hi))
7611 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007612 /* Remove the item before deleting it, just in case there is
7613 * something recursive causing trouble. */
7614 di = HI2DI(hi);
7615 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007616 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007617 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 --todo;
7619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007622}
7623
7624 static void
7625dict_free_dict(dict_T *d)
7626{
7627 /* Remove the dict from the list of dicts for garbage collection. */
7628 if (d->dv_used_prev == NULL)
7629 first_dict = d->dv_used_next;
7630 else
7631 d->dv_used_prev->dv_used_next = d->dv_used_next;
7632 if (d->dv_used_next != NULL)
7633 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 vim_free(d);
7635}
7636
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007637 void
7638dict_free(dict_T *d)
7639{
7640 if (!in_free_unref_items)
7641 {
7642 dict_free_contents(d);
7643 dict_free_dict(d);
7644 }
7645}
7646
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647/*
7648 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 * The "key" is copied to the new item.
7650 * Note that the value of the item "di_tv" still needs to be initialized!
7651 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007653 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007658 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007662 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007663 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665}
7666
7667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 * Make a copy of a Dictionary item.
7669 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672{
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007675 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7676 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 if (di != NULL)
7678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007680 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 copy_tv(&org->di_tv, &di->di_tv);
7682 }
7683 return di;
7684}
7685
7686/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 * Remove item "item" from Dictionary "dict" and free it.
7688 */
7689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007690dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691{
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 if (HASHITEM_EMPTY(hi))
7696 EMSG2(_(e_intern2), "dictitem_remove()");
7697 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 dictitem_free(item);
7700}
7701
7702/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 * Free a dict item. Also clears the value.
7704 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007706dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007709 if (item->di_flags & DI_FLAGS_ALLOC)
7710 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711}
7712
7713/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7715 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007716 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717 * Returns NULL when out of memory.
7718 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007721{
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 dict_T *copy;
7723 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726
7727 if (orig == NULL)
7728 return NULL;
7729
7730 copy = dict_alloc();
7731 if (copy != NULL)
7732 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007733 if (copyID != 0)
7734 {
7735 orig->dv_copyID = copyID;
7736 orig->dv_copydict = copy;
7737 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007738 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007739 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 --todo;
7744
7745 di = dictitem_alloc(hi->hi_key);
7746 if (di == NULL)
7747 break;
7748 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007749 {
7750 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7751 copyID) == FAIL)
7752 {
7753 vim_free(di);
7754 break;
7755 }
7756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 else
7758 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7759 if (dict_add(copy, di) == FAIL)
7760 {
7761 dictitem_free(di);
7762 break;
7763 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766
Bram Moolenaare9a41262005-01-15 22:18:47 +00007767 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007768 if (todo > 0)
7769 {
7770 dict_unref(copy);
7771 copy = NULL;
7772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007773 }
7774
7775 return copy;
7776}
7777
7778/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007780 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007783dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784{
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786}
7787
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 * Add a number or string entry to dictionary "d".
7790 * When "str" is NULL use number "nr", otherwise use "str".
7791 * Returns FAIL when out of memory and when key already exists.
7792 */
7793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794dict_add_nr_str(
7795 dict_T *d,
7796 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007797 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799{
7800 dictitem_T *item;
7801
7802 item = dictitem_alloc((char_u *)key);
7803 if (item == NULL)
7804 return FAIL;
7805 item->di_tv.v_lock = 0;
7806 if (str == NULL)
7807 {
7808 item->di_tv.v_type = VAR_NUMBER;
7809 item->di_tv.vval.v_number = nr;
7810 }
7811 else
7812 {
7813 item->di_tv.v_type = VAR_STRING;
7814 item->di_tv.vval.v_string = vim_strsave(str);
7815 }
7816 if (dict_add(d, item) == FAIL)
7817 {
7818 dictitem_free(item);
7819 return FAIL;
7820 }
7821 return OK;
7822}
7823
7824/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007825 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007826 * Returns FAIL when out of memory and when key already exists.
7827 */
7828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007830{
7831 dictitem_T *item;
7832
7833 item = dictitem_alloc((char_u *)key);
7834 if (item == NULL)
7835 return FAIL;
7836 item->di_tv.v_lock = 0;
7837 item->di_tv.v_type = VAR_LIST;
7838 item->di_tv.vval.v_list = list;
7839 if (dict_add(d, item) == FAIL)
7840 {
7841 dictitem_free(item);
7842 return FAIL;
7843 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007844 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007845 return OK;
7846}
7847
7848/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 * Get the number of items in a Dictionary.
7850 */
7851 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007852dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 if (d == NULL)
7855 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007856 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857}
7858
7859/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 * Find item "key[len]" in Dictionary "d".
7861 * If "len" is negative use strlen(key).
7862 * Returns NULL when not found.
7863 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007864 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007865dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007867#define AKEYLEN 200
7868 char_u buf[AKEYLEN];
7869 char_u *akey;
7870 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007873 if (d == NULL)
7874 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 if (len < 0)
7876 akey = key;
7877 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 tofree = akey = vim_strnsave(key, len);
7880 if (akey == NULL)
7881 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007882 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 else
7884 {
7885 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007886 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007887 akey = buf;
7888 }
7889
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007891 vim_free(tofree);
7892 if (HASHITEM_EMPTY(hi))
7893 return NULL;
7894 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895}
7896
7897/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007898 * Get a string item from a dictionary.
7899 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007900 * Returns NULL if the entry doesn't exist or out of memory.
7901 */
7902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007903get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007904{
7905 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007906 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007907
7908 di = dict_find(d, key, -1);
7909 if (di == NULL)
7910 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007911 s = get_tv_string(&di->di_tv);
7912 if (save && s != NULL)
7913 s = vim_strsave(s);
7914 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007915}
7916
7917/*
7918 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007919 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007920 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007921 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007922get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007923{
7924 dictitem_T *di;
7925
7926 di = dict_find(d, key, -1);
7927 if (di == NULL)
7928 return 0;
7929 return get_tv_number(&di->di_tv);
7930}
7931
7932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 * Return an allocated string with the string representation of a Dictionary.
7934 * May return NULL.
7935 */
7936 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007937dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938{
7939 garray_T ga;
7940 int first = TRUE;
7941 char_u *tofree;
7942 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007946 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007948 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 return NULL;
7950 ga_init2(&ga, (int)sizeof(char), 80);
7951 ga_append(&ga, '{');
7952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007954 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007956 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 --todo;
7959
7960 if (first)
7961 first = FALSE;
7962 else
7963 ga_concat(&ga, (char_u *)", ");
7964
7965 tofree = string_quote(hi->hi_key, FALSE);
7966 if (tofree != NULL)
7967 {
7968 ga_concat(&ga, tofree);
7969 vim_free(tofree);
7970 }
7971 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007972 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7973 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 if (s != NULL)
7975 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007977 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007978 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007979 line_breakcheck();
7980
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007983 if (todo > 0)
7984 {
7985 vim_free(ga.ga_data);
7986 return NULL;
7987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988
7989 ga_append(&ga, '}');
7990 ga_append(&ga, NUL);
7991 return (char_u *)ga.ga_data;
7992}
7993
7994/*
7995 * Allocate a variable for a Dictionary and fill it from "*arg".
7996 * Return OK or FAIL. Returns NOTDONE for {expr}.
7997 */
7998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007999get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008000{
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dict_T *d = NULL;
8002 typval_T tvkey;
8003 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008004 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008007 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008
8009 /*
8010 * First check if it's not a curly-braces thing: {expr}.
8011 * Must do this without evaluating, otherwise a function may be called
8012 * twice. Unfortunately this means we need to call eval1() twice for the
8013 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 if (*start != '}')
8017 {
8018 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8019 return FAIL;
8020 if (*start == '}')
8021 return NOTDONE;
8022 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023
8024 if (evaluate)
8025 {
8026 d = dict_alloc();
8027 if (d == NULL)
8028 return FAIL;
8029 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008030 tvkey.v_type = VAR_UNKNOWN;
8031 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032
8033 *arg = skipwhite(*arg + 1);
8034 while (**arg != '}' && **arg != NUL)
8035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008036 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 goto failret;
8038 if (**arg != ':')
8039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008040 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008041 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 goto failret;
8043 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008044 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008047 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008048 {
8049 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008050 clear_tv(&tvkey);
8051 goto failret;
8052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054
8055 *arg = skipwhite(*arg + 1);
8056 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8057 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008058 if (evaluate)
8059 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 goto failret;
8061 }
8062 if (evaluate)
8063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008064 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 if (item != NULL)
8066 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008067 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008068 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 clear_tv(&tv);
8070 goto failret;
8071 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008072 item = dictitem_alloc(key);
8073 clear_tv(&tvkey);
8074 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008077 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008078 if (dict_add(d, item) == FAIL)
8079 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008080 }
8081 }
8082
8083 if (**arg == '}')
8084 break;
8085 if (**arg != ',')
8086 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008087 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008088 goto failret;
8089 }
8090 *arg = skipwhite(*arg + 1);
8091 }
8092
8093 if (**arg != '}')
8094 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008095 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008096failret:
8097 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008098 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008099 return FAIL;
8100 }
8101
8102 *arg = skipwhite(*arg + 1);
8103 if (evaluate)
8104 {
8105 rettv->v_type = VAR_DICT;
8106 rettv->vval.v_dict = d;
8107 ++d->dv_refcount;
8108 }
8109
8110 return OK;
8111}
8112
Bram Moolenaar17a13432016-01-24 14:22:10 +01008113 static char *
8114get_var_special_name(int nr)
8115{
8116 switch (nr)
8117 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008118 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008119 case VVAL_TRUE: return "v:true";
8120 case VVAL_NONE: return "v:none";
8121 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008122 }
8123 EMSG2(_(e_intern2), "get_var_special_name()");
8124 return "42";
8125}
8126
Bram Moolenaar8c711452005-01-14 21:53:12 +00008127/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 * Return a string with the string representation of a variable.
8129 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008130 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008132 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8133 * "string()", otherwise does not put quotes around strings, as ":echo"
8134 * displays values.
8135 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8136 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008137 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 */
8139 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008140echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008141 typval_T *tv,
8142 char_u **tofree,
8143 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008144 int copyID,
8145 int echo_style,
8146 int restore_copyID,
8147 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 static int recurse = 0;
8150 char_u *r = NULL;
8151
Bram Moolenaar33570922005-01-25 22:26:29 +00008152 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008154 if (!did_echo_string_emsg)
8155 {
8156 /* Only give this message once for a recursive call to avoid
8157 * flooding the user with errors. And stop iterating over lists
8158 * and dicts. */
8159 did_echo_string_emsg = TRUE;
8160 EMSG(_("E724: variable nested too deep for displaying"));
8161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008163 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008164 }
8165 ++recurse;
8166
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167 switch (tv->v_type)
8168 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008169 case VAR_STRING:
8170 if (echo_style && !dict_val)
8171 {
8172 *tofree = NULL;
8173 r = get_tv_string_buf(tv, numbuf);
8174 }
8175 else
8176 {
8177 *tofree = string_quote(tv->vval.v_string, FALSE);
8178 r = *tofree;
8179 }
8180 break;
8181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008183 if (echo_style)
8184 {
8185 *tofree = NULL;
8186 r = tv->vval.v_string;
8187 }
8188 else
8189 {
8190 *tofree = string_quote(tv->vval.v_string, TRUE);
8191 r = *tofree;
8192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008194
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008195 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008196 {
8197 partial_T *pt = tv->vval.v_partial;
8198 char_u *fname = string_quote(pt == NULL ? NULL
8199 : pt->pt_name, FALSE);
8200 garray_T ga;
8201 int i;
8202 char_u *tf;
8203
8204 ga_init2(&ga, 1, 100);
8205 ga_concat(&ga, (char_u *)"function(");
8206 if (fname != NULL)
8207 {
8208 ga_concat(&ga, fname);
8209 vim_free(fname);
8210 }
8211 if (pt != NULL && pt->pt_argc > 0)
8212 {
8213 ga_concat(&ga, (char_u *)", [");
8214 for (i = 0; i < pt->pt_argc; ++i)
8215 {
8216 if (i > 0)
8217 ga_concat(&ga, (char_u *)", ");
8218 ga_concat(&ga,
8219 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8220 vim_free(tf);
8221 }
8222 ga_concat(&ga, (char_u *)"]");
8223 }
8224 if (pt != NULL && pt->pt_dict != NULL)
8225 {
8226 typval_T dtv;
8227
8228 ga_concat(&ga, (char_u *)", ");
8229 dtv.v_type = VAR_DICT;
8230 dtv.vval.v_dict = pt->pt_dict;
8231 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8232 vim_free(tf);
8233 }
8234 ga_concat(&ga, (char_u *)")");
8235
8236 *tofree = ga.ga_data;
8237 r = *tofree;
8238 break;
8239 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008240
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008241 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008242 if (tv->vval.v_list == NULL)
8243 {
8244 *tofree = NULL;
8245 r = NULL;
8246 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008247 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8248 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008249 {
8250 *tofree = NULL;
8251 r = (char_u *)"[...]";
8252 }
8253 else
8254 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008255 int old_copyID = tv->vval.v_list->lv_copyID;
8256
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008258 *tofree = list2string(tv, copyID, restore_copyID);
8259 if (restore_copyID)
8260 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008261 r = *tofree;
8262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008263 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008264
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008266 if (tv->vval.v_dict == NULL)
8267 {
8268 *tofree = NULL;
8269 r = NULL;
8270 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008271 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8272 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008273 {
8274 *tofree = NULL;
8275 r = (char_u *)"{...}";
8276 }
8277 else
8278 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008279 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008280 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008281 *tofree = dict2string(tv, copyID, restore_copyID);
8282 if (restore_copyID)
8283 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008284 r = *tofree;
8285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008288 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008289 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008290 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008291 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 *tofree = NULL;
8293 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008294 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008296 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008297#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 *tofree = NULL;
8299 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8300 r = numbuf;
8301 break;
8302#endif
8303
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008304 case VAR_SPECIAL:
8305 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008306 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008307 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008308 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008309
Bram Moolenaar8502c702014-06-17 12:51:16 +02008310 if (--recurse == 0)
8311 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008312 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008313}
8314
8315/*
8316 * Return a string with the string representation of a variable.
8317 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8318 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008319 * Does not put quotes around strings, as ":echo" displays values.
8320 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8321 * May return NULL.
8322 */
8323 static char_u *
8324echo_string(
8325 typval_T *tv,
8326 char_u **tofree,
8327 char_u *numbuf,
8328 int copyID)
8329{
8330 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8331}
8332
8333/*
8334 * Return a string with the string representation of a variable.
8335 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8336 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008337 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008338 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008339 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008341tv2string(
8342 typval_T *tv,
8343 char_u **tofree,
8344 char_u *numbuf,
8345 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008346{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008347 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348}
8349
8350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 * Return string "str" in ' quotes, doubling ' characters.
8352 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008354 */
8355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008356string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008357{
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008359 char_u *p, *r, *s;
8360
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 len = (function ? 13 : 3);
8362 if (str != NULL)
8363 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008364 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 for (p = str; *p != NUL; mb_ptr_adv(p))
8366 if (*p == '\'')
8367 ++len;
8368 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008369 s = r = alloc(len);
8370 if (r != NULL)
8371 {
8372 if (function)
8373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008375 r += 10;
8376 }
8377 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 if (str != NULL)
8380 for (p = str; *p != NUL; )
8381 {
8382 if (*p == '\'')
8383 *r++ = '\'';
8384 MB_COPY_CHAR(p, r);
8385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008386 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008387 if (function)
8388 *r++ = ')';
8389 *r++ = NUL;
8390 }
8391 return s;
8392}
8393
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008394#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395/*
8396 * Convert the string "text" to a floating point number.
8397 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8398 * this always uses a decimal point.
8399 * Returns the length of the text that was consumed.
8400 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008402string2float(
8403 char_u *text,
8404 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008405{
8406 char *s = (char *)text;
8407 float_T f;
8408
8409 f = strtod(s, &s);
8410 *value = f;
8411 return (int)((char_u *)s - text);
8412}
8413#endif
8414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 * Get the value of an environment variable.
8417 * "arg" is pointing to the '$'. It is advanced to after the name.
8418 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008419 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 */
8421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008422get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 char_u *string = NULL;
8425 int len;
8426 int cc;
8427 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008428 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
8430 ++*arg;
8431 name = *arg;
8432 len = get_env_len(arg);
8433 if (evaluate)
8434 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008435 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008436 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008437
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008438 cc = name[len];
8439 name[len] = NUL;
8440 /* first try vim_getenv(), fast for normal environment vars */
8441 string = vim_getenv(name, &mustfree);
8442 if (string != NULL && *string != NUL)
8443 {
8444 if (!mustfree)
8445 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008447 else
8448 {
8449 if (mustfree)
8450 vim_free(string);
8451
8452 /* next try expanding things like $VIM and ${HOME} */
8453 string = expand_env_save(name - 1);
8454 if (string != NULL && *string == '$')
8455 {
8456 vim_free(string);
8457 string = NULL;
8458 }
8459 }
8460 name[len] = cc;
8461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->v_type = VAR_STRING;
8463 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 }
8465
8466 return OK;
8467}
8468
8469/*
8470 * Array with names and number of arguments of all internal functions
8471 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8472 */
8473static struct fst
8474{
8475 char *f_name; /* function name */
8476 char f_min_argc; /* minimal number of arguments */
8477 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008478 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008479 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480} functions[] =
8481{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008482#ifdef FEAT_FLOAT
8483 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008484 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008485#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008487 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"append", 2, 2, f_append},
8489 {"argc", 0, 0, f_argc},
8490 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008491 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008492 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008493#ifdef FEAT_FLOAT
8494 {"asin", 1, 1, f_asin}, /* WJMc */
8495#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008496 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008497 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008498 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008499 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008500 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008501 {"assert_notequal", 2, 3, f_assert_notequal},
8502 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008503 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008504#ifdef FEAT_FLOAT
8505 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008509 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"bufexists", 1, 1, f_bufexists},
8511 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8512 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8513 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8514 {"buflisted", 1, 1, f_buflisted},
8515 {"bufloaded", 1, 1, f_bufloaded},
8516 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008517 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008518 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"bufwinnr", 1, 1, f_bufwinnr},
8520 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008521 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008522 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008524#ifdef FEAT_FLOAT
8525 {"ceil", 1, 1, f_ceil},
8526#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008527#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008528 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008529 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8530 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008531 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008532 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008533 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008534 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008535 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008536 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008537 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008538 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008539 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8540 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008541 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008542 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008543#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008544 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008545 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008547 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008549#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008550 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008551 {"complete_add", 1, 1, f_complete_add},
8552 {"complete_check", 0, 0, f_complete_check},
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008555 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008556#ifdef FEAT_FLOAT
8557 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008558 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008559#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008560 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008562 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008564 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008566 {"diff_filler", 1, 1, f_diff_filler},
8567 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008568 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008570 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"eventhandler", 0, 0, f_eventhandler},
8572 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02008573 {"execute", 1, 2, f_execute},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008574 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008576#ifdef FEAT_FLOAT
8577 {"exp", 1, 1, f_exp},
8578#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008579 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008580 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008581 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8583 {"filereadable", 1, 1, f_filereadable},
8584 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008585 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008586 {"finddir", 1, 3, f_finddir},
8587 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#ifdef FEAT_FLOAT
8589 {"float2nr", 1, 1, f_float2nr},
8590 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008591 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008593 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"fnamemodify", 2, 2, f_fnamemodify},
8595 {"foldclosed", 1, 1, f_foldclosed},
8596 {"foldclosedend", 1, 1, f_foldclosedend},
8597 {"foldlevel", 1, 1, f_foldlevel},
8598 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008599 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008601 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008602 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008603 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008604 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008605 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {"getchar", 0, 1, f_getchar},
8607 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008608 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {"getcmdline", 0, 0, f_getcmdline},
8610 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008611 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008612 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02008613#if defined(FEAT_CMDL_COMPL)
8614 {"getcompletion", 2, 2, f_getcompletion},
8615#endif
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008616 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008617 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008618 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008619 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {"getfsize", 1, 1, f_getfsize},
8621 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008622 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008623 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008624 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008625 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008626 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008627 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008628 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008629 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008631 {"gettabvar", 2, 3, f_gettabvar},
8632 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {"getwinposx", 0, 0, f_getwinposx},
8634 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008635 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008636 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008637 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008638 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008641 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008642 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8644 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8645 {"histadd", 2, 2, f_histadd},
8646 {"histdel", 1, 2, f_histdel},
8647 {"histget", 1, 2, f_histget},
8648 {"histnr", 1, 1, f_histnr},
8649 {"hlID", 1, 1, f_hlID},
8650 {"hlexists", 1, 1, f_hlexists},
8651 {"hostname", 0, 0, f_hostname},
8652 {"iconv", 3, 3, f_iconv},
8653 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008654 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008655 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008657 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 {"inputrestore", 0, 0, f_inputrestore},
8659 {"inputsave", 0, 0, f_inputsave},
8660 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008662 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008664 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8666 {"isnan", 1, 1, f_isnan},
8667#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008668 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008669#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008670 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008671 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008672 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008673 {"job_start", 1, 2, f_job_start},
8674 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008675 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008676#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008677 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008678 {"js_decode", 1, 1, f_js_decode},
8679 {"js_encode", 1, 1, f_js_encode},
8680 {"json_decode", 1, 1, f_json_decode},
8681 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008682 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {"libcall", 3, 3, f_libcall},
8686 {"libcallnr", 3, 3, f_libcallnr},
8687 {"line", 1, 1, f_line},
8688 {"line2byte", 1, 1, f_line2byte},
8689 {"lispindent", 1, 1, f_lispindent},
8690 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008691#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008692 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693 {"log10", 1, 1, f_log10},
8694#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008695#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008696 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008697#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008698 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008699 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008700 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008702 {"matchadd", 2, 5, f_matchadd},
8703 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008704 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008705 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008706 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008707 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008708 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008709 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008710 {"max", 1, 1, f_max},
8711 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008712#ifdef vim_mkdir
8713 {"mkdir", 1, 3, f_mkdir},
8714#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008715 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008716#ifdef FEAT_MZSCHEME
8717 {"mzeval", 1, 1, f_mzeval},
8718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008720 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008721 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008722 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008723#ifdef FEAT_PERL
8724 {"perleval", 1, 1, f_perleval},
8725#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008726#ifdef FEAT_FLOAT
8727 {"pow", 2, 2, f_pow},
8728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008730 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008731 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008732#ifdef FEAT_PYTHON3
8733 {"py3eval", 1, 1, f_py3eval},
8734#endif
8735#ifdef FEAT_PYTHON
8736 {"pyeval", 1, 1, f_pyeval},
8737#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008738 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008739 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008740 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008741#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008742 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008743#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008744 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {"remote_expr", 2, 3, f_remote_expr},
8746 {"remote_foreground", 1, 1, f_remote_foreground},
8747 {"remote_peek", 1, 2, f_remote_peek},
8748 {"remote_read", 1, 1, f_remote_read},
8749 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008750 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008752 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008755#ifdef FEAT_FLOAT
8756 {"round", 1, 1, f_round},
8757#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008758 {"screenattr", 2, 2, f_screenattr},
8759 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008760 {"screencol", 0, 0, f_screencol},
8761 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008762 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008763 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008764 {"searchpair", 3, 7, f_searchpair},
8765 {"searchpairpos", 3, 7, f_searchpairpos},
8766 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 {"server2client", 2, 2, f_server2client},
8768 {"serverlist", 0, 0, f_serverlist},
8769 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008770 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008772 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008774 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008775 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008776 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008777 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008779 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008780 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008782#ifdef FEAT_CRYPT
8783 {"sha256", 1, 1, f_sha256},
8784#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008785 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008786 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008788#ifdef FEAT_FLOAT
8789 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008790 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008792 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008793 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008794 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008795 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008796 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008797#ifdef FEAT_FLOAT
8798 {"sqrt", 1, 1, f_sqrt},
8799 {"str2float", 1, 1, f_str2float},
8800#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008801 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008802 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008803 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008804 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805#ifdef HAVE_STRFTIME
8806 {"strftime", 1, 2, f_strftime},
8807#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008808 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008810 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 {"strlen", 1, 1, f_strlen},
8812 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008813 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008815 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008816 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {"substitute", 4, 4, f_substitute},
8818 {"synID", 3, 3, f_synID},
8819 {"synIDattr", 2, 3, f_synIDattr},
8820 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008821 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008822 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008823 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008824 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008825 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008826 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008827 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008828 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008829 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830#ifdef FEAT_FLOAT
8831 {"tan", 1, 1, f_tan},
8832 {"tanh", 1, 1, f_tanh},
8833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008835 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
Bram Moolenaar5c719942016-07-09 23:40:45 +02008836 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008837 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008838 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8839#ifdef FEAT_JOB_CHANNEL
8840 {"test_null_channel", 0, 0, f_test_null_channel},
8841#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008842 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008843#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008844 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008845#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008846 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008847 {"test_null_partial", 0, 0, f_test_null_partial},
8848 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008849 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008850#ifdef FEAT_TIMERS
8851 {"timer_start", 2, 3, f_timer_start},
8852 {"timer_stop", 1, 1, f_timer_stop},
8853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {"tolower", 1, 1, f_tolower},
8855 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008856 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008857#ifdef FEAT_FLOAT
8858 {"trunc", 1, 1, f_trunc},
8859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008861 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008862 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008863 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008864 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {"virtcol", 1, 1, f_virtcol},
8866 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008867 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008868 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008869 {"win_getid", 0, 2, f_win_getid},
8870 {"win_gotoid", 1, 1, f_win_gotoid},
8871 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8872 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {"winbufnr", 1, 1, f_winbufnr},
8874 {"wincol", 0, 0, f_wincol},
8875 {"winheight", 1, 1, f_winheight},
8876 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008877 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008879 {"winrestview", 1, 1, f_winrestview},
8880 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008882 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008883 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008884 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885};
8886
8887#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8888
8889/*
8890 * Function given to ExpandGeneric() to obtain the list of internal
8891 * or user defined function names.
8892 */
8893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 static int intidx = -1;
8897 char_u *name;
8898
8899 if (idx == 0)
8900 intidx = -1;
8901 if (intidx < 0)
8902 {
8903 name = get_user_func_name(xp, idx);
8904 if (name != NULL)
8905 return name;
8906 }
8907 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8908 {
8909 STRCPY(IObuff, functions[intidx].f_name);
8910 STRCAT(IObuff, "(");
8911 if (functions[intidx].f_max_argc == 0)
8912 STRCAT(IObuff, ")");
8913 return IObuff;
8914 }
8915
8916 return NULL;
8917}
8918
8919/*
8920 * Function given to ExpandGeneric() to obtain the list of internal or
8921 * user defined variable or function names.
8922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008924get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 static int intidx = -1;
8927 char_u *name;
8928
8929 if (idx == 0)
8930 intidx = -1;
8931 if (intidx < 0)
8932 {
8933 name = get_function_name(xp, idx);
8934 if (name != NULL)
8935 return name;
8936 }
8937 return get_user_var_name(xp, ++intidx);
8938}
8939
8940#endif /* FEAT_CMDL_COMPL */
8941
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008942#if defined(EBCDIC) || defined(PROTO)
8943/*
8944 * Compare struct fst by function name.
8945 */
8946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008947compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008948{
8949 struct fst *p1 = (struct fst *)s1;
8950 struct fst *p2 = (struct fst *)s2;
8951
8952 return STRCMP(p1->f_name, p2->f_name);
8953}
8954
8955/*
8956 * Sort the function table by function name.
8957 * The sorting of the table above is ASCII dependant.
8958 * On machines using EBCDIC we have to sort it.
8959 */
8960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008961sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008962{
8963 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8964
8965 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8966}
8967#endif
8968
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970/*
8971 * Find internal function in table above.
8972 * Return index, or -1 if not found
8973 */
8974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975find_internal_func(
8976 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 int first = 0;
8979 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8980 int cmp;
8981 int x;
8982
8983 /*
8984 * Find the function name in the table. Binary search.
8985 */
8986 while (first <= last)
8987 {
8988 x = first + ((unsigned)(last - first) >> 1);
8989 cmp = STRCMP(name, functions[x].f_name);
8990 if (cmp < 0)
8991 last = x - 1;
8992 else if (cmp > 0)
8993 first = x + 1;
8994 else
8995 return x;
8996 }
8997 return -1;
8998}
8999
9000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
9002 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01009003 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
9004 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 */
9006 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01009007deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008{
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 int cc;
9011
Bram Moolenaar65639032016-03-16 21:40:30 +01009012 if (partialp != NULL)
9013 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009014
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 cc = name[*lenp];
9016 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009017 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009018 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 {
9023 *lenp = 0;
9024 return (char_u *)""; /* just in case */
9025 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009026 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 }
9029
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009030 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9031 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009032 partial_T *pt = v->di_tv.vval.v_partial;
9033
9034 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 {
9036 *lenp = 0;
9037 return (char_u *)""; /* just in case */
9038 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009039 if (partialp != NULL)
9040 *partialp = pt;
9041 *lenp = (int)STRLEN(pt->pt_name);
9042 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 }
9044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009045 return name;
9046}
9047
9048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 * Allocate a variable for the result of a function.
9050 * Return OK or FAIL.
9051 */
9052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053get_func_tv(
9054 char_u *name, /* name of the function */
9055 int len, /* length of "name" */
9056 typval_T *rettv,
9057 char_u **arg, /* argument, pointing to the '(' */
9058 linenr_T firstline, /* first line of range */
9059 linenr_T lastline, /* last line of range */
9060 int *doesrange, /* return: function handled range */
9061 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009062 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 char_u *argp;
9066 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009067 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int argcount = 0; /* number of arguments found */
9069
9070 /*
9071 * Get the arguments.
9072 */
9073 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009074 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 {
9076 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9077 if (*argp == ')' || *argp == ',' || *argp == NUL)
9078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9080 {
9081 ret = FAIL;
9082 break;
9083 }
9084 ++argcount;
9085 if (*argp != ',')
9086 break;
9087 }
9088 if (*argp == ')')
9089 ++argp;
9090 else
9091 ret = FAIL;
9092
9093 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009094 {
9095 int i = 0;
9096
9097 if (get_vim_var_nr(VV_TESTING))
9098 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009099 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009100 * what variables are used on the call stack. */
9101 if (funcargs.ga_itemsize == 0)
9102 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9103 for (i = 0; i < argcount; ++i)
9104 if (ga_grow(&funcargs, 1) == OK)
9105 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9106 &argvars[i];
9107 }
9108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009110 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009111
9112 funcargs.ga_len -= i;
9113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 {
9116 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009117 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009119 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
9122 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
9125 *arg = skipwhite(argp);
9126 return ret;
9127}
9128
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009129#define ERROR_UNKNOWN 0
9130#define ERROR_TOOMANY 1
9131#define ERROR_TOOFEW 2
9132#define ERROR_SCRIPT 3
9133#define ERROR_DICT 4
9134#define ERROR_NONE 5
9135#define ERROR_OTHER 6
9136#define FLEN_FIXED 40
9137
9138/*
9139 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9140 * Change <SNR>123_name() to K_SNR 123_name().
9141 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9142 * (slow).
9143 */
9144 static char_u *
9145fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9146{
9147 int llen;
9148 char_u *fname;
9149 int i;
9150
9151 llen = eval_fname_script(name);
9152 if (llen > 0)
9153 {
9154 fname_buf[0] = K_SPECIAL;
9155 fname_buf[1] = KS_EXTRA;
9156 fname_buf[2] = (int)KE_SNR;
9157 i = 3;
9158 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9159 {
9160 if (current_SID <= 0)
9161 *error = ERROR_SCRIPT;
9162 else
9163 {
9164 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9165 i = (int)STRLEN(fname_buf);
9166 }
9167 }
9168 if (i + STRLEN(name + llen) < FLEN_FIXED)
9169 {
9170 STRCPY(fname_buf + i, name + llen);
9171 fname = fname_buf;
9172 }
9173 else
9174 {
9175 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9176 if (fname == NULL)
9177 *error = ERROR_OTHER;
9178 else
9179 {
9180 *tofree = fname;
9181 mch_memmove(fname, fname_buf, (size_t)i);
9182 STRCPY(fname + i, name + llen);
9183 }
9184 }
9185 }
9186 else
9187 fname = name;
9188 return fname;
9189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
9191/*
9192 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009193 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009194 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009197call_func(
9198 char_u *funcname, /* name of the function */
9199 int len, /* length of "name" */
9200 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009201 int argcount_in, /* number of "argvars" */
9202 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009203 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204 linenr_T firstline, /* first line of range */
9205 linenr_T lastline, /* last line of range */
9206 int *doesrange, /* return: function handled range */
9207 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009208 partial_T *partial, /* optional, can be NULL */
9209 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 int error = ERROR_NONE;
9213 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009216 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009218 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009219 int argcount = argcount_in;
9220 typval_T *argvars = argvars_in;
9221 dict_T *selfdict = selfdict_in;
9222 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9223 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009224
9225 /* Make a copy of the name, if it comes from a funcref variable it could
9226 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009227 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009228 if (name == NULL)
9229 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009231 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 *doesrange = FALSE;
9234
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 if (partial != NULL)
9236 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009237 /* When the function has a partial with a dict and there is a dict
9238 * argument, use the dict argument. That is backwards compatible.
9239 * When the dict was bound explicitly use the one from the partial. */
9240 if (partial->pt_dict != NULL
9241 && (selfdict_in == NULL || !partial->pt_auto))
9242 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009243 if (error == ERROR_NONE && partial->pt_argc > 0)
9244 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009245 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9246 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9247 for (i = 0; i < argcount_in; ++i)
9248 argv[i + argv_clear] = argvars_in[i];
9249 argvars = argv;
9250 argcount = partial->pt_argc + argcount_in;
9251 }
9252 }
9253
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254
9255 /* execute the function if no errors detected and executing */
9256 if (evaluate && error == ERROR_NONE)
9257 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009258 char_u *rfname = fname;
9259
9260 /* Ignore "g:" before a function name. */
9261 if (fname[0] == 'g' && fname[1] == ':')
9262 rfname = fname + 2;
9263
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009264 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9265 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 error = ERROR_UNKNOWN;
9267
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009268 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 {
9270 /*
9271 * User defined function.
9272 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009273 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009276 /* Trigger FuncUndefined event, may load the function. */
9277 if (fp == NULL
9278 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009279 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009282 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009283 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 }
9285#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009286 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009287 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009288 {
9289 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009290 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009291 }
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (fp != NULL)
9294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009295 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009297 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009299 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009301 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009302 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 else
9304 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009305 int did_save_redo = FALSE;
9306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 /*
9308 * Call the user function.
9309 * Save and restore search patterns, script variables and
9310 * redo buffer.
9311 */
9312 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009313#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009314 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009315#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009316 {
9317 saveRedobuff();
9318 did_save_redo = TRUE;
9319 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009320 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009322 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009323 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9324 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9325 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009326 /* Function was unreferenced while being used, free it
9327 * now. */
9328 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009329 if (did_save_redo)
9330 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 restore_search_patterns();
9332 error = ERROR_NONE;
9333 }
9334 }
9335 }
9336 else
9337 {
9338 /*
9339 * Find the function name in the table, call its implementation.
9340 */
9341 i = find_internal_func(fname);
9342 if (i >= 0)
9343 {
9344 if (argcount < functions[i].f_min_argc)
9345 error = ERROR_TOOFEW;
9346 else if (argcount > functions[i].f_max_argc)
9347 error = ERROR_TOOMANY;
9348 else
9349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009350 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 error = ERROR_NONE;
9353 }
9354 }
9355 }
9356 /*
9357 * The function call (or "FuncUndefined" autocommand sequence) might
9358 * have been aborted by an error, an interrupt, or an explicitly thrown
9359 * exception that has not been caught so far. This situation can be
9360 * tested for by calling aborting(). For an error in an internal
9361 * function or for the "E132" error in call_user_func(), however, the
9362 * throw point at which the "force_abort" flag (temporarily reset by
9363 * emsg()) is normally updated has not been reached yet. We need to
9364 * update that flag first to make aborting() reliable.
9365 */
9366 update_force_abort();
9367 }
9368 if (error == ERROR_NONE)
9369 ret = OK;
9370
9371 /*
9372 * Report an error unless the argument evaluation or function call has been
9373 * cancelled due to an aborting error, an interrupt, or an exception.
9374 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009375 if (!aborting())
9376 {
9377 switch (error)
9378 {
9379 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009380 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009381 break;
9382 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009383 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009384 break;
9385 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009387 name);
9388 break;
9389 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009390 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009391 name);
9392 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009393 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009394 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 name);
9396 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009397 }
9398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009400 while (argv_clear > 0)
9401 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009402 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009403 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
9405 return ret;
9406}
9407
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009408/*
9409 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009410 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009411 */
9412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009413emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009414{
9415 char_u *p;
9416
9417 if (*name == K_SPECIAL)
9418 p = concat_str((char_u *)"<SNR>", name + 3);
9419 else
9420 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009421 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009422 if (p != name)
9423 vim_free(p);
9424}
9425
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009426/*
9427 * Return TRUE for a non-zero Number and a non-empty String.
9428 */
9429 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009431{
9432 return ((argvars[0].v_type == VAR_NUMBER
9433 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009434 || (argvars[0].v_type == VAR_SPECIAL
9435 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009436 || (argvars[0].v_type == VAR_STRING
9437 && argvars[0].vval.v_string != NULL
9438 && *argvars[0].vval.v_string != NUL));
9439}
9440
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441/*********************************************
9442 * Implementation of the built-in functions
9443 */
9444
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009445#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009446static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009447
9448/*
9449 * Get the float value of "argvars[0]" into "f".
9450 * Returns FAIL when the argument is not a Number or Float.
9451 */
9452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009454{
9455 if (argvars[0].v_type == VAR_FLOAT)
9456 {
9457 *f = argvars[0].vval.v_float;
9458 return OK;
9459 }
9460 if (argvars[0].v_type == VAR_NUMBER)
9461 {
9462 *f = (float_T)argvars[0].vval.v_number;
9463 return OK;
9464 }
9465 EMSG(_("E808: Number or Float required"));
9466 return FAIL;
9467}
9468
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009469/*
9470 * "abs(expr)" function
9471 */
9472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009473f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009474{
9475 if (argvars[0].v_type == VAR_FLOAT)
9476 {
9477 rettv->v_type = VAR_FLOAT;
9478 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9479 }
9480 else
9481 {
9482 varnumber_T n;
9483 int error = FALSE;
9484
9485 n = get_tv_number_chk(&argvars[0], &error);
9486 if (error)
9487 rettv->vval.v_number = -1;
9488 else if (n > 0)
9489 rettv->vval.v_number = n;
9490 else
9491 rettv->vval.v_number = -n;
9492 }
9493}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494
9495/*
9496 * "acos()" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009501 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = acos(f);
9506 else
9507 rettv->vval.v_float = 0.0;
9508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509#endif
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009512 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 */
9514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009515f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009522 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009523 && !tv_check_lock(l->lv_lock,
9524 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009525 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527 }
9528 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529 EMSG(_(e_listreq));
9530}
9531
9532/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009533 * "and(expr, expr)" function
9534 */
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009537{
9538 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9539 & get_tv_number_chk(&argvars[1], NULL);
9540}
9541
9542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009543 * "append(lnum, string/list)" function
9544 */
9545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009547{
9548 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 list_T *l = NULL;
9551 listitem_T *li = NULL;
9552 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553 long added = 0;
9554
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009555 /* When coming here from Insert mode, sync undo, so that this can be
9556 * undone separately from what was previously inserted. */
9557 if (u_sync_once == 2)
9558 {
9559 u_sync_once = 1; /* notify that u_sync() was called */
9560 u_sync(TRUE);
9561 }
9562
Bram Moolenaar0d660222005-01-07 21:51:51 +00009563 lnum = get_tv_lnum(argvars);
9564 if (lnum >= 0
9565 && lnum <= curbuf->b_ml.ml_line_count
9566 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570 l = argvars[1].vval.v_list;
9571 if (l == NULL)
9572 return;
9573 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009575 for (;;)
9576 {
9577 if (l == NULL)
9578 tv = &argvars[1]; /* append a string */
9579 else if (li == NULL)
9580 break; /* end of list */
9581 else
9582 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 line = get_tv_string_chk(tv);
9584 if (line == NULL) /* type error */
9585 {
9586 rettv->vval.v_number = 1; /* Failed */
9587 break;
9588 }
9589 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009590 ++added;
9591 if (l == NULL)
9592 break;
9593 li = li->li_next;
9594 }
9595
9596 appended_lines_mark(lnum, added);
9597 if (curwin->w_cursor.lnum > lnum)
9598 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 else
9601 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602}
9603
9604/*
9605 * "argc()" function
9606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
9613/*
9614 * "argidx()" function
9615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009623 * "arglistid()" function
9624 */
9625 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009626f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009627{
9628 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009629
9630 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009631 wp = find_tabwin(&argvars[0], &argvars[1]);
9632 if (wp != NULL)
9633 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009634}
9635
9636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * "argv(nr)" function
9638 */
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 int idx;
9643
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009644 if (argvars[0].v_type != VAR_UNKNOWN)
9645 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009646 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009647 if (idx >= 0 && idx < ARGCOUNT)
9648 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9649 else
9650 rettv->vval.v_string = NULL;
9651 rettv->v_type = VAR_STRING;
9652 }
9653 else if (rettv_list_alloc(rettv) == OK)
9654 for (idx = 0; idx < ARGCOUNT; ++idx)
9655 list_append_string(rettv->vval.v_list,
9656 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009659typedef enum
9660{
9661 ASSERT_EQUAL,
9662 ASSERT_NOTEQUAL,
9663 ASSERT_MATCH,
9664 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009665 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009666} assert_type_T;
9667
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009668static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669static 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 +01009670static void assert_error(garray_T *gap);
9671static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009672
9673/*
9674 * Prepare "gap" for an assert error and add the sourcing position.
9675 */
9676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009677prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009678{
9679 char buf[NUMBUFLEN];
9680
9681 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009682 if (sourcing_name != NULL)
9683 {
9684 ga_concat(gap, sourcing_name);
9685 if (sourcing_lnum > 0)
9686 ga_concat(gap, (char_u *)" ");
9687 }
9688 if (sourcing_lnum > 0)
9689 {
9690 sprintf(buf, "line %ld", (long)sourcing_lnum);
9691 ga_concat(gap, (char_u *)buf);
9692 }
9693 if (sourcing_name != NULL || sourcing_lnum > 0)
9694 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009695}
9696
9697/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009698 * Append "str" to "gap", escaping unprintable characters.
9699 * Changes NL to \n, CR to \r, etc.
9700 */
9701 static void
9702ga_concat_esc(garray_T *gap, char_u *str)
9703{
9704 char_u *p;
9705 char_u buf[NUMBUFLEN];
9706
Bram Moolenaarf1551962016-03-15 12:55:58 +01009707 if (str == NULL)
9708 {
9709 ga_concat(gap, (char_u *)"NULL");
9710 return;
9711 }
9712
Bram Moolenaar23689172016-02-15 22:37:37 +01009713 for (p = str; *p != NUL; ++p)
9714 switch (*p)
9715 {
9716 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9717 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9718 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9719 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9720 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9721 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9722 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9723 default:
9724 if (*p < ' ')
9725 {
9726 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9727 ga_concat(gap, buf);
9728 }
9729 else
9730 ga_append(gap, *p);
9731 break;
9732 }
9733}
9734
9735/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009736 * Fill "gap" with information about an assert error.
9737 */
9738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009739fill_assert_error(
9740 garray_T *gap,
9741 typval_T *opt_msg_tv,
9742 char_u *exp_str,
9743 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009744 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009745 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009746{
9747 char_u numbuf[NUMBUFLEN];
9748 char_u *tofree;
9749
9750 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9751 {
9752 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9753 vim_free(tofree);
9754 }
9755 else
9756 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009757 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009758 ga_concat(gap, (char_u *)"Pattern ");
9759 else
9760 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009761 if (exp_str == NULL)
9762 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009763 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009764 vim_free(tofree);
9765 }
9766 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009767 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009768 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009769 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009770 else if (atype == ASSERT_NOTMATCH)
9771 ga_concat(gap, (char_u *)" does match ");
9772 else if (atype == ASSERT_NOTEQUAL)
9773 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009774 else
9775 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009776 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009777 vim_free(tofree);
9778 }
9779}
Bram Moolenaar43345542015-11-29 17:35:35 +01009780
9781/*
9782 * Add an assert error to v:errors.
9783 */
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009786{
9787 struct vimvar *vp = &vimvars[VV_ERRORS];
9788
9789 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9790 /* Make sure v:errors is a list. */
9791 set_vim_var_list(VV_ERRORS, list_alloc());
9792 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9793}
9794
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009795 static void
9796assert_equal_common(typval_T *argvars, assert_type_T atype)
9797{
9798 garray_T ga;
9799
9800 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9801 != (atype == ASSERT_EQUAL))
9802 {
9803 prepare_assert_error(&ga);
9804 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9805 atype);
9806 assert_error(&ga);
9807 ga_clear(&ga);
9808 }
9809}
9810
Bram Moolenaar43345542015-11-29 17:35:35 +01009811/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009812 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009813 */
9814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009816{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009817 assert_equal_common(argvars, ASSERT_EQUAL);
9818}
Bram Moolenaar43345542015-11-29 17:35:35 +01009819
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009820/*
9821 * "assert_notequal(expected, actual[, msg])" function
9822 */
9823 static void
9824f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9825{
9826 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009827}
9828
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009829/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009830 * "assert_exception(string[, msg])" function
9831 */
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009834{
9835 garray_T ga;
9836 char *error;
9837
9838 error = (char *)get_tv_string_chk(&argvars[0]);
9839 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9840 {
9841 prepare_assert_error(&ga);
9842 ga_concat(&ga, (char_u *)"v:exception is not set");
9843 assert_error(&ga);
9844 ga_clear(&ga);
9845 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009846 else if (error != NULL
9847 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009848 {
9849 prepare_assert_error(&ga);
9850 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009851 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009852 assert_error(&ga);
9853 ga_clear(&ga);
9854 }
9855}
9856
9857/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009858 * "assert_fails(cmd [, error])" function
9859 */
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009862{
9863 char_u *cmd = get_tv_string_chk(&argvars[0]);
9864 garray_T ga;
9865
9866 called_emsg = FALSE;
9867 suppress_errthrow = TRUE;
9868 emsg_silent = TRUE;
9869 do_cmdline_cmd(cmd);
9870 if (!called_emsg)
9871 {
9872 prepare_assert_error(&ga);
9873 ga_concat(&ga, (char_u *)"command did not fail: ");
9874 ga_concat(&ga, cmd);
9875 assert_error(&ga);
9876 ga_clear(&ga);
9877 }
9878 else if (argvars[1].v_type != VAR_UNKNOWN)
9879 {
9880 char_u buf[NUMBUFLEN];
9881 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9882
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009883 if (error == NULL
9884 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009885 {
9886 prepare_assert_error(&ga);
9887 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009888 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009889 assert_error(&ga);
9890 ga_clear(&ga);
9891 }
9892 }
9893
9894 called_emsg = FALSE;
9895 suppress_errthrow = FALSE;
9896 emsg_silent = FALSE;
9897 emsg_on_display = FALSE;
9898 set_vim_var_string(VV_ERRMSG, NULL, 0);
9899}
9900
9901/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009902 * Common for assert_true() and assert_false().
9903 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009905assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009906{
9907 int error = FALSE;
9908 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009909
Bram Moolenaar37127922016-02-06 20:29:28 +01009910 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009911 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009912 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009913 if (argvars[0].v_type != VAR_NUMBER
9914 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9915 || error)
9916 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009917 prepare_assert_error(&ga);
9918 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009919 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009920 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009921 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009922 ga_clear(&ga);
9923 }
9924}
9925
9926/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009927 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009928 */
9929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009930f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009931{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009932 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009933}
9934
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009935 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009936assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009937{
9938 garray_T ga;
9939 char_u buf1[NUMBUFLEN];
9940 char_u buf2[NUMBUFLEN];
9941 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9942 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9943
Bram Moolenaar72188e92016-03-28 22:48:29 +02009944 if (pat == NULL || text == NULL)
9945 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009946 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009947 {
9948 prepare_assert_error(&ga);
9949 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009950 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009951 assert_error(&ga);
9952 ga_clear(&ga);
9953 }
9954}
9955
9956/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009957 * "assert_match(pattern, actual[, msg])" function
9958 */
9959 static void
9960f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9961{
9962 assert_match_common(argvars, ASSERT_MATCH);
9963}
9964
9965/*
9966 * "assert_notmatch(pattern, actual[, msg])" function
9967 */
9968 static void
9969f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9970{
9971 assert_match_common(argvars, ASSERT_NOTMATCH);
9972}
9973
9974/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009975 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009976 */
9977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009978f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009979{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009980 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009981}
9982
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009983#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009984/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009985 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009986 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009988f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009990 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009991
9992 rettv->v_type = VAR_FLOAT;
9993 if (get_float_arg(argvars, &f) == OK)
9994 rettv->vval.v_float = asin(f);
9995 else
9996 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009997}
9998
9999/*
10000 * "atan()" function
10001 */
10002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +010010005 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010006
10007 rettv->v_type = VAR_FLOAT;
10008 if (get_float_arg(argvars, &f) == OK)
10009 rettv->vval.v_float = atan(f);
10010 else
10011 rettv->vval.v_float = 0.0;
10012}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010013
10014/*
10015 * "atan2()" function
10016 */
10017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010019{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010020 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010021
10022 rettv->v_type = VAR_FLOAT;
10023 if (get_float_arg(argvars, &fx) == OK
10024 && get_float_arg(&argvars[1], &fy) == OK)
10025 rettv->vval.v_float = atan2(fx, fy);
10026 else
10027 rettv->vval.v_float = 0.0;
10028}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010029#endif
10030
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031/*
10032 * "browse(save, title, initdir, default)" function
10033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010035f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037#ifdef FEAT_BROWSE
10038 int save;
10039 char_u *title;
10040 char_u *initdir;
10041 char_u *defname;
10042 char_u buf[NUMBUFLEN];
10043 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010046 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010047 title = get_tv_string_chk(&argvars[1]);
10048 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10049 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010051 if (error || title == NULL || initdir == NULL || defname == NULL)
10052 rettv->vval.v_string = NULL;
10053 else
10054 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010055 do_browse(save ? BROWSE_SAVE : 0,
10056 title, defname, NULL, initdir, NULL, curbuf);
10057#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010059#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010061}
10062
10063/*
10064 * "browsedir(title, initdir)" function
10065 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010067f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010068{
10069#ifdef FEAT_BROWSE
10070 char_u *title;
10071 char_u *initdir;
10072 char_u buf[NUMBUFLEN];
10073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 title = get_tv_string_chk(&argvars[0]);
10075 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 if (title == NULL || initdir == NULL)
10078 rettv->vval.v_string = NULL;
10079 else
10080 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010081 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010088static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010089
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090/*
10091 * Find a buffer by number or exact name.
10092 */
10093 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010094find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
10096 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010098 if (avar->v_type == VAR_NUMBER)
10099 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010100 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010102 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010103 if (buf == NULL)
10104 {
10105 /* No full path name match, try a match with a URL or a "nofile"
10106 * buffer, these don't use the full path. */
10107 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10108 if (buf->b_fname != NULL
10109 && (path_with_url(buf->b_fname)
10110#ifdef FEAT_QUICKFIX
10111 || bt_nofile(buf)
10112#endif
10113 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010114 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010115 break;
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 }
10118 return buf;
10119}
10120
10121/*
10122 * "bufexists(expr)" function
10123 */
10124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010125f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128}
10129
10130/*
10131 * "buflisted(expr)" function
10132 */
10133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010134f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136 buf_T *buf;
10137
10138 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
10141
10142/*
10143 * "bufloaded(expr)" function
10144 */
10145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010146f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
10148 buf_T *buf;
10149
10150 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010154 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010155buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 int save_magic;
10158 char_u *save_cpo;
10159 buf_T *buf;
10160
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10162 save_magic = p_magic;
10163 p_magic = TRUE;
10164 save_cpo = p_cpo;
10165 p_cpo = (char_u *)"";
10166
10167 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010168 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169
10170 p_magic = save_magic;
10171 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010172 return buf;
10173}
10174
10175/*
10176 * Get buffer by number or pattern.
10177 */
10178 static buf_T *
10179get_buf_tv(typval_T *tv, int curtab_only)
10180{
10181 char_u *name = tv->vval.v_string;
10182 buf_T *buf;
10183
10184 if (tv->v_type == VAR_NUMBER)
10185 return buflist_findnr((int)tv->vval.v_number);
10186 if (tv->v_type != VAR_STRING)
10187 return NULL;
10188 if (name == NULL || *name == NUL)
10189 return curbuf;
10190 if (name[0] == '$' && name[1] == NUL)
10191 return lastbuf;
10192
10193 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
10195 /* If not found, try expanding the name, like done for bufexists(). */
10196 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
10199 return buf;
10200}
10201
10202/*
10203 * "bufname(expr)" function
10204 */
10205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207{
10208 buf_T *buf;
10209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010212 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 --emsg_off;
10219}
10220
10221/*
10222 * "bufnr(expr)" function
10223 */
10224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010225f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010228 int error = FALSE;
10229 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010233 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010234 --emsg_off;
10235
10236 /* If the buffer isn't found and the second argument is not zero create a
10237 * new buffer. */
10238 if (buf == NULL
10239 && argvars[1].v_type != VAR_UNKNOWN
10240 && get_tv_number_chk(&argvars[1], &error) != 0
10241 && !error
10242 && (name = get_tv_string_chk(&argvars[0])) != NULL
10243 && !error)
10244 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250}
10251
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010253buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255#ifdef FEAT_WINDOWS
10256 win_T *wp;
10257 int winnr = 0;
10258#endif
10259 buf_T *buf;
10260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010263 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#ifdef FEAT_WINDOWS
10265 for (wp = firstwin; wp; wp = wp->w_next)
10266 {
10267 ++winnr;
10268 if (wp->w_buffer == buf)
10269 break;
10270 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010271 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010273 rettv->vval.v_number = (curwin->w_buffer == buf
10274 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#endif
10276 --emsg_off;
10277}
10278
10279/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010280 * "bufwinid(nr)" function
10281 */
10282 static void
10283f_bufwinid(typval_T *argvars, typval_T *rettv)
10284{
10285 buf_win_common(argvars, rettv, FALSE);
10286}
10287
10288/*
10289 * "bufwinnr(nr)" function
10290 */
10291 static void
10292f_bufwinnr(typval_T *argvars, typval_T *rettv)
10293{
10294 buf_win_common(argvars, rettv, TRUE);
10295}
10296
10297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 * "byte2line(byte)" function
10299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
10303#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#else
10306 long boff = 0;
10307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 (linenr_T)0, &boff);
10314#endif
10315}
10316
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010318byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010319{
10320#ifdef FEAT_MBYTE
10321 char_u *t;
10322#endif
10323 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010324 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 str = get_tv_string_chk(&argvars[0]);
10327 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010330 return;
10331
10332#ifdef FEAT_MBYTE
10333 t = str;
10334 for ( ; idx > 0; idx--)
10335 {
10336 if (*t == NUL) /* EOL reached */
10337 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010338 if (enc_utf8 && comp)
10339 t += utf_ptr2len(t);
10340 else
10341 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010342 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010343 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010344#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010345 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010347#endif
10348}
10349
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010350/*
10351 * "byteidx()" function
10352 */
10353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010354f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010355{
10356 byteidx(argvars, rettv, FALSE);
10357}
10358
10359/*
10360 * "byteidxcomp()" function
10361 */
10362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010364{
10365 byteidx(argvars, rettv, TRUE);
10366}
10367
Bram Moolenaardb913952012-06-29 12:54:53 +020010368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010369func_call(
10370 char_u *name,
10371 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010372 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010373 dict_T *selfdict,
10374 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010375{
10376 listitem_T *item;
10377 typval_T argv[MAX_FUNC_ARGS + 1];
10378 int argc = 0;
10379 int dummy;
10380 int r = 0;
10381
10382 for (item = args->vval.v_list->lv_first; item != NULL;
10383 item = item->li_next)
10384 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010385 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010386 {
10387 EMSG(_("E699: Too many arguments"));
10388 break;
10389 }
10390 /* Make a copy of each argument. This is needed to be able to set
10391 * v_lock to VAR_FIXED in the copy without changing the original list.
10392 */
10393 copy_tv(&item->li_tv, &argv[argc++]);
10394 }
10395
10396 if (item == NULL)
10397 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10398 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010399 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010400
10401 /* Free the arguments. */
10402 while (argc > 0)
10403 clear_tv(&argv[--argc]);
10404
10405 return r;
10406}
10407
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010408/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010409 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 */
10411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010412f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010413{
10414 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010415 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010416 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010417
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418 if (argvars[1].v_type != VAR_LIST)
10419 {
10420 EMSG(_(e_listreq));
10421 return;
10422 }
10423 if (argvars[1].vval.v_list == NULL)
10424 return;
10425
10426 if (argvars[0].v_type == VAR_FUNC)
10427 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010428 else if (argvars[0].v_type == VAR_PARTIAL)
10429 {
10430 partial = argvars[0].vval.v_partial;
10431 func = partial->pt_name;
10432 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010433 else
10434 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 if (*func == NUL)
10436 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010437
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 if (argvars[2].v_type != VAR_UNKNOWN)
10439 {
10440 if (argvars[2].v_type != VAR_DICT)
10441 {
10442 EMSG(_(e_dictreq));
10443 return;
10444 }
10445 selfdict = argvars[2].vval.v_dict;
10446 }
10447
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010448 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010449}
10450
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451#ifdef FEAT_FLOAT
10452/*
10453 * "ceil({float})" function
10454 */
10455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010456f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010458 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010459
10460 rettv->v_type = VAR_FLOAT;
10461 if (get_float_arg(argvars, &f) == OK)
10462 rettv->vval.v_float = ceil(f);
10463 else
10464 rettv->vval.v_float = 0.0;
10465}
10466#endif
10467
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010468#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010469/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010470 * "ch_close()" function
10471 */
10472 static void
10473f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10474{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010475 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010476
10477 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010478 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010479 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010480 channel_clear(channel);
10481 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010482}
10483
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010484/*
10485 * "ch_getbufnr()" function
10486 */
10487 static void
10488f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10489{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010490 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010491
10492 rettv->vval.v_number = -1;
10493 if (channel != NULL)
10494 {
10495 char_u *what = get_tv_string(&argvars[1]);
10496 int part;
10497
10498 if (STRCMP(what, "err") == 0)
10499 part = PART_ERR;
10500 else if (STRCMP(what, "out") == 0)
10501 part = PART_OUT;
10502 else if (STRCMP(what, "in") == 0)
10503 part = PART_IN;
10504 else
10505 part = PART_SOCK;
10506 if (channel->ch_part[part].ch_buffer != NULL)
10507 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10508 }
10509}
10510
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010511/*
10512 * "ch_getjob()" function
10513 */
10514 static void
10515f_ch_getjob(typval_T *argvars, typval_T *rettv)
10516{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010517 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010518
10519 if (channel != NULL)
10520 {
10521 rettv->v_type = VAR_JOB;
10522 rettv->vval.v_job = channel->ch_job;
10523 if (channel->ch_job != NULL)
10524 ++channel->ch_job->jv_refcount;
10525 }
10526}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010527
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010528/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010529 * "ch_info()" function
10530 */
10531 static void
10532f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10533{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010534 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010535
10536 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10537 channel_info(channel, rettv->vval.v_dict);
10538}
10539
10540/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010541 * "ch_log()" function
10542 */
10543 static void
10544f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10545{
10546 char_u *msg = get_tv_string(&argvars[0]);
10547 channel_T *channel = NULL;
10548
10549 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010550 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010551
10552 ch_log(channel, (char *)msg);
10553}
10554
10555/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010556 * "ch_logfile()" function
10557 */
10558 static void
10559f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10560{
10561 char_u *fname;
10562 char_u *opt = (char_u *)"";
10563 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010564
10565 fname = get_tv_string(&argvars[0]);
10566 if (argvars[1].v_type == VAR_STRING)
10567 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010568 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010569}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010570
10571/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010572 * "ch_open()" function
10573 */
10574 static void
10575f_ch_open(typval_T *argvars, typval_T *rettv)
10576{
Bram Moolenaar77073442016-02-13 23:23:53 +010010577 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010578 if (check_restricted() || check_secure())
10579 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010580 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010581}
10582
10583/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010584 * "ch_read()" function
10585 */
10586 static void
10587f_ch_read(typval_T *argvars, typval_T *rettv)
10588{
10589 common_channel_read(argvars, rettv, FALSE);
10590}
10591
10592/*
10593 * "ch_readraw()" function
10594 */
10595 static void
10596f_ch_readraw(typval_T *argvars, typval_T *rettv)
10597{
10598 common_channel_read(argvars, rettv, TRUE);
10599}
10600
10601/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010602 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010603 */
10604 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010605f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10606{
10607 ch_expr_common(argvars, rettv, TRUE);
10608}
10609
10610/*
10611 * "ch_sendexpr()" function
10612 */
10613 static void
10614f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10615{
10616 ch_expr_common(argvars, rettv, FALSE);
10617}
10618
10619/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010620 * "ch_evalraw()" function
10621 */
10622 static void
10623f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10624{
10625 ch_raw_common(argvars, rettv, TRUE);
10626}
10627
10628/*
10629 * "ch_sendraw()" function
10630 */
10631 static void
10632f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10633{
10634 ch_raw_common(argvars, rettv, FALSE);
10635}
10636
10637/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010638 * "ch_setoptions()" function
10639 */
10640 static void
10641f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10642{
10643 channel_T *channel;
10644 jobopt_T opt;
10645
Bram Moolenaar437905c2016-04-26 19:01:05 +020010646 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010647 if (channel == NULL)
10648 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010649 clear_job_options(&opt);
10650 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010651 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10652 channel_set_options(channel, &opt);
10653 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010654}
10655
10656/*
10657 * "ch_status()" function
10658 */
10659 static void
10660f_ch_status(typval_T *argvars, typval_T *rettv)
10661{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010662 channel_T *channel;
10663
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010664 /* return an empty string by default */
10665 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010666 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010667
Bram Moolenaar437905c2016-04-26 19:01:05 +020010668 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010669 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010670}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010671#endif
10672
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010673/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010674 * "changenr()" function
10675 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010677f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010678{
10679 rettv->vval.v_number = curbuf->b_u_seq_cur;
10680}
10681
10682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 * "char2nr(string)" function
10684 */
10685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688#ifdef FEAT_MBYTE
10689 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010690 {
10691 int utf8 = 0;
10692
10693 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010694 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010695
10696 if (utf8)
10697 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10698 else
10699 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 else
10702#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010703 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704}
10705
10706/*
10707 * "cindent(lnum)" function
10708 */
10709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712#ifdef FEAT_CINDENT
10713 pos_T pos;
10714 linenr_T lnum;
10715
10716 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010717 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10719 {
10720 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 curwin->w_cursor = pos;
10723 }
10724 else
10725#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010726 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727}
10728
10729/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010730 * "clearmatches()" function
10731 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010734{
10735#ifdef FEAT_SEARCH_EXTRA
10736 clear_matches(curwin);
10737#endif
10738}
10739
10740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 * "col(string)" function
10742 */
10743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010744f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745{
10746 colnr_T col = 0;
10747 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010748 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010750 fp = var2fpos(&argvars[0], FALSE, &fnum);
10751 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 {
10753 if (fp->col == MAXCOL)
10754 {
10755 /* '> can be MAXCOL, get the length of the line then */
10756 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010757 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 else
10759 col = MAXCOL;
10760 }
10761 else
10762 {
10763 col = fp->col + 1;
10764#ifdef FEAT_VIRTUALEDIT
10765 /* col(".") when the cursor is on the NUL at the end of the line
10766 * because of "coladd" can be seen as an extra column. */
10767 if (virtual_active() && fp == &curwin->w_cursor)
10768 {
10769 char_u *p = ml_get_cursor();
10770
10771 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10772 curwin->w_virtcol - curwin->w_cursor.coladd))
10773 {
10774# ifdef FEAT_MBYTE
10775 int l;
10776
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010777 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 col += l;
10779# else
10780 if (*p != NUL && p[1] == NUL)
10781 ++col;
10782# endif
10783 }
10784 }
10785#endif
10786 }
10787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789}
10790
Bram Moolenaar572cb562005-08-05 21:35:02 +000010791#if defined(FEAT_INS_EXPAND)
10792/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010793 * "complete()" function
10794 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010796f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010797{
10798 int startcol;
10799
10800 if ((State & INSERT) == 0)
10801 {
10802 EMSG(_("E785: complete() can only be used in Insert mode"));
10803 return;
10804 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010805
10806 /* Check for undo allowed here, because if something was already inserted
10807 * the line was already saved for undo and this check isn't done. */
10808 if (!undo_allowed())
10809 return;
10810
Bram Moolenaarade00832006-03-10 21:46:58 +000010811 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10812 {
10813 EMSG(_(e_invarg));
10814 return;
10815 }
10816
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010817 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010818 if (startcol <= 0)
10819 return;
10820
10821 set_completion(startcol - 1, argvars[1].vval.v_list);
10822}
10823
10824/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010825 * "complete_add()" function
10826 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010828f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010829{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010830 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010831}
10832
10833/*
10834 * "complete_check()" function
10835 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010837f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010838{
10839 int saved = RedrawingDisabled;
10840
10841 RedrawingDisabled = 0;
10842 ins_compl_check_keys(0);
10843 rettv->vval.v_number = compl_interrupted;
10844 RedrawingDisabled = saved;
10845}
10846#endif
10847
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848/*
10849 * "confirm(message, buttons[, default [, type]])" function
10850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010852f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853{
10854#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10855 char_u *message;
10856 char_u *buttons = NULL;
10857 char_u buf[NUMBUFLEN];
10858 char_u buf2[NUMBUFLEN];
10859 int def = 1;
10860 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 char_u *typestr;
10862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010864 message = get_tv_string_chk(&argvars[0]);
10865 if (message == NULL)
10866 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010867 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10870 if (buttons == NULL)
10871 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010872 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010874 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10878 if (typestr == NULL)
10879 error = TRUE;
10880 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 switch (TOUPPER_ASC(*typestr))
10883 {
10884 case 'E': type = VIM_ERROR; break;
10885 case 'Q': type = VIM_QUESTION; break;
10886 case 'I': type = VIM_INFO; break;
10887 case 'W': type = VIM_WARNING; break;
10888 case 'G': type = VIM_GENERIC; break;
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 }
10891 }
10892 }
10893 }
10894
10895 if (buttons == NULL || *buttons == NUL)
10896 buttons = (char_u *)_("&Ok");
10897
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010898 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010900 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901#endif
10902}
10903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010904/*
10905 * "copy()" function
10906 */
10907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010908f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010909{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010910 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010911}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010913#ifdef FEAT_FLOAT
10914/*
10915 * "cos()" function
10916 */
10917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010918f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010919{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010920 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010921
10922 rettv->v_type = VAR_FLOAT;
10923 if (get_float_arg(argvars, &f) == OK)
10924 rettv->vval.v_float = cos(f);
10925 else
10926 rettv->vval.v_float = 0.0;
10927}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010928
10929/*
10930 * "cosh()" function
10931 */
10932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010933f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010934{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010935 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010936
10937 rettv->v_type = VAR_FLOAT;
10938 if (get_float_arg(argvars, &f) == OK)
10939 rettv->vval.v_float = cosh(f);
10940 else
10941 rettv->vval.v_float = 0.0;
10942}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010943#endif
10944
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010946 * "count()" function
10947 */
10948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010949f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010950{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010951 long n = 0;
10952 int ic = FALSE;
10953
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010955 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010956 listitem_T *li;
10957 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010959
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 if ((l = argvars[0].vval.v_list) != NULL)
10961 {
10962 li = l->lv_first;
10963 if (argvars[2].v_type != VAR_UNKNOWN)
10964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 int error = FALSE;
10966
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010967 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 if (argvars[3].v_type != VAR_UNKNOWN)
10969 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010970 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 if (!error)
10972 {
10973 li = list_find(l, idx);
10974 if (li == NULL)
10975 EMSGN(_(e_listidx), idx);
10976 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010977 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 if (error)
10979 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010980 }
10981
10982 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010983 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984 ++n;
10985 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010987 else if (argvars[0].v_type == VAR_DICT)
10988 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 int todo;
10990 dict_T *d;
10991 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010993 if ((d = argvars[0].vval.v_dict) != NULL)
10994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010995 int error = FALSE;
10996
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 if (argvars[2].v_type != VAR_UNKNOWN)
10998 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010999 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 if (argvars[3].v_type != VAR_UNKNOWN)
11001 EMSG(_(e_invarg));
11002 }
11003
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011004 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011006 {
11007 if (!HASHITEM_EMPTY(hi))
11008 {
11009 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011010 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011011 ++n;
11012 }
11013 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 }
11015 }
11016 else
11017 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011018 rettv->vval.v_number = n;
11019}
11020
11021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11023 *
11024 * Checks the existence of a cscope connection.
11025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011027f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028{
11029#ifdef FEAT_CSCOPE
11030 int num = 0;
11031 char_u *dbpath = NULL;
11032 char_u *prepend = NULL;
11033 char_u buf[NUMBUFLEN];
11034
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011035 if (argvars[0].v_type != VAR_UNKNOWN
11036 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038 num = (int)get_tv_number(&argvars[0]);
11039 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011041 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 }
11043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045#endif
11046}
11047
11048/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011049 * "cursor(lnum, col)" function, or
11050 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011052 * Moves the cursor to the specified line and column.
11053 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011056f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011059#ifdef FEAT_VIRTUALEDIT
11060 long coladd = 0;
11061#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011062 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011064 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011065 if (argvars[1].v_type == VAR_UNKNOWN)
11066 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011067 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011068 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011069
Bram Moolenaar493c1782014-05-28 14:34:46 +020011070 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011071 {
11072 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011073 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011074 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011075 line = pos.lnum;
11076 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011077#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011078 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011079#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011080 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011081 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011082 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011083 set_curswant = FALSE;
11084 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011085 }
11086 else
11087 {
11088 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011089 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011090#ifdef FEAT_VIRTUALEDIT
11091 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011092 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011093#endif
11094 }
11095 if (line < 0 || col < 0
11096#ifdef FEAT_VIRTUALEDIT
11097 || coladd < 0
11098#endif
11099 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 if (line > 0)
11102 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103 if (col > 0)
11104 curwin->w_cursor.col = col - 1;
11105#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011106 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107#endif
11108
11109 /* Make sure the cursor is in a valid position. */
11110 check_cursor();
11111#ifdef FEAT_MBYTE
11112 /* Correct cursor for multi-byte character. */
11113 if (has_mbyte)
11114 mb_adjust_cursor();
11115#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011116
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011117 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011118 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119}
11120
11121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011122 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 */
11124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011125f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011127 int noref = 0;
11128
11129 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011130 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011131 if (noref < 0 || noref > 1)
11132 EMSG(_(e_invarg));
11133 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011134 {
11135 current_copyID += COPYID_INC;
11136 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138}
11139
11140/*
11141 * "delete()" function
11142 */
11143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011144f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011146 char_u nbuf[NUMBUFLEN];
11147 char_u *name;
11148 char_u *flags;
11149
11150 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011152 return;
11153
11154 name = get_tv_string(&argvars[0]);
11155 if (name == NULL || *name == NUL)
11156 {
11157 EMSG(_(e_invarg));
11158 return;
11159 }
11160
11161 if (argvars[1].v_type != VAR_UNKNOWN)
11162 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011164 flags = (char_u *)"";
11165
11166 if (*flags == NUL)
11167 /* delete a file */
11168 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11169 else if (STRCMP(flags, "d") == 0)
11170 /* delete an empty directory */
11171 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11172 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011173 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011174 rettv->vval.v_number = delete_recursive(name);
11175 else
11176 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
11180 * "did_filetype()" function
11181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011183f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011186 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187#endif
11188}
11189
11190/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011191 * "diff_filler()" function
11192 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011194f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011195{
11196#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011197 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011198#endif
11199}
11200
11201/*
11202 * "diff_hlID()" function
11203 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011205f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011206{
11207#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011208 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011209 static linenr_T prev_lnum = 0;
11210 static int changedtick = 0;
11211 static int fnum = 0;
11212 static int change_start = 0;
11213 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011214 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011215 int filler_lines;
11216 int col;
11217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 if (lnum < 0) /* ignore type error in {lnum} arg */
11219 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011220 if (lnum != prev_lnum
11221 || changedtick != curbuf->b_changedtick
11222 || fnum != curbuf->b_fnum)
11223 {
11224 /* New line, buffer, change: need to get the values. */
11225 filler_lines = diff_check(curwin, lnum);
11226 if (filler_lines < 0)
11227 {
11228 if (filler_lines == -1)
11229 {
11230 change_start = MAXCOL;
11231 change_end = -1;
11232 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11233 hlID = HLF_ADD; /* added line */
11234 else
11235 hlID = HLF_CHD; /* changed line */
11236 }
11237 else
11238 hlID = HLF_ADD; /* added line */
11239 }
11240 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011241 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011242 prev_lnum = lnum;
11243 changedtick = curbuf->b_changedtick;
11244 fnum = curbuf->b_fnum;
11245 }
11246
11247 if (hlID == HLF_CHD || hlID == HLF_TXD)
11248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011250 if (col >= change_start && col <= change_end)
11251 hlID = HLF_TXD; /* changed text */
11252 else
11253 hlID = HLF_CHD; /* changed line */
11254 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011255 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011256#endif
11257}
11258
11259/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011260 * "empty({expr})" function
11261 */
11262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011263f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011264{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011265 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011266
11267 switch (argvars[0].v_type)
11268 {
11269 case VAR_STRING:
11270 case VAR_FUNC:
11271 n = argvars[0].vval.v_string == NULL
11272 || *argvars[0].vval.v_string == NUL;
11273 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011274 case VAR_PARTIAL:
11275 n = FALSE;
11276 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011277 case VAR_NUMBER:
11278 n = argvars[0].vval.v_number == 0;
11279 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011280 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011281#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011282 n = argvars[0].vval.v_float == 0.0;
11283 break;
11284#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011285 case VAR_LIST:
11286 n = argvars[0].vval.v_list == NULL
11287 || argvars[0].vval.v_list->lv_first == NULL;
11288 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011289 case VAR_DICT:
11290 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011291 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011292 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011293 case VAR_SPECIAL:
11294 n = argvars[0].vval.v_number != VVAL_TRUE;
11295 break;
11296
Bram Moolenaar835dc632016-02-07 14:27:38 +010011297 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011298#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011299 n = argvars[0].vval.v_job == NULL
11300 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11301 break;
11302#endif
11303 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011304#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011305 n = argvars[0].vval.v_channel == NULL
11306 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011307 break;
11308#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011309 case VAR_UNKNOWN:
11310 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11311 n = TRUE;
11312 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011313 }
11314
11315 rettv->vval.v_number = n;
11316}
11317
11318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 * "escape({string}, {chars})" function
11320 */
11321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011322f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323{
11324 char_u buf[NUMBUFLEN];
11325
Bram Moolenaar758711c2005-02-02 23:11:38 +000011326 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11327 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329}
11330
11331/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011332 * "eval()" function
11333 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011335f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011336{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011337 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 s = get_tv_string_chk(&argvars[0]);
11340 if (s != NULL)
11341 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342
Bram Moolenaar615b9972015-01-14 17:15:05 +010011343 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011344 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11345 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011346 if (p != NULL && !aborting())
11347 EMSG2(_(e_invexpr2), p);
11348 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011350 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011351 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011352 else if (*s != NUL)
11353 EMSG(_(e_trailing));
11354}
11355
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 * "eventhandler()" function
11358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011360f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363}
11364
11365/*
11366 * "executable()" function
11367 */
11368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011369f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011371 char_u *name = get_tv_string(&argvars[0]);
11372
11373 /* Check in $PATH and also check directly if there is a directory name. */
11374 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11375 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011376}
11377
Bram Moolenaar79815f12016-07-09 17:07:29 +020011378static garray_T redir_execute_ga;
11379
11380/*
11381 * Append "value[value_len]" to the execute() output.
11382 */
11383 void
11384execute_redir_str(char_u *value, int value_len)
11385{
11386 int len;
11387
11388 if (value_len == -1)
11389 len = (int)STRLEN(value); /* Append the entire string */
11390 else
11391 len = value_len; /* Append only "value_len" characters */
11392 if (ga_grow(&redir_execute_ga, len) == OK)
11393 {
11394 mch_memmove((char *)redir_execute_ga.ga_data
11395 + redir_execute_ga.ga_len, value, len);
11396 redir_execute_ga.ga_len += len;
11397 }
11398}
11399
11400/*
11401 * Get next line from a list.
11402 * Called by do_cmdline() to get the next line.
11403 * Returns allocated string, or NULL for end of function.
11404 */
11405
11406 static char_u *
11407get_list_line(
11408 int c UNUSED,
11409 void *cookie,
11410 int indent UNUSED)
11411{
11412 listitem_T **p = (listitem_T **)cookie;
11413 listitem_T *item = *p;
11414 char_u buf[NUMBUFLEN];
11415 char_u *s;
11416
11417 if (item == NULL)
11418 return NULL;
11419 s = get_tv_string_buf_chk(&item->li_tv, buf);
11420 *p = item->li_next;
11421 return s == NULL ? NULL : vim_strsave(s);
11422}
11423
11424/*
11425 * "execute()" function
11426 */
11427 static void
11428f_execute(typval_T *argvars, typval_T *rettv)
11429{
11430 char_u *cmd = NULL;
11431 list_T *list = NULL;
11432 int save_msg_silent = msg_silent;
11433 int save_emsg_silent = emsg_silent;
11434 int save_emsg_noredir = emsg_noredir;
11435 int save_redir_execute = redir_execute;
11436 garray_T save_ga;
11437
11438 rettv->vval.v_string = NULL;
11439 rettv->v_type = VAR_STRING;
11440
11441 if (argvars[0].v_type == VAR_LIST)
11442 {
11443 list = argvars[0].vval.v_list;
11444 if (list == NULL || list->lv_first == NULL)
11445 /* empty list, no commands, empty output */
11446 return;
11447 ++list->lv_refcount;
11448 }
11449 else
11450 {
11451 cmd = get_tv_string_chk(&argvars[0]);
11452 if (cmd == NULL)
11453 return;
11454 }
11455
Bram Moolenaar79815f12016-07-09 17:07:29 +020011456 if (argvars[1].v_type != VAR_UNKNOWN)
11457 {
11458 char_u buf[NUMBUFLEN];
11459 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
11460
11461 if (s == NULL)
11462 return;
11463 if (STRNCMP(s, "silent", 6) == 0)
11464 ++msg_silent;
11465 if (STRCMP(s, "silent!") == 0)
11466 {
11467 emsg_silent = TRUE;
11468 emsg_noredir = TRUE;
11469 }
11470 }
11471 else
11472 ++msg_silent;
11473
Bram Moolenaared59aa62016-07-09 17:41:12 +020011474 if (redir_execute)
11475 save_ga = redir_execute_ga;
11476 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
11477 redir_execute = TRUE;
11478
Bram Moolenaar79815f12016-07-09 17:07:29 +020011479 if (cmd != NULL)
11480 do_cmdline_cmd(cmd);
11481 else
11482 {
11483 listitem_T *item = list->lv_first;
11484
11485 do_cmdline(NULL, get_list_line, (void *)&item,
11486 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
11487 --list->lv_refcount;
11488 }
11489
11490 rettv->vval.v_string = redir_execute_ga.ga_data;
11491 msg_silent = save_msg_silent;
11492 emsg_silent = save_emsg_silent;
11493 emsg_noredir = save_emsg_noredir;
11494
11495 redir_execute = save_redir_execute;
11496 if (redir_execute)
11497 redir_execute_ga = save_ga;
11498
11499 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11500 * line. Put it back in the first column. */
11501 msg_col = 0;
11502}
11503
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011504/*
11505 * "exepath()" function
11506 */
11507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011508f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011509{
11510 char_u *p = NULL;
11511
Bram Moolenaarb5971142015-03-21 17:32:19 +010011512 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011513 rettv->v_type = VAR_STRING;
11514 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
11517/*
11518 * "exists()" function
11519 */
11520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011521f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522{
11523 char_u *p;
11524 char_u *name;
11525 int n = FALSE;
11526 int len = 0;
11527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 if (*p == '$') /* environment variable */
11530 {
11531 /* first try "normal" environment variables (fast) */
11532 if (mch_getenv(p + 1) != NULL)
11533 n = TRUE;
11534 else
11535 {
11536 /* try expanding things like $VIM and ${HOME} */
11537 p = expand_env_save(p);
11538 if (p != NULL && *p != '$')
11539 n = TRUE;
11540 vim_free(p);
11541 }
11542 }
11543 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011545 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011546 if (*skipwhite(p) != NUL)
11547 n = FALSE; /* trailing garbage */
11548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 else if (*p == '*') /* internal or user defined function */
11550 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011551 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 }
11553 else if (*p == ':')
11554 {
11555 n = cmd_exists(p + 1);
11556 }
11557 else if (*p == '#')
11558 {
11559#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011560 if (p[1] == '#')
11561 n = autocmd_supported(p + 2);
11562 else
11563 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564#endif
11565 }
11566 else /* internal variable */
11567 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011568 char_u *tofree;
11569 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011571 /* get_name_len() takes care of expanding curly braces */
11572 name = p;
11573 len = get_name_len(&p, &tofree, TRUE, FALSE);
11574 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011576 if (tofree != NULL)
11577 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011578 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011579 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011581 /* handle d.key, l[idx], f(expr) */
11582 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11583 if (n)
11584 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011585 }
11586 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011587 if (*p != NUL)
11588 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011590 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 }
11592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011594}
11595
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011596#ifdef FEAT_FLOAT
11597/*
11598 * "exp()" function
11599 */
11600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011601f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011602{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011603 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011604
11605 rettv->v_type = VAR_FLOAT;
11606 if (get_float_arg(argvars, &f) == OK)
11607 rettv->vval.v_float = exp(f);
11608 else
11609 rettv->vval.v_float = 0.0;
11610}
11611#endif
11612
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613/*
11614 * "expand()" function
11615 */
11616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011617f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
11619 char_u *s;
11620 int len;
11621 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011622 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011625 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011628 if (argvars[1].v_type != VAR_UNKNOWN
11629 && argvars[2].v_type != VAR_UNKNOWN
11630 && get_tv_number_chk(&argvars[2], &error)
11631 && !error)
11632 {
11633 rettv->v_type = VAR_LIST;
11634 rettv->vval.v_list = NULL;
11635 }
11636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 if (*s == '%' || *s == '#' || *s == '<')
11639 {
11640 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011641 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011642 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011643 if (rettv->v_type == VAR_LIST)
11644 {
11645 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11646 list_append_string(rettv->vval.v_list, result, -1);
11647 else
11648 vim_free(result);
11649 }
11650 else
11651 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 }
11653 else
11654 {
11655 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011656 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657 if (argvars[1].v_type != VAR_UNKNOWN
11658 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011659 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 if (!error)
11661 {
11662 ExpandInit(&xpc);
11663 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011664 if (p_wic)
11665 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011666 if (rettv->v_type == VAR_STRING)
11667 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11668 options, WILD_ALL);
11669 else if (rettv_list_alloc(rettv) != FAIL)
11670 {
11671 int i;
11672
11673 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11674 for (i = 0; i < xpc.xp_numfiles; i++)
11675 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11676 ExpandCleanup(&xpc);
11677 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 }
11679 else
11680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 }
11682}
11683
11684/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011685 * Go over all entries in "d2" and add them to "d1".
11686 * When "action" is "error" then a duplicate key is an error.
11687 * When "action" is "force" then a duplicate key is overwritten.
11688 * Otherwise duplicate keys are ignored ("action" is "keep").
11689 */
11690 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011691dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011692{
11693 dictitem_T *di1;
11694 hashitem_T *hi2;
11695 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011696 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011697
11698 todo = (int)d2->dv_hashtab.ht_used;
11699 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11700 {
11701 if (!HASHITEM_EMPTY(hi2))
11702 {
11703 --todo;
11704 di1 = dict_find(d1, hi2->hi_key, -1);
11705 if (d1->dv_scope != 0)
11706 {
11707 /* Disallow replacing a builtin function in l: and g:.
11708 * Check the key to be valid when adding to any
11709 * scope. */
11710 if (d1->dv_scope == VAR_DEF_SCOPE
11711 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11712 && var_check_func_name(hi2->hi_key,
11713 di1 == NULL))
11714 break;
11715 if (!valid_varname(hi2->hi_key))
11716 break;
11717 }
11718 if (di1 == NULL)
11719 {
11720 di1 = dictitem_copy(HI2DI(hi2));
11721 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11722 dictitem_free(di1);
11723 }
11724 else if (*action == 'e')
11725 {
11726 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11727 break;
11728 }
11729 else if (*action == 'f' && HI2DI(hi2) != di1)
11730 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011731 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11732 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011733 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011734 clear_tv(&di1->di_tv);
11735 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11736 }
11737 }
11738 }
11739}
11740
11741/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011742 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011743 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011744 */
11745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011746f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011747{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011748 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011749
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011751 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011752 list_T *l1, *l2;
11753 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011754 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011755 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011756
Bram Moolenaare9a41262005-01-15 22:18:47 +000011757 l1 = argvars[0].vval.v_list;
11758 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011759 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011760 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 {
11762 if (argvars[2].v_type != VAR_UNKNOWN)
11763 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011764 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 if (error)
11766 return; /* type error; errmsg already given */
11767
Bram Moolenaar758711c2005-02-02 23:11:38 +000011768 if (before == l1->lv_len)
11769 item = NULL;
11770 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011771 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011772 item = list_find(l1, before);
11773 if (item == NULL)
11774 {
11775 EMSGN(_(e_listidx), before);
11776 return;
11777 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 }
11779 }
11780 else
11781 item = NULL;
11782 list_extend(l1, l2, item);
11783
Bram Moolenaare9a41262005-01-15 22:18:47 +000011784 copy_tv(&argvars[0], rettv);
11785 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011787 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11788 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011789 dict_T *d1, *d2;
11790 char_u *action;
11791 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011792
11793 d1 = argvars[0].vval.v_dict;
11794 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011795 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011796 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011797 {
11798 /* Check the third argument. */
11799 if (argvars[2].v_type != VAR_UNKNOWN)
11800 {
11801 static char *(av[]) = {"keep", "force", "error"};
11802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 action = get_tv_string_chk(&argvars[2]);
11804 if (action == NULL)
11805 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011806 for (i = 0; i < 3; ++i)
11807 if (STRCMP(action, av[i]) == 0)
11808 break;
11809 if (i == 3)
11810 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011811 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011812 return;
11813 }
11814 }
11815 else
11816 action = (char_u *)"force";
11817
Bram Moolenaara9922d62013-05-30 13:01:18 +020011818 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011819
Bram Moolenaare9a41262005-01-15 22:18:47 +000011820 copy_tv(&argvars[0], rettv);
11821 }
11822 }
11823 else
11824 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011825}
11826
11827/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011828 * "feedkeys()" function
11829 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011831f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011832{
11833 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011834 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011835 char_u *keys, *flags;
11836 char_u nbuf[NUMBUFLEN];
11837 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011838 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011839 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011840 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011841
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011842 /* This is not allowed in the sandbox. If the commands would still be
11843 * executed in the sandbox it would be OK, but it probably happens later,
11844 * when "sandbox" is no longer set. */
11845 if (check_secure())
11846 return;
11847
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011848 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011849
11850 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011851 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011852 flags = get_tv_string_buf(&argvars[1], nbuf);
11853 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011854 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011855 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011856 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011857 case 'n': remap = FALSE; break;
11858 case 'm': remap = TRUE; break;
11859 case 't': typed = TRUE; break;
11860 case 'i': insert = TRUE; break;
11861 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011862 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011863 }
11864 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011865 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011866
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011867 if (*keys != NUL || execute)
11868 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011869 /* Need to escape K_SPECIAL and CSI before putting the string in the
11870 * typeahead buffer. */
11871 keys_esc = vim_strsave_escape_csi(keys);
11872 if (keys_esc != NULL)
11873 {
11874 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011875 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011876 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011877 if (vgetc_busy)
11878 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011879 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011880 {
11881 int save_msg_scroll = msg_scroll;
11882
11883 /* Avoid a 1 second delay when the keys start Insert mode. */
11884 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011885
Bram Moolenaar245c4102016-04-20 17:37:41 +020011886 if (!dangerous)
11887 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011888 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011889 if (!dangerous)
11890 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011891 msg_scroll |= save_msg_scroll;
11892 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011893 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011894 }
11895}
11896
11897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898 * "filereadable()" function
11899 */
11900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011901f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011903 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 char_u *p;
11905 int n;
11906
Bram Moolenaarc236c162008-07-13 17:41:49 +000011907#ifndef O_NONBLOCK
11908# define O_NONBLOCK 0
11909#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011911 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11912 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 {
11914 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011915 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 }
11917 else
11918 n = FALSE;
11919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011920 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921}
11922
11923/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011924 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925 * rights to write into.
11926 */
11927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011930 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931}
11932
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011933 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011934findfilendir(
11935 typval_T *argvars UNUSED,
11936 typval_T *rettv,
11937 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011938{
11939#ifdef FEAT_SEARCHPATH
11940 char_u *fname;
11941 char_u *fresult = NULL;
11942 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11943 char_u *p;
11944 char_u pathbuf[NUMBUFLEN];
11945 int count = 1;
11946 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011947 int error = FALSE;
11948#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011949
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011950 rettv->vval.v_string = NULL;
11951 rettv->v_type = VAR_STRING;
11952
11953#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011954 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011955
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011956 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11959 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011960 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011961 else
11962 {
11963 if (*p != NUL)
11964 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011967 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011969 }
11970
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011971 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11972 error = TRUE;
11973
11974 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 do
11977 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011978 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011979 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 fresult = find_file_in_path_option(first ? fname : NULL,
11981 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011982 0, first, path,
11983 find_what,
11984 curbuf->b_ffname,
11985 find_what == FINDFILE_DIR
11986 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011987 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011988
11989 if (fresult != NULL && rettv->v_type == VAR_LIST)
11990 list_append_string(rettv->vval.v_list, fresult, -1);
11991
11992 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011994
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011995 if (rettv->v_type == VAR_STRING)
11996 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011997#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011998}
11999
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012000static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012001static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012002
12003/*
12004 * Implementation of map() and filter().
12005 */
12006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012007filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012008{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012009 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000012010 listitem_T *li, *nli;
12011 list_T *l = NULL;
12012 dictitem_T *di;
12013 hashtab_T *ht;
12014 hashitem_T *hi;
12015 dict_T *d = NULL;
12016 typval_T save_val;
12017 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012018 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012019 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012020 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012021 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012022 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012023 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012024 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012025
Bram Moolenaare9a41262005-01-15 22:18:47 +000012026 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012027 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012028 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012029 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012030 return;
12031 }
12032 else if (argvars[0].v_type == VAR_DICT)
12033 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012034 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012035 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012036 return;
12037 }
12038 else
12039 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012040 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012041 return;
12042 }
12043
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012044 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012045 /* On type errors, the preceding call has already displayed an error
12046 * message. Avoid a misleading error message for an empty string that
12047 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012048 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012051
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012052 /* We reset "did_emsg" to be able to detect whether an error
12053 * occurred during evaluation of the expression. */
12054 save_did_emsg = did_emsg;
12055 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012056
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012057 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 vimvars[VV_KEY].vv_type = VAR_STRING;
12061
12062 ht = &d->dv_hashtab;
12063 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012064 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012065 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 if (!HASHITEM_EMPTY(hi))
12068 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012069 int r;
12070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 --todo;
12072 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012073 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012074 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12075 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012076 break;
12077 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012078 r = filter_map_one(&di->di_tv, expr, map, &rem);
12079 clear_tv(&vimvars[VV_KEY].vv_tv);
12080 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 break;
12082 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012083 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012084 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12085 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012086 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012087 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 }
12090 }
12091 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012092 }
12093 else
12094 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012095 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012097 for (li = l->lv_first; li != NULL; li = nli)
12098 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012099 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012100 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012101 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012102 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012103 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012104 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012105 break;
12106 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012107 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012108 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012109 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012110 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012111
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012112 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012113 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012114
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012115 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012116 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012117
12118 copy_tv(&argvars[0], rettv);
12119}
12120
12121 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012122filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012123{
Bram Moolenaar33570922005-01-25 22:26:29 +000012124 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012125 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012126 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012127 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012128 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012129 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012130
Bram Moolenaar33570922005-01-25 22:26:29 +000012131 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012132 argv[0] = vimvars[VV_KEY].vv_tv;
12133 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012134 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012135 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012136 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012137 if (call_func(s, (int)STRLEN(s),
12138 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12139 goto theend;
12140 }
12141 else if (expr->v_type == VAR_PARTIAL)
12142 {
12143 partial_T *partial = expr->vval.v_partial;
12144
12145 s = partial->pt_name;
12146 if (call_func(s, (int)STRLEN(s),
12147 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12148 == FAIL)
12149 goto theend;
12150 }
12151 else
12152 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012153 s = get_tv_string_buf_chk(expr, buf);
12154 if (s == NULL)
12155 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012156 s = skipwhite(s);
12157 if (eval1(&s, &rettv, TRUE) == FAIL)
12158 goto theend;
12159 if (*s != NUL) /* check for trailing chars after expr */
12160 {
12161 EMSG2(_(e_invexpr2), s);
12162 goto theend;
12163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012164 }
12165 if (map)
12166 {
12167 /* map(): replace the list item value */
12168 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012169 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012170 *tv = rettv;
12171 }
12172 else
12173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 int error = FALSE;
12175
Bram Moolenaare9a41262005-01-15 22:18:47 +000012176 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012177 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012178 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012179 /* On type error, nothing has been removed; return FAIL to stop the
12180 * loop. The error message was given by get_tv_number_chk(). */
12181 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012182 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012183 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012184 retval = OK;
12185theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012186 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012187 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012188}
12189
12190/*
12191 * "filter()" function
12192 */
12193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012194f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012195{
12196 filter_map(argvars, rettv, FALSE);
12197}
12198
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012200 * "finddir({fname}[, {path}[, {count}]])" function
12201 */
12202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012203f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012205 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206}
12207
12208/*
12209 * "findfile({fname}[, {path}[, {count}]])" function
12210 */
12211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012212f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012214 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012215}
12216
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012217#ifdef FEAT_FLOAT
12218/*
12219 * "float2nr({float})" function
12220 */
12221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012222f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012223{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012224 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012225
12226 if (get_float_arg(argvars, &f) == OK)
12227 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012228# ifdef FEAT_NUM64
12229 if (f < -0x7fffffffffffffff)
12230 rettv->vval.v_number = -0x7fffffffffffffff;
12231 else if (f > 0x7fffffffffffffff)
12232 rettv->vval.v_number = 0x7fffffffffffffff;
12233 else
12234 rettv->vval.v_number = (varnumber_T)f;
12235# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012236 if (f < -0x7fffffff)
12237 rettv->vval.v_number = -0x7fffffff;
12238 else if (f > 0x7fffffff)
12239 rettv->vval.v_number = 0x7fffffff;
12240 else
12241 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012242# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012243 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012244}
12245
12246/*
12247 * "floor({float})" function
12248 */
12249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012250f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012251{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012252 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012253
12254 rettv->v_type = VAR_FLOAT;
12255 if (get_float_arg(argvars, &f) == OK)
12256 rettv->vval.v_float = floor(f);
12257 else
12258 rettv->vval.v_float = 0.0;
12259}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012260
12261/*
12262 * "fmod()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012266{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012267 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012268
12269 rettv->v_type = VAR_FLOAT;
12270 if (get_float_arg(argvars, &fx) == OK
12271 && get_float_arg(&argvars[1], &fy) == OK)
12272 rettv->vval.v_float = fmod(fx, fy);
12273 else
12274 rettv->vval.v_float = 0.0;
12275}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012276#endif
12277
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012279 * "fnameescape({string})" function
12280 */
12281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012282f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012283{
12284 rettv->vval.v_string = vim_strsave_fnameescape(
12285 get_tv_string(&argvars[0]), FALSE);
12286 rettv->v_type = VAR_STRING;
12287}
12288
12289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 * "fnamemodify({fname}, {mods})" function
12291 */
12292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012293f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294{
12295 char_u *fname;
12296 char_u *mods;
12297 int usedlen = 0;
12298 int len;
12299 char_u *fbuf = NULL;
12300 char_u buf[NUMBUFLEN];
12301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012302 fname = get_tv_string_chk(&argvars[0]);
12303 mods = get_tv_string_buf_chk(&argvars[1], buf);
12304 if (fname == NULL || mods == NULL)
12305 fname = NULL;
12306 else
12307 {
12308 len = (int)STRLEN(fname);
12309 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317 vim_free(fbuf);
12318}
12319
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012320static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321
12322/*
12323 * "foldclosed()" function
12324 */
12325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012326foldclosed_both(
12327 typval_T *argvars UNUSED,
12328 typval_T *rettv,
12329 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330{
12331#ifdef FEAT_FOLDING
12332 linenr_T lnum;
12333 linenr_T first, last;
12334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12337 {
12338 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12339 {
12340 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 return;
12345 }
12346 }
12347#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349}
12350
12351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352 * "foldclosed()" function
12353 */
12354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012355f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012356{
12357 foldclosed_both(argvars, rettv, FALSE);
12358}
12359
12360/*
12361 * "foldclosedend()" function
12362 */
12363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012364f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012365{
12366 foldclosed_both(argvars, rettv, TRUE);
12367}
12368
12369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 * "foldlevel()" function
12371 */
12372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012373f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374{
12375#ifdef FEAT_FOLDING
12376 linenr_T lnum;
12377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382}
12383
12384/*
12385 * "foldtext()" function
12386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012388f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389{
12390#ifdef FEAT_FOLDING
12391 linenr_T lnum;
12392 char_u *s;
12393 char_u *r;
12394 int len;
12395 char *txt;
12396#endif
12397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 rettv->v_type = VAR_STRING;
12399 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012401 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12402 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12403 <= curbuf->b_ml.ml_line_count
12404 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 {
12406 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012407 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12408 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 {
12410 if (!linewhite(lnum))
12411 break;
12412 ++lnum;
12413 }
12414
12415 /* Find interesting text in this line. */
12416 s = skipwhite(ml_get(lnum));
12417 /* skip C comment-start */
12418 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012421 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012422 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012423 {
12424 s = skipwhite(ml_get(lnum + 1));
12425 if (*s == '*')
12426 s = skipwhite(s + 1);
12427 }
12428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 txt = _("+-%s%3ld lines: ");
12430 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 + 20 /* for %3ld */
12433 + STRLEN(s))); /* concatenated */
12434 if (r != NULL)
12435 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012436 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12437 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12438 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 len = (int)STRLEN(r);
12440 STRCAT(r, s);
12441 /* remove 'foldmarker' and 'commentstring' */
12442 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 }
12445 }
12446#endif
12447}
12448
12449/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012450 * "foldtextresult(lnum)" function
12451 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012453f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012454{
12455#ifdef FEAT_FOLDING
12456 linenr_T lnum;
12457 char_u *text;
12458 char_u buf[51];
12459 foldinfo_T foldinfo;
12460 int fold_count;
12461#endif
12462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 rettv->v_type = VAR_STRING;
12464 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012465#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012467 /* treat illegal types and illegal string values for {lnum} the same */
12468 if (lnum < 0)
12469 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012470 fold_count = foldedCount(curwin, lnum, &foldinfo);
12471 if (fold_count > 0)
12472 {
12473 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12474 &foldinfo, buf);
12475 if (text == buf)
12476 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012477 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012478 }
12479#endif
12480}
12481
12482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483 * "foreground()" function
12484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488#ifdef FEAT_GUI
12489 if (gui.in_use)
12490 gui_mch_set_foreground();
12491#else
12492# ifdef WIN32
12493 win32_set_foreground();
12494# endif
12495#endif
12496}
12497
12498/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012499 * "function()" function
12500 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012502f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012503{
12504 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012505 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012506 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012507 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012508
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012509 if (argvars[0].v_type == VAR_FUNC)
12510 {
12511 /* function(MyFunc, [arg], dict) */
12512 s = argvars[0].vval.v_string;
12513 }
12514 else if (argvars[0].v_type == VAR_PARTIAL
12515 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012516 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012517 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012518 arg_pt = argvars[0].vval.v_partial;
12519 s = arg_pt->pt_name;
12520 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012521 else
12522 {
12523 /* function('MyFunc', [arg], dict) */
12524 s = get_tv_string(&argvars[0]);
12525 use_string = TRUE;
12526 }
12527
12528 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012529 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012530 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012531 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12532 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012533 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012534 else
12535 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012536 int dict_idx = 0;
12537 int arg_idx = 0;
12538 list_T *list = NULL;
12539
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012540 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012541 {
12542 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012543 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012544
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012545 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12546 * also be called from another script. Using trans_function_name()
12547 * would also work, but some plugins depend on the name being
12548 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012549 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012550 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12551 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012552 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012553 STRCPY(name, sid_buf);
12554 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012555 }
12556 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012557 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012558 name = vim_strsave(s);
12559
12560 if (argvars[1].v_type != VAR_UNKNOWN)
12561 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012562 if (argvars[2].v_type != VAR_UNKNOWN)
12563 {
12564 /* function(name, [args], dict) */
12565 arg_idx = 1;
12566 dict_idx = 2;
12567 }
12568 else if (argvars[1].v_type == VAR_DICT)
12569 /* function(name, dict) */
12570 dict_idx = 1;
12571 else
12572 /* function(name, [args]) */
12573 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012574 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012575 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012576 if (argvars[dict_idx].v_type != VAR_DICT)
12577 {
12578 EMSG(_("E922: expected a dict"));
12579 vim_free(name);
12580 return;
12581 }
12582 if (argvars[dict_idx].vval.v_dict == NULL)
12583 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012584 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012585 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012586 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012587 if (argvars[arg_idx].v_type != VAR_LIST)
12588 {
12589 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12590 vim_free(name);
12591 return;
12592 }
12593 list = argvars[arg_idx].vval.v_list;
12594 if (list == NULL || list->lv_len == 0)
12595 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012596 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012597 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012598 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012599 {
12600 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012601
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012602 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012603 if (pt == NULL)
12604 vim_free(name);
12605 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012606 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012607 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012608 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012609 listitem_T *li;
12610 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012611 int arg_len = 0;
12612 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012613
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012614 if (arg_pt != NULL)
12615 arg_len = arg_pt->pt_argc;
12616 if (list != NULL)
12617 lv_len = list->lv_len;
12618 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012619 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012620 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012621 if (pt->pt_argv == NULL)
12622 {
12623 vim_free(pt);
12624 vim_free(name);
12625 return;
12626 }
12627 else
12628 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012629 for (i = 0; i < arg_len; i++)
12630 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12631 if (lv_len > 0)
12632 for (li = list->lv_first; li != NULL;
12633 li = li->li_next)
12634 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012635 }
12636 }
12637
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012638 /* For "function(dict.func, [], dict)" and "func" is a partial
12639 * use "dict". That is backwards compatible. */
12640 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012641 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012642 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012643 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12644 ++pt->pt_dict->dv_refcount;
12645 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012646 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012647 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012648 /* If the dict was bound automatically the result is also
12649 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012650 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012651 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012652 if (pt->pt_dict != NULL)
12653 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012654 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012655
12656 pt->pt_refcount = 1;
12657 pt->pt_name = name;
12658 func_ref(pt->pt_name);
12659 }
12660 rettv->v_type = VAR_PARTIAL;
12661 rettv->vval.v_partial = pt;
12662 }
12663 else
12664 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012665 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012666 rettv->v_type = VAR_FUNC;
12667 rettv->vval.v_string = name;
12668 func_ref(name);
12669 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012670 }
12671}
12672
12673/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012674 * "garbagecollect()" function
12675 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012677f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012678{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012679 /* This is postponed until we are back at the toplevel, because we may be
12680 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12681 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012682
12683 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12684 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012685}
12686
12687/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012688 * "get()" function
12689 */
12690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012691f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012692{
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 listitem_T *li;
12694 list_T *l;
12695 dictitem_T *di;
12696 dict_T *d;
12697 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698
Bram Moolenaare9a41262005-01-15 22:18:47 +000012699 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012700 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012703 int error = FALSE;
12704
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012705 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012706 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012707 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012708 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012710 else if (argvars[0].v_type == VAR_DICT)
12711 {
12712 if ((d = argvars[0].vval.v_dict) != NULL)
12713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012714 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012715 if (di != NULL)
12716 tv = &di->di_tv;
12717 }
12718 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012719 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012720 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012721 partial_T *pt;
12722 partial_T fref_pt;
12723
12724 if (argvars[0].v_type == VAR_PARTIAL)
12725 pt = argvars[0].vval.v_partial;
12726 else
12727 {
12728 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12729 fref_pt.pt_name = argvars[0].vval.v_string;
12730 pt = &fref_pt;
12731 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012732
12733 if (pt != NULL)
12734 {
12735 char_u *what = get_tv_string(&argvars[1]);
12736
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012737 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012738 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012739 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012740 if (pt->pt_name == NULL)
12741 rettv->vval.v_string = NULL;
12742 else
12743 rettv->vval.v_string = vim_strsave(pt->pt_name);
12744 }
12745 else if (STRCMP(what, "dict") == 0)
12746 {
12747 rettv->v_type = VAR_DICT;
12748 rettv->vval.v_dict = pt->pt_dict;
12749 if (pt->pt_dict != NULL)
12750 ++pt->pt_dict->dv_refcount;
12751 }
12752 else if (STRCMP(what, "args") == 0)
12753 {
12754 rettv->v_type = VAR_LIST;
12755 if (rettv_list_alloc(rettv) == OK)
12756 {
12757 int i;
12758
12759 for (i = 0; i < pt->pt_argc; ++i)
12760 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12761 }
12762 }
12763 else
12764 EMSG2(_(e_invarg2), what);
12765 return;
12766 }
12767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012768 else
12769 EMSG2(_(e_listdictarg), "get()");
12770
12771 if (tv == NULL)
12772 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012773 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012774 copy_tv(&argvars[2], rettv);
12775 }
12776 else
12777 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012778}
12779
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012780static 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 +000012781
12782/*
12783 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012784 * Return a range (from start to end) of lines in rettv from the specified
12785 * buffer.
12786 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012787 */
12788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012789get_buffer_lines(
12790 buf_T *buf,
12791 linenr_T start,
12792 linenr_T end,
12793 int retlist,
12794 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012795{
12796 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012797
Bram Moolenaar959a1432013-12-14 12:17:38 +010012798 rettv->v_type = VAR_STRING;
12799 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012800 if (retlist && rettv_list_alloc(rettv) == FAIL)
12801 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012802
12803 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12804 return;
12805
12806 if (!retlist)
12807 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012808 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12809 p = ml_get_buf(buf, start, FALSE);
12810 else
12811 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012812 rettv->vval.v_string = vim_strsave(p);
12813 }
12814 else
12815 {
12816 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012817 return;
12818
12819 if (start < 1)
12820 start = 1;
12821 if (end > buf->b_ml.ml_line_count)
12822 end = buf->b_ml.ml_line_count;
12823 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012824 if (list_append_string(rettv->vval.v_list,
12825 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012826 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012827 }
12828}
12829
12830/*
12831 * "getbufline()" function
12832 */
12833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012834f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012835{
12836 linenr_T lnum;
12837 linenr_T end;
12838 buf_T *buf;
12839
12840 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12841 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012842 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012843 --emsg_off;
12844
Bram Moolenaar661b1822005-07-28 22:36:45 +000012845 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012846 if (argvars[2].v_type == VAR_UNKNOWN)
12847 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012848 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012849 end = get_tv_lnum_buf(&argvars[2], buf);
12850
Bram Moolenaar342337a2005-07-21 21:11:17 +000012851 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012852}
12853
Bram Moolenaar0d660222005-01-07 21:51:51 +000012854/*
12855 * "getbufvar()" function
12856 */
12857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012858f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859{
12860 buf_T *buf;
12861 buf_T *save_curbuf;
12862 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012864 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12867 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012868 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012869 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012870
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012871 rettv->v_type = VAR_STRING;
12872 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873
12874 if (buf != NULL && varname != NULL)
12875 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012876 /* set curbuf to be our buf, temporarily */
12877 save_curbuf = curbuf;
12878 curbuf = buf;
12879
Bram Moolenaar0d660222005-01-07 21:51:51 +000012880 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012881 {
12882 if (get_option_tv(&varname, rettv, TRUE) == OK)
12883 done = TRUE;
12884 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012885 else if (STRCMP(varname, "changedtick") == 0)
12886 {
12887 rettv->v_type = VAR_NUMBER;
12888 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012889 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012890 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012891 else
12892 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012893 /* Look up the variable. */
12894 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12895 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12896 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012897 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012898 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012899 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012900 done = TRUE;
12901 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012902 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012903
12904 /* restore previous notion of curbuf */
12905 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012906 }
12907
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012908 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12909 /* use the default value */
12910 copy_tv(&argvars[2], rettv);
12911
Bram Moolenaar0d660222005-01-07 21:51:51 +000012912 --emsg_off;
12913}
12914
12915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 * "getchar()" function
12917 */
12918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012919f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
12921 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012922 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012924 /* Position the cursor. Needed after a message that ends in a space. */
12925 windgoto(msg_row, msg_col);
12926
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 ++no_mapping;
12928 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012929 for (;;)
12930 {
12931 if (argvars[0].v_type == VAR_UNKNOWN)
12932 /* getchar(): blocking wait. */
12933 n = safe_vgetc();
12934 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12935 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012936 n = vpeekc_any();
12937 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012938 /* illegal argument or getchar(0) and no char avail: return zero */
12939 n = 0;
12940 else
12941 /* getchar(0) and char avail: return char */
12942 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012943
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012944 if (n == K_IGNORE)
12945 continue;
12946 break;
12947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 --no_mapping;
12949 --allow_keys;
12950
Bram Moolenaar219b8702006-11-01 14:32:36 +000012951 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012952 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012953 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12954 vimvars[VV_MOUSE_COL].vv_nr = 0;
12955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957 if (IS_SPECIAL(n) || mod_mask != 0)
12958 {
12959 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12960 int i = 0;
12961
12962 /* Turn a special key into three bytes, plus modifier. */
12963 if (mod_mask != 0)
12964 {
12965 temp[i++] = K_SPECIAL;
12966 temp[i++] = KS_MODIFIER;
12967 temp[i++] = mod_mask;
12968 }
12969 if (IS_SPECIAL(n))
12970 {
12971 temp[i++] = K_SPECIAL;
12972 temp[i++] = K_SECOND(n);
12973 temp[i++] = K_THIRD(n);
12974 }
12975#ifdef FEAT_MBYTE
12976 else if (has_mbyte)
12977 i += (*mb_char2bytes)(n, temp + i);
12978#endif
12979 else
12980 temp[i++] = n;
12981 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->v_type = VAR_STRING;
12983 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012984
12985#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012986 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012987 {
12988 int row = mouse_row;
12989 int col = mouse_col;
12990 win_T *win;
12991 linenr_T lnum;
12992# ifdef FEAT_WINDOWS
12993 win_T *wp;
12994# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012995 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012996
12997 if (row >= 0 && col >= 0)
12998 {
12999 /* Find the window at the mouse coordinates and compute the
13000 * text position. */
13001 win = mouse_find_win(&row, &col);
13002 (void)mouse_comp_pos(win, &row, &col, &lnum);
13003# ifdef FEAT_WINDOWS
13004 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013005 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013006# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013007 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020013008 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013009 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
13010 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
13011 }
13012 }
13013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 }
13015}
13016
13017/*
13018 * "getcharmod()" function
13019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013021f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024}
13025
13026/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013027 * "getcharsearch()" function
13028 */
13029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013030f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013031{
13032 if (rettv_dict_alloc(rettv) != FAIL)
13033 {
13034 dict_T *dict = rettv->vval.v_dict;
13035
13036 dict_add_nr_str(dict, "char", 0L, last_csearch());
13037 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
13038 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
13039 }
13040}
13041
13042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043 * "getcmdline()" function
13044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013046f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 rettv->v_type = VAR_STRING;
13049 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050}
13051
13052/*
13053 * "getcmdpos()" function
13054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013056f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013058 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059}
13060
13061/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013062 * "getcmdtype()" function
13063 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013065f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013066{
13067 rettv->v_type = VAR_STRING;
13068 rettv->vval.v_string = alloc(2);
13069 if (rettv->vval.v_string != NULL)
13070 {
13071 rettv->vval.v_string[0] = get_cmdline_type();
13072 rettv->vval.v_string[1] = NUL;
13073 }
13074}
13075
13076/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013077 * "getcmdwintype()" function
13078 */
13079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013080f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013081{
13082 rettv->v_type = VAR_STRING;
13083 rettv->vval.v_string = NULL;
13084#ifdef FEAT_CMDWIN
13085 rettv->vval.v_string = alloc(2);
13086 if (rettv->vval.v_string != NULL)
13087 {
13088 rettv->vval.v_string[0] = cmdwin_type;
13089 rettv->vval.v_string[1] = NUL;
13090 }
13091#endif
13092}
13093
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013094#if defined(FEAT_CMDL_COMPL)
13095/*
13096 * "getcompletion()" function
13097 */
13098 static void
13099f_getcompletion(typval_T *argvars, typval_T *rettv)
13100{
13101 char_u *pat;
13102 expand_T xpc;
13103 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
13104 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
13105
13106 if (p_wic)
13107 options |= WILD_ICASE;
13108
13109 ExpandInit(&xpc);
13110 xpc.xp_pattern = get_tv_string(&argvars[0]);
13111 xpc.xp_pattern_len = STRLEN(xpc.xp_pattern);
13112 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
13113 if (xpc.xp_context == EXPAND_NOTHING)
13114 {
13115 if (argvars[1].v_type == VAR_STRING)
13116 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
13117 else
13118 EMSG(_(e_invarg));
13119 return;
13120 }
13121
13122 if (xpc.xp_context == EXPAND_MENUS)
13123 {
13124 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
13125 xpc.xp_pattern_len = STRLEN(xpc.xp_pattern);
13126 }
13127
13128 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
13129 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
13130 {
13131 int i;
13132
13133 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
13134
13135 for (i = 0; i < xpc.xp_numfiles; i++)
13136 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13137 }
13138 vim_free(pat);
13139 ExpandCleanup(&xpc);
13140}
13141#endif
13142
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144 * "getcwd()" function
13145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013147f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013149 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013150 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013153 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013154
13155 wp = find_tabwin(&argvars[0], &argvars[1]);
13156 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013158 if (wp->w_localdir != NULL)
13159 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar5c719942016-07-09 23:40:45 +020013160 else if (globaldir != NULL)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013161 rettv->vval.v_string = vim_strsave(globaldir);
13162 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013163 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013164 cwd = alloc(MAXPATHL);
13165 if (cwd != NULL)
13166 {
13167 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13168 rettv->vval.v_string = vim_strsave(cwd);
13169 vim_free(cwd);
13170 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013171 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013172#ifdef BACKSLASH_IN_FILENAME
13173 if (rettv->vval.v_string != NULL)
13174 slash_adjust(rettv->vval.v_string);
13175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 }
13177}
13178
13179/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013180 * "getfontname()" function
13181 */
13182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013183f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->v_type = VAR_STRING;
13186 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013187#ifdef FEAT_GUI
13188 if (gui.in_use)
13189 {
13190 GuiFont font;
13191 char_u *name = NULL;
13192
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013193 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013194 {
13195 /* Get the "Normal" font. Either the name saved by
13196 * hl_set_font_name() or from the font ID. */
13197 font = gui.norm_font;
13198 name = hl_get_font_name();
13199 }
13200 else
13201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013203 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13204 return;
13205 font = gui_mch_get_font(name, FALSE);
13206 if (font == NOFONT)
13207 return; /* Invalid font name, return empty string. */
13208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013209 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013210 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013211 gui_mch_free_font(font);
13212 }
13213#endif
13214}
13215
13216/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013217 * "getfperm({fname})" function
13218 */
13219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013220f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013221{
13222 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013223 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013224 char_u *perm = NULL;
13225 char_u flags[] = "rwx";
13226 int i;
13227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013231 if (mch_stat((char *)fname, &st) >= 0)
13232 {
13233 perm = vim_strsave((char_u *)"---------");
13234 if (perm != NULL)
13235 {
13236 for (i = 0; i < 9; i++)
13237 {
13238 if (st.st_mode & (1 << (8 - i)))
13239 perm[i] = flags[i % 3];
13240 }
13241 }
13242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013244}
13245
13246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 * "getfsize({fname})" function
13248 */
13249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013250f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251{
13252 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013253 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258
13259 if (mch_stat((char *)fname, &st) >= 0)
13260 {
13261 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013266
13267 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013268 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013269 rettv->vval.v_number = -2;
13270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 }
13272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274}
13275
13276/*
13277 * "getftime({fname})" function
13278 */
13279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013280f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281{
13282 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013283 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286
13287 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291}
13292
13293/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013294 * "getftype({fname})" function
13295 */
13296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013297f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013298{
13299 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013300 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013301 char_u *type = NULL;
13302 char *t;
13303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013306 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013307 if (mch_lstat((char *)fname, &st) >= 0)
13308 {
13309#ifdef S_ISREG
13310 if (S_ISREG(st.st_mode))
13311 t = "file";
13312 else if (S_ISDIR(st.st_mode))
13313 t = "dir";
13314# ifdef S_ISLNK
13315 else if (S_ISLNK(st.st_mode))
13316 t = "link";
13317# endif
13318# ifdef S_ISBLK
13319 else if (S_ISBLK(st.st_mode))
13320 t = "bdev";
13321# endif
13322# ifdef S_ISCHR
13323 else if (S_ISCHR(st.st_mode))
13324 t = "cdev";
13325# endif
13326# ifdef S_ISFIFO
13327 else if (S_ISFIFO(st.st_mode))
13328 t = "fifo";
13329# endif
13330# ifdef S_ISSOCK
13331 else if (S_ISSOCK(st.st_mode))
13332 t = "fifo";
13333# endif
13334 else
13335 t = "other";
13336#else
13337# ifdef S_IFMT
13338 switch (st.st_mode & S_IFMT)
13339 {
13340 case S_IFREG: t = "file"; break;
13341 case S_IFDIR: t = "dir"; break;
13342# ifdef S_IFLNK
13343 case S_IFLNK: t = "link"; break;
13344# endif
13345# ifdef S_IFBLK
13346 case S_IFBLK: t = "bdev"; break;
13347# endif
13348# ifdef S_IFCHR
13349 case S_IFCHR: t = "cdev"; break;
13350# endif
13351# ifdef S_IFIFO
13352 case S_IFIFO: t = "fifo"; break;
13353# endif
13354# ifdef S_IFSOCK
13355 case S_IFSOCK: t = "socket"; break;
13356# endif
13357 default: t = "other";
13358 }
13359# else
13360 if (mch_isdir(fname))
13361 t = "dir";
13362 else
13363 t = "file";
13364# endif
13365#endif
13366 type = vim_strsave((char_u *)t);
13367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013368 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013369}
13370
13371/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013372 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013373 */
13374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013375f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376{
13377 linenr_T lnum;
13378 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013379 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013380
13381 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013382 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013383 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013384 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013385 retlist = FALSE;
13386 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013387 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013388 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013389 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013390 retlist = TRUE;
13391 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013392
Bram Moolenaar342337a2005-07-21 21:11:17 +000013393 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013394}
13395
13396/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013397 * "getmatches()" function
13398 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013400f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013401{
13402#ifdef FEAT_SEARCH_EXTRA
13403 dict_T *dict;
13404 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013405 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013406
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013407 if (rettv_list_alloc(rettv) == OK)
13408 {
13409 while (cur != NULL)
13410 {
13411 dict = dict_alloc();
13412 if (dict == NULL)
13413 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013414 if (cur->match.regprog == NULL)
13415 {
13416 /* match added with matchaddpos() */
13417 for (i = 0; i < MAXPOSMATCH; ++i)
13418 {
13419 llpos_T *llpos;
13420 char buf[6];
13421 list_T *l;
13422
13423 llpos = &cur->pos.pos[i];
13424 if (llpos->lnum == 0)
13425 break;
13426 l = list_alloc();
13427 if (l == NULL)
13428 break;
13429 list_append_number(l, (varnumber_T)llpos->lnum);
13430 if (llpos->col > 0)
13431 {
13432 list_append_number(l, (varnumber_T)llpos->col);
13433 list_append_number(l, (varnumber_T)llpos->len);
13434 }
13435 sprintf(buf, "pos%d", i + 1);
13436 dict_add_list(dict, buf, l);
13437 }
13438 }
13439 else
13440 {
13441 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13442 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013443 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013444 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13445 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013446# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013447 if (cur->conceal_char)
13448 {
13449 char_u buf[MB_MAXBYTES + 1];
13450
13451 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13452 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13453 }
13454# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013455 list_append_dict(rettv->vval.v_list, dict);
13456 cur = cur->next;
13457 }
13458 }
13459#endif
13460}
13461
13462/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013463 * "getpid()" function
13464 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013466f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013467{
13468 rettv->vval.v_number = mch_get_pid();
13469}
13470
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013472getpos_both(
13473 typval_T *argvars,
13474 typval_T *rettv,
13475 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013476{
Bram Moolenaara5525202006-03-02 22:52:09 +000013477 pos_T *fp;
13478 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013479 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013480
13481 if (rettv_list_alloc(rettv) == OK)
13482 {
13483 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013484 if (getcurpos)
13485 fp = &curwin->w_cursor;
13486 else
13487 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013488 if (fnum != -1)
13489 list_append_number(l, (varnumber_T)fnum);
13490 else
13491 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013492 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13493 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013494 list_append_number(l, (fp != NULL)
13495 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013496 : (varnumber_T)0);
13497 list_append_number(l,
13498#ifdef FEAT_VIRTUALEDIT
13499 (fp != NULL) ? (varnumber_T)fp->coladd :
13500#endif
13501 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013502 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013503 {
13504 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013505 list_append_number(l, curwin->w_curswant == MAXCOL ?
13506 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013507 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013508 }
13509 else
13510 rettv->vval.v_number = FALSE;
13511}
13512
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013513
13514/*
13515 * "getcurpos()" function
13516 */
13517 static void
13518f_getcurpos(typval_T *argvars, typval_T *rettv)
13519{
13520 getpos_both(argvars, rettv, TRUE);
13521}
13522
13523/*
13524 * "getpos(string)" function
13525 */
13526 static void
13527f_getpos(typval_T *argvars, typval_T *rettv)
13528{
13529 getpos_both(argvars, rettv, FALSE);
13530}
13531
Bram Moolenaara5525202006-03-02 22:52:09 +000013532/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013533 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013534 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013536f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013537{
13538#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013539 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013540#endif
13541
Bram Moolenaar2641f772005-03-25 21:58:17 +000013542#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013543 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013544 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013545 wp = NULL;
13546 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13547 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013548 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013549 if (wp == NULL)
13550 return;
13551 }
13552
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013553 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013554 }
13555#endif
13556}
13557
13558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 * "getreg()" function
13560 */
13561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013562f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563{
13564 char_u *strregname;
13565 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013566 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013567 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013570 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013572 strregname = get_tv_string_chk(&argvars[0]);
13573 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013574 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013575 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013576 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013577 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013578 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013579 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013582 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013583
13584 if (error)
13585 return;
13586
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 regname = (strregname == NULL ? '"' : *strregname);
13588 if (regname == 0)
13589 regname = '"';
13590
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013591 if (return_list)
13592 {
13593 rettv->v_type = VAR_LIST;
13594 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13595 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013596 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013597 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013598 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013599 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013600 }
13601 else
13602 {
13603 rettv->v_type = VAR_STRING;
13604 rettv->vval.v_string = get_reg_contents(regname,
13605 arg2 ? GREG_EXPR_SRC : 0);
13606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607}
13608
13609/*
13610 * "getregtype()" function
13611 */
13612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013613f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614{
13615 char_u *strregname;
13616 int regname;
13617 char_u buf[NUMBUFLEN + 2];
13618 long reglen = 0;
13619
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013620 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 {
13622 strregname = get_tv_string_chk(&argvars[0]);
13623 if (strregname == NULL) /* type error; errmsg already given */
13624 {
13625 rettv->v_type = VAR_STRING;
13626 rettv->vval.v_string = NULL;
13627 return;
13628 }
13629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 else
13631 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013632 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633
13634 regname = (strregname == NULL ? '"' : *strregname);
13635 if (regname == 0)
13636 regname = '"';
13637
13638 buf[0] = NUL;
13639 buf[1] = NUL;
13640 switch (get_reg_type(regname, &reglen))
13641 {
13642 case MLINE: buf[0] = 'V'; break;
13643 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 case MBLOCK:
13645 buf[0] = Ctrl_V;
13646 sprintf((char *)buf + 1, "%ld", reglen + 1);
13647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->v_type = VAR_STRING;
13650 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651}
13652
13653/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013654 * "gettabvar()" function
13655 */
13656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013657f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013658{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013659 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013660 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013661 dictitem_T *v;
13662 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013663 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013664
13665 rettv->v_type = VAR_STRING;
13666 rettv->vval.v_string = NULL;
13667
13668 varname = get_tv_string_chk(&argvars[1]);
13669 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13670 if (tp != NULL && varname != NULL)
13671 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013672 /* Set tp to be our tabpage, temporarily. Also set the window to the
13673 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013674 if (switch_win(&oldcurwin, &oldtabpage,
13675 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013676 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013677 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013678 /* look up the variable */
13679 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13680 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13681 if (v != NULL)
13682 {
13683 copy_tv(&v->di_tv, rettv);
13684 done = TRUE;
13685 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013686 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013687
13688 /* restore previous notion of curwin */
13689 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013690 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013691
13692 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013693 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013694}
13695
13696/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013697 * "gettabwinvar()" function
13698 */
13699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013700f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013701{
13702 getwinvar(argvars, rettv, 1);
13703}
13704
13705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 * "getwinposx()" function
13707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013709f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712#ifdef FEAT_GUI
13713 if (gui.in_use)
13714 {
13715 int x, y;
13716
13717 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 }
13720#endif
13721}
13722
13723/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013724 * "win_findbuf()" function
13725 */
13726 static void
13727f_win_findbuf(typval_T *argvars, typval_T *rettv)
13728{
13729 if (rettv_list_alloc(rettv) != FAIL)
13730 win_findbuf(argvars, rettv->vval.v_list);
13731}
13732
13733/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013734 * "win_getid()" function
13735 */
13736 static void
13737f_win_getid(typval_T *argvars, typval_T *rettv)
13738{
13739 rettv->vval.v_number = win_getid(argvars);
13740}
13741
13742/*
13743 * "win_gotoid()" function
13744 */
13745 static void
13746f_win_gotoid(typval_T *argvars, typval_T *rettv)
13747{
13748 rettv->vval.v_number = win_gotoid(argvars);
13749}
13750
13751/*
13752 * "win_id2tabwin()" function
13753 */
13754 static void
13755f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13756{
13757 if (rettv_list_alloc(rettv) != FAIL)
13758 win_id2tabwin(argvars, rettv->vval.v_list);
13759}
13760
13761/*
13762 * "win_id2win()" function
13763 */
13764 static void
13765f_win_id2win(typval_T *argvars, typval_T *rettv)
13766{
13767 rettv->vval.v_number = win_id2win(argvars);
13768}
13769
13770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771 * "getwinposy()" function
13772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013774f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777#ifdef FEAT_GUI
13778 if (gui.in_use)
13779 {
13780 int x, y;
13781
13782 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 }
13785#endif
13786}
13787
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013788/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013789 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013790 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013791 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013792find_win_by_nr(
13793 typval_T *vp,
13794 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013795{
13796#ifdef FEAT_WINDOWS
13797 win_T *wp;
13798#endif
13799 int nr;
13800
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013801 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013802
13803#ifdef FEAT_WINDOWS
13804 if (nr < 0)
13805 return NULL;
13806 if (nr == 0)
13807 return curwin;
13808
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013809 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13810 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013811 if (nr >= LOWEST_WIN_ID)
13812 {
13813 if (wp->w_id == nr)
13814 return wp;
13815 }
13816 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013817 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013818 if (nr >= LOWEST_WIN_ID)
13819 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013820 return wp;
13821#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013822 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013823 return curwin;
13824 return NULL;
13825#endif
13826}
13827
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013829 * Find window specified by "wvp" in tabpage "tvp".
13830 */
13831 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013832find_tabwin(
13833 typval_T *wvp, /* VAR_UNKNOWN for current window */
13834 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013835{
13836 win_T *wp = NULL;
13837 tabpage_T *tp = NULL;
13838 long n;
13839
13840 if (wvp->v_type != VAR_UNKNOWN)
13841 {
13842 if (tvp->v_type != VAR_UNKNOWN)
13843 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013844 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013845 if (n >= 0)
13846 tp = find_tabpage(n);
13847 }
13848 else
13849 tp = curtab;
13850
13851 if (tp != NULL)
13852 wp = find_win_by_nr(wvp, tp);
13853 }
13854 else
13855 wp = curwin;
13856
13857 return wp;
13858}
13859
13860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 * "getwinvar()" function
13862 */
13863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013864f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013866 getwinvar(argvars, rettv, 0);
13867}
13868
13869/*
13870 * getwinvar() and gettabwinvar()
13871 */
13872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013873getwinvar(
13874 typval_T *argvars,
13875 typval_T *rettv,
13876 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013877{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013878 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013880 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013881 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013882 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013883#ifdef FEAT_WINDOWS
13884 win_T *oldcurwin;
13885 tabpage_T *oldtabpage;
13886 int need_switch_win;
13887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013889#ifdef FEAT_WINDOWS
13890 if (off == 1)
13891 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13892 else
13893 tp = curtab;
13894#endif
13895 win = find_win_by_nr(&argvars[off], tp);
13896 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013899 rettv->v_type = VAR_STRING;
13900 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901
13902 if (win != NULL && varname != NULL)
13903 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013904#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013905 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013906 * otherwise the window is not valid. Only do this when needed,
13907 * autocommands get blocked. */
13908 need_switch_win = !(tp == curtab && win == curwin);
13909 if (!need_switch_win
13910 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13911#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013912 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013913 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013914 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013915 if (get_option_tv(&varname, rettv, 1) == OK)
13916 done = TRUE;
13917 }
13918 else
13919 {
13920 /* Look up the variable. */
13921 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13922 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13923 varname, FALSE);
13924 if (v != NULL)
13925 {
13926 copy_tv(&v->di_tv, rettv);
13927 done = TRUE;
13928 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013931
Bram Moolenaarba117c22015-09-29 16:53:22 +020013932#ifdef FEAT_WINDOWS
13933 if (need_switch_win)
13934 /* restore previous notion of curwin */
13935 restore_win(oldcurwin, oldtabpage, TRUE);
13936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 }
13938
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013939 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13940 /* use the default return value */
13941 copy_tv(&argvars[off + 2], rettv);
13942
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 --emsg_off;
13944}
13945
13946/*
13947 * "glob()" function
13948 */
13949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013950f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013952 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013954 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013956 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013957 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013959 if (argvars[1].v_type != VAR_UNKNOWN)
13960 {
13961 if (get_tv_number_chk(&argvars[1], &error))
13962 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013964 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013965 if (get_tv_number_chk(&argvars[2], &error))
13966 {
13967 rettv->v_type = VAR_LIST;
13968 rettv->vval.v_list = NULL;
13969 }
13970 if (argvars[3].v_type != VAR_UNKNOWN
13971 && get_tv_number_chk(&argvars[3], &error))
13972 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013973 }
13974 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013975 if (!error)
13976 {
13977 ExpandInit(&xpc);
13978 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013979 if (p_wic)
13980 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013981 if (rettv->v_type == VAR_STRING)
13982 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013983 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013984 else if (rettv_list_alloc(rettv) != FAIL)
13985 {
13986 int i;
13987
13988 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13989 NULL, options, WILD_ALL_KEEP);
13990 for (i = 0; i < xpc.xp_numfiles; i++)
13991 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13992
13993 ExpandCleanup(&xpc);
13994 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013995 }
13996 else
13997 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998}
13999
14000/*
14001 * "globpath()" function
14002 */
14003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014004f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014006 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014008 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014009 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014010 garray_T ga;
14011 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014013 /* When the optional second argument is non-zero, don't remove matches
14014 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014016 if (argvars[2].v_type != VAR_UNKNOWN)
14017 {
14018 if (get_tv_number_chk(&argvars[2], &error))
14019 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010014020 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014021 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010014022 if (get_tv_number_chk(&argvars[3], &error))
14023 {
14024 rettv->v_type = VAR_LIST;
14025 rettv->vval.v_list = NULL;
14026 }
14027 if (argvars[4].v_type != VAR_UNKNOWN
14028 && get_tv_number_chk(&argvars[4], &error))
14029 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014030 }
14031 }
14032 if (file != NULL && !error)
14033 {
14034 ga_init2(&ga, (int)sizeof(char_u *), 10);
14035 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
14036 if (rettv->v_type == VAR_STRING)
14037 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
14038 else if (rettv_list_alloc(rettv) != FAIL)
14039 for (i = 0; i < ga.ga_len; ++i)
14040 list_append_string(rettv->vval.v_list,
14041 ((char_u **)(ga.ga_data))[i], -1);
14042 ga_clear_strings(&ga);
14043 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014045 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046}
14047
14048/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014049 * "glob2regpat()" function
14050 */
14051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014052f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014053{
14054 char_u *pat = get_tv_string_chk(&argvars[0]);
14055
14056 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010014057 rettv->vval.v_string = (pat == NULL)
14058 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014059}
14060
14061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 * "has()" function
14063 */
14064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014065f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066{
14067 int i;
14068 char_u *name;
14069 int n = FALSE;
14070 static char *(has_list[]) =
14071 {
14072#ifdef AMIGA
14073 "amiga",
14074# ifdef FEAT_ARP
14075 "arp",
14076# endif
14077#endif
14078#ifdef __BEOS__
14079 "beos",
14080#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000014081#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 "mac",
14083#endif
14084#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010014085 "macunix", /* built with 'darwin' enabled */
14086#endif
14087#if defined(__APPLE__) && __APPLE__ == 1
14088 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#ifdef __QNX__
14091 "qnx",
14092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093#ifdef UNIX
14094 "unix",
14095#endif
14096#ifdef VMS
14097 "vms",
14098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099#ifdef WIN32
14100 "win32",
14101#endif
14102#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
14103 "win32unix",
14104#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010014105#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 "win64",
14107#endif
14108#ifdef EBCDIC
14109 "ebcdic",
14110#endif
14111#ifndef CASE_INSENSITIVE_FILENAME
14112 "fname_case",
14113#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014114#ifdef HAVE_ACL
14115 "acl",
14116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117#ifdef FEAT_ARABIC
14118 "arabic",
14119#endif
14120#ifdef FEAT_AUTOCMD
14121 "autocmd",
14122#endif
14123#ifdef FEAT_BEVAL
14124 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000014125# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
14126 "balloon_multiline",
14127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128#endif
14129#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14130 "builtin_terms",
14131# ifdef ALL_BUILTIN_TCAPS
14132 "all_builtin_terms",
14133# endif
14134#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014135#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14136 || defined(FEAT_GUI_W32) \
14137 || defined(FEAT_GUI_MOTIF))
14138 "browsefilter",
14139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140#ifdef FEAT_BYTEOFF
14141 "byte_offset",
14142#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014144 "channel",
14145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146#ifdef FEAT_CINDENT
14147 "cindent",
14148#endif
14149#ifdef FEAT_CLIENTSERVER
14150 "clientserver",
14151#endif
14152#ifdef FEAT_CLIPBOARD
14153 "clipboard",
14154#endif
14155#ifdef FEAT_CMDL_COMPL
14156 "cmdline_compl",
14157#endif
14158#ifdef FEAT_CMDHIST
14159 "cmdline_hist",
14160#endif
14161#ifdef FEAT_COMMENTS
14162 "comments",
14163#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014164#ifdef FEAT_CONCEAL
14165 "conceal",
14166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167#ifdef FEAT_CRYPT
14168 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014169 "crypt-blowfish",
14170 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171#endif
14172#ifdef FEAT_CSCOPE
14173 "cscope",
14174#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014175#ifdef FEAT_CURSORBIND
14176 "cursorbind",
14177#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014178#ifdef CURSOR_SHAPE
14179 "cursorshape",
14180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181#ifdef DEBUG
14182 "debug",
14183#endif
14184#ifdef FEAT_CON_DIALOG
14185 "dialog_con",
14186#endif
14187#ifdef FEAT_GUI_DIALOG
14188 "dialog_gui",
14189#endif
14190#ifdef FEAT_DIFF
14191 "diff",
14192#endif
14193#ifdef FEAT_DIGRAPHS
14194 "digraphs",
14195#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014196#ifdef FEAT_DIRECTX
14197 "directx",
14198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199#ifdef FEAT_DND
14200 "dnd",
14201#endif
14202#ifdef FEAT_EMACS_TAGS
14203 "emacs_tags",
14204#endif
14205 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014206 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207#ifdef FEAT_SEARCH_EXTRA
14208 "extra_search",
14209#endif
14210#ifdef FEAT_FKMAP
14211 "farsi",
14212#endif
14213#ifdef FEAT_SEARCHPATH
14214 "file_in_path",
14215#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014216#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014217 "filterpipe",
14218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#ifdef FEAT_FIND_ID
14220 "find_in_path",
14221#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014222#ifdef FEAT_FLOAT
14223 "float",
14224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225#ifdef FEAT_FOLDING
14226 "folding",
14227#endif
14228#ifdef FEAT_FOOTER
14229 "footer",
14230#endif
14231#if !defined(USE_SYSTEM) && defined(UNIX)
14232 "fork",
14233#endif
14234#ifdef FEAT_GETTEXT
14235 "gettext",
14236#endif
14237#ifdef FEAT_GUI
14238 "gui",
14239#endif
14240#ifdef FEAT_GUI_ATHENA
14241# ifdef FEAT_GUI_NEXTAW
14242 "gui_neXtaw",
14243# else
14244 "gui_athena",
14245# endif
14246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247#ifdef FEAT_GUI_GTK
14248 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014249# ifdef USE_GTK3
14250 "gui_gtk3",
14251# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014253# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014255#ifdef FEAT_GUI_GNOME
14256 "gui_gnome",
14257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258#ifdef FEAT_GUI_MAC
14259 "gui_mac",
14260#endif
14261#ifdef FEAT_GUI_MOTIF
14262 "gui_motif",
14263#endif
14264#ifdef FEAT_GUI_PHOTON
14265 "gui_photon",
14266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267#ifdef FEAT_GUI_W32
14268 "gui_win32",
14269#endif
14270#ifdef FEAT_HANGULIN
14271 "hangul_input",
14272#endif
14273#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14274 "iconv",
14275#endif
14276#ifdef FEAT_INS_EXPAND
14277 "insert_expand",
14278#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014279#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014280 "job",
14281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282#ifdef FEAT_JUMPLIST
14283 "jumplist",
14284#endif
14285#ifdef FEAT_KEYMAP
14286 "keymap",
14287#endif
14288#ifdef FEAT_LANGMAP
14289 "langmap",
14290#endif
14291#ifdef FEAT_LIBCALL
14292 "libcall",
14293#endif
14294#ifdef FEAT_LINEBREAK
14295 "linebreak",
14296#endif
14297#ifdef FEAT_LISP
14298 "lispindent",
14299#endif
14300#ifdef FEAT_LISTCMDS
14301 "listcmds",
14302#endif
14303#ifdef FEAT_LOCALMAP
14304 "localmap",
14305#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014306#ifdef FEAT_LUA
14307# ifndef DYNAMIC_LUA
14308 "lua",
14309# endif
14310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311#ifdef FEAT_MENU
14312 "menu",
14313#endif
14314#ifdef FEAT_SESSION
14315 "mksession",
14316#endif
14317#ifdef FEAT_MODIFY_FNAME
14318 "modify_fname",
14319#endif
14320#ifdef FEAT_MOUSE
14321 "mouse",
14322#endif
14323#ifdef FEAT_MOUSESHAPE
14324 "mouseshape",
14325#endif
14326#if defined(UNIX) || defined(VMS)
14327# ifdef FEAT_MOUSE_DEC
14328 "mouse_dec",
14329# endif
14330# ifdef FEAT_MOUSE_GPM
14331 "mouse_gpm",
14332# endif
14333# ifdef FEAT_MOUSE_JSB
14334 "mouse_jsbterm",
14335# endif
14336# ifdef FEAT_MOUSE_NET
14337 "mouse_netterm",
14338# endif
14339# ifdef FEAT_MOUSE_PTERM
14340 "mouse_pterm",
14341# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014342# ifdef FEAT_MOUSE_SGR
14343 "mouse_sgr",
14344# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014345# ifdef FEAT_SYSMOUSE
14346 "mouse_sysmouse",
14347# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014348# ifdef FEAT_MOUSE_URXVT
14349 "mouse_urxvt",
14350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351# ifdef FEAT_MOUSE_XTERM
14352 "mouse_xterm",
14353# endif
14354#endif
14355#ifdef FEAT_MBYTE
14356 "multi_byte",
14357#endif
14358#ifdef FEAT_MBYTE_IME
14359 "multi_byte_ime",
14360#endif
14361#ifdef FEAT_MULTI_LANG
14362 "multi_lang",
14363#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014364#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014365#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014366 "mzscheme",
14367#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014368#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014369#ifdef FEAT_NUM64
14370 "num64",
14371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372#ifdef FEAT_OLE
14373 "ole",
14374#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014375 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376#ifdef FEAT_PATH_EXTRA
14377 "path_extra",
14378#endif
14379#ifdef FEAT_PERL
14380#ifndef DYNAMIC_PERL
14381 "perl",
14382#endif
14383#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014384#ifdef FEAT_PERSISTENT_UNDO
14385 "persistent_undo",
14386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387#ifdef FEAT_PYTHON
14388#ifndef DYNAMIC_PYTHON
14389 "python",
14390#endif
14391#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014392#ifdef FEAT_PYTHON3
14393#ifndef DYNAMIC_PYTHON3
14394 "python3",
14395#endif
14396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397#ifdef FEAT_POSTSCRIPT
14398 "postscript",
14399#endif
14400#ifdef FEAT_PRINTER
14401 "printer",
14402#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014403#ifdef FEAT_PROFILE
14404 "profile",
14405#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014406#ifdef FEAT_RELTIME
14407 "reltime",
14408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409#ifdef FEAT_QUICKFIX
14410 "quickfix",
14411#endif
14412#ifdef FEAT_RIGHTLEFT
14413 "rightleft",
14414#endif
14415#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14416 "ruby",
14417#endif
14418#ifdef FEAT_SCROLLBIND
14419 "scrollbind",
14420#endif
14421#ifdef FEAT_CMDL_INFO
14422 "showcmd",
14423 "cmdline_info",
14424#endif
14425#ifdef FEAT_SIGNS
14426 "signs",
14427#endif
14428#ifdef FEAT_SMARTINDENT
14429 "smartindent",
14430#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014431#ifdef STARTUPTIME
14432 "startuptime",
14433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434#ifdef FEAT_STL_OPT
14435 "statusline",
14436#endif
14437#ifdef FEAT_SUN_WORKSHOP
14438 "sun_workshop",
14439#endif
14440#ifdef FEAT_NETBEANS_INTG
14441 "netbeans_intg",
14442#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014443#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014444 "spell",
14445#endif
14446#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447 "syntax",
14448#endif
14449#if defined(USE_SYSTEM) || !defined(UNIX)
14450 "system",
14451#endif
14452#ifdef FEAT_TAG_BINS
14453 "tag_binary",
14454#endif
14455#ifdef FEAT_TAG_OLDSTATIC
14456 "tag_old_static",
14457#endif
14458#ifdef FEAT_TAG_ANYWHITE
14459 "tag_any_white",
14460#endif
14461#ifdef FEAT_TCL
14462# ifndef DYNAMIC_TCL
14463 "tcl",
14464# endif
14465#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014466#ifdef FEAT_TERMGUICOLORS
14467 "termguicolors",
14468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469#ifdef TERMINFO
14470 "terminfo",
14471#endif
14472#ifdef FEAT_TERMRESPONSE
14473 "termresponse",
14474#endif
14475#ifdef FEAT_TEXTOBJ
14476 "textobjects",
14477#endif
14478#ifdef HAVE_TGETENT
14479 "tgetent",
14480#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014481#ifdef FEAT_TIMERS
14482 "timers",
14483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484#ifdef FEAT_TITLE
14485 "title",
14486#endif
14487#ifdef FEAT_TOOLBAR
14488 "toolbar",
14489#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014490#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14491 "unnamedplus",
14492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493#ifdef FEAT_USR_CMDS
14494 "user-commands", /* was accidentally included in 5.4 */
14495 "user_commands",
14496#endif
14497#ifdef FEAT_VIMINFO
14498 "viminfo",
14499#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014500#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 "vertsplit",
14502#endif
14503#ifdef FEAT_VIRTUALEDIT
14504 "virtualedit",
14505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507#ifdef FEAT_VISUALEXTRA
14508 "visualextra",
14509#endif
14510#ifdef FEAT_VREPLACE
14511 "vreplace",
14512#endif
14513#ifdef FEAT_WILDIGN
14514 "wildignore",
14515#endif
14516#ifdef FEAT_WILDMENU
14517 "wildmenu",
14518#endif
14519#ifdef FEAT_WINDOWS
14520 "windows",
14521#endif
14522#ifdef FEAT_WAK
14523 "winaltkeys",
14524#endif
14525#ifdef FEAT_WRITEBACKUP
14526 "writebackup",
14527#endif
14528#ifdef FEAT_XIM
14529 "xim",
14530#endif
14531#ifdef FEAT_XFONTSET
14532 "xfontset",
14533#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014534#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014535 "xpm",
14536 "xpm_w32", /* for backward compatibility */
14537#else
14538# if defined(HAVE_XPM)
14539 "xpm",
14540# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542#ifdef USE_XSMP
14543 "xsmp",
14544#endif
14545#ifdef USE_XSMP_INTERACT
14546 "xsmp_interact",
14547#endif
14548#ifdef FEAT_XCLIPBOARD
14549 "xterm_clipboard",
14550#endif
14551#ifdef FEAT_XTERM_SAVE
14552 "xterm_save",
14553#endif
14554#if defined(UNIX) && defined(FEAT_X11)
14555 "X11",
14556#endif
14557 NULL
14558 };
14559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014560 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 for (i = 0; has_list[i] != NULL; ++i)
14562 if (STRICMP(name, has_list[i]) == 0)
14563 {
14564 n = TRUE;
14565 break;
14566 }
14567
14568 if (n == FALSE)
14569 {
14570 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014571 {
14572 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014573 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014574 && vim_isdigit(name[6])
14575 && vim_isdigit(name[8])
14576 && vim_isdigit(name[10]))
14577 {
14578 int major = atoi((char *)name + 6);
14579 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014580
14581 /* Expect "patch-9.9.01234". */
14582 n = (major < VIM_VERSION_MAJOR
14583 || (major == VIM_VERSION_MAJOR
14584 && (minor < VIM_VERSION_MINOR
14585 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014586 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014587 }
14588 else
14589 n = has_patch(atoi((char *)name + 5));
14590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 else if (STRICMP(name, "vim_starting") == 0)
14592 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014593#ifdef FEAT_MBYTE
14594 else if (STRICMP(name, "multi_byte_encoding") == 0)
14595 n = has_mbyte;
14596#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014597#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14598 else if (STRICMP(name, "balloon_multiline") == 0)
14599 n = multiline_balloon_available();
14600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601#ifdef DYNAMIC_TCL
14602 else if (STRICMP(name, "tcl") == 0)
14603 n = tcl_enabled(FALSE);
14604#endif
14605#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14606 else if (STRICMP(name, "iconv") == 0)
14607 n = iconv_enabled(FALSE);
14608#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014609#ifdef DYNAMIC_LUA
14610 else if (STRICMP(name, "lua") == 0)
14611 n = lua_enabled(FALSE);
14612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014613#ifdef DYNAMIC_MZSCHEME
14614 else if (STRICMP(name, "mzscheme") == 0)
14615 n = mzscheme_enabled(FALSE);
14616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617#ifdef DYNAMIC_RUBY
14618 else if (STRICMP(name, "ruby") == 0)
14619 n = ruby_enabled(FALSE);
14620#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014621#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622#ifdef DYNAMIC_PYTHON
14623 else if (STRICMP(name, "python") == 0)
14624 n = python_enabled(FALSE);
14625#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014626#endif
14627#ifdef FEAT_PYTHON3
14628#ifdef DYNAMIC_PYTHON3
14629 else if (STRICMP(name, "python3") == 0)
14630 n = python3_enabled(FALSE);
14631#endif
14632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633#ifdef DYNAMIC_PERL
14634 else if (STRICMP(name, "perl") == 0)
14635 n = perl_enabled(FALSE);
14636#endif
14637#ifdef FEAT_GUI
14638 else if (STRICMP(name, "gui_running") == 0)
14639 n = (gui.in_use || gui.starting);
14640# ifdef FEAT_GUI_W32
14641 else if (STRICMP(name, "gui_win32s") == 0)
14642 n = gui_is_win32s();
14643# endif
14644# ifdef FEAT_BROWSE
14645 else if (STRICMP(name, "browse") == 0)
14646 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14647# endif
14648#endif
14649#ifdef FEAT_SYN_HL
14650 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014651 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652#endif
14653#if defined(WIN3264)
14654 else if (STRICMP(name, "win95") == 0)
14655 n = mch_windows95();
14656#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014657#ifdef FEAT_NETBEANS_INTG
14658 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014659 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 }
14662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664}
14665
14666/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014667 * "has_key()" function
14668 */
14669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014670f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014671{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014672 if (argvars[0].v_type != VAR_DICT)
14673 {
14674 EMSG(_(e_dictreq));
14675 return;
14676 }
14677 if (argvars[0].vval.v_dict == NULL)
14678 return;
14679
14680 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014681 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014682}
14683
14684/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014685 * "haslocaldir()" function
14686 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014688f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014689{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014690 win_T *wp = NULL;
14691
14692 wp = find_tabwin(&argvars[0], &argvars[1]);
14693 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014694}
14695
14696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 * "hasmapto()" function
14698 */
14699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014700f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701{
14702 char_u *name;
14703 char_u *mode;
14704 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014705 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014707 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014708 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 mode = (char_u *)"nvo";
14710 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014713 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014714 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716
Bram Moolenaar2c932302006-03-18 21:42:09 +000014717 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014718 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014720 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721}
14722
14723/*
14724 * "histadd()" function
14725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728{
14729#ifdef FEAT_CMDHIST
14730 int histype;
14731 char_u *str;
14732 char_u buf[NUMBUFLEN];
14733#endif
14734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 if (check_restricted() || check_secure())
14737 return;
14738#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014739 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14740 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 if (histype >= 0)
14742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014743 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 if (*str != NUL)
14745 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014746 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 return;
14750 }
14751 }
14752#endif
14753}
14754
14755/*
14756 * "histdel()" function
14757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014759f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760{
14761#ifdef FEAT_CMDHIST
14762 int n;
14763 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014764 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014766 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14767 if (str == NULL)
14768 n = 0;
14769 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014772 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014775 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 else
14777 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014778 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014779 get_tv_string_buf(&argvars[1], buf));
14780 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781#endif
14782}
14783
14784/*
14785 * "histget()" function
14786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014788f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789{
14790#ifdef FEAT_CMDHIST
14791 int type;
14792 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014793 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014795 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14796 if (str == NULL)
14797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 {
14800 type = get_histtype(str);
14801 if (argvars[1].v_type == VAR_UNKNOWN)
14802 idx = get_history_idx(type);
14803 else
14804 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14805 /* -1 on type error */
14806 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014809 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014811 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812}
14813
14814/*
14815 * "histnr()" function
14816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014818f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819{
14820 int i;
14821
14822#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 char_u *history = get_tv_string_chk(&argvars[0]);
14824
14825 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 if (i >= HIST_CMD && i < HIST_COUNT)
14827 i = get_history_idx(i);
14828 else
14829#endif
14830 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832}
14833
14834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 * "highlightID(name)" function
14836 */
14837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014838f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014840 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841}
14842
14843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014844 * "highlight_exists()" function
14845 */
14846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014847f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014848{
14849 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14850}
14851
14852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 * "hostname()" function
14854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014856f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857{
14858 char_u hostname[256];
14859
14860 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861 rettv->v_type = VAR_STRING;
14862 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863}
14864
14865/*
14866 * iconv() function
14867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014869f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870{
14871#ifdef FEAT_MBYTE
14872 char_u buf1[NUMBUFLEN];
14873 char_u buf2[NUMBUFLEN];
14874 char_u *from, *to, *str;
14875 vimconv_T vimconv;
14876#endif
14877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878 rettv->v_type = VAR_STRING;
14879 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880
14881#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014882 str = get_tv_string(&argvars[0]);
14883 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14884 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 vimconv.vc_type = CONV_NONE;
14886 convert_setup(&vimconv, from, to);
14887
14888 /* If the encodings are equal, no conversion needed. */
14889 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014890 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014892 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
14894 convert_setup(&vimconv, NULL, NULL);
14895 vim_free(from);
14896 vim_free(to);
14897#endif
14898}
14899
14900/*
14901 * "indent()" function
14902 */
14903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014904f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905{
14906 linenr_T lnum;
14907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913}
14914
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014915/*
14916 * "index()" function
14917 */
14918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014919f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014920{
Bram Moolenaar33570922005-01-25 22:26:29 +000014921 list_T *l;
14922 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014923 long idx = 0;
14924 int ic = FALSE;
14925
14926 rettv->vval.v_number = -1;
14927 if (argvars[0].v_type != VAR_LIST)
14928 {
14929 EMSG(_(e_listreq));
14930 return;
14931 }
14932 l = argvars[0].vval.v_list;
14933 if (l != NULL)
14934 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014935 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014936 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014937 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014938 int error = FALSE;
14939
Bram Moolenaar758711c2005-02-02 23:11:38 +000014940 /* Start at specified item. Use the cached index that list_find()
14941 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014942 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014943 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014944 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014945 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014946 if (error)
14947 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014948 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014949
Bram Moolenaar758711c2005-02-02 23:11:38 +000014950 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014951 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014952 {
14953 rettv->vval.v_number = idx;
14954 break;
14955 }
14956 }
14957}
14958
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959static int inputsecret_flag = 0;
14960
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014961static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014962
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014964 * This function is used by f_input() and f_inputdialog() functions. The third
14965 * argument to f_input() specifies the type of completion to use at the
14966 * prompt. The third argument to f_inputdialog() specifies the value to return
14967 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 */
14969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014970get_user_input(
14971 typval_T *argvars,
14972 typval_T *rettv,
14973 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014975 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 char_u *p = NULL;
14977 int c;
14978 char_u buf[NUMBUFLEN];
14979 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014980 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014981 int xp_type = EXPAND_NOTHING;
14982 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014985 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986
14987#ifdef NO_CONSOLE_INPUT
14988 /* While starting up, there is no place to enter text. */
14989 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991#endif
14992
14993 cmd_silent = FALSE; /* Want to see the prompt. */
14994 if (prompt != NULL)
14995 {
14996 /* Only the part of the message after the last NL is considered as
14997 * prompt for the command line */
14998 p = vim_strrchr(prompt, '\n');
14999 if (p == NULL)
15000 p = prompt;
15001 else
15002 {
15003 ++p;
15004 c = *p;
15005 *p = NUL;
15006 msg_start();
15007 msg_clr_eos();
15008 msg_puts_attr(prompt, echo_attr);
15009 msg_didout = FALSE;
15010 msg_starthere();
15011 *p = c;
15012 }
15013 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015015 if (argvars[1].v_type != VAR_UNKNOWN)
15016 {
15017 defstr = get_tv_string_buf_chk(&argvars[1], buf);
15018 if (defstr != NULL)
15019 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015021 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015022 {
15023 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000015024 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000015025 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015026
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015027 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000015028 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015029
Bram Moolenaar4463f292005-09-25 22:20:24 +000015030 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
15031 if (xp_name == NULL)
15032 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015033
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015034 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015035
Bram Moolenaar4463f292005-09-25 22:20:24 +000015036 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
15037 &xp_arg) == FAIL)
15038 return;
15039 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015040 }
15041
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015042 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015043 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015044 int save_ex_normal_busy = ex_normal_busy;
15045 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015046 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015047 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
15048 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015049 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015050 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020015051 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015052 && argvars[1].v_type != VAR_UNKNOWN
15053 && argvars[2].v_type != VAR_UNKNOWN)
15054 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
15055 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015056
15057 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015059 /* since the user typed this, no need to wait for return */
15060 need_wait_return = FALSE;
15061 msg_didout = FALSE;
15062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 cmd_silent = cmd_silent_save;
15064}
15065
15066/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015067 * "input()" function
15068 * Also handles inputsecret() when inputsecret is set.
15069 */
15070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015072{
15073 get_user_input(argvars, rettv, FALSE);
15074}
15075
15076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 * "inputdialog()" function
15078 */
15079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015080f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081{
15082#if defined(FEAT_GUI_TEXTDIALOG)
15083 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
15084 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
15085 {
15086 char_u *message;
15087 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015088 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015090 message = get_tv_string_chk(&argvars[0]);
15091 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000015092 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000015093 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 else
15095 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015096 if (message != NULL && defstr != NULL
15097 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010015098 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015099 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015100 else
15101 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015102 if (message != NULL && defstr != NULL
15103 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015104 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015105 rettv->vval.v_string = vim_strsave(
15106 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015108 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015110 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111 }
15112 else
15113#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015114 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115}
15116
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015117/*
15118 * "inputlist()" function
15119 */
15120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015121f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015122{
15123 listitem_T *li;
15124 int selected;
15125 int mouse_used;
15126
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015127#ifdef NO_CONSOLE_INPUT
15128 /* While starting up, there is no place to enter text. */
15129 if (no_console_input())
15130 return;
15131#endif
15132 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15133 {
15134 EMSG2(_(e_listarg), "inputlist()");
15135 return;
15136 }
15137
15138 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015139 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015140 lines_left = Rows; /* avoid more prompt */
15141 msg_scroll = TRUE;
15142 msg_clr_eos();
15143
15144 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15145 {
15146 msg_puts(get_tv_string(&li->li_tv));
15147 msg_putchar('\n');
15148 }
15149
15150 /* Ask for choice. */
15151 selected = prompt_for_number(&mouse_used);
15152 if (mouse_used)
15153 selected -= lines_left;
15154
15155 rettv->vval.v_number = selected;
15156}
15157
15158
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15160
15161/*
15162 * "inputrestore()" function
15163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015165f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166{
15167 if (ga_userinput.ga_len > 0)
15168 {
15169 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15171 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015172 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 }
15174 else if (p_verbose > 1)
15175 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015176 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015177 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 }
15179}
15180
15181/*
15182 * "inputsave()" function
15183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015185f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015187 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188 if (ga_grow(&ga_userinput, 1) == OK)
15189 {
15190 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15191 + ga_userinput.ga_len);
15192 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015193 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 }
15195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015196 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197}
15198
15199/*
15200 * "inputsecret()" function
15201 */
15202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015203f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204{
15205 ++cmdline_star;
15206 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 --cmdline_star;
15209 --inputsecret_flag;
15210}
15211
15212/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015213 * "insert()" function
15214 */
15215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015216f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015217{
15218 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 listitem_T *item;
15220 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015221 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015222
15223 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015225 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015226 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 {
15228 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015229 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015230 if (error)
15231 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015232
Bram Moolenaar758711c2005-02-02 23:11:38 +000015233 if (before == l->lv_len)
15234 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015235 else
15236 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015237 item = list_find(l, before);
15238 if (item == NULL)
15239 {
15240 EMSGN(_(e_listidx), before);
15241 l = NULL;
15242 }
15243 }
15244 if (l != NULL)
15245 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015246 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015247 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 }
15249 }
15250}
15251
15252/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015253 * "invert(expr)" function
15254 */
15255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015256f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015257{
15258 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15259}
15260
15261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 * "isdirectory()" function
15263 */
15264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015265f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015268}
15269
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015270/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015271 * "islocked()" function
15272 */
15273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015274f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015275{
15276 lval_T lv;
15277 char_u *end;
15278 dictitem_T *di;
15279
15280 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015281 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15282 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015283 if (end != NULL && lv.ll_name != NULL)
15284 {
15285 if (*end != NUL)
15286 EMSG(_(e_trailing));
15287 else
15288 {
15289 if (lv.ll_tv == NULL)
15290 {
15291 if (check_changedtick(lv.ll_name))
15292 rettv->vval.v_number = 1; /* always locked */
15293 else
15294 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015295 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015296 if (di != NULL)
15297 {
15298 /* Consider a variable locked when:
15299 * 1. the variable itself is locked
15300 * 2. the value of the variable is locked.
15301 * 3. the List or Dict value is locked.
15302 */
15303 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15304 || tv_islocked(&di->di_tv));
15305 }
15306 }
15307 }
15308 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015309 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015310 else if (lv.ll_newkey != NULL)
15311 EMSG2(_(e_dictkey), lv.ll_newkey);
15312 else if (lv.ll_list != NULL)
15313 /* List item. */
15314 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15315 else
15316 /* Dictionary item. */
15317 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15318 }
15319 }
15320
15321 clear_lval(&lv);
15322}
15323
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015324#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15325/*
15326 * "isnan()" function
15327 */
15328 static void
15329f_isnan(typval_T *argvars, typval_T *rettv)
15330{
15331 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15332 && isnan(argvars[0].vval.v_float);
15333}
15334#endif
15335
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015336static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015337
15338/*
15339 * Turn a dict into a list:
15340 * "what" == 0: list of keys
15341 * "what" == 1: list of values
15342 * "what" == 2: list of items
15343 */
15344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015345dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015346{
Bram Moolenaar33570922005-01-25 22:26:29 +000015347 list_T *l2;
15348 dictitem_T *di;
15349 hashitem_T *hi;
15350 listitem_T *li;
15351 listitem_T *li2;
15352 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015353 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015354
Bram Moolenaar8c711452005-01-14 21:53:12 +000015355 if (argvars[0].v_type != VAR_DICT)
15356 {
15357 EMSG(_(e_dictreq));
15358 return;
15359 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015360 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015361 return;
15362
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015363 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015364 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015365
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015366 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015367 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015369 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015371 --todo;
15372 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015373
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015374 li = listitem_alloc();
15375 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015376 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015377 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015378
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015379 if (what == 0)
15380 {
15381 /* keys() */
15382 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015383 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015384 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15385 }
15386 else if (what == 1)
15387 {
15388 /* values() */
15389 copy_tv(&di->di_tv, &li->li_tv);
15390 }
15391 else
15392 {
15393 /* items() */
15394 l2 = list_alloc();
15395 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015396 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015397 li->li_tv.vval.v_list = l2;
15398 if (l2 == NULL)
15399 break;
15400 ++l2->lv_refcount;
15401
15402 li2 = listitem_alloc();
15403 if (li2 == NULL)
15404 break;
15405 list_append(l2, li2);
15406 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015407 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015408 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15409
15410 li2 = listitem_alloc();
15411 if (li2 == NULL)
15412 break;
15413 list_append(l2, li2);
15414 copy_tv(&di->di_tv, &li2->li_tv);
15415 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015416 }
15417 }
15418}
15419
15420/*
15421 * "items(dict)" function
15422 */
15423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015424f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015425{
15426 dict_list(argvars, rettv, 2);
15427}
15428
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015429#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015430/*
15431 * Get the job from the argument.
15432 * Returns NULL if the job is invalid.
15433 */
15434 static job_T *
15435get_job_arg(typval_T *tv)
15436{
15437 job_T *job;
15438
15439 if (tv->v_type != VAR_JOB)
15440 {
15441 EMSG2(_(e_invarg2), get_tv_string(tv));
15442 return NULL;
15443 }
15444 job = tv->vval.v_job;
15445
15446 if (job == NULL)
15447 EMSG(_("E916: not a valid job"));
15448 return job;
15449}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015450
Bram Moolenaar835dc632016-02-07 14:27:38 +010015451/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015452 * "job_getchannel()" function
15453 */
15454 static void
15455f_job_getchannel(typval_T *argvars, typval_T *rettv)
15456{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015457 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015458
Bram Moolenaar65edff82016-02-21 16:40:11 +010015459 if (job != NULL)
15460 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015461 rettv->v_type = VAR_CHANNEL;
15462 rettv->vval.v_channel = job->jv_channel;
15463 if (job->jv_channel != NULL)
15464 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015465 }
15466}
15467
15468/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015469 * "job_info()" function
15470 */
15471 static void
15472f_job_info(typval_T *argvars, typval_T *rettv)
15473{
15474 job_T *job = get_job_arg(&argvars[0]);
15475
15476 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15477 job_info(job, rettv->vval.v_dict);
15478}
15479
15480/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015481 * "job_setoptions()" function
15482 */
15483 static void
15484f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15485{
15486 job_T *job = get_job_arg(&argvars[0]);
15487 jobopt_T opt;
15488
15489 if (job == NULL)
15490 return;
15491 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015492 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15493 job_set_options(job, &opt);
15494 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015495}
15496
15497/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015498 * "job_start()" function
15499 */
15500 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015501f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015502{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015503 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015504 if (check_restricted() || check_secure())
15505 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015506 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015507}
15508
15509/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015510 * "job_status()" function
15511 */
15512 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015513f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015514{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015515 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015516
Bram Moolenaar65edff82016-02-21 16:40:11 +010015517 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015518 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015519 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015520 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015521 }
15522}
15523
15524/*
15525 * "job_stop()" function
15526 */
15527 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015528f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015529{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015530 job_T *job = get_job_arg(&argvars[0]);
15531
15532 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015533 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015534}
15535#endif
15536
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015538 * "join()" function
15539 */
15540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015541f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015542{
15543 garray_T ga;
15544 char_u *sep;
15545
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015546 if (argvars[0].v_type != VAR_LIST)
15547 {
15548 EMSG(_(e_listreq));
15549 return;
15550 }
15551 if (argvars[0].vval.v_list == NULL)
15552 return;
15553 if (argvars[1].v_type == VAR_UNKNOWN)
15554 sep = (char_u *)" ";
15555 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015556 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015557
15558 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015559
15560 if (sep != NULL)
15561 {
15562 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015563 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015564 ga_append(&ga, NUL);
15565 rettv->vval.v_string = (char_u *)ga.ga_data;
15566 }
15567 else
15568 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015569}
15570
15571/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015572 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015573 */
15574 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015575f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015576{
15577 js_read_T reader;
15578
15579 reader.js_buf = get_tv_string(&argvars[0]);
15580 reader.js_fill = NULL;
15581 reader.js_used = 0;
15582 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15583 EMSG(_(e_invarg));
15584}
15585
15586/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015587 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015588 */
15589 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015590f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015591{
15592 rettv->v_type = VAR_STRING;
15593 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15594}
15595
15596/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015597 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015598 */
15599 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015600f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015601{
15602 js_read_T reader;
15603
15604 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015605 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015606 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015607 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015608 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015609}
15610
15611/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015612 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015613 */
15614 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015615f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015616{
15617 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015618 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015619}
15620
15621/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015622 * "keys()" function
15623 */
15624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015625f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015626{
15627 dict_list(argvars, rettv, 0);
15628}
15629
15630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 * "last_buffer_nr()" function.
15632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015634f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635{
15636 int n = 0;
15637 buf_T *buf;
15638
15639 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15640 if (n < buf->b_fnum)
15641 n = buf->b_fnum;
15642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015643 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015644}
15645
15646/*
15647 * "len()" function
15648 */
15649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015650f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015651{
15652 switch (argvars[0].v_type)
15653 {
15654 case VAR_STRING:
15655 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 rettv->vval.v_number = (varnumber_T)STRLEN(
15657 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015658 break;
15659 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015661 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015662 case VAR_DICT:
15663 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15664 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015665 case VAR_UNKNOWN:
15666 case VAR_SPECIAL:
15667 case VAR_FLOAT:
15668 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015669 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015670 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015671 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015672 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015673 break;
15674 }
15675}
15676
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015677static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015678
15679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015680libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015681{
15682#ifdef FEAT_LIBCALL
15683 char_u *string_in;
15684 char_u **string_result;
15685 int nr_result;
15686#endif
15687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015688 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015689 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015690 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015691
15692 if (check_restricted() || check_secure())
15693 return;
15694
15695#ifdef FEAT_LIBCALL
15696 /* The first two args must be strings, otherwise its meaningless */
15697 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15698 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015699 string_in = NULL;
15700 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015701 string_in = argvars[2].vval.v_string;
15702 if (type == VAR_NUMBER)
15703 string_result = NULL;
15704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015705 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015706 if (mch_libcall(argvars[0].vval.v_string,
15707 argvars[1].vval.v_string,
15708 string_in,
15709 argvars[2].vval.v_number,
15710 string_result,
15711 &nr_result) == OK
15712 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015713 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015714 }
15715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716}
15717
15718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015719 * "libcall()" function
15720 */
15721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015722f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723{
15724 libcall_common(argvars, rettv, VAR_STRING);
15725}
15726
15727/*
15728 * "libcallnr()" function
15729 */
15730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015731f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015732{
15733 libcall_common(argvars, rettv, VAR_NUMBER);
15734}
15735
15736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737 * "line(string)" function
15738 */
15739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015740f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741{
15742 linenr_T lnum = 0;
15743 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015744 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015746 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 if (fp != NULL)
15748 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015749 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750}
15751
15752/*
15753 * "line2byte(lnum)" function
15754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015756f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757{
15758#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760#else
15761 linenr_T lnum;
15762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015763 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015765 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015767 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15768 if (rettv->vval.v_number >= 0)
15769 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770#endif
15771}
15772
15773/*
15774 * "lispindent(lnum)" function
15775 */
15776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015777f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778{
15779#ifdef FEAT_LISP
15780 pos_T pos;
15781 linenr_T lnum;
15782
15783 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15786 {
15787 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015788 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 curwin->w_cursor = pos;
15790 }
15791 else
15792#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794}
15795
15796/*
15797 * "localtime()" function
15798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015800f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015802 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803}
15804
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015805static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806
15807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015808get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809{
15810 char_u *keys;
15811 char_u *which;
15812 char_u buf[NUMBUFLEN];
15813 char_u *keys_buf = NULL;
15814 char_u *rhs;
15815 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015816 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015817 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015818 mapblock_T *mp;
15819 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820
15821 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822 rettv->v_type = VAR_STRING;
15823 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015825 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826 if (*keys == NUL)
15827 return;
15828
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015829 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015831 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015833 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015834 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015835 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015836 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015837 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 else
15840 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015841 if (which == NULL)
15842 return;
15843
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 mode = get_map_mode(&which, 0);
15845
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015846 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015847 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015849
15850 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015852 /* Return a string. */
15853 if (rhs != NULL)
15854 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855
Bram Moolenaarbd743252010-10-20 21:23:33 +020015856 }
15857 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15858 {
15859 /* Return a dictionary. */
15860 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15861 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15862 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015863
Bram Moolenaarbd743252010-10-20 21:23:33 +020015864 dict_add_nr_str(dict, "lhs", 0L, lhs);
15865 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15866 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15867 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15868 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15869 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15870 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015871 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015872 dict_add_nr_str(dict, "mode", 0L, mapmode);
15873
15874 vim_free(lhs);
15875 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 }
15877}
15878
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015879#ifdef FEAT_FLOAT
15880/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015881 * "log()" function
15882 */
15883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015884f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015885{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015886 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015887
15888 rettv->v_type = VAR_FLOAT;
15889 if (get_float_arg(argvars, &f) == OK)
15890 rettv->vval.v_float = log(f);
15891 else
15892 rettv->vval.v_float = 0.0;
15893}
15894
15895/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015896 * "log10()" function
15897 */
15898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015899f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015900{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015901 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015902
15903 rettv->v_type = VAR_FLOAT;
15904 if (get_float_arg(argvars, &f) == OK)
15905 rettv->vval.v_float = log10(f);
15906 else
15907 rettv->vval.v_float = 0.0;
15908}
15909#endif
15910
Bram Moolenaar1dced572012-04-05 16:54:08 +020015911#ifdef FEAT_LUA
15912/*
15913 * "luaeval()" function
15914 */
15915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015916f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015917{
15918 char_u *str;
15919 char_u buf[NUMBUFLEN];
15920
15921 str = get_tv_string_buf(&argvars[0], buf);
15922 do_luaeval(str, argvars + 1, rettv);
15923}
15924#endif
15925
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015927 * "map()" function
15928 */
15929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015930f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015931{
15932 filter_map(argvars, rettv, TRUE);
15933}
15934
15935/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015936 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 */
15938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015939f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942}
15943
15944/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 */
15947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015948f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015951}
15952
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015953static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954
15955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015956find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015958 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015959 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015960 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961 char_u *pat;
15962 regmatch_T regmatch;
15963 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015964 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 char_u *save_cpo;
15966 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015967 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015968 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015969 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015970 list_T *l = NULL;
15971 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015972 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015973 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974
15975 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15976 save_cpo = p_cpo;
15977 p_cpo = (char_u *)"";
15978
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015979 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015980 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015981 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015982 /* type 3: return empty list when there are no matches.
15983 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015984 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015985 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015986 if (type == 4
15987 && (list_append_string(rettv->vval.v_list,
15988 (char_u *)"", 0) == FAIL
15989 || list_append_number(rettv->vval.v_list,
15990 (varnumber_T)-1) == FAIL
15991 || list_append_number(rettv->vval.v_list,
15992 (varnumber_T)-1) == FAIL
15993 || list_append_number(rettv->vval.v_list,
15994 (varnumber_T)-1) == FAIL))
15995 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015996 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015997 rettv->vval.v_list = NULL;
15998 goto theend;
15999 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016000 }
16001 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 rettv->v_type = VAR_STRING;
16004 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016007 if (argvars[0].v_type == VAR_LIST)
16008 {
16009 if ((l = argvars[0].vval.v_list) == NULL)
16010 goto theend;
16011 li = l->lv_first;
16012 }
16013 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016014 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016015 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016016 len = (long)STRLEN(str);
16017 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16020 if (pat == NULL)
16021 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016022
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016023 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016025 int error = FALSE;
16026
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016027 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 if (error)
16029 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016030 if (l != NULL)
16031 {
16032 li = list_find(l, start);
16033 if (li == NULL)
16034 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016035 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016036 }
16037 else
16038 {
16039 if (start < 0)
16040 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016041 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016042 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016043 /* When "count" argument is there ignore matches before "start",
16044 * otherwise skip part of the string. Differs when pattern is "^"
16045 * or "\<". */
16046 if (argvars[3].v_type != VAR_UNKNOWN)
16047 startcol = start;
16048 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016049 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016050 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016051 len -= start;
16052 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016053 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016055 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016056 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016057 if (error)
16058 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 }
16060
16061 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16062 if (regmatch.regprog != NULL)
16063 {
16064 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016065
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016066 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016067 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016068 if (l != NULL)
16069 {
16070 if (li == NULL)
16071 {
16072 match = FALSE;
16073 break;
16074 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016075 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016076 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016077 if (str == NULL)
16078 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016079 }
16080
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000016081 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016082
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016083 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016084 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016085 if (l == NULL && !match)
16086 break;
16087
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016088 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016089 if (l != NULL)
16090 {
16091 li = li->li_next;
16092 ++idx;
16093 }
16094 else
16095 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016096#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016097 startcol = (colnr_T)(regmatch.startp[0]
16098 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016099#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020016100 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016101#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016102 if (startcol > (colnr_T)len
16103 || str + startcol <= regmatch.startp[0])
16104 {
16105 match = FALSE;
16106 break;
16107 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016108 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016109 }
16110
16111 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016113 if (type == 4)
16114 {
16115 listitem_T *li1 = rettv->vval.v_list->lv_first;
16116 listitem_T *li2 = li1->li_next;
16117 listitem_T *li3 = li2->li_next;
16118 listitem_T *li4 = li3->li_next;
16119
Bram Moolenaar3c809342016-06-01 22:34:48 +020016120 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016121 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
16122 (int)(regmatch.endp[0] - regmatch.startp[0]));
16123 li3->li_tv.vval.v_number =
16124 (varnumber_T)(regmatch.startp[0] - expr);
16125 li4->li_tv.vval.v_number =
16126 (varnumber_T)(regmatch.endp[0] - expr);
16127 if (l != NULL)
16128 li2->li_tv.vval.v_number = (varnumber_T)idx;
16129 }
16130 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016131 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016132 int i;
16133
16134 /* return list with matched string and submatches */
16135 for (i = 0; i < NSUBEXP; ++i)
16136 {
16137 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016138 {
16139 if (list_append_string(rettv->vval.v_list,
16140 (char_u *)"", 0) == FAIL)
16141 break;
16142 }
16143 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016144 regmatch.startp[i],
16145 (int)(regmatch.endp[i] - regmatch.startp[i]))
16146 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016147 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016148 }
16149 }
16150 else if (type == 2)
16151 {
16152 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016153 if (l != NULL)
16154 copy_tv(&li->li_tv, rettv);
16155 else
16156 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016158 }
16159 else if (l != NULL)
16160 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 else
16162 {
16163 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016164 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 (varnumber_T)(regmatch.startp[0] - str);
16166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016169 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170 }
16171 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016172 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 }
16174
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016175 if (type == 4 && l == NULL)
16176 /* matchstrpos() without a list: drop the second item. */
16177 listitem_remove(rettv->vval.v_list,
16178 rettv->vval.v_list->lv_first->li_next);
16179
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016181 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 p_cpo = save_cpo;
16183}
16184
16185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 * "match()" function
16187 */
16188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016189f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016190{
16191 find_some_match(argvars, rettv, 1);
16192}
16193
16194/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016195 * "matchadd()" function
16196 */
16197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016198f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016199{
16200#ifdef FEAT_SEARCH_EXTRA
16201 char_u buf[NUMBUFLEN];
16202 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16203 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16204 int prio = 10; /* default priority */
16205 int id = -1;
16206 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016207 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016208
16209 rettv->vval.v_number = -1;
16210
16211 if (grp == NULL || pat == NULL)
16212 return;
16213 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016214 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016215 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016216 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016217 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016218 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016219 if (argvars[4].v_type != VAR_UNKNOWN)
16220 {
16221 if (argvars[4].v_type != VAR_DICT)
16222 {
16223 EMSG(_(e_dictreq));
16224 return;
16225 }
16226 if (dict_find(argvars[4].vval.v_dict,
16227 (char_u *)"conceal", -1) != NULL)
16228 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16229 (char_u *)"conceal", FALSE);
16230 }
16231 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016232 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016233 if (error == TRUE)
16234 return;
16235 if (id >= 1 && id <= 3)
16236 {
16237 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16238 return;
16239 }
16240
Bram Moolenaar6561d522015-07-21 15:48:27 +020016241 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16242 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016243#endif
16244}
16245
16246/*
16247 * "matchaddpos()" function
16248 */
16249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016250f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016251{
16252#ifdef FEAT_SEARCH_EXTRA
16253 char_u buf[NUMBUFLEN];
16254 char_u *group;
16255 int prio = 10;
16256 int id = -1;
16257 int error = FALSE;
16258 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016259 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016260
16261 rettv->vval.v_number = -1;
16262
16263 group = get_tv_string_buf_chk(&argvars[0], buf);
16264 if (group == NULL)
16265 return;
16266
16267 if (argvars[1].v_type != VAR_LIST)
16268 {
16269 EMSG2(_(e_listarg), "matchaddpos()");
16270 return;
16271 }
16272 l = argvars[1].vval.v_list;
16273 if (l == NULL)
16274 return;
16275
16276 if (argvars[2].v_type != VAR_UNKNOWN)
16277 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016278 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016279 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016280 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016281 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016282 if (argvars[4].v_type != VAR_UNKNOWN)
16283 {
16284 if (argvars[4].v_type != VAR_DICT)
16285 {
16286 EMSG(_(e_dictreq));
16287 return;
16288 }
16289 if (dict_find(argvars[4].vval.v_dict,
16290 (char_u *)"conceal", -1) != NULL)
16291 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16292 (char_u *)"conceal", FALSE);
16293 }
16294 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016295 }
16296 if (error == TRUE)
16297 return;
16298
16299 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16300 if (id == 1 || id == 2)
16301 {
16302 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16303 return;
16304 }
16305
Bram Moolenaar6561d522015-07-21 15:48:27 +020016306 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16307 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016308#endif
16309}
16310
16311/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016312 * "matcharg()" function
16313 */
16314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016316{
16317 if (rettv_list_alloc(rettv) == OK)
16318 {
16319#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016320 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016321 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016322
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016323 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016324 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016325 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16326 {
16327 list_append_string(rettv->vval.v_list,
16328 syn_id2name(m->hlg_id), -1);
16329 list_append_string(rettv->vval.v_list, m->pattern, -1);
16330 }
16331 else
16332 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016333 list_append_string(rettv->vval.v_list, NULL, -1);
16334 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016335 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016336 }
16337#endif
16338 }
16339}
16340
16341/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016342 * "matchdelete()" function
16343 */
16344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016345f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016346{
16347#ifdef FEAT_SEARCH_EXTRA
16348 rettv->vval.v_number = match_delete(curwin,
16349 (int)get_tv_number(&argvars[0]), TRUE);
16350#endif
16351}
16352
16353/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 * "matchend()" function
16355 */
16356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016357f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358{
16359 find_some_match(argvars, rettv, 0);
16360}
16361
16362/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016363 * "matchlist()" function
16364 */
16365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016366f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016367{
16368 find_some_match(argvars, rettv, 3);
16369}
16370
16371/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016372 * "matchstr()" function
16373 */
16374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016375f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376{
16377 find_some_match(argvars, rettv, 2);
16378}
16379
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016380/*
16381 * "matchstrpos()" function
16382 */
16383 static void
16384f_matchstrpos(typval_T *argvars, typval_T *rettv)
16385{
16386 find_some_match(argvars, rettv, 4);
16387}
16388
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016389static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016390
16391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016392max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016393{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016394 varnumber_T n = 0;
16395 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016396 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016397
16398 if (argvars[0].v_type == VAR_LIST)
16399 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016400 list_T *l;
16401 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016402
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016403 l = argvars[0].vval.v_list;
16404 if (l != NULL)
16405 {
16406 li = l->lv_first;
16407 if (li != NULL)
16408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016410 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016411 {
16412 li = li->li_next;
16413 if (li == NULL)
16414 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016415 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016416 if (domax ? i > n : i < n)
16417 n = i;
16418 }
16419 }
16420 }
16421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016422 else if (argvars[0].v_type == VAR_DICT)
16423 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016425 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016426 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016427 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016428
16429 d = argvars[0].vval.v_dict;
16430 if (d != NULL)
16431 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016432 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016433 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016434 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016435 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016436 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016437 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016438 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016439 if (first)
16440 {
16441 n = i;
16442 first = FALSE;
16443 }
16444 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016445 n = i;
16446 }
16447 }
16448 }
16449 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016450 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016451 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016452 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016453}
16454
16455/*
16456 * "max()" function
16457 */
16458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016459f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016460{
16461 max_min(argvars, rettv, TRUE);
16462}
16463
16464/*
16465 * "min()" function
16466 */
16467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016468f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016469{
16470 max_min(argvars, rettv, FALSE);
16471}
16472
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016473static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016474
16475/*
16476 * Create the directory in which "dir" is located, and higher levels when
16477 * needed.
16478 */
16479 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016480mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016481{
16482 char_u *p;
16483 char_u *updir;
16484 int r = FAIL;
16485
16486 /* Get end of directory name in "dir".
16487 * We're done when it's "/" or "c:/". */
16488 p = gettail_sep(dir);
16489 if (p <= get_past_head(dir))
16490 return OK;
16491
16492 /* If the directory exists we're done. Otherwise: create it.*/
16493 updir = vim_strnsave(dir, (int)(p - dir));
16494 if (updir == NULL)
16495 return FAIL;
16496 if (mch_isdir(updir))
16497 r = OK;
16498 else if (mkdir_recurse(updir, prot) == OK)
16499 r = vim_mkdir_emsg(updir, prot);
16500 vim_free(updir);
16501 return r;
16502}
16503
16504#ifdef vim_mkdir
16505/*
16506 * "mkdir()" function
16507 */
16508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016509f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016510{
16511 char_u *dir;
16512 char_u buf[NUMBUFLEN];
16513 int prot = 0755;
16514
16515 rettv->vval.v_number = FAIL;
16516 if (check_restricted() || check_secure())
16517 return;
16518
16519 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016520 if (*dir == NUL)
16521 rettv->vval.v_number = FAIL;
16522 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016523 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016524 if (*gettail(dir) == NUL)
16525 /* remove trailing slashes */
16526 *gettail_sep(dir) = NUL;
16527
16528 if (argvars[1].v_type != VAR_UNKNOWN)
16529 {
16530 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016531 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016532 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16533 mkdir_recurse(dir, prot);
16534 }
16535 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016536 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016537}
16538#endif
16539
Bram Moolenaar0d660222005-01-07 21:51:51 +000016540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 * "mode()" function
16542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016544f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016546 char_u buf[3];
16547
16548 buf[1] = NUL;
16549 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016551 if (time_for_testing == 93784)
16552 {
16553 /* Testing the two-character code. */
16554 buf[0] = 'x';
16555 buf[1] = '!';
16556 }
16557 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 {
16559 if (VIsual_select)
16560 buf[0] = VIsual_mode + 's' - 'v';
16561 else
16562 buf[0] = VIsual_mode;
16563 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016564 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016565 || State == CONFIRM)
16566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016568 if (State == ASKMORE)
16569 buf[1] = 'm';
16570 else if (State == CONFIRM)
16571 buf[1] = '?';
16572 }
16573 else if (State == EXTERNCMD)
16574 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016575 else if (State & INSERT)
16576 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016577#ifdef FEAT_VREPLACE
16578 if (State & VREPLACE_FLAG)
16579 {
16580 buf[0] = 'R';
16581 buf[1] = 'v';
16582 }
16583 else
16584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 if (State & REPLACE_FLAG)
16586 buf[0] = 'R';
16587 else
16588 buf[0] = 'i';
16589 }
16590 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016592 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016593 if (exmode_active)
16594 buf[1] = 'v';
16595 }
16596 else if (exmode_active)
16597 {
16598 buf[0] = 'c';
16599 buf[1] = 'e';
16600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016601 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016604 if (finish_op)
16605 buf[1] = 'o';
16606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016607
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016608 /* Clear out the minor mode when the argument is not a non-zero number or
16609 * non-empty string. */
16610 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016611 buf[1] = NUL;
16612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016613 rettv->vval.v_string = vim_strsave(buf);
16614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016615}
16616
Bram Moolenaar429fa852013-04-15 12:27:36 +020016617#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016618/*
16619 * "mzeval()" function
16620 */
16621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016622f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016623{
16624 char_u *str;
16625 char_u buf[NUMBUFLEN];
16626
16627 str = get_tv_string_buf(&argvars[0], buf);
16628 do_mzeval(str, rettv);
16629}
Bram Moolenaar75676462013-01-30 14:55:42 +010016630
16631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016632mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016633{
16634 typval_T argvars[3];
16635
16636 argvars[0].v_type = VAR_STRING;
16637 argvars[0].vval.v_string = name;
16638 copy_tv(args, &argvars[1]);
16639 argvars[2].v_type = VAR_UNKNOWN;
16640 f_call(argvars, rettv);
16641 clear_tv(&argvars[1]);
16642}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016643#endif
16644
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 * "nextnonblank()" function
16647 */
16648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016649f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650{
16651 linenr_T lnum;
16652
16653 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016655 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016656 {
16657 lnum = 0;
16658 break;
16659 }
16660 if (*skipwhite(ml_get(lnum)) != NUL)
16661 break;
16662 }
16663 rettv->vval.v_number = lnum;
16664}
16665
16666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 * "nr2char()" function
16668 */
16669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016670f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016671{
16672 char_u buf[NUMBUFLEN];
16673
16674#ifdef FEAT_MBYTE
16675 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016676 {
16677 int utf8 = 0;
16678
16679 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016680 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016681 if (utf8)
16682 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16683 else
16684 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016686 else
16687#endif
16688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016689 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690 buf[1] = NUL;
16691 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692 rettv->v_type = VAR_STRING;
16693 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016694}
16695
16696/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016697 * "or(expr, expr)" function
16698 */
16699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016700f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016701{
16702 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16703 | get_tv_number_chk(&argvars[1], NULL);
16704}
16705
16706/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016707 * "pathshorten()" function
16708 */
16709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016710f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016711{
16712 char_u *p;
16713
16714 rettv->v_type = VAR_STRING;
16715 p = get_tv_string_chk(&argvars[0]);
16716 if (p == NULL)
16717 rettv->vval.v_string = NULL;
16718 else
16719 {
16720 p = vim_strsave(p);
16721 rettv->vval.v_string = p;
16722 if (p != NULL)
16723 shorten_dir(p);
16724 }
16725}
16726
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016727#ifdef FEAT_PERL
16728/*
16729 * "perleval()" function
16730 */
16731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016732f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016733{
16734 char_u *str;
16735 char_u buf[NUMBUFLEN];
16736
16737 str = get_tv_string_buf(&argvars[0], buf);
16738 do_perleval(str, rettv);
16739}
16740#endif
16741
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016742#ifdef FEAT_FLOAT
16743/*
16744 * "pow()" function
16745 */
16746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016747f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016748{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016749 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016750
16751 rettv->v_type = VAR_FLOAT;
16752 if (get_float_arg(argvars, &fx) == OK
16753 && get_float_arg(&argvars[1], &fy) == OK)
16754 rettv->vval.v_float = pow(fx, fy);
16755 else
16756 rettv->vval.v_float = 0.0;
16757}
16758#endif
16759
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016761 * "prevnonblank()" function
16762 */
16763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016764f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016765{
16766 linenr_T lnum;
16767
16768 lnum = get_tv_lnum(argvars);
16769 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16770 lnum = 0;
16771 else
16772 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16773 --lnum;
16774 rettv->vval.v_number = lnum;
16775}
16776
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016777/* This dummy va_list is here because:
16778 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16779 * - locally in the function results in a "used before set" warning
16780 * - using va_start() to initialize it gives "function with fixed args" error */
16781static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016782
Bram Moolenaar8c711452005-01-14 21:53:12 +000016783/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016784 * "printf()" function
16785 */
16786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016787f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016788{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016789 char_u buf[NUMBUFLEN];
16790 int len;
16791 char_u *s;
16792 int saved_did_emsg = did_emsg;
16793 char *fmt;
16794
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016795 rettv->v_type = VAR_STRING;
16796 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016797
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016798 /* Get the required length, allocate the buffer and do it for real. */
16799 did_emsg = FALSE;
16800 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16801 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16802 if (!did_emsg)
16803 {
16804 s = alloc(len + 1);
16805 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016806 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016807 rettv->vval.v_string = s;
16808 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016809 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016810 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016811 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016812}
16813
16814/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016815 * "pumvisible()" function
16816 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016818f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016819{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820#ifdef FEAT_INS_EXPAND
16821 if (pum_visible())
16822 rettv->vval.v_number = 1;
16823#endif
16824}
16825
Bram Moolenaardb913952012-06-29 12:54:53 +020016826#ifdef FEAT_PYTHON3
16827/*
16828 * "py3eval()" function
16829 */
16830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016831f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016832{
16833 char_u *str;
16834 char_u buf[NUMBUFLEN];
16835
16836 str = get_tv_string_buf(&argvars[0], buf);
16837 do_py3eval(str, rettv);
16838}
16839#endif
16840
16841#ifdef FEAT_PYTHON
16842/*
16843 * "pyeval()" function
16844 */
16845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016846f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016847{
16848 char_u *str;
16849 char_u buf[NUMBUFLEN];
16850
16851 str = get_tv_string_buf(&argvars[0], buf);
16852 do_pyeval(str, rettv);
16853}
16854#endif
16855
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016856/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016857 * "range()" function
16858 */
16859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016860f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016861{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016862 varnumber_T start;
16863 varnumber_T end;
16864 varnumber_T stride = 1;
16865 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016866 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016867
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016868 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016869 if (argvars[1].v_type == VAR_UNKNOWN)
16870 {
16871 end = start - 1;
16872 start = 0;
16873 }
16874 else
16875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016878 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016879 }
16880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 if (error)
16882 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016883 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016884 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016885 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016886 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016887 else
16888 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016889 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016890 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016891 if (list_append_number(rettv->vval.v_list,
16892 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016893 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016894 }
16895}
16896
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016897/*
16898 * "readfile()" function
16899 */
16900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016901f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016902{
16903 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016904 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016905 char_u *fname;
16906 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016907 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16908 int io_size = sizeof(buf);
16909 int readlen; /* size of last fread() */
16910 char_u *prev = NULL; /* previously read bytes, if any */
16911 long prevlen = 0; /* length of data in prev */
16912 long prevsize = 0; /* size of prev buffer */
16913 long maxline = MAXLNUM;
16914 long cnt = 0;
16915 char_u *p; /* position in buf */
16916 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016917
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016918 if (argvars[1].v_type != VAR_UNKNOWN)
16919 {
16920 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16921 binary = TRUE;
16922 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016923 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016924 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016925
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016926 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016927 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016928
16929 /* Always open the file in binary mode, library functions have a mind of
16930 * their own about CR-LF conversion. */
16931 fname = get_tv_string(&argvars[0]);
16932 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16933 {
16934 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16935 return;
16936 }
16937
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016938 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016939 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016940 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016941
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016942 /* This for loop processes what was read, but is also entered at end
16943 * of file so that either:
16944 * - an incomplete line gets written
16945 * - a "binary" file gets an empty line at the end if it ends in a
16946 * newline. */
16947 for (p = buf, start = buf;
16948 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16949 ++p)
16950 {
16951 if (*p == '\n' || readlen <= 0)
16952 {
16953 listitem_T *li;
16954 char_u *s = NULL;
16955 long_u len = p - start;
16956
16957 /* Finished a line. Remove CRs before NL. */
16958 if (readlen > 0 && !binary)
16959 {
16960 while (len > 0 && start[len - 1] == '\r')
16961 --len;
16962 /* removal may cross back to the "prev" string */
16963 if (len == 0)
16964 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16965 --prevlen;
16966 }
16967 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016968 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016969 else
16970 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016971 /* Change "prev" buffer to be the right size. This way
16972 * the bytes are only copied once, and very long lines are
16973 * allocated only once. */
16974 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016975 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016976 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016977 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016978 prev = NULL; /* the list will own the string */
16979 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016980 }
16981 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016982 if (s == NULL)
16983 {
16984 do_outofmem_msg((long_u) prevlen + len + 1);
16985 failed = TRUE;
16986 break;
16987 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016988
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016989 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016990 {
16991 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016992 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016993 break;
16994 }
16995 li->li_tv.v_type = VAR_STRING;
16996 li->li_tv.v_lock = 0;
16997 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016998 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016999
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017000 start = p + 1; /* step over newline */
17001 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017002 break;
17003 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017004 else if (*p == NUL)
17005 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020017006#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017007 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
17008 * when finding the BF and check the previous two bytes. */
17009 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020017010 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017011 /* Find the two bytes before the 0xbf. If p is at buf, or buf
17012 * + 1, these may be in the "prev" string. */
17013 char_u back1 = p >= buf + 1 ? p[-1]
17014 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
17015 char_u back2 = p >= buf + 2 ? p[-2]
17016 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
17017 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017018
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017019 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017020 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017021 char_u *dest = p - 2;
17022
17023 /* Usually a BOM is at the beginning of a file, and so at
17024 * the beginning of a line; then we can just step over it.
17025 */
17026 if (start == dest)
17027 start = p + 1;
17028 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020017029 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017030 /* have to shuffle buf to close gap */
17031 int adjust_prevlen = 0;
17032
17033 if (dest < buf)
17034 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017035 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017036 dest = buf;
17037 }
17038 if (readlen > p - buf + 1)
17039 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
17040 readlen -= 3 - adjust_prevlen;
17041 prevlen -= adjust_prevlen;
17042 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020017043 }
17044 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017045 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017046#endif
17047 } /* for */
17048
17049 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
17050 break;
17051 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017052 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017053 /* There's part of a line in buf, store it in "prev". */
17054 if (p - start + prevlen >= prevsize)
17055 {
17056 /* need bigger "prev" buffer */
17057 char_u *newprev;
17058
17059 /* A common use case is ordinary text files and "prev" gets a
17060 * fragment of a line, so the first allocation is made
17061 * small, to avoid repeatedly 'allocing' large and
17062 * 'reallocing' small. */
17063 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017064 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017065 else
17066 {
17067 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017068 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017069 prevsize = grow50pc > growmin ? grow50pc : growmin;
17070 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020017071 newprev = prev == NULL ? alloc(prevsize)
17072 : vim_realloc(prev, prevsize);
17073 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017074 {
17075 do_outofmem_msg((long_u)prevsize);
17076 failed = TRUE;
17077 break;
17078 }
17079 prev = newprev;
17080 }
17081 /* Add the line part to end of "prev". */
17082 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017083 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017084 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017085 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017086
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017087 /*
17088 * For a negative line count use only the lines at the end of the file,
17089 * free the rest.
17090 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017091 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017092 while (cnt > -maxline)
17093 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017094 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017095 --cnt;
17096 }
17097
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017098 if (failed)
17099 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020017100 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017101 /* readfile doc says an empty list is returned on error */
17102 rettv->vval.v_list = list_alloc();
17103 }
17104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017105 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017106 fclose(fd);
17107}
17108
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017109#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017110static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017111
17112/*
17113 * Convert a List to proftime_T.
17114 * Return FAIL when there is something wrong.
17115 */
17116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017117list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017118{
17119 long n1, n2;
17120 int error = FALSE;
17121
17122 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
17123 || arg->vval.v_list->lv_len != 2)
17124 return FAIL;
17125 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
17126 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
17127# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017128 tm->HighPart = n1;
17129 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017130# else
17131 tm->tv_sec = n1;
17132 tm->tv_usec = n2;
17133# endif
17134 return error ? FAIL : OK;
17135}
17136#endif /* FEAT_RELTIME */
17137
17138/*
17139 * "reltime()" function
17140 */
17141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017142f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017143{
17144#ifdef FEAT_RELTIME
17145 proftime_T res;
17146 proftime_T start;
17147
17148 if (argvars[0].v_type == VAR_UNKNOWN)
17149 {
17150 /* No arguments: get current time. */
17151 profile_start(&res);
17152 }
17153 else if (argvars[1].v_type == VAR_UNKNOWN)
17154 {
17155 if (list2proftime(&argvars[0], &res) == FAIL)
17156 return;
17157 profile_end(&res);
17158 }
17159 else
17160 {
17161 /* Two arguments: compute the difference. */
17162 if (list2proftime(&argvars[0], &start) == FAIL
17163 || list2proftime(&argvars[1], &res) == FAIL)
17164 return;
17165 profile_sub(&res, &start);
17166 }
17167
17168 if (rettv_list_alloc(rettv) == OK)
17169 {
17170 long n1, n2;
17171
17172# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017173 n1 = res.HighPart;
17174 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017175# else
17176 n1 = res.tv_sec;
17177 n2 = res.tv_usec;
17178# endif
17179 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17180 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17181 }
17182#endif
17183}
17184
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017185#ifdef FEAT_FLOAT
17186/*
17187 * "reltimefloat()" function
17188 */
17189 static void
17190f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17191{
17192# ifdef FEAT_RELTIME
17193 proftime_T tm;
17194# endif
17195
17196 rettv->v_type = VAR_FLOAT;
17197 rettv->vval.v_float = 0;
17198# ifdef FEAT_RELTIME
17199 if (list2proftime(&argvars[0], &tm) == OK)
17200 rettv->vval.v_float = profile_float(&tm);
17201# endif
17202}
17203#endif
17204
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017205/*
17206 * "reltimestr()" function
17207 */
17208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017209f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017210{
17211#ifdef FEAT_RELTIME
17212 proftime_T tm;
17213#endif
17214
17215 rettv->v_type = VAR_STRING;
17216 rettv->vval.v_string = NULL;
17217#ifdef FEAT_RELTIME
17218 if (list2proftime(&argvars[0], &tm) == OK)
17219 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17220#endif
17221}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017222
Bram Moolenaar0d660222005-01-07 21:51:51 +000017223#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017224static void make_connection(void);
17225static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226
17227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017228make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229{
17230 if (X_DISPLAY == NULL
17231# ifdef FEAT_GUI
17232 && !gui.in_use
17233# endif
17234 )
17235 {
17236 x_force_connect = TRUE;
17237 setup_term_clip();
17238 x_force_connect = FALSE;
17239 }
17240}
17241
17242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017243check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017244{
17245 make_connection();
17246 if (X_DISPLAY == NULL)
17247 {
17248 EMSG(_("E240: No connection to Vim server"));
17249 return FAIL;
17250 }
17251 return OK;
17252}
17253#endif
17254
17255#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017257remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017258{
17259 char_u *server_name;
17260 char_u *keys;
17261 char_u *r = NULL;
17262 char_u buf[NUMBUFLEN];
17263# ifdef WIN32
17264 HWND w;
17265# else
17266 Window w;
17267# endif
17268
17269 if (check_restricted() || check_secure())
17270 return;
17271
17272# ifdef FEAT_X11
17273 if (check_connection() == FAIL)
17274 return;
17275# endif
17276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017277 server_name = get_tv_string_chk(&argvars[0]);
17278 if (server_name == NULL)
17279 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 keys = get_tv_string_buf(&argvars[1], buf);
17281# ifdef WIN32
17282 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17283# else
17284 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17285 < 0)
17286# endif
17287 {
17288 if (r != NULL)
17289 EMSG(r); /* sending worked but evaluation failed */
17290 else
17291 EMSG2(_("E241: Unable to send to %s"), server_name);
17292 return;
17293 }
17294
17295 rettv->vval.v_string = r;
17296
17297 if (argvars[2].v_type != VAR_UNKNOWN)
17298 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017299 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017300 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017301 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017302
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017303 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017304 v.di_tv.v_type = VAR_STRING;
17305 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017306 idvar = get_tv_string_chk(&argvars[2]);
17307 if (idvar != NULL)
17308 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017309 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310 }
17311}
17312#endif
17313
17314/*
17315 * "remote_expr()" function
17316 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017318f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017319{
17320 rettv->v_type = VAR_STRING;
17321 rettv->vval.v_string = NULL;
17322#ifdef FEAT_CLIENTSERVER
17323 remote_common(argvars, rettv, TRUE);
17324#endif
17325}
17326
17327/*
17328 * "remote_foreground()" function
17329 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017331f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017332{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333#ifdef FEAT_CLIENTSERVER
17334# ifdef WIN32
17335 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017336 {
17337 char_u *server_name = get_tv_string_chk(&argvars[0]);
17338
17339 if (server_name != NULL)
17340 serverForeground(server_name);
17341 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017342# else
17343 /* Send a foreground() expression to the server. */
17344 argvars[1].v_type = VAR_STRING;
17345 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17346 argvars[2].v_type = VAR_UNKNOWN;
17347 remote_common(argvars, rettv, TRUE);
17348 vim_free(argvars[1].vval.v_string);
17349# endif
17350#endif
17351}
17352
Bram Moolenaar0d660222005-01-07 21:51:51 +000017353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017354f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017355{
17356#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017357 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017358 char_u *s = NULL;
17359# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017360 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017361# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017362 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017363
17364 if (check_restricted() || check_secure())
17365 {
17366 rettv->vval.v_number = -1;
17367 return;
17368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 serverid = get_tv_string_chk(&argvars[0]);
17370 if (serverid == NULL)
17371 {
17372 rettv->vval.v_number = -1;
17373 return; /* type error; errmsg already given */
17374 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017376 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017377 if (n == 0)
17378 rettv->vval.v_number = -1;
17379 else
17380 {
17381 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17382 rettv->vval.v_number = (s != NULL);
17383 }
17384# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017385 if (check_connection() == FAIL)
17386 return;
17387
17388 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017389 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017390# endif
17391
17392 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017394 char_u *retvar;
17395
Bram Moolenaar33570922005-01-25 22:26:29 +000017396 v.di_tv.v_type = VAR_STRING;
17397 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017398 retvar = get_tv_string_chk(&argvars[1]);
17399 if (retvar != NULL)
17400 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017401 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017402 }
17403#else
17404 rettv->vval.v_number = -1;
17405#endif
17406}
17407
Bram Moolenaar0d660222005-01-07 21:51:51 +000017408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017409f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017410{
17411 char_u *r = NULL;
17412
17413#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017414 char_u *serverid = get_tv_string_chk(&argvars[0]);
17415
17416 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017417 {
17418# ifdef WIN32
17419 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017420 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017422 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017423 if (n != 0)
17424 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17425 if (r == NULL)
17426# else
17427 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017428 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017429# endif
17430 EMSG(_("E277: Unable to read a server reply"));
17431 }
17432#endif
17433 rettv->v_type = VAR_STRING;
17434 rettv->vval.v_string = r;
17435}
17436
17437/*
17438 * "remote_send()" function
17439 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017441f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017442{
17443 rettv->v_type = VAR_STRING;
17444 rettv->vval.v_string = NULL;
17445#ifdef FEAT_CLIENTSERVER
17446 remote_common(argvars, rettv, FALSE);
17447#endif
17448}
17449
17450/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017451 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017452 */
17453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017454f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017455{
Bram Moolenaar33570922005-01-25 22:26:29 +000017456 list_T *l;
17457 listitem_T *item, *item2;
17458 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017459 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017460 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017461 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017462 dict_T *d;
17463 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017464 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017465
Bram Moolenaar8c711452005-01-14 21:53:12 +000017466 if (argvars[0].v_type == VAR_DICT)
17467 {
17468 if (argvars[2].v_type != VAR_UNKNOWN)
17469 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017470 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017471 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017473 key = get_tv_string_chk(&argvars[1]);
17474 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017476 di = dict_find(d, key, -1);
17477 if (di == NULL)
17478 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017479 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17480 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017481 {
17482 *rettv = di->di_tv;
17483 init_tv(&di->di_tv);
17484 dictitem_remove(d, di);
17485 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017486 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017487 }
17488 }
17489 else if (argvars[0].v_type != VAR_LIST)
17490 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017491 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017492 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017494 int error = FALSE;
17495
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017496 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017497 if (error)
17498 ; /* type error: do nothing, errmsg already given */
17499 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017500 EMSGN(_(e_listidx), idx);
17501 else
17502 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017503 if (argvars[2].v_type == VAR_UNKNOWN)
17504 {
17505 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017506 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017507 *rettv = item->li_tv;
17508 vim_free(item);
17509 }
17510 else
17511 {
17512 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017513 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017514 if (error)
17515 ; /* type error: do nothing */
17516 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017517 EMSGN(_(e_listidx), end);
17518 else
17519 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017520 int cnt = 0;
17521
17522 for (li = item; li != NULL; li = li->li_next)
17523 {
17524 ++cnt;
17525 if (li == item2)
17526 break;
17527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017528 if (li == NULL) /* didn't find "item2" after "item" */
17529 EMSG(_(e_invrange));
17530 else
17531 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017532 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017533 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017534 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017535 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017536 l->lv_first = item;
17537 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017538 item->li_prev = NULL;
17539 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017540 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017541 }
17542 }
17543 }
17544 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017545 }
17546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547}
17548
17549/*
17550 * "rename({from}, {to})" function
17551 */
17552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017553f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554{
17555 char_u buf[NUMBUFLEN];
17556
17557 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17561 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562}
17563
17564/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017565 * "repeat()" function
17566 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017568f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017569{
17570 char_u *p;
17571 int n;
17572 int slen;
17573 int len;
17574 char_u *r;
17575 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017576
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017577 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017578 if (argvars[0].v_type == VAR_LIST)
17579 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017580 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017581 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017582 if (list_extend(rettv->vval.v_list,
17583 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017584 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017585 }
17586 else
17587 {
17588 p = get_tv_string(&argvars[0]);
17589 rettv->v_type = VAR_STRING;
17590 rettv->vval.v_string = NULL;
17591
17592 slen = (int)STRLEN(p);
17593 len = slen * n;
17594 if (len <= 0)
17595 return;
17596
17597 r = alloc(len + 1);
17598 if (r != NULL)
17599 {
17600 for (i = 0; i < n; i++)
17601 mch_memmove(r + i * slen, p, (size_t)slen);
17602 r[len] = NUL;
17603 }
17604
17605 rettv->vval.v_string = r;
17606 }
17607}
17608
17609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 * "resolve()" function
17611 */
17612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017613f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614{
17615 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017616#ifdef HAVE_READLINK
17617 char_u *buf = NULL;
17618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017620 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621#ifdef FEAT_SHORTCUT
17622 {
17623 char_u *v = NULL;
17624
17625 v = mch_resolve_shortcut(p);
17626 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017629 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630 }
17631#else
17632# ifdef HAVE_READLINK
17633 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 char_u *cpy;
17635 int len;
17636 char_u *remain = NULL;
17637 char_u *q;
17638 int is_relative_to_current = FALSE;
17639 int has_trailing_pathsep = FALSE;
17640 int limit = 100;
17641
17642 p = vim_strsave(p);
17643
17644 if (p[0] == '.' && (vim_ispathsep(p[1])
17645 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17646 is_relative_to_current = TRUE;
17647
17648 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017649 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017652 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654
17655 q = getnextcomp(p);
17656 if (*q != NUL)
17657 {
17658 /* Separate the first path component in "p", and keep the
17659 * remainder (beginning with the path separator). */
17660 remain = vim_strsave(q - 1);
17661 q[-1] = NUL;
17662 }
17663
Bram Moolenaard9462e32011-04-11 21:35:11 +020017664 buf = alloc(MAXPATHL + 1);
17665 if (buf == NULL)
17666 goto fail;
17667
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 for (;;)
17669 {
17670 for (;;)
17671 {
17672 len = readlink((char *)p, (char *)buf, MAXPATHL);
17673 if (len <= 0)
17674 break;
17675 buf[len] = NUL;
17676
17677 if (limit-- == 0)
17678 {
17679 vim_free(p);
17680 vim_free(remain);
17681 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017683 goto fail;
17684 }
17685
17686 /* Ensure that the result will have a trailing path separator
17687 * if the argument has one. */
17688 if (remain == NULL && has_trailing_pathsep)
17689 add_pathsep(buf);
17690
17691 /* Separate the first path component in the link value and
17692 * concatenate the remainders. */
17693 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17694 if (*q != NUL)
17695 {
17696 if (remain == NULL)
17697 remain = vim_strsave(q - 1);
17698 else
17699 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017700 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 if (cpy != NULL)
17702 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 vim_free(remain);
17704 remain = cpy;
17705 }
17706 }
17707 q[-1] = NUL;
17708 }
17709
17710 q = gettail(p);
17711 if (q > p && *q == NUL)
17712 {
17713 /* Ignore trailing path separator. */
17714 q[-1] = NUL;
17715 q = gettail(p);
17716 }
17717 if (q > p && !mch_isFullName(buf))
17718 {
17719 /* symlink is relative to directory of argument */
17720 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17721 if (cpy != NULL)
17722 {
17723 STRCPY(cpy, p);
17724 STRCPY(gettail(cpy), buf);
17725 vim_free(p);
17726 p = cpy;
17727 }
17728 }
17729 else
17730 {
17731 vim_free(p);
17732 p = vim_strsave(buf);
17733 }
17734 }
17735
17736 if (remain == NULL)
17737 break;
17738
17739 /* Append the first path component of "remain" to "p". */
17740 q = getnextcomp(remain + 1);
17741 len = q - remain - (*q != NUL);
17742 cpy = vim_strnsave(p, STRLEN(p) + len);
17743 if (cpy != NULL)
17744 {
17745 STRNCAT(cpy, remain, len);
17746 vim_free(p);
17747 p = cpy;
17748 }
17749 /* Shorten "remain". */
17750 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017751 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 else
17753 {
17754 vim_free(remain);
17755 remain = NULL;
17756 }
17757 }
17758
17759 /* If the result is a relative path name, make it explicitly relative to
17760 * the current directory if and only if the argument had this form. */
17761 if (!vim_ispathsep(*p))
17762 {
17763 if (is_relative_to_current
17764 && *p != NUL
17765 && !(p[0] == '.'
17766 && (p[1] == NUL
17767 || vim_ispathsep(p[1])
17768 || (p[1] == '.'
17769 && (p[2] == NUL
17770 || vim_ispathsep(p[2]))))))
17771 {
17772 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017773 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 if (cpy != NULL)
17775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 vim_free(p);
17777 p = cpy;
17778 }
17779 }
17780 else if (!is_relative_to_current)
17781 {
17782 /* Strip leading "./". */
17783 q = p;
17784 while (q[0] == '.' && vim_ispathsep(q[1]))
17785 q += 2;
17786 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017787 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 }
17789 }
17790
17791 /* Ensure that the result will have no trailing path separator
17792 * if the argument had none. But keep "/" or "//". */
17793 if (!has_trailing_pathsep)
17794 {
17795 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017796 if (after_pathsep(p, q))
17797 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 }
17799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017800 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 }
17802# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017803 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804# endif
17805#endif
17806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017807 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
17809#ifdef HAVE_READLINK
17810fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017811 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017813 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017814}
17815
17816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 */
17819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017820f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821{
Bram Moolenaar33570922005-01-25 22:26:29 +000017822 list_T *l;
17823 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825 if (argvars[0].v_type != VAR_LIST)
17826 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017827 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017828 && !tv_check_lock(l->lv_lock,
17829 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017830 {
17831 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017832 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017833 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017834 while (li != NULL)
17835 {
17836 ni = li->li_prev;
17837 list_append(l, li);
17838 li = ni;
17839 }
17840 rettv->vval.v_list = l;
17841 rettv->v_type = VAR_LIST;
17842 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017843 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845}
17846
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017847#define SP_NOMOVE 0x01 /* don't move cursor */
17848#define SP_REPEAT 0x02 /* repeat to find outer pair */
17849#define SP_RETCOUNT 0x04 /* return matchcount */
17850#define SP_SETPCMARK 0x08 /* set previous context mark */
17851#define SP_START 0x10 /* accept match at start position */
17852#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17853#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017854#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017855
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017856static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017857
17858/*
17859 * Get flags for a search function.
17860 * Possibly sets "p_ws".
17861 * Returns BACKWARD, FORWARD or zero (for an error).
17862 */
17863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017864get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017865{
17866 int dir = FORWARD;
17867 char_u *flags;
17868 char_u nbuf[NUMBUFLEN];
17869 int mask;
17870
17871 if (varp->v_type != VAR_UNKNOWN)
17872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017873 flags = get_tv_string_buf_chk(varp, nbuf);
17874 if (flags == NULL)
17875 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017876 while (*flags != NUL)
17877 {
17878 switch (*flags)
17879 {
17880 case 'b': dir = BACKWARD; break;
17881 case 'w': p_ws = TRUE; break;
17882 case 'W': p_ws = FALSE; break;
17883 default: mask = 0;
17884 if (flagsp != NULL)
17885 switch (*flags)
17886 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017887 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017888 case 'e': mask = SP_END; break;
17889 case 'm': mask = SP_RETCOUNT; break;
17890 case 'n': mask = SP_NOMOVE; break;
17891 case 'p': mask = SP_SUBPAT; break;
17892 case 'r': mask = SP_REPEAT; break;
17893 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017894 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017895 }
17896 if (mask == 0)
17897 {
17898 EMSG2(_(e_invarg2), flags);
17899 dir = 0;
17900 }
17901 else
17902 *flagsp |= mask;
17903 }
17904 if (dir == 0)
17905 break;
17906 ++flags;
17907 }
17908 }
17909 return dir;
17910}
17911
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017913 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017914 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017916search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017918 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 char_u *pat;
17920 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017921 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 int save_p_ws = p_ws;
17923 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017924 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017925 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017926 proftime_T tm;
17927#ifdef FEAT_RELTIME
17928 long time_limit = 0;
17929#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017930 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017931 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017934 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017935 if (dir == 0)
17936 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017937 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017938 if (flags & SP_START)
17939 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017940 if (flags & SP_END)
17941 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017942 if (flags & SP_COLUMN)
17943 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017944
Bram Moolenaar76929292008-01-06 19:07:36 +000017945 /* Optional arguments: line number to stop searching and timeout. */
17946 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017947 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017948 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017949 if (lnum_stop < 0)
17950 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017951#ifdef FEAT_RELTIME
17952 if (argvars[3].v_type != VAR_UNKNOWN)
17953 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017954 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017955 if (time_limit < 0)
17956 goto theend;
17957 }
17958#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017959 }
17960
Bram Moolenaar76929292008-01-06 19:07:36 +000017961#ifdef FEAT_RELTIME
17962 /* Set the time limit, if there is one. */
17963 profile_setlimit(time_limit, &tm);
17964#endif
17965
Bram Moolenaar231334e2005-07-25 20:46:57 +000017966 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017967 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017968 * Check to make sure only those flags are set.
17969 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17970 * flags cannot be set. Check for that condition also.
17971 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017972 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017973 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017975 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017976 goto theend;
17977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017979 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017980 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017981 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017982 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017984 if (flags & SP_SUBPAT)
17985 retval = subpatnum;
17986 else
17987 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017988 if (flags & SP_SETPCMARK)
17989 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017991 if (match_pos != NULL)
17992 {
17993 /* Store the match cursor position */
17994 match_pos->lnum = pos.lnum;
17995 match_pos->col = pos.col + 1;
17996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 /* "/$" will put the cursor after the end of the line, may need to
17998 * correct that here */
17999 check_cursor();
18000 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018001
18002 /* If 'n' flag is used: restore cursor position. */
18003 if (flags & SP_NOMOVE)
18004 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000018005 else
18006 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018007theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018009
18010 return retval;
18011}
18012
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018013#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018014
18015/*
18016 * round() is not in C90, use ceil() or floor() instead.
18017 */
18018 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010018019vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018020{
18021 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
18022}
18023
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018024/*
18025 * "round({float})" function
18026 */
18027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018028f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018029{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018030 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018031
18032 rettv->v_type = VAR_FLOAT;
18033 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018034 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018035 else
18036 rettv->vval.v_float = 0.0;
18037}
18038#endif
18039
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018040/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020018041 * "screenattr()" function
18042 */
18043 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018044f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018045{
18046 int row;
18047 int col;
18048 int c;
18049
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018050 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18051 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018052 if (row < 0 || row >= screen_Rows
18053 || col < 0 || col >= screen_Columns)
18054 c = -1;
18055 else
18056 c = ScreenAttrs[LineOffset[row] + col];
18057 rettv->vval.v_number = c;
18058}
18059
18060/*
18061 * "screenchar()" function
18062 */
18063 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018064f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018065{
18066 int row;
18067 int col;
18068 int off;
18069 int c;
18070
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018071 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18072 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018073 if (row < 0 || row >= screen_Rows
18074 || col < 0 || col >= screen_Columns)
18075 c = -1;
18076 else
18077 {
18078 off = LineOffset[row] + col;
18079#ifdef FEAT_MBYTE
18080 if (enc_utf8 && ScreenLinesUC[off] != 0)
18081 c = ScreenLinesUC[off];
18082 else
18083#endif
18084 c = ScreenLines[off];
18085 }
18086 rettv->vval.v_number = c;
18087}
18088
18089/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018090 * "screencol()" function
18091 *
18092 * First column is 1 to be consistent with virtcol().
18093 */
18094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018096{
18097 rettv->vval.v_number = screen_screencol() + 1;
18098}
18099
18100/*
18101 * "screenrow()" function
18102 */
18103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018104f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018105{
18106 rettv->vval.v_number = screen_screenrow() + 1;
18107}
18108
18109/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018110 * "search()" function
18111 */
18112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018113f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018114{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018115 int flags = 0;
18116
18117 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118}
18119
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018121 * "searchdecl()" function
18122 */
18123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018124f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018125{
18126 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018127 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018128 int error = FALSE;
18129 char_u *name;
18130
18131 rettv->vval.v_number = 1; /* default: FAIL */
18132
18133 name = get_tv_string_chk(&argvars[0]);
18134 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018135 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018136 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018137 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018138 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018139 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018140 if (!error && name != NULL)
18141 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018142 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018143}
18144
18145/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018146 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018148 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018149searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150{
18151 char_u *spat, *mpat, *epat;
18152 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 int dir;
18155 int flags = 0;
18156 char_u nbuf1[NUMBUFLEN];
18157 char_u nbuf2[NUMBUFLEN];
18158 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018159 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018160 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018161 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018164 spat = get_tv_string_chk(&argvars[0]);
18165 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18166 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18167 if (spat == NULL || mpat == NULL || epat == NULL)
18168 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 /* Handle the optional fourth argument: flags */
18171 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018172 if (dir == 0)
18173 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018174
18175 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018176 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18177 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018178 if ((flags & (SP_END | SP_SUBPAT)) != 0
18179 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018180 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018181 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018182 goto theend;
18183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018185 /* Using 'r' implies 'W', otherwise it doesn't work. */
18186 if (flags & SP_REPEAT)
18187 p_ws = FALSE;
18188
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018189 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018190 if (argvars[3].v_type == VAR_UNKNOWN
18191 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 skip = (char_u *)"";
18193 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018195 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018196 if (argvars[5].v_type != VAR_UNKNOWN)
18197 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018198 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018199 if (lnum_stop < 0)
18200 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018201#ifdef FEAT_RELTIME
18202 if (argvars[6].v_type != VAR_UNKNOWN)
18203 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018204 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018205 if (time_limit < 0)
18206 goto theend;
18207 }
18208#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018209 }
18210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018211 if (skip == NULL)
18212 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018214 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018215 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018216
18217theend:
18218 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018219
18220 return retval;
18221}
18222
18223/*
18224 * "searchpair()" function
18225 */
18226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018227f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018228{
18229 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18230}
18231
18232/*
18233 * "searchpairpos()" function
18234 */
18235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018236f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018237{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018238 pos_T match_pos;
18239 int lnum = 0;
18240 int col = 0;
18241
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018242 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018243 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018244
18245 if (searchpair_cmn(argvars, &match_pos) > 0)
18246 {
18247 lnum = match_pos.lnum;
18248 col = match_pos.col;
18249 }
18250
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018251 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18252 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018253}
18254
18255/*
18256 * Search for a start/middle/end thing.
18257 * Used by searchpair(), see its documentation for the details.
18258 * Returns 0 or -1 for no match,
18259 */
18260 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018261do_searchpair(
18262 char_u *spat, /* start pattern */
18263 char_u *mpat, /* middle pattern */
18264 char_u *epat, /* end pattern */
18265 int dir, /* BACKWARD or FORWARD */
18266 char_u *skip, /* skip expression */
18267 int flags, /* SP_SETPCMARK and other SP_ values */
18268 pos_T *match_pos,
18269 linenr_T lnum_stop, /* stop at this line if not zero */
18270 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018271{
18272 char_u *save_cpo;
18273 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18274 long retval = 0;
18275 pos_T pos;
18276 pos_T firstpos;
18277 pos_T foundpos;
18278 pos_T save_cursor;
18279 pos_T save_pos;
18280 int n;
18281 int r;
18282 int nest = 1;
18283 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018284 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018285 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018286
18287 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18288 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018289 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018290
Bram Moolenaar76929292008-01-06 19:07:36 +000018291#ifdef FEAT_RELTIME
18292 /* Set the time limit, if there is one. */
18293 profile_setlimit(time_limit, &tm);
18294#endif
18295
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018296 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18297 * start/middle/end (pat3, for the top pair). */
18298 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18299 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18300 if (pat2 == NULL || pat3 == NULL)
18301 goto theend;
18302 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18303 if (*mpat == NUL)
18304 STRCPY(pat3, pat2);
18305 else
18306 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18307 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018308 if (flags & SP_START)
18309 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018310
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 save_cursor = curwin->w_cursor;
18312 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018313 clearpos(&firstpos);
18314 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315 pat = pat3;
18316 for (;;)
18317 {
18318 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018319 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18321 /* didn't find it or found the first match again: FAIL */
18322 break;
18323
18324 if (firstpos.lnum == 0)
18325 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018326 if (equalpos(pos, foundpos))
18327 {
18328 /* Found the same position again. Can happen with a pattern that
18329 * has "\zs" at the end and searching backwards. Advance one
18330 * character and try again. */
18331 if (dir == BACKWARD)
18332 decl(&pos);
18333 else
18334 incl(&pos);
18335 }
18336 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018338 /* clear the start flag to avoid getting stuck here */
18339 options &= ~SEARCH_START;
18340
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 /* If the skip pattern matches, ignore this match. */
18342 if (*skip != NUL)
18343 {
18344 save_pos = curwin->w_cursor;
18345 curwin->w_cursor = pos;
18346 r = eval_to_bool(skip, &err, NULL, FALSE);
18347 curwin->w_cursor = save_pos;
18348 if (err)
18349 {
18350 /* Evaluating {skip} caused an error, break here. */
18351 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018352 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 break;
18354 }
18355 if (r)
18356 continue;
18357 }
18358
18359 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18360 {
18361 /* Found end when searching backwards or start when searching
18362 * forward: nested pair. */
18363 ++nest;
18364 pat = pat2; /* nested, don't search for middle */
18365 }
18366 else
18367 {
18368 /* Found end when searching forward or start when searching
18369 * backward: end of (nested) pair; or found middle in outer pair. */
18370 if (--nest == 1)
18371 pat = pat3; /* outer level, search for middle */
18372 }
18373
18374 if (nest == 0)
18375 {
18376 /* Found the match: return matchcount or line number. */
18377 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018378 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018380 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018381 if (flags & SP_SETPCMARK)
18382 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 curwin->w_cursor = pos;
18384 if (!(flags & SP_REPEAT))
18385 break;
18386 nest = 1; /* search for next unmatched */
18387 }
18388 }
18389
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018390 if (match_pos != NULL)
18391 {
18392 /* Store the match cursor position */
18393 match_pos->lnum = curwin->w_cursor.lnum;
18394 match_pos->col = curwin->w_cursor.col + 1;
18395 }
18396
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018398 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 curwin->w_cursor = save_cursor;
18400
18401theend:
18402 vim_free(pat2);
18403 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018404 if (p_cpo == empty_option)
18405 p_cpo = save_cpo;
18406 else
18407 /* Darn, evaluating the {skip} expression changed the value. */
18408 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018409
18410 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411}
18412
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018413/*
18414 * "searchpos()" function
18415 */
18416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018417f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018418{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018419 pos_T match_pos;
18420 int lnum = 0;
18421 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018422 int n;
18423 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018425 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018426 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018427
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018428 n = search_cmn(argvars, &match_pos, &flags);
18429 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018430 {
18431 lnum = match_pos.lnum;
18432 col = match_pos.col;
18433 }
18434
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018435 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18436 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018437 if (flags & SP_SUBPAT)
18438 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018439}
18440
Bram Moolenaar0d660222005-01-07 21:51:51 +000018441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018442f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018444#ifdef FEAT_CLIENTSERVER
18445 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018446 char_u *server = get_tv_string_chk(&argvars[0]);
18447 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448
Bram Moolenaar0d660222005-01-07 21:51:51 +000018449 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 if (server == NULL || reply == NULL)
18451 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452 if (check_restricted() || check_secure())
18453 return;
18454# ifdef FEAT_X11
18455 if (check_connection() == FAIL)
18456 return;
18457# endif
18458
18459 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018461 EMSG(_("E258: Unable to send to client"));
18462 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 rettv->vval.v_number = 0;
18465#else
18466 rettv->vval.v_number = -1;
18467#endif
18468}
18469
Bram Moolenaar0d660222005-01-07 21:51:51 +000018470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018471f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018472{
18473 char_u *r = NULL;
18474
18475#ifdef FEAT_CLIENTSERVER
18476# ifdef WIN32
18477 r = serverGetVimNames();
18478# else
18479 make_connection();
18480 if (X_DISPLAY != NULL)
18481 r = serverGetVimNames(X_DISPLAY);
18482# endif
18483#endif
18484 rettv->v_type = VAR_STRING;
18485 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486}
18487
18488/*
18489 * "setbufvar()" function
18490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018492f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493{
18494 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018497 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 char_u nbuf[NUMBUFLEN];
18499
18500 if (check_restricted() || check_secure())
18501 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018502 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18503 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018504 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 varp = &argvars[2];
18506
18507 if (buf != NULL && varname != NULL && varp != NULL)
18508 {
18509 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511
18512 if (*varname == '&')
18513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018514 long numval;
18515 char_u *strval;
18516 int error = FALSE;
18517
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018519 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018520 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018521 if (!error && strval != NULL)
18522 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 }
18524 else
18525 {
18526 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18527 if (bufvarname != NULL)
18528 {
18529 STRCPY(bufvarname, "b:");
18530 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018531 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532 vim_free(bufvarname);
18533 }
18534 }
18535
18536 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539}
18540
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018542f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018543{
18544 dict_T *d;
18545 dictitem_T *di;
18546 char_u *csearch;
18547
18548 if (argvars[0].v_type != VAR_DICT)
18549 {
18550 EMSG(_(e_dictreq));
18551 return;
18552 }
18553
18554 if ((d = argvars[0].vval.v_dict) != NULL)
18555 {
18556 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18557 if (csearch != NULL)
18558 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018559#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018560 if (enc_utf8)
18561 {
18562 int pcc[MAX_MCO];
18563 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018564
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018565 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18566 }
18567 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018568#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018569 set_last_csearch(PTR2CHAR(csearch),
18570 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018571 }
18572
18573 di = dict_find(d, (char_u *)"forward", -1);
18574 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018575 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018576 ? FORWARD : BACKWARD);
18577
18578 di = dict_find(d, (char_u *)"until", -1);
18579 if (di != NULL)
18580 set_csearch_until(!!get_tv_number(&di->di_tv));
18581 }
18582}
18583
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584/*
18585 * "setcmdpos()" function
18586 */
18587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018588f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018590 int pos = (int)get_tv_number(&argvars[0]) - 1;
18591
18592 if (pos >= 0)
18593 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018594}
18595
18596/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018597 * "setfperm({fname}, {mode})" function
18598 */
18599 static void
18600f_setfperm(typval_T *argvars, typval_T *rettv)
18601{
18602 char_u *fname;
18603 char_u modebuf[NUMBUFLEN];
18604 char_u *mode_str;
18605 int i;
18606 int mask;
18607 int mode = 0;
18608
18609 rettv->vval.v_number = 0;
18610 fname = get_tv_string_chk(&argvars[0]);
18611 if (fname == NULL)
18612 return;
18613 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18614 if (mode_str == NULL)
18615 return;
18616 if (STRLEN(mode_str) != 9)
18617 {
18618 EMSG2(_(e_invarg2), mode_str);
18619 return;
18620 }
18621
18622 mask = 1;
18623 for (i = 8; i >= 0; --i)
18624 {
18625 if (mode_str[i] != '-')
18626 mode |= mask;
18627 mask = mask << 1;
18628 }
18629 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18630}
18631
18632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 * "setline()" function
18634 */
18635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018636f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
18638 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018639 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018640 list_T *l = NULL;
18641 listitem_T *li = NULL;
18642 long added = 0;
18643 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018645 lnum = get_tv_lnum(&argvars[0]);
18646 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018648 l = argvars[1].vval.v_list;
18649 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018651 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018652 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018653
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018654 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018655 for (;;)
18656 {
18657 if (l != NULL)
18658 {
18659 /* list argument, get next string */
18660 if (li == NULL)
18661 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018662 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018663 li = li->li_next;
18664 }
18665
18666 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018667 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018668 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018669
18670 /* When coming here from Insert mode, sync undo, so that this can be
18671 * undone separately from what was previously inserted. */
18672 if (u_sync_once == 2)
18673 {
18674 u_sync_once = 1; /* notify that u_sync() was called */
18675 u_sync(TRUE);
18676 }
18677
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018678 if (lnum <= curbuf->b_ml.ml_line_count)
18679 {
18680 /* existing line, replace it */
18681 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18682 {
18683 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018684 if (lnum == curwin->w_cursor.lnum)
18685 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018686 rettv->vval.v_number = 0; /* OK */
18687 }
18688 }
18689 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18690 {
18691 /* lnum is one past the last line, append the line */
18692 ++added;
18693 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18694 rettv->vval.v_number = 0; /* OK */
18695 }
18696
18697 if (l == NULL) /* only one string argument */
18698 break;
18699 ++lnum;
18700 }
18701
18702 if (added > 0)
18703 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704}
18705
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018706static 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 +000018707
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018709 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018710 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018712set_qf_ll_list(
18713 win_T *wp UNUSED,
18714 typval_T *list_arg UNUSED,
18715 typval_T *action_arg UNUSED,
18716 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018717{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018718#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018719 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018720 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018721 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018722#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018723
Bram Moolenaar2641f772005-03-25 21:58:17 +000018724 rettv->vval.v_number = -1;
18725
18726#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018727 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018728 EMSG(_(e_listreq));
18729 else
18730 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018731 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018732
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018733 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018734 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018735 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018736 if (act == NULL)
18737 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018738 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018739 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018740 else
18741 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018742 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018743 else if (action_arg->v_type == VAR_UNKNOWN)
18744 action = ' ';
18745 else
18746 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018747
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018748 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018749 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018750 rettv->vval.v_number = 0;
18751 }
18752#endif
18753}
18754
18755/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018756 * "setloclist()" function
18757 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018759f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018760{
18761 win_T *win;
18762
18763 rettv->vval.v_number = -1;
18764
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018765 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018766 if (win != NULL)
18767 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18768}
18769
18770/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018771 * "setmatches()" function
18772 */
18773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018774f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018775{
18776#ifdef FEAT_SEARCH_EXTRA
18777 list_T *l;
18778 listitem_T *li;
18779 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018780 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018781
18782 rettv->vval.v_number = -1;
18783 if (argvars[0].v_type != VAR_LIST)
18784 {
18785 EMSG(_(e_listreq));
18786 return;
18787 }
18788 if ((l = argvars[0].vval.v_list) != NULL)
18789 {
18790
18791 /* To some extent make sure that we are dealing with a list from
18792 * "getmatches()". */
18793 li = l->lv_first;
18794 while (li != NULL)
18795 {
18796 if (li->li_tv.v_type != VAR_DICT
18797 || (d = li->li_tv.vval.v_dict) == NULL)
18798 {
18799 EMSG(_(e_invarg));
18800 return;
18801 }
18802 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018803 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18804 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018805 && dict_find(d, (char_u *)"priority", -1) != NULL
18806 && dict_find(d, (char_u *)"id", -1) != NULL))
18807 {
18808 EMSG(_(e_invarg));
18809 return;
18810 }
18811 li = li->li_next;
18812 }
18813
18814 clear_matches(curwin);
18815 li = l->lv_first;
18816 while (li != NULL)
18817 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018818 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018819 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018820 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018821 char_u *group;
18822 int priority;
18823 int id;
18824 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018825
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018826 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018827 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18828 {
18829 if (s == NULL)
18830 {
18831 s = list_alloc();
18832 if (s == NULL)
18833 return;
18834 }
18835
18836 /* match from matchaddpos() */
18837 for (i = 1; i < 9; i++)
18838 {
18839 sprintf((char *)buf, (char *)"pos%d", i);
18840 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18841 {
18842 if (di->di_tv.v_type != VAR_LIST)
18843 return;
18844
18845 list_append_tv(s, &di->di_tv);
18846 s->lv_refcount++;
18847 }
18848 else
18849 break;
18850 }
18851 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018852
18853 group = get_dict_string(d, (char_u *)"group", FALSE);
18854 priority = (int)get_dict_number(d, (char_u *)"priority");
18855 id = (int)get_dict_number(d, (char_u *)"id");
18856 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18857 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18858 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018859 if (i == 0)
18860 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018861 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018862 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018863 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018864 }
18865 else
18866 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018867 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018868 list_unref(s);
18869 s = NULL;
18870 }
18871
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018872 li = li->li_next;
18873 }
18874 rettv->vval.v_number = 0;
18875 }
18876#endif
18877}
18878
18879/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018880 * "setpos()" function
18881 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018883f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018884{
18885 pos_T pos;
18886 int fnum;
18887 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018888 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018889
Bram Moolenaar08250432008-02-13 11:42:46 +000018890 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018891 name = get_tv_string_chk(argvars);
18892 if (name != NULL)
18893 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018894 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018895 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018896 if (--pos.col < 0)
18897 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018898 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018899 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018900 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018901 if (fnum == curbuf->b_fnum)
18902 {
18903 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018904 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018905 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018906 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018907 curwin->w_set_curswant = FALSE;
18908 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018909 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018910 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018911 }
18912 else
18913 EMSG(_(e_invarg));
18914 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018915 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18916 {
18917 /* set mark */
18918 if (setmark_pos(name[1], &pos, fnum) == OK)
18919 rettv->vval.v_number = 0;
18920 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018921 else
18922 EMSG(_(e_invarg));
18923 }
18924 }
18925}
18926
18927/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018928 * "setqflist()" function
18929 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018931f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018932{
18933 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18934}
18935
18936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 * "setreg()" function
18938 */
18939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018940f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018941{
18942 int regname;
18943 char_u *strregname;
18944 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018945 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 int append;
18947 char_u yank_type;
18948 long block_len;
18949
18950 block_len = -1;
18951 yank_type = MAUTO;
18952 append = FALSE;
18953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018954 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018955 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018957 if (strregname == NULL)
18958 return; /* type error; errmsg already given */
18959 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 if (regname == 0 || regname == '@')
18961 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018965 stropt = get_tv_string_chk(&argvars[2]);
18966 if (stropt == NULL)
18967 return; /* type error */
18968 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 switch (*stropt)
18970 {
18971 case 'a': case 'A': /* append */
18972 append = TRUE;
18973 break;
18974 case 'v': case 'c': /* character-wise selection */
18975 yank_type = MCHAR;
18976 break;
18977 case 'V': case 'l': /* line-wise selection */
18978 yank_type = MLINE;
18979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 case 'b': case Ctrl_V: /* block-wise selection */
18981 yank_type = MBLOCK;
18982 if (VIM_ISDIGIT(stropt[1]))
18983 {
18984 ++stropt;
18985 block_len = getdigits(&stropt) - 1;
18986 --stropt;
18987 }
18988 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989 }
18990 }
18991
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018992 if (argvars[1].v_type == VAR_LIST)
18993 {
18994 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018995 char_u **allocval;
18996 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018997 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018998 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018999 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019000 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019001 int len;
19002
19003 /* If the list is NULL handle like an empty list. */
19004 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019005
Bram Moolenaar7d647822014-04-05 21:28:56 +020019006 /* First half: use for pointers to result lines; second half: use for
19007 * pointers to allocated copies. */
19008 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019009 if (lstval == NULL)
19010 return;
19011 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020019012 allocval = lstval + len + 2;
19013 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019014
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019015 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019016 li = li->li_next)
19017 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019018 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019019 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020019020 goto free_lstval;
19021 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019022 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019023 /* Need to make a copy, next get_tv_string_buf_chk() will
19024 * overwrite the string. */
19025 strval = vim_strsave(buf);
19026 if (strval == NULL)
19027 goto free_lstval;
19028 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019029 }
19030 *curval++ = strval;
19031 }
19032 *curval++ = NULL;
19033
19034 write_reg_contents_lst(regname, lstval, -1,
19035 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020019036free_lstval:
19037 while (curallocval > allocval)
19038 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019039 vim_free(lstval);
19040 }
19041 else
19042 {
19043 strval = get_tv_string_chk(&argvars[1]);
19044 if (strval == NULL)
19045 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019049 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050}
19051
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019052/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019053 * "settabvar()" function
19054 */
19055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019056f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019057{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019058#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019059 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019060 tabpage_T *tp;
19061#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019062 char_u *varname, *tabvarname;
19063 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019064
19065 rettv->vval.v_number = 0;
19066
19067 if (check_restricted() || check_secure())
19068 return;
19069
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019070#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019071 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019072#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019073 varname = get_tv_string_chk(&argvars[1]);
19074 varp = &argvars[2];
19075
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019076 if (varname != NULL && varp != NULL
19077#ifdef FEAT_WINDOWS
19078 && tp != NULL
19079#endif
19080 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019081 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019082#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019083 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019084 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019085#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019086
19087 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
19088 if (tabvarname != NULL)
19089 {
19090 STRCPY(tabvarname, "t:");
19091 STRCPY(tabvarname + 2, varname);
19092 set_var(tabvarname, varp, TRUE);
19093 vim_free(tabvarname);
19094 }
19095
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019096#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019097 /* Restore current tabpage */
19098 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019099 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019100#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019101 }
19102}
19103
19104/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019105 * "settabwinvar()" function
19106 */
19107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019108f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019109{
19110 setwinvar(argvars, rettv, 1);
19111}
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112
19113/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019114 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019117f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019119 setwinvar(argvars, rettv, 0);
19120}
19121
19122/*
19123 * "setwinvar()" and "settabwinvar()" functions
19124 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020019125
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019127setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019128{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129 win_T *win;
19130#ifdef FEAT_WINDOWS
19131 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019132 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019133 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134#endif
19135 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019138 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139
19140 if (check_restricted() || check_secure())
19141 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019142
19143#ifdef FEAT_WINDOWS
19144 if (off == 1)
19145 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19146 else
19147 tp = curtab;
19148#endif
19149 win = find_win_by_nr(&argvars[off], tp);
19150 varname = get_tv_string_chk(&argvars[off + 1]);
19151 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152
19153 if (win != NULL && varname != NULL && varp != NULL)
19154 {
19155#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019156 need_switch_win = !(tp == curtab && win == curwin);
19157 if (!need_switch_win
19158 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019161 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019163 long numval;
19164 char_u *strval;
19165 int error = FALSE;
19166
19167 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019168 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019169 strval = get_tv_string_buf_chk(varp, nbuf);
19170 if (!error && strval != NULL)
19171 set_option_value(varname, numval, strval, OPT_LOCAL);
19172 }
19173 else
19174 {
19175 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19176 if (winvarname != NULL)
19177 {
19178 STRCPY(winvarname, "w:");
19179 STRCPY(winvarname + 2, varname);
19180 set_var(winvarname, varp, TRUE);
19181 vim_free(winvarname);
19182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183 }
19184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019186 if (need_switch_win)
19187 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188#endif
19189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190}
19191
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019192#ifdef FEAT_CRYPT
19193/*
19194 * "sha256({string})" function
19195 */
19196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019197f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019198{
19199 char_u *p;
19200
19201 p = get_tv_string(&argvars[0]);
19202 rettv->vval.v_string = vim_strsave(
19203 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19204 rettv->v_type = VAR_STRING;
19205}
19206#endif /* FEAT_CRYPT */
19207
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019209 * "shellescape({string})" function
19210 */
19211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019212f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019213{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019214 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019215 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019216 rettv->v_type = VAR_STRING;
19217}
19218
19219/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019220 * shiftwidth() function
19221 */
19222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019223f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019224{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019225 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019226}
19227
19228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019229 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 */
19231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019232f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235
Bram Moolenaar0d660222005-01-07 21:51:51 +000019236 p = get_tv_string(&argvars[0]);
19237 rettv->vval.v_string = vim_strsave(p);
19238 simplify_filename(rettv->vval.v_string); /* simplify in place */
19239 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240}
19241
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019242#ifdef FEAT_FLOAT
19243/*
19244 * "sin()" function
19245 */
19246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019247f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019248{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019249 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019250
19251 rettv->v_type = VAR_FLOAT;
19252 if (get_float_arg(argvars, &f) == OK)
19253 rettv->vval.v_float = sin(f);
19254 else
19255 rettv->vval.v_float = 0.0;
19256}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019257
19258/*
19259 * "sinh()" function
19260 */
19261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019262f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019263{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019264 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019265
19266 rettv->v_type = VAR_FLOAT;
19267 if (get_float_arg(argvars, &f) == OK)
19268 rettv->vval.v_float = sinh(f);
19269 else
19270 rettv->vval.v_float = 0.0;
19271}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019272#endif
19273
Bram Moolenaar0d660222005-01-07 21:51:51 +000019274static int
19275#ifdef __BORLANDC__
19276 _RTLENTRYF
19277#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019278 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279static int
19280#ifdef __BORLANDC__
19281 _RTLENTRYF
19282#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019283 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019284
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019285/* struct used in the array that's given to qsort() */
19286typedef struct
19287{
19288 listitem_T *item;
19289 int idx;
19290} sortItem_T;
19291
Bram Moolenaar0b962472016-02-22 22:51:33 +010019292/* struct storing information about current sort */
19293typedef struct
19294{
19295 int item_compare_ic;
19296 int item_compare_numeric;
19297 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019298#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019299 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019300#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019301 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019302 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019303 dict_T *item_compare_selfdict;
19304 int item_compare_func_err;
19305 int item_compare_keep_zero;
19306} sortinfo_T;
19307static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019308static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019309#define ITEM_COMPARE_FAIL 999
19310
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019312 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 static int
19315#ifdef __BORLANDC__
19316_RTLENTRYF
19317#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019318item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019320 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019321 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019322 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019323 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 int res;
19325 char_u numbuf1[NUMBUFLEN];
19326 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019328 si1 = (sortItem_T *)s1;
19329 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019330 tv1 = &si1->item->li_tv;
19331 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019332
Bram Moolenaar0b962472016-02-22 22:51:33 +010019333 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019334 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019335 varnumber_T v1 = get_tv_number(tv1);
19336 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019337
19338 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19339 }
19340
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019341#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019342 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019343 {
19344 float_T v1 = get_tv_float(tv1);
19345 float_T v2 = get_tv_float(tv2);
19346
19347 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19348 }
19349#endif
19350
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019351 /* tv2string() puts quotes around a string and allocates memory. Don't do
19352 * that for string variables. Use a single quote when comparing with a
19353 * non-string to do what the docs promise. */
19354 if (tv1->v_type == VAR_STRING)
19355 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019356 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019357 p1 = (char_u *)"'";
19358 else
19359 p1 = tv1->vval.v_string;
19360 }
19361 else
19362 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19363 if (tv2->v_type == VAR_STRING)
19364 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019366 p2 = (char_u *)"'";
19367 else
19368 p2 = tv2->vval.v_string;
19369 }
19370 else
19371 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019372 if (p1 == NULL)
19373 p1 = (char_u *)"";
19374 if (p2 == NULL)
19375 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019376 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019377 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019378 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019379 res = STRICMP(p1, p2);
19380 else
19381 res = STRCMP(p1, p2);
19382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019384 {
19385 double n1, n2;
19386 n1 = strtod((char *)p1, (char **)&p1);
19387 n2 = strtod((char *)p2, (char **)&p2);
19388 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19389 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019390
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019391 /* When the result would be zero, compare the item indexes. Makes the
19392 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019393 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019394 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019395
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396 vim_free(tofree1);
19397 vim_free(tofree2);
19398 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399}
19400
19401 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402#ifdef __BORLANDC__
19403_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019405item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019406{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019407 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019408 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019409 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019410 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019411 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019412 char_u *func_name;
19413 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019415 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019416 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019417 return 0;
19418
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019419 si1 = (sortItem_T *)s1;
19420 si2 = (sortItem_T *)s2;
19421
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019422 if (partial == NULL)
19423 func_name = sortinfo->item_compare_func;
19424 else
19425 func_name = partial->pt_name;
19426
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019427 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019428 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019429 copy_tv(&si1->item->li_tv, &argv[0]);
19430 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431
19432 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019433 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019434 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019435 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019436 clear_tv(&argv[0]);
19437 clear_tv(&argv[1]);
19438
19439 if (res == FAIL)
19440 res = ITEM_COMPARE_FAIL;
19441 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019442 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019443 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019444 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019445 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019446
19447 /* When the result would be zero, compare the pointers themselves. Makes
19448 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019449 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019450 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019451
Bram Moolenaar0d660222005-01-07 21:51:51 +000019452 return res;
19453}
19454
19455/*
19456 * "sort({list})" function
19457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019459do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460{
Bram Moolenaar33570922005-01-25 22:26:29 +000019461 list_T *l;
19462 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019463 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019464 sortinfo_T *old_sortinfo;
19465 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019466 long len;
19467 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468
Bram Moolenaar0b962472016-02-22 22:51:33 +010019469 /* Pointer to current info struct used in compare function. Save and
19470 * restore the current one for nested calls. */
19471 old_sortinfo = sortinfo;
19472 sortinfo = &info;
19473
Bram Moolenaar0d660222005-01-07 21:51:51 +000019474 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019475 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 else
19477 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019478 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019479 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019480 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19481 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019482 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019483 rettv->vval.v_list = l;
19484 rettv->v_type = VAR_LIST;
19485 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486
Bram Moolenaar0d660222005-01-07 21:51:51 +000019487 len = list_len(l);
19488 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019489 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490
Bram Moolenaar0b962472016-02-22 22:51:33 +010019491 info.item_compare_ic = FALSE;
19492 info.item_compare_numeric = FALSE;
19493 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019494#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019495 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019496#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019497 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019498 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019499 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019500 if (argvars[1].v_type != VAR_UNKNOWN)
19501 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019502 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019503 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019504 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019505 else if (argvars[1].v_type == VAR_PARTIAL)
19506 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019507 else
19508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019509 int error = FALSE;
19510
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019511 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019512 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019513 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019514 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019515 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019516 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019517 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019518 else if (i != 0)
19519 {
19520 EMSG(_(e_invarg));
19521 goto theend;
19522 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019523 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019524 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019525 if (*info.item_compare_func == NUL)
19526 {
19527 /* empty string means default sort */
19528 info.item_compare_func = NULL;
19529 }
19530 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019531 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019532 info.item_compare_func = NULL;
19533 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019534 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019535 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019536 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019537 info.item_compare_func = NULL;
19538 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019539 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019540#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019541 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019542 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019543 info.item_compare_func = NULL;
19544 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019545 }
19546#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019547 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019548 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019549 info.item_compare_func = NULL;
19550 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019551 }
19552 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019553 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019554
19555 if (argvars[2].v_type != VAR_UNKNOWN)
19556 {
19557 /* optional third argument: {dict} */
19558 if (argvars[2].v_type != VAR_DICT)
19559 {
19560 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019561 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019562 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019563 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566
Bram Moolenaar0d660222005-01-07 21:51:51 +000019567 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019568 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019569 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019570 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571
Bram Moolenaar327aa022014-03-25 18:24:23 +010019572 i = 0;
19573 if (sort)
19574 {
19575 /* sort(): ptrs will be the list to sort */
19576 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019577 {
19578 ptrs[i].item = li;
19579 ptrs[i].idx = i;
19580 ++i;
19581 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019582
Bram Moolenaar0b962472016-02-22 22:51:33 +010019583 info.item_compare_func_err = FALSE;
19584 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019585 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019586 if ((info.item_compare_func != NULL
19587 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019588 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019589 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019590 EMSG(_("E702: Sort compare function failed"));
19591 else
19592 {
19593 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019594 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019595 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019596 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019597 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019598
Bram Moolenaar0b962472016-02-22 22:51:33 +010019599 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019600 {
19601 /* Clear the List and append the items in sorted order. */
19602 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19603 l->lv_len = 0;
19604 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019605 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019606 }
19607 }
19608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019610 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019611 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019612
19613 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019614 info.item_compare_func_err = FALSE;
19615 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019616 item_compare_func_ptr = info.item_compare_func != NULL
19617 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019618 ? item_compare2 : item_compare;
19619
19620 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19621 li = li->li_next)
19622 {
19623 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19624 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019625 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019626 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019627 {
19628 EMSG(_("E882: Uniq compare function failed"));
19629 break;
19630 }
19631 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019632
Bram Moolenaar0b962472016-02-22 22:51:33 +010019633 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019634 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019635 while (--i >= 0)
19636 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019637 li = ptrs[i].item->li_next;
19638 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019639 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019640 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019641 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019642 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019643 list_fix_watch(l, li);
19644 listitem_free(li);
19645 l->lv_len--;
19646 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019647 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019648 }
19649
19650 vim_free(ptrs);
19651 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019652theend:
19653 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019654}
19655
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019656/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019657 * "sort({list})" function
19658 */
19659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019660f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019661{
19662 do_sort_uniq(argvars, rettv, TRUE);
19663}
19664
19665/*
19666 * "uniq({list})" function
19667 */
19668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019669f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019670{
19671 do_sort_uniq(argvars, rettv, FALSE);
19672}
19673
19674/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019675 * "soundfold({word})" function
19676 */
19677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019678f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019679{
19680 char_u *s;
19681
19682 rettv->v_type = VAR_STRING;
19683 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019684#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019685 rettv->vval.v_string = eval_soundfold(s);
19686#else
19687 rettv->vval.v_string = vim_strsave(s);
19688#endif
19689}
19690
19691/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019692 * "spellbadword()" function
19693 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019695f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019696{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019697 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019698 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019699 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019702 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019703
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019704#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019705 if (argvars[0].v_type == VAR_UNKNOWN)
19706 {
19707 /* Find the start and length of the badly spelled word. */
19708 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19709 if (len != 0)
19710 word = ml_get_cursor();
19711 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019712 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019713 {
19714 char_u *str = get_tv_string_chk(&argvars[0]);
19715 int capcol = -1;
19716
19717 if (str != NULL)
19718 {
19719 /* Check the argument for spelling. */
19720 while (*str != NUL)
19721 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019722 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019723 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019724 {
19725 word = str;
19726 break;
19727 }
19728 str += len;
19729 }
19730 }
19731 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019732#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019733
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019734 list_append_string(rettv->vval.v_list, word, len);
19735 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019736 attr == HLF_SPB ? "bad" :
19737 attr == HLF_SPR ? "rare" :
19738 attr == HLF_SPL ? "local" :
19739 attr == HLF_SPC ? "caps" :
19740 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019741}
19742
19743/*
19744 * "spellsuggest()" function
19745 */
19746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019747f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019748{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019749#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019750 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019751 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019752 int maxcount;
19753 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019754 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019755 listitem_T *li;
19756 int need_capital = FALSE;
19757#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019758
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019759 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019760 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019761
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019762#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019763 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019764 {
19765 str = get_tv_string(&argvars[0]);
19766 if (argvars[1].v_type != VAR_UNKNOWN)
19767 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019768 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019769 if (maxcount <= 0)
19770 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019771 if (argvars[2].v_type != VAR_UNKNOWN)
19772 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019773 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019774 if (typeerr)
19775 return;
19776 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019777 }
19778 else
19779 maxcount = 25;
19780
Bram Moolenaar4770d092006-01-12 23:22:24 +000019781 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019782
19783 for (i = 0; i < ga.ga_len; ++i)
19784 {
19785 str = ((char_u **)ga.ga_data)[i];
19786
19787 li = listitem_alloc();
19788 if (li == NULL)
19789 vim_free(str);
19790 else
19791 {
19792 li->li_tv.v_type = VAR_STRING;
19793 li->li_tv.v_lock = 0;
19794 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019795 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019796 }
19797 }
19798 ga_clear(&ga);
19799 }
19800#endif
19801}
19802
Bram Moolenaar0d660222005-01-07 21:51:51 +000019803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019804f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019805{
19806 char_u *str;
19807 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019808 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019809 regmatch_T regmatch;
19810 char_u patbuf[NUMBUFLEN];
19811 char_u *save_cpo;
19812 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019813 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019814 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019815 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019816
19817 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19818 save_cpo = p_cpo;
19819 p_cpo = (char_u *)"";
19820
19821 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019822 if (argvars[1].v_type != VAR_UNKNOWN)
19823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019824 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19825 if (pat == NULL)
19826 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019827 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019828 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019829 }
19830 if (pat == NULL || *pat == NUL)
19831 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019832
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019833 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019835 if (typeerr)
19836 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837
Bram Moolenaar0d660222005-01-07 21:51:51 +000019838 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19839 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019841 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019842 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019843 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019844 if (*str == NUL)
19845 match = FALSE; /* empty item at the end */
19846 else
19847 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019848 if (match)
19849 end = regmatch.startp[0];
19850 else
19851 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019852 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19853 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019854 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019855 if (list_append_string(rettv->vval.v_list, str,
19856 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019857 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019858 }
19859 if (!match)
19860 break;
19861 /* Advance to just after the match. */
19862 if (regmatch.endp[0] > str)
19863 col = 0;
19864 else
19865 {
19866 /* Don't get stuck at the same match. */
19867#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019868 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019869#else
19870 col = 1;
19871#endif
19872 }
19873 str = regmatch.endp[0];
19874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875
Bram Moolenaar473de612013-06-08 18:19:48 +020019876 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878
Bram Moolenaar0d660222005-01-07 21:51:51 +000019879 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880}
19881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019882#ifdef FEAT_FLOAT
19883/*
19884 * "sqrt()" function
19885 */
19886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019887f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019888{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019889 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019890
19891 rettv->v_type = VAR_FLOAT;
19892 if (get_float_arg(argvars, &f) == OK)
19893 rettv->vval.v_float = sqrt(f);
19894 else
19895 rettv->vval.v_float = 0.0;
19896}
19897
19898/*
19899 * "str2float()" function
19900 */
19901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019902f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019903{
19904 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19905
19906 if (*p == '+')
19907 p = skipwhite(p + 1);
19908 (void)string2float(p, &rettv->vval.v_float);
19909 rettv->v_type = VAR_FLOAT;
19910}
19911#endif
19912
Bram Moolenaar2c932302006-03-18 21:42:09 +000019913/*
19914 * "str2nr()" function
19915 */
19916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019917f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019918{
19919 int base = 10;
19920 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019921 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019922 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019923
19924 if (argvars[1].v_type != VAR_UNKNOWN)
19925 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019926 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019927 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019928 {
19929 EMSG(_(e_invarg));
19930 return;
19931 }
19932 }
19933
19934 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019935 if (*p == '+')
19936 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019937 switch (base)
19938 {
19939 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19940 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19941 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19942 default: what = 0;
19943 }
19944 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019945 rettv->vval.v_number = n;
19946}
19947
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948#ifdef HAVE_STRFTIME
19949/*
19950 * "strftime({format}[, {time}])" function
19951 */
19952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019953f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954{
19955 char_u result_buf[256];
19956 struct tm *curtime;
19957 time_t seconds;
19958 char_u *p;
19959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019960 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019962 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 seconds = time(NULL);
19965 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019966 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 curtime = localtime(&seconds);
19968 /* MSVC returns NULL for an invalid value of seconds. */
19969 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019970 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 else
19972 {
19973# ifdef FEAT_MBYTE
19974 vimconv_T conv;
19975 char_u *enc;
19976
19977 conv.vc_type = CONV_NONE;
19978 enc = enc_locale();
19979 convert_setup(&conv, p_enc, enc);
19980 if (conv.vc_type != CONV_NONE)
19981 p = string_convert(&conv, p, NULL);
19982# endif
19983 if (p != NULL)
19984 (void)strftime((char *)result_buf, sizeof(result_buf),
19985 (char *)p, curtime);
19986 else
19987 result_buf[0] = NUL;
19988
19989# ifdef FEAT_MBYTE
19990 if (conv.vc_type != CONV_NONE)
19991 vim_free(p);
19992 convert_setup(&conv, enc, p_enc);
19993 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 else
19996# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998
19999# ifdef FEAT_MBYTE
20000 /* Release conversion descriptors */
20001 convert_setup(&conv, NULL, NULL);
20002 vim_free(enc);
20003# endif
20004 }
20005}
20006#endif
20007
20008/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020009 * "strgetchar()" function
20010 */
20011 static void
20012f_strgetchar(typval_T *argvars, typval_T *rettv)
20013{
20014 char_u *str;
20015 int len;
20016 int error = FALSE;
20017 int charidx;
20018
20019 rettv->vval.v_number = -1;
20020 str = get_tv_string_chk(&argvars[0]);
20021 if (str == NULL)
20022 return;
20023 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020024 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020025 if (error)
20026 return;
20027#ifdef FEAT_MBYTE
20028 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020029 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020030
20031 while (charidx >= 0 && byteidx < len)
20032 {
20033 if (charidx == 0)
20034 {
20035 rettv->vval.v_number = mb_ptr2char(str + byteidx);
20036 break;
20037 }
20038 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020039 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020040 }
20041 }
20042#else
20043 if (charidx < len)
20044 rettv->vval.v_number = str[charidx];
20045#endif
20046}
20047
20048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 * "stridx()" function
20050 */
20051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020052f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053{
20054 char_u buf[NUMBUFLEN];
20055 char_u *needle;
20056 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000020057 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000020059 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020061 needle = get_tv_string_chk(&argvars[1]);
20062 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000020063 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020064 if (needle == NULL || haystack == NULL)
20065 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066
Bram Moolenaar33570922005-01-25 22:26:29 +000020067 if (argvars[2].v_type != VAR_UNKNOWN)
20068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020069 int error = FALSE;
20070
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020071 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020072 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000020073 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020074 if (start_idx >= 0)
20075 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 }
20077
20078 pos = (char_u *)strstr((char *)haystack, (char *)needle);
20079 if (pos != NULL)
20080 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081}
20082
20083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020084 * "string()" function
20085 */
20086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020087f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020088{
20089 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020090 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010020093 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
20094 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020095 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020096 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098}
20099
20100/*
20101 * "strlen()" function
20102 */
20103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020104f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 rettv->vval.v_number = (varnumber_T)(STRLEN(
20107 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108}
20109
20110/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020111 * "strchars()" function
20112 */
20113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020114f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020115{
20116 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020117 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020020118#ifdef FEAT_MBYTE
20119 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020120 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020020121#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020122
20123 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020124 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020125 if (skipcc < 0 || skipcc > 1)
20126 EMSG(_(e_invarg));
20127 else
20128 {
20129#ifdef FEAT_MBYTE
20130 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20131 while (*s != NUL)
20132 {
20133 func_mb_ptr2char_adv(&s);
20134 ++len;
20135 }
20136 rettv->vval.v_number = len;
20137#else
20138 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20139#endif
20140 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020141}
20142
20143/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020144 * "strdisplaywidth()" function
20145 */
20146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020147f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020148{
20149 char_u *s = get_tv_string(&argvars[0]);
20150 int col = 0;
20151
20152 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020153 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020154
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020155 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020156}
20157
20158/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020159 * "strwidth()" function
20160 */
20161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020162f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020163{
20164 char_u *s = get_tv_string(&argvars[0]);
20165
20166 rettv->vval.v_number = (varnumber_T)(
20167#ifdef FEAT_MBYTE
20168 mb_string2cells(s, -1)
20169#else
20170 STRLEN(s)
20171#endif
20172 );
20173}
20174
20175/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020176 * "strcharpart()" function
20177 */
20178 static void
20179f_strcharpart(typval_T *argvars, typval_T *rettv)
20180{
20181#ifdef FEAT_MBYTE
20182 char_u *p;
20183 int nchar;
20184 int nbyte = 0;
20185 int charlen;
20186 int len = 0;
20187 int slen;
20188 int error = FALSE;
20189
20190 p = get_tv_string(&argvars[0]);
20191 slen = (int)STRLEN(p);
20192
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020193 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020194 if (!error)
20195 {
20196 if (nchar > 0)
20197 while (nchar > 0 && nbyte < slen)
20198 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020199 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020200 --nchar;
20201 }
20202 else
20203 nbyte = nchar;
20204 if (argvars[2].v_type != VAR_UNKNOWN)
20205 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020206 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020207 while (charlen > 0 && nbyte + len < slen)
20208 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020209 int off = nbyte + len;
20210
20211 if (off < 0)
20212 len += 1;
20213 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020214 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020215 --charlen;
20216 }
20217 }
20218 else
20219 len = slen - nbyte; /* default: all bytes that are available. */
20220 }
20221
20222 /*
20223 * Only return the overlap between the specified part and the actual
20224 * string.
20225 */
20226 if (nbyte < 0)
20227 {
20228 len += nbyte;
20229 nbyte = 0;
20230 }
20231 else if (nbyte > slen)
20232 nbyte = slen;
20233 if (len < 0)
20234 len = 0;
20235 else if (nbyte + len > slen)
20236 len = slen - nbyte;
20237
20238 rettv->v_type = VAR_STRING;
20239 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20240#else
20241 f_strpart(argvars, rettv);
20242#endif
20243}
20244
20245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 * "strpart()" function
20247 */
20248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020249f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250{
20251 char_u *p;
20252 int n;
20253 int len;
20254 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020257 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258 slen = (int)STRLEN(p);
20259
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020260 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020261 if (error)
20262 len = 0;
20263 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020264 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 else
20266 len = slen - n; /* default len: all bytes that are available. */
20267
20268 /*
20269 * Only return the overlap between the specified part and the actual
20270 * string.
20271 */
20272 if (n < 0)
20273 {
20274 len += n;
20275 n = 0;
20276 }
20277 else if (n > slen)
20278 n = slen;
20279 if (len < 0)
20280 len = 0;
20281 else if (n + len > slen)
20282 len = slen - n;
20283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020284 rettv->v_type = VAR_STRING;
20285 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286}
20287
20288/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020289 * "strridx()" function
20290 */
20291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020292f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020293{
20294 char_u buf[NUMBUFLEN];
20295 char_u *needle;
20296 char_u *haystack;
20297 char_u *rest;
20298 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020299 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020301 needle = get_tv_string_chk(&argvars[1]);
20302 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020303
20304 rettv->vval.v_number = -1;
20305 if (needle == NULL || haystack == NULL)
20306 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020307
20308 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020309 if (argvars[2].v_type != VAR_UNKNOWN)
20310 {
20311 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020312 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020313 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020314 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020315 }
20316 else
20317 end_idx = haystack_len;
20318
Bram Moolenaar0d660222005-01-07 21:51:51 +000020319 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020320 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020321 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020322 lastmatch = haystack + end_idx;
20323 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020324 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020325 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020326 for (rest = haystack; *rest != '\0'; ++rest)
20327 {
20328 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020329 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020330 break;
20331 lastmatch = rest;
20332 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020333 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020334
20335 if (lastmatch == NULL)
20336 rettv->vval.v_number = -1;
20337 else
20338 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20339}
20340
20341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 * "strtrans()" function
20343 */
20344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020345f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020347 rettv->v_type = VAR_STRING;
20348 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349}
20350
20351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020352 * "submatch()" function
20353 */
20354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020355f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020356{
Bram Moolenaar41571762014-04-02 19:00:58 +020020357 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020358 int no;
20359 int retList = 0;
20360
20361 no = (int)get_tv_number_chk(&argvars[0], &error);
20362 if (error)
20363 return;
20364 error = FALSE;
20365 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020366 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020367 if (error)
20368 return;
20369
20370 if (retList == 0)
20371 {
20372 rettv->v_type = VAR_STRING;
20373 rettv->vval.v_string = reg_submatch(no);
20374 }
20375 else
20376 {
20377 rettv->v_type = VAR_LIST;
20378 rettv->vval.v_list = reg_submatch_list(no);
20379 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020380}
20381
20382/*
20383 * "substitute()" function
20384 */
20385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020386f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020387{
20388 char_u patbuf[NUMBUFLEN];
20389 char_u subbuf[NUMBUFLEN];
20390 char_u flagsbuf[NUMBUFLEN];
20391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020392 char_u *str = get_tv_string_chk(&argvars[0]);
20393 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20394 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20395 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20396
Bram Moolenaar0d660222005-01-07 21:51:51 +000020397 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020398 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20399 rettv->vval.v_string = NULL;
20400 else
20401 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020402}
20403
20404/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020405 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020408f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409{
20410 int id = 0;
20411#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020412 linenr_T lnum;
20413 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020415 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020417 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020418 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20419 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020421 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020422 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020423 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424#endif
20425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020426 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427}
20428
20429/*
20430 * "synIDattr(id, what [, mode])" function
20431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020433f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434{
20435 char_u *p = NULL;
20436#ifdef FEAT_SYN_HL
20437 int id;
20438 char_u *what;
20439 char_u *mode;
20440 char_u modebuf[NUMBUFLEN];
20441 int modec;
20442
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020443 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020449 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 modec = 0; /* replace invalid with current */
20451 }
20452 else
20453 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020454#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020455 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 modec = 'g';
20457 else
20458#endif
20459 if (t_colors > 1)
20460 modec = 'c';
20461 else
20462 modec = 't';
20463 }
20464
20465
20466 switch (TOLOWER_ASC(what[0]))
20467 {
20468 case 'b':
20469 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20470 p = highlight_color(id, what, modec);
20471 else /* bold */
20472 p = highlight_has_attr(id, HL_BOLD, modec);
20473 break;
20474
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020475 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 p = highlight_color(id, what, modec);
20477 break;
20478
20479 case 'i':
20480 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20481 p = highlight_has_attr(id, HL_INVERSE, modec);
20482 else /* italic */
20483 p = highlight_has_attr(id, HL_ITALIC, modec);
20484 break;
20485
20486 case 'n': /* name */
20487 p = get_highlight_name(NULL, id - 1);
20488 break;
20489
20490 case 'r': /* reverse */
20491 p = highlight_has_attr(id, HL_INVERSE, modec);
20492 break;
20493
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020494 case 's':
20495 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20496 p = highlight_color(id, what, modec);
20497 else /* standout */
20498 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 break;
20500
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020501 case 'u':
20502 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20503 /* underline */
20504 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20505 else
20506 /* undercurl */
20507 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 break;
20509 }
20510
20511 if (p != NULL)
20512 p = vim_strsave(p);
20513#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020514 rettv->v_type = VAR_STRING;
20515 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516}
20517
20518/*
20519 * "synIDtrans(id)" function
20520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020522f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523{
20524 int id;
20525
20526#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020527 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528
20529 if (id > 0)
20530 id = syn_get_final_id(id);
20531 else
20532#endif
20533 id = 0;
20534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020535 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536}
20537
20538/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020539 * "synconcealed(lnum, col)" function
20540 */
20541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020542f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020543{
20544#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020545 linenr_T lnum;
20546 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020547 int syntax_flags = 0;
20548 int cchar;
20549 int matchid = 0;
20550 char_u str[NUMBUFLEN];
20551#endif
20552
20553 rettv->v_type = VAR_LIST;
20554 rettv->vval.v_list = NULL;
20555
20556#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20557 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020558 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020559
20560 vim_memset(str, NUL, sizeof(str));
20561
20562 if (rettv_list_alloc(rettv) != FAIL)
20563 {
20564 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20565 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20566 && curwin->w_p_cole > 0)
20567 {
20568 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20569 syntax_flags = get_syntax_info(&matchid);
20570
20571 /* get the conceal character */
20572 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20573 {
20574 cchar = syn_get_sub_char();
20575 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20576 cchar = lcs_conceal;
20577 if (cchar != NUL)
20578 {
20579# ifdef FEAT_MBYTE
20580 if (has_mbyte)
20581 (*mb_char2bytes)(cchar, str);
20582 else
20583# endif
20584 str[0] = cchar;
20585 }
20586 }
20587 }
20588
20589 list_append_number(rettv->vval.v_list,
20590 (syntax_flags & HL_CONCEAL) != 0);
20591 /* -1 to auto-determine strlen */
20592 list_append_string(rettv->vval.v_list, str, -1);
20593 list_append_number(rettv->vval.v_list, matchid);
20594 }
20595#endif
20596}
20597
20598/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020599 * "synstack(lnum, col)" function
20600 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020602f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020603{
20604#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020605 linenr_T lnum;
20606 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020607 int i;
20608 int id;
20609#endif
20610
20611 rettv->v_type = VAR_LIST;
20612 rettv->vval.v_list = NULL;
20613
20614#ifdef FEAT_SYN_HL
20615 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020616 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020617
20618 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020619 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020620 && rettv_list_alloc(rettv) != FAIL)
20621 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020622 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020623 for (i = 0; ; ++i)
20624 {
20625 id = syn_get_stack_item(i);
20626 if (id < 0)
20627 break;
20628 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20629 break;
20630 }
20631 }
20632#endif
20633}
20634
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636get_cmd_output_as_rettv(
20637 typval_T *argvars,
20638 typval_T *rettv,
20639 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020641 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020643 char_u *infile = NULL;
20644 char_u buf[NUMBUFLEN];
20645 int err = FALSE;
20646 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020647 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020648 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020650 rettv->v_type = VAR_STRING;
20651 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020652 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020653 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020654
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020655 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020656 {
20657 /*
20658 * Write the string to a temp file, to be used for input of the shell
20659 * command.
20660 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020661 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020662 {
20663 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020664 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020665 }
20666
20667 fd = mch_fopen((char *)infile, WRITEBIN);
20668 if (fd == NULL)
20669 {
20670 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020671 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020672 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020673 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020674 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020675 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20676 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020677 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020678 else
20679 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020680 size_t len;
20681
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020682 p = get_tv_string_buf_chk(&argvars[1], buf);
20683 if (p == NULL)
20684 {
20685 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020686 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020687 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020688 len = STRLEN(p);
20689 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020690 err = TRUE;
20691 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020692 if (fclose(fd) != 0)
20693 err = TRUE;
20694 if (err)
20695 {
20696 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020697 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020698 }
20699 }
20700
Bram Moolenaar52a72462014-08-29 15:53:52 +020020701 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20702 * echoes typeahead, that messes up the display. */
20703 if (!msg_silent)
20704 flags += SHELL_COOKED;
20705
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020706 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020707 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020708 int len;
20709 listitem_T *li;
20710 char_u *s = NULL;
20711 char_u *start;
20712 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020713 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714
Bram Moolenaar52a72462014-08-29 15:53:52 +020020715 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020716 if (res == NULL)
20717 goto errret;
20718
20719 list = list_alloc();
20720 if (list == NULL)
20721 goto errret;
20722
20723 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020725 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020726 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020727 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020728 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020729
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020730 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020731 if (s == NULL)
20732 goto errret;
20733
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020734 for (p = s; start < end; ++p, ++start)
20735 *p = *start == NUL ? NL : *start;
20736 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020737
20738 li = listitem_alloc();
20739 if (li == NULL)
20740 {
20741 vim_free(s);
20742 goto errret;
20743 }
20744 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020745 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020746 li->li_tv.vval.v_string = s;
20747 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020749
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020750 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020751 rettv->v_type = VAR_LIST;
20752 rettv->vval.v_list = list;
20753 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020755 else
20756 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020757 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020758#ifdef USE_CR
20759 /* translate <CR> into <NL> */
20760 if (res != NULL)
20761 {
20762 char_u *s;
20763
20764 for (s = res; *s; ++s)
20765 {
20766 if (*s == CAR)
20767 *s = NL;
20768 }
20769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770#else
20771# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020772 /* translate <CR><NL> into <NL> */
20773 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020775 char_u *s, *d;
20776
20777 d = res;
20778 for (s = res; *s; ++s)
20779 {
20780 if (s[0] == CAR && s[1] == NL)
20781 ++s;
20782 *d++ = *s;
20783 }
20784 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786# endif
20787#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020788 rettv->vval.v_string = res;
20789 res = NULL;
20790 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020791
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020792errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020793 if (infile != NULL)
20794 {
20795 mch_remove(infile);
20796 vim_free(infile);
20797 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020798 if (res != NULL)
20799 vim_free(res);
20800 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020801 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020802}
20803
20804/*
20805 * "system()" function
20806 */
20807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020808f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020809{
20810 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20811}
20812
20813/*
20814 * "systemlist()" function
20815 */
20816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020817f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020818{
20819 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820}
20821
20822/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020823 * "tabpagebuflist()" function
20824 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020826f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020827{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020828#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020829 tabpage_T *tp;
20830 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020831
20832 if (argvars[0].v_type == VAR_UNKNOWN)
20833 wp = firstwin;
20834 else
20835 {
20836 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20837 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020838 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020839 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020840 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020841 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020842 for (; wp != NULL; wp = wp->w_next)
20843 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020844 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020845 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020846 }
20847#endif
20848}
20849
20850
20851/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020852 * "tabpagenr()" function
20853 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020855f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020856{
20857 int nr = 1;
20858#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020859 char_u *arg;
20860
20861 if (argvars[0].v_type != VAR_UNKNOWN)
20862 {
20863 arg = get_tv_string_chk(&argvars[0]);
20864 nr = 0;
20865 if (arg != NULL)
20866 {
20867 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020868 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020869 else
20870 EMSG2(_(e_invexpr2), arg);
20871 }
20872 }
20873 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020874 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020875#endif
20876 rettv->vval.v_number = nr;
20877}
20878
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020879
20880#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020881static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020882
20883/*
20884 * Common code for tabpagewinnr() and winnr().
20885 */
20886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020887get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020888{
20889 win_T *twin;
20890 int nr = 1;
20891 win_T *wp;
20892 char_u *arg;
20893
20894 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20895 if (argvar->v_type != VAR_UNKNOWN)
20896 {
20897 arg = get_tv_string_chk(argvar);
20898 if (arg == NULL)
20899 nr = 0; /* type error; errmsg already given */
20900 else if (STRCMP(arg, "$") == 0)
20901 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20902 else if (STRCMP(arg, "#") == 0)
20903 {
20904 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20905 if (twin == NULL)
20906 nr = 0;
20907 }
20908 else
20909 {
20910 EMSG2(_(e_invexpr2), arg);
20911 nr = 0;
20912 }
20913 }
20914
20915 if (nr > 0)
20916 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20917 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020918 {
20919 if (wp == NULL)
20920 {
20921 /* didn't find it in this tabpage */
20922 nr = 0;
20923 break;
20924 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020925 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020926 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020927 return nr;
20928}
20929#endif
20930
20931/*
20932 * "tabpagewinnr()" function
20933 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020935f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020936{
20937 int nr = 1;
20938#ifdef FEAT_WINDOWS
20939 tabpage_T *tp;
20940
20941 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20942 if (tp == NULL)
20943 nr = 0;
20944 else
20945 nr = get_winnr(tp, &argvars[1]);
20946#endif
20947 rettv->vval.v_number = nr;
20948}
20949
20950
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020951/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020952 * "tagfiles()" function
20953 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020955f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020956{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020957 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020958 tagname_T tn;
20959 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020960
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020961 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020962 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020963 fname = alloc(MAXPATHL);
20964 if (fname == NULL)
20965 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020966
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020967 for (first = TRUE; ; first = FALSE)
20968 if (get_tagfname(&tn, first, fname) == FAIL
20969 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020970 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020971 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020972 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020973}
20974
20975/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020976 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020977 */
20978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020979f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020980{
20981 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020982
20983 tag_pattern = get_tv_string(&argvars[0]);
20984
20985 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020986 if (*tag_pattern == NUL)
20987 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020989 if (rettv_list_alloc(rettv) == OK)
20990 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020991}
20992
20993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 * "tempname()" function
20995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020997f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998{
20999 static int x = 'A';
21000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021001 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020021002 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003
21004 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
21005 * names. Skip 'I' and 'O', they are used for shell redirection. */
21006 do
21007 {
21008 if (x == 'Z')
21009 x = '0';
21010 else if (x == '9')
21011 x = 'A';
21012 else
21013 {
21014#ifdef EBCDIC
21015 if (x == 'I')
21016 x = 'J';
21017 else if (x == 'R')
21018 x = 'S';
21019 else
21020#endif
21021 ++x;
21022 }
21023 } while (x == 'I' || x == 'O');
21024}
21025
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021026#ifdef FEAT_FLOAT
21027/*
21028 * "tan()" function
21029 */
21030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021031f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021032{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021033 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021034
21035 rettv->v_type = VAR_FLOAT;
21036 if (get_float_arg(argvars, &f) == OK)
21037 rettv->vval.v_float = tan(f);
21038 else
21039 rettv->vval.v_float = 0.0;
21040}
21041
21042/*
21043 * "tanh()" function
21044 */
21045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021046f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021047{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021048 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021049
21050 rettv->v_type = VAR_FLOAT;
21051 if (get_float_arg(argvars, &f) == OK)
21052 rettv->vval.v_float = tanh(f);
21053 else
21054 rettv->vval.v_float = 0.0;
21055}
21056#endif
21057
Bram Moolenaar574860b2016-05-24 17:33:34 +020021058/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021059 * "test_alloc_fail(id, countdown, repeat)" function
21060 */
21061 static void
21062f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
21063{
21064 if (argvars[0].v_type != VAR_NUMBER
21065 || argvars[0].vval.v_number <= 0
21066 || argvars[1].v_type != VAR_NUMBER
21067 || argvars[1].vval.v_number < 0
21068 || argvars[2].v_type != VAR_NUMBER)
21069 EMSG(_(e_invarg));
21070 else
21071 {
21072 alloc_fail_id = argvars[0].vval.v_number;
21073 if (alloc_fail_id >= aid_last)
21074 EMSG(_(e_invarg));
21075 alloc_fail_countdown = argvars[1].vval.v_number;
21076 alloc_fail_repeat = argvars[2].vval.v_number;
21077 did_outofmem_msg = FALSE;
21078 }
21079}
21080
21081/*
Bram Moolenaar5c719942016-07-09 23:40:45 +020021082 * "test_autochdir()"
21083 */
21084 static void
21085f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21086{
21087#if defined(FEAT_AUTOCHDIR)
21088 test_autochdir = TRUE;
21089#endif
21090}
21091
21092/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021093 * "test_disable_char_avail({expr})" function
21094 */
21095 static void
21096f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
21097{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021098 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021099}
21100
21101/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020021102 * "test_garbagecollect_now()" function
21103 */
21104 static void
21105f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21106{
21107 /* This is dangerous, any Lists and Dicts used internally may be freed
21108 * while still in use. */
21109 garbage_collect(TRUE);
21110}
21111
21112#ifdef FEAT_JOB_CHANNEL
21113 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021114f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021115{
21116 rettv->v_type = VAR_CHANNEL;
21117 rettv->vval.v_channel = NULL;
21118}
21119#endif
21120
21121 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021122f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021123{
21124 rettv->v_type = VAR_DICT;
21125 rettv->vval.v_dict = NULL;
21126}
21127
21128#ifdef FEAT_JOB_CHANNEL
21129 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021130f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021131{
21132 rettv->v_type = VAR_JOB;
21133 rettv->vval.v_job = NULL;
21134}
21135#endif
21136
21137 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021138f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021139{
21140 rettv->v_type = VAR_LIST;
21141 rettv->vval.v_list = NULL;
21142}
21143
21144 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021145f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021146{
21147 rettv->v_type = VAR_PARTIAL;
21148 rettv->vval.v_partial = NULL;
21149}
21150
21151 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021152f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021153{
21154 rettv->v_type = VAR_STRING;
21155 rettv->vval.v_string = NULL;
21156}
21157
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021158 static void
21159f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21160{
21161 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21162}
21163
Bram Moolenaar975b5272016-03-15 23:10:59 +010021164#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21165/*
21166 * Get a callback from "arg". It can be a Funcref or a function name.
21167 * When "arg" is zero return an empty string.
21168 * Return NULL for an invalid argument.
21169 */
21170 char_u *
21171get_callback(typval_T *arg, partial_T **pp)
21172{
21173 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21174 {
21175 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021176 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021177 return (*pp)->pt_name;
21178 }
21179 *pp = NULL;
21180 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21181 return arg->vval.v_string;
21182 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21183 return (char_u *)"";
21184 EMSG(_("E921: Invalid callback argument"));
21185 return NULL;
21186}
21187#endif
21188
21189#ifdef FEAT_TIMERS
21190/*
21191 * "timer_start(time, callback [, options])" function
21192 */
21193 static void
21194f_timer_start(typval_T *argvars, typval_T *rettv)
21195{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021196 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021197 timer_T *timer;
21198 int repeat = 0;
21199 char_u *callback;
21200 dict_T *dict;
21201
Bram Moolenaar38499922016-04-22 20:46:52 +020021202 if (check_secure())
21203 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021204 if (argvars[2].v_type != VAR_UNKNOWN)
21205 {
21206 if (argvars[2].v_type != VAR_DICT
21207 || (dict = argvars[2].vval.v_dict) == NULL)
21208 {
21209 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21210 return;
21211 }
21212 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21213 repeat = get_dict_number(dict, (char_u *)"repeat");
21214 }
21215
21216 timer = create_timer(msec, repeat);
21217 callback = get_callback(&argvars[1], &timer->tr_partial);
21218 if (callback == NULL)
21219 {
21220 stop_timer(timer);
21221 rettv->vval.v_number = -1;
21222 }
21223 else
21224 {
21225 timer->tr_callback = vim_strsave(callback);
21226 rettv->vval.v_number = timer->tr_id;
21227 }
21228}
21229
21230/*
21231 * "timer_stop(timer)" function
21232 */
21233 static void
21234f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21235{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021236 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021237
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021238 if (argvars[0].v_type != VAR_NUMBER)
21239 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020021240 EMSG(_(e_number_exp));
21241 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021242 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021243 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021244 if (timer != NULL)
21245 stop_timer(timer);
21246}
21247#endif
21248
Bram Moolenaard52d9742005-08-21 22:20:28 +000021249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 * "tolower(string)" function
21251 */
21252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021253f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254{
21255 char_u *p;
21256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021257 p = vim_strsave(get_tv_string(&argvars[0]));
21258 rettv->v_type = VAR_STRING;
21259 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260
21261 if (p != NULL)
21262 while (*p != NUL)
21263 {
21264#ifdef FEAT_MBYTE
21265 int l;
21266
21267 if (enc_utf8)
21268 {
21269 int c, lc;
21270
21271 c = utf_ptr2char(p);
21272 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021273 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 /* TODO: reallocate string when byte count changes. */
21275 if (utf_char2len(lc) == l)
21276 utf_char2bytes(lc, p);
21277 p += l;
21278 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021279 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 p += l; /* skip multi-byte character */
21281 else
21282#endif
21283 {
21284 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21285 ++p;
21286 }
21287 }
21288}
21289
21290/*
21291 * "toupper(string)" function
21292 */
21293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021294f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021296 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021297 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298}
21299
21300/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021301 * "tr(string, fromstr, tostr)" function
21302 */
21303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021304f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021305{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021306 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021307 char_u *fromstr;
21308 char_u *tostr;
21309 char_u *p;
21310#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021311 int inlen;
21312 int fromlen;
21313 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021314 int idx;
21315 char_u *cpstr;
21316 int cplen;
21317 int first = TRUE;
21318#endif
21319 char_u buf[NUMBUFLEN];
21320 char_u buf2[NUMBUFLEN];
21321 garray_T ga;
21322
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021323 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021324 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21325 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021326
21327 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328 rettv->v_type = VAR_STRING;
21329 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021330 if (fromstr == NULL || tostr == NULL)
21331 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021332 ga_init2(&ga, (int)sizeof(char), 80);
21333
21334#ifdef FEAT_MBYTE
21335 if (!has_mbyte)
21336#endif
21337 /* not multi-byte: fromstr and tostr must be the same length */
21338 if (STRLEN(fromstr) != STRLEN(tostr))
21339 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021340#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021341error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021342#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021343 EMSG2(_(e_invarg2), fromstr);
21344 ga_clear(&ga);
21345 return;
21346 }
21347
21348 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021349 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021350 {
21351#ifdef FEAT_MBYTE
21352 if (has_mbyte)
21353 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021354 inlen = (*mb_ptr2len)(in_str);
21355 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021356 cplen = inlen;
21357 idx = 0;
21358 for (p = fromstr; *p != NUL; p += fromlen)
21359 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021360 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021361 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021362 {
21363 for (p = tostr; *p != NUL; p += tolen)
21364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021365 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021366 if (idx-- == 0)
21367 {
21368 cplen = tolen;
21369 cpstr = p;
21370 break;
21371 }
21372 }
21373 if (*p == NUL) /* tostr is shorter than fromstr */
21374 goto error;
21375 break;
21376 }
21377 ++idx;
21378 }
21379
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021380 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021381 {
21382 /* Check that fromstr and tostr have the same number of
21383 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021384 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021385 first = FALSE;
21386 for (p = tostr; *p != NUL; p += tolen)
21387 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021388 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021389 --idx;
21390 }
21391 if (idx != 0)
21392 goto error;
21393 }
21394
Bram Moolenaarcde88542015-08-11 19:14:00 +020021395 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021396 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021397 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021398
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021399 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021400 }
21401 else
21402#endif
21403 {
21404 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021405 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021406 if (p != NULL)
21407 ga_append(&ga, tostr[p - fromstr]);
21408 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021409 ga_append(&ga, *in_str);
21410 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021411 }
21412 }
21413
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021414 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021415 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021416 ga_append(&ga, NUL);
21417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021419}
21420
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021421#ifdef FEAT_FLOAT
21422/*
21423 * "trunc({float})" function
21424 */
21425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021426f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021427{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021428 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021429
21430 rettv->v_type = VAR_FLOAT;
21431 if (get_float_arg(argvars, &f) == OK)
21432 /* trunc() is not in C90, use floor() or ceil() instead. */
21433 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21434 else
21435 rettv->vval.v_float = 0.0;
21436}
21437#endif
21438
Bram Moolenaar8299df92004-07-10 09:47:34 +000021439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 * "type(expr)" function
21441 */
21442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021443f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021445 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021446
21447 switch (argvars[0].v_type)
21448 {
21449 case VAR_NUMBER: n = 0; break;
21450 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021451 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021452 case VAR_FUNC: n = 2; break;
21453 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021454 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021455 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021456 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021457 if (argvars[0].vval.v_number == VVAL_FALSE
21458 || argvars[0].vval.v_number == VVAL_TRUE)
21459 n = 6;
21460 else
21461 n = 7;
21462 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021463 case VAR_JOB: n = 8; break;
21464 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021465 case VAR_UNKNOWN:
21466 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21467 n = -1;
21468 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021469 }
21470 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471}
21472
21473/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021474 * "undofile(name)" function
21475 */
21476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021477f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021478{
21479 rettv->v_type = VAR_STRING;
21480#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021481 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021482 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021483
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021484 if (*fname == NUL)
21485 {
21486 /* If there is no file name there will be no undo file. */
21487 rettv->vval.v_string = NULL;
21488 }
21489 else
21490 {
21491 char_u *ffname = FullName_save(fname, FALSE);
21492
21493 if (ffname != NULL)
21494 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21495 vim_free(ffname);
21496 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021497 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021498#else
21499 rettv->vval.v_string = NULL;
21500#endif
21501}
21502
21503/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021504 * "undotree()" function
21505 */
21506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021507f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021508{
21509 if (rettv_dict_alloc(rettv) == OK)
21510 {
21511 dict_T *dict = rettv->vval.v_dict;
21512 list_T *list;
21513
Bram Moolenaar730cde92010-06-27 05:18:54 +020021514 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021515 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021516 dict_add_nr_str(dict, "save_last",
21517 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021518 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21519 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021520 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021521
21522 list = list_alloc();
21523 if (list != NULL)
21524 {
21525 u_eval_tree(curbuf->b_u_oldhead, list);
21526 dict_add_list(dict, "entries", list);
21527 }
21528 }
21529}
21530
21531/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021532 * "values(dict)" function
21533 */
21534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021535f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021536{
21537 dict_list(argvars, rettv, 1);
21538}
21539
21540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 * "virtcol(string)" function
21542 */
21543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021544f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
21546 colnr_T vcol = 0;
21547 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021548 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021550 fp = var2fpos(&argvars[0], FALSE, &fnum);
21551 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21552 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 {
21554 getvvcol(curwin, fp, NULL, NULL, &vcol);
21555 ++vcol;
21556 }
21557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021558 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559}
21560
21561/*
21562 * "visualmode()" function
21563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021565f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 char_u str[2];
21568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021569 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 str[0] = curbuf->b_visual_mode_eval;
21571 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021572 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573
21574 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021575 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577}
21578
21579/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021580 * "wildmenumode()" function
21581 */
21582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021583f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021584{
21585#ifdef FEAT_WILDMENU
21586 if (wild_menu_showing)
21587 rettv->vval.v_number = 1;
21588#endif
21589}
21590
21591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 * "winbufnr(nr)" function
21593 */
21594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021595f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596{
21597 win_T *wp;
21598
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021599 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021603 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604}
21605
21606/*
21607 * "wincol()" function
21608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021610f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611{
21612 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021613 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614}
21615
21616/*
21617 * "winheight(nr)" function
21618 */
21619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021620f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621{
21622 win_T *wp;
21623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021624 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021626 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021628 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629}
21630
21631/*
21632 * "winline()" function
21633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021635f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636{
21637 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021638 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639}
21640
21641/*
21642 * "winnr()" function
21643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021645f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646{
21647 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021648
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021650 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021652 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653}
21654
21655/*
21656 * "winrestcmd()" function
21657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021659f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660{
21661#ifdef FEAT_WINDOWS
21662 win_T *wp;
21663 int winnr = 1;
21664 garray_T ga;
21665 char_u buf[50];
21666
21667 ga_init2(&ga, (int)sizeof(char), 70);
21668 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21669 {
21670 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21671 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21673 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 ++winnr;
21675 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021676 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021678 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021682 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683}
21684
21685/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021686 * "winrestview()" function
21687 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021689f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021690{
21691 dict_T *dict;
21692
21693 if (argvars[0].v_type != VAR_DICT
21694 || (dict = argvars[0].vval.v_dict) == NULL)
21695 EMSG(_(e_invarg));
21696 else
21697 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021698 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021699 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021700 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021701 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021702#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021703 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021704 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021705#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021706 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21707 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021708 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021709 curwin->w_set_curswant = FALSE;
21710 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021711
Bram Moolenaar82c25852014-05-28 16:47:16 +020021712 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021713 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021714#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021715 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021716 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021717#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021718 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021719 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021720 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021721 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021722
21723 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021724 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021725# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021726 win_new_width(curwin, W_WIDTH(curwin));
21727# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021728 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021729
Bram Moolenaarb851a962014-10-31 15:45:52 +010021730 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021731 curwin->w_topline = 1;
21732 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21733 curwin->w_topline = curbuf->b_ml.ml_line_count;
21734#ifdef FEAT_DIFF
21735 check_topfill(curwin, TRUE);
21736#endif
21737 }
21738}
21739
21740/*
21741 * "winsaveview()" function
21742 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021744f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021745{
21746 dict_T *dict;
21747
Bram Moolenaara800b422010-06-27 01:15:55 +020021748 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021749 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021750 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021751
21752 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21753 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21754#ifdef FEAT_VIRTUALEDIT
21755 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21756#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021757 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021758 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21759
21760 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21761#ifdef FEAT_DIFF
21762 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21763#endif
21764 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21765 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21766}
21767
21768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 * "winwidth(nr)" function
21770 */
21771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021772f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773{
21774 win_T *wp;
21775
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021776 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021778 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021780#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021781 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021783 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784#endif
21785}
21786
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021788 * "wordcount()" function
21789 */
21790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021791f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021792{
21793 if (rettv_dict_alloc(rettv) == FAIL)
21794 return;
21795 cursor_pos_info(rettv->vval.v_dict);
21796}
21797
21798/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021799 * Write list of strings to file
21800 */
21801 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021803{
21804 listitem_T *li;
21805 int c;
21806 int ret = OK;
21807 char_u *s;
21808
21809 for (li = list->lv_first; li != NULL; li = li->li_next)
21810 {
21811 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21812 {
21813 if (*s == '\n')
21814 c = putc(NUL, fd);
21815 else
21816 c = putc(*s, fd);
21817 if (c == EOF)
21818 {
21819 ret = FAIL;
21820 break;
21821 }
21822 }
21823 if (!binary || li->li_next != NULL)
21824 if (putc('\n', fd) == EOF)
21825 {
21826 ret = FAIL;
21827 break;
21828 }
21829 if (ret == FAIL)
21830 {
21831 EMSG(_(e_write));
21832 break;
21833 }
21834 }
21835 return ret;
21836}
21837
21838/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021839 * "writefile()" function
21840 */
21841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021842f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021843{
21844 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021845 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021846 char_u *fname;
21847 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021848 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021849
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021850 if (check_restricted() || check_secure())
21851 return;
21852
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021853 if (argvars[0].v_type != VAR_LIST)
21854 {
21855 EMSG2(_(e_listarg), "writefile()");
21856 return;
21857 }
21858 if (argvars[0].vval.v_list == NULL)
21859 return;
21860
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021861 if (argvars[2].v_type != VAR_UNKNOWN)
21862 {
21863 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21864 binary = TRUE;
21865 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21866 append = TRUE;
21867 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021868
21869 /* Always open the file in binary mode, library functions have a mind of
21870 * their own about CR-LF conversion. */
21871 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021872 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21873 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021874 {
21875 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21876 ret = -1;
21877 }
21878 else
21879 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021880 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21881 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021882 fclose(fd);
21883 }
21884
21885 rettv->vval.v_number = ret;
21886}
21887
21888/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021889 * "xor(expr, expr)" function
21890 */
21891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021892f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021893{
21894 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21895 ^ get_tv_number_chk(&argvars[1], NULL);
21896}
21897
21898
21899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021901 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 */
21903 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021904var2fpos(
21905 typval_T *varp,
21906 int dollar_lnum, /* TRUE when $ is last line */
21907 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021909 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021910 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021911 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912
Bram Moolenaara5525202006-03-02 22:52:09 +000021913 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021914 if (varp->v_type == VAR_LIST)
21915 {
21916 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021917 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021918 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021919 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021920
21921 l = varp->vval.v_list;
21922 if (l == NULL)
21923 return NULL;
21924
21925 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021926 pos.lnum = list_find_nr(l, 0L, &error);
21927 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021928 return NULL; /* invalid line number */
21929
21930 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021931 pos.col = list_find_nr(l, 1L, &error);
21932 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021933 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021934 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021935
21936 /* We accept "$" for the column number: last column. */
21937 li = list_find(l, 1L);
21938 if (li != NULL && li->li_tv.v_type == VAR_STRING
21939 && li->li_tv.vval.v_string != NULL
21940 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21941 pos.col = len + 1;
21942
Bram Moolenaara5525202006-03-02 22:52:09 +000021943 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021944 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021945 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021946 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021947
Bram Moolenaara5525202006-03-02 22:52:09 +000021948#ifdef FEAT_VIRTUALEDIT
21949 /* Get the virtual offset. Defaults to zero. */
21950 pos.coladd = list_find_nr(l, 2L, &error);
21951 if (error)
21952 pos.coladd = 0;
21953#endif
21954
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021955 return &pos;
21956 }
21957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021958 name = get_tv_string_chk(varp);
21959 if (name == NULL)
21960 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021961 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021963 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21964 {
21965 if (VIsual_active)
21966 return &VIsual;
21967 return &curwin->w_cursor;
21968 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021969 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021971 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21973 return NULL;
21974 return pp;
21975 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021976
21977#ifdef FEAT_VIRTUALEDIT
21978 pos.coladd = 0;
21979#endif
21980
Bram Moolenaar477933c2007-07-17 14:32:23 +000021981 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021982 {
21983 pos.col = 0;
21984 if (name[1] == '0') /* "w0": first visible line */
21985 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021986 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021987 pos.lnum = curwin->w_topline;
21988 return &pos;
21989 }
21990 else if (name[1] == '$') /* "w$": last visible line */
21991 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021992 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021993 pos.lnum = curwin->w_botline - 1;
21994 return &pos;
21995 }
21996 }
21997 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021999 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 {
22001 pos.lnum = curbuf->b_ml.ml_line_count;
22002 pos.col = 0;
22003 }
22004 else
22005 {
22006 pos.lnum = curwin->w_cursor.lnum;
22007 pos.col = (colnr_T)STRLEN(ml_get_curline());
22008 }
22009 return &pos;
22010 }
22011 return NULL;
22012}
22013
22014/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022015 * Convert list in "arg" into a position and optional file number.
22016 * When "fnump" is NULL there is no file number, only 3 items.
22017 * Note that the column is passed on as-is, the caller may want to decrement
22018 * it to use 1 for the first column.
22019 * Return FAIL when conversion is not possible, doesn't check the position for
22020 * validity.
22021 */
22022 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022023list2fpos(
22024 typval_T *arg,
22025 pos_T *posp,
22026 int *fnump,
22027 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022028{
22029 list_T *l = arg->vval.v_list;
22030 long i = 0;
22031 long n;
22032
Bram Moolenaar493c1782014-05-28 14:34:46 +020022033 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
22034 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000022035 if (arg->v_type != VAR_LIST
22036 || l == NULL
22037 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020022038 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022039 return FAIL;
22040
22041 if (fnump != NULL)
22042 {
22043 n = list_find_nr(l, i++, NULL); /* fnum */
22044 if (n < 0)
22045 return FAIL;
22046 if (n == 0)
22047 n = curbuf->b_fnum; /* current buffer */
22048 *fnump = n;
22049 }
22050
22051 n = list_find_nr(l, i++, NULL); /* lnum */
22052 if (n < 0)
22053 return FAIL;
22054 posp->lnum = n;
22055
22056 n = list_find_nr(l, i++, NULL); /* col */
22057 if (n < 0)
22058 return FAIL;
22059 posp->col = n;
22060
22061#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020022062 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022063 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000022064 posp->coladd = 0;
22065 else
22066 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022067#endif
22068
Bram Moolenaar493c1782014-05-28 14:34:46 +020022069 if (curswantp != NULL)
22070 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
22071
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022072 return OK;
22073}
22074
22075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 * Get the length of an environment variable name.
22077 * Advance "arg" to the first character after the name.
22078 * Return 0 for error.
22079 */
22080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022081get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082{
22083 char_u *p;
22084 int len;
22085
22086 for (p = *arg; vim_isIDc(*p); ++p)
22087 ;
22088 if (p == *arg) /* no name found */
22089 return 0;
22090
22091 len = (int)(p - *arg);
22092 *arg = p;
22093 return len;
22094}
22095
22096/*
22097 * Get the length of the name of a function or internal variable.
22098 * "arg" is advanced to the first non-white character after the name.
22099 * Return 0 if something is wrong.
22100 */
22101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
22104 char_u *p;
22105 int len;
22106
22107 /* Find the end of the name. */
22108 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022109 {
22110 if (*p == ':')
22111 {
22112 /* "s:" is start of "s:var", but "n:" is not and can be used in
22113 * slice "[n:]". Also "xx:" is not a namespace. */
22114 len = (int)(p - *arg);
22115 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
22116 || len > 1)
22117 break;
22118 }
22119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 if (p == *arg) /* no name found */
22121 return 0;
22122
22123 len = (int)(p - *arg);
22124 *arg = skipwhite(p);
22125
22126 return len;
22127}
22128
22129/*
Bram Moolenaara7043832005-01-21 11:56:39 +000022130 * Get the length of the name of a variable or function.
22131 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022133 * Return -1 if curly braces expansion failed.
22134 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 * If the name contains 'magic' {}'s, expand them and return the
22136 * expanded name in an allocated string via 'alias' - caller must free.
22137 */
22138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022139get_name_len(
22140 char_u **arg,
22141 char_u **alias,
22142 int evaluate,
22143 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144{
22145 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 char_u *p;
22147 char_u *expr_start;
22148 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149
22150 *alias = NULL; /* default to no alias */
22151
22152 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22153 && (*arg)[2] == (int)KE_SNR)
22154 {
22155 /* hard coded <SNR>, already translated */
22156 *arg += 3;
22157 return get_id_len(arg) + 3;
22158 }
22159 len = eval_fname_script(*arg);
22160 if (len > 0)
22161 {
22162 /* literal "<SID>", "s:" or "<SNR>" */
22163 *arg += len;
22164 }
22165
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022167 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022169 p = find_name_end(*arg, &expr_start, &expr_end,
22170 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 if (expr_start != NULL)
22172 {
22173 char_u *temp_string;
22174
22175 if (!evaluate)
22176 {
22177 len += (int)(p - *arg);
22178 *arg = skipwhite(p);
22179 return len;
22180 }
22181
22182 /*
22183 * Include any <SID> etc in the expanded string:
22184 * Thus the -len here.
22185 */
22186 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22187 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022188 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 *alias = temp_string;
22190 *arg = skipwhite(p);
22191 return (int)STRLEN(temp_string);
22192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193
22194 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022195 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 EMSG2(_(e_invexpr2), *arg);
22197
22198 return len;
22199}
22200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022201/*
22202 * Find the end of a variable or function name, taking care of magic braces.
22203 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22204 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022205 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022206 * Return a pointer to just after the name. Equal to "arg" if there is no
22207 * valid name.
22208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022210find_name_end(
22211 char_u *arg,
22212 char_u **expr_start,
22213 char_u **expr_end,
22214 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022216 int mb_nest = 0;
22217 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022219 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022221 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022223 *expr_start = NULL;
22224 *expr_end = NULL;
22225 }
22226
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022227 /* Quick check for valid starting character. */
22228 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22229 return arg;
22230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022231 for (p = arg; *p != NUL
22232 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022233 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022234 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022235 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022236 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022237 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022238 if (*p == '\'')
22239 {
22240 /* skip over 'string' to avoid counting [ and ] inside it. */
22241 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22242 ;
22243 if (*p == NUL)
22244 break;
22245 }
22246 else if (*p == '"')
22247 {
22248 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22249 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22250 if (*p == '\\' && p[1] != NUL)
22251 ++p;
22252 if (*p == NUL)
22253 break;
22254 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022255 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22256 {
22257 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022258 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022259 len = (int)(p - arg);
22260 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022261 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022262 break;
22263 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022265 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022267 if (*p == '[')
22268 ++br_nest;
22269 else if (*p == ']')
22270 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022275 if (*p == '{')
22276 {
22277 mb_nest++;
22278 if (expr_start != NULL && *expr_start == NULL)
22279 *expr_start = p;
22280 }
22281 else if (*p == '}')
22282 {
22283 mb_nest--;
22284 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22285 *expr_end = p;
22286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 }
22289
22290 return p;
22291}
22292
22293/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022294 * Expands out the 'magic' {}'s in a variable/function name.
22295 * Note that this can call itself recursively, to deal with
22296 * constructs like foo{bar}{baz}{bam}
22297 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22298 * "in_start" ^
22299 * "expr_start" ^
22300 * "expr_end" ^
22301 * "in_end" ^
22302 *
22303 * Returns a new allocated string, which the caller must free.
22304 * Returns NULL for failure.
22305 */
22306 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022307make_expanded_name(
22308 char_u *in_start,
22309 char_u *expr_start,
22310 char_u *expr_end,
22311 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022312{
22313 char_u c1;
22314 char_u *retval = NULL;
22315 char_u *temp_result;
22316 char_u *nextcmd = NULL;
22317
22318 if (expr_end == NULL || in_end == NULL)
22319 return NULL;
22320 *expr_start = NUL;
22321 *expr_end = NUL;
22322 c1 = *in_end;
22323 *in_end = NUL;
22324
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022325 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022326 if (temp_result != NULL && nextcmd == NULL)
22327 {
22328 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22329 + (in_end - expr_end) + 1));
22330 if (retval != NULL)
22331 {
22332 STRCPY(retval, in_start);
22333 STRCAT(retval, temp_result);
22334 STRCAT(retval, expr_end + 1);
22335 }
22336 }
22337 vim_free(temp_result);
22338
22339 *in_end = c1; /* put char back for error messages */
22340 *expr_start = '{';
22341 *expr_end = '}';
22342
22343 if (retval != NULL)
22344 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022345 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022346 if (expr_start != NULL)
22347 {
22348 /* Further expansion! */
22349 temp_result = make_expanded_name(retval, expr_start,
22350 expr_end, temp_result);
22351 vim_free(retval);
22352 retval = temp_result;
22353 }
22354 }
22355
22356 return retval;
22357}
22358
22359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022361 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 */
22363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022364eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022366 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22367}
22368
22369/*
22370 * Return TRUE if character "c" can be used as the first character in a
22371 * variable or function name (excluding '{' and '}').
22372 */
22373 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022374eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022375{
22376 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377}
22378
22379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 * Set number v: variable to "val".
22381 */
22382 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022383set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022385 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386}
22387
22388/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022389 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022391 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022392get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022394 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395}
22396
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022397/*
22398 * Get string v: variable value. Uses a static buffer, can only be used once.
22399 */
22400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022401get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022402{
22403 return get_tv_string(&vimvars[idx].vv_tv);
22404}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022405
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022407 * Get List v: variable value. Caller must take care of reference count when
22408 * needed.
22409 */
22410 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022411get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022412{
22413 return vimvars[idx].vv_list;
22414}
22415
22416/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022417 * Set v:char to character "c".
22418 */
22419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022420set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022421{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022422 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022423
22424#ifdef FEAT_MBYTE
22425 if (has_mbyte)
22426 buf[(*mb_char2bytes)(c, buf)] = NUL;
22427 else
22428#endif
22429 {
22430 buf[0] = c;
22431 buf[1] = NUL;
22432 }
22433 set_vim_var_string(VV_CHAR, buf, -1);
22434}
22435
22436/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022437 * Set v:count to "count" and v:count1 to "count1".
22438 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 */
22440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022441set_vcount(
22442 long count,
22443 long count1,
22444 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022446 if (set_prevcount)
22447 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022448 vimvars[VV_COUNT].vv_nr = count;
22449 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450}
22451
22452/*
22453 * Set string v: variable to a copy of "val".
22454 */
22455 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022456set_vim_var_string(
22457 int idx,
22458 char_u *val,
22459 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460{
Bram Moolenaara542c682016-01-31 16:28:04 +010022461 clear_tv(&vimvars[idx].vv_di.di_tv);
22462 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022464 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022466 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022468 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469}
22470
22471/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022472 * Set List v: variable to "val".
22473 */
22474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022475set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022476{
Bram Moolenaara542c682016-01-31 16:28:04 +010022477 clear_tv(&vimvars[idx].vv_di.di_tv);
22478 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022479 vimvars[idx].vv_list = val;
22480 if (val != NULL)
22481 ++val->lv_refcount;
22482}
22483
22484/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022485 * Set Dictionary v: variable to "val".
22486 */
22487 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022488set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022489{
22490 int todo;
22491 hashitem_T *hi;
22492
Bram Moolenaara542c682016-01-31 16:28:04 +010022493 clear_tv(&vimvars[idx].vv_di.di_tv);
22494 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022495 vimvars[idx].vv_dict = val;
22496 if (val != NULL)
22497 {
22498 ++val->dv_refcount;
22499
22500 /* Set readonly */
22501 todo = (int)val->dv_hashtab.ht_used;
22502 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22503 {
22504 if (HASHITEM_EMPTY(hi))
22505 continue;
22506 --todo;
22507 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22508 }
22509 }
22510}
22511
22512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 * Set v:register if needed.
22514 */
22515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022516set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517{
22518 char_u regname;
22519
22520 if (c == 0 || c == ' ')
22521 regname = '"';
22522 else
22523 regname = c;
22524 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022525 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 set_vim_var_string(VV_REG, &regname, 1);
22527}
22528
22529/*
22530 * Get or set v:exception. If "oldval" == NULL, return the current value.
22531 * Otherwise, restore the value to "oldval" and return NULL.
22532 * Must always be called in pairs to save and restore v:exception! Does not
22533 * take care of memory allocations.
22534 */
22535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022536v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537{
22538 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022539 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540
Bram Moolenaare9a41262005-01-15 22:18:47 +000022541 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 return NULL;
22543}
22544
22545/*
22546 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22547 * Otherwise, restore the value to "oldval" and return NULL.
22548 * Must always be called in pairs to save and restore v:throwpoint! Does not
22549 * take care of memory allocations.
22550 */
22551 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022552v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553{
22554 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022555 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556
Bram Moolenaare9a41262005-01-15 22:18:47 +000022557 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 return NULL;
22559}
22560
22561#if defined(FEAT_AUTOCMD) || defined(PROTO)
22562/*
22563 * Set v:cmdarg.
22564 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22565 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22566 * Must always be called in pairs!
22567 */
22568 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022569set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570{
22571 char_u *oldval;
22572 char_u *newval;
22573 unsigned len;
22574
Bram Moolenaare9a41262005-01-15 22:18:47 +000022575 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022576 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022578 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022579 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022580 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 }
22582
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022583 if (eap->force_bin == FORCE_BIN)
22584 len = 6;
22585 else if (eap->force_bin == FORCE_NOBIN)
22586 len = 8;
22587 else
22588 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022589
22590 if (eap->read_edit)
22591 len += 7;
22592
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022593 if (eap->force_ff != 0)
22594 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22595# ifdef FEAT_MBYTE
22596 if (eap->force_enc != 0)
22597 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022598 if (eap->bad_char != 0)
22599 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022600# endif
22601
22602 newval = alloc(len + 1);
22603 if (newval == NULL)
22604 return NULL;
22605
22606 if (eap->force_bin == FORCE_BIN)
22607 sprintf((char *)newval, " ++bin");
22608 else if (eap->force_bin == FORCE_NOBIN)
22609 sprintf((char *)newval, " ++nobin");
22610 else
22611 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022612
22613 if (eap->read_edit)
22614 STRCAT(newval, " ++edit");
22615
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022616 if (eap->force_ff != 0)
22617 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22618 eap->cmd + eap->force_ff);
22619# ifdef FEAT_MBYTE
22620 if (eap->force_enc != 0)
22621 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22622 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022623 if (eap->bad_char == BAD_KEEP)
22624 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22625 else if (eap->bad_char == BAD_DROP)
22626 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22627 else if (eap->bad_char != 0)
22628 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022629# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022630 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022631 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632}
22633#endif
22634
22635/*
22636 * Get the value of internal variable "name".
22637 * Return OK or FAIL.
22638 */
22639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022640get_var_tv(
22641 char_u *name,
22642 int len, /* length of "name" */
22643 typval_T *rettv, /* NULL when only checking existence */
22644 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22645 int verbose, /* may give error message */
22646 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647{
22648 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022649 typval_T *tv = NULL;
22650 typval_T atv;
22651 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653
22654 /* truncate the name, so that we can use strcmp() */
22655 cc = name[len];
22656 name[len] = NUL;
22657
22658 /*
22659 * Check for "b:changedtick".
22660 */
22661 if (STRCMP(name, "b:changedtick") == 0)
22662 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022663 atv.v_type = VAR_NUMBER;
22664 atv.vval.v_number = curbuf->b_changedtick;
22665 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 }
22667
22668 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 * Check for user-defined variables.
22670 */
22671 else
22672 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022673 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022675 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022677 if (dip != NULL)
22678 *dip = v;
22679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 }
22681
Bram Moolenaare9a41262005-01-15 22:18:47 +000022682 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022683 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022684 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022685 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 ret = FAIL;
22687 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022688 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022689 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690
22691 name[len] = cc;
22692
22693 return ret;
22694}
22695
22696/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022697 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22698 * Also handle function call with Funcref variable: func(expr)
22699 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22700 */
22701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022702handle_subscript(
22703 char_u **arg,
22704 typval_T *rettv,
22705 int evaluate, /* do more than finding the end */
22706 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022707{
22708 int ret = OK;
22709 dict_T *selfdict = NULL;
22710 char_u *s;
22711 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022712 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022713
22714 while (ret == OK
22715 && (**arg == '['
22716 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022717 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22718 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022719 && !vim_iswhite(*(*arg - 1)))
22720 {
22721 if (**arg == '(')
22722 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022723 partial_T *pt = NULL;
22724
Bram Moolenaard9fba312005-06-26 22:34:35 +000022725 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022726 if (evaluate)
22727 {
22728 functv = *rettv;
22729 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022730
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022731 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022732 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022733 {
22734 pt = functv.vval.v_partial;
22735 s = pt->pt_name;
22736 }
22737 else
22738 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022739 }
22740 else
22741 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022742 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022743 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022744 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022745
22746 /* Clear the funcref afterwards, so that deleting it while
22747 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022748 if (evaluate)
22749 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022750
22751 /* Stop the expression evaluation when immediately aborting on
22752 * error, or when an interrupt occurred or an exception was thrown
22753 * but not caught. */
22754 if (aborting())
22755 {
22756 if (ret == OK)
22757 clear_tv(rettv);
22758 ret = FAIL;
22759 }
22760 dict_unref(selfdict);
22761 selfdict = NULL;
22762 }
22763 else /* **arg == '[' || **arg == '.' */
22764 {
22765 dict_unref(selfdict);
22766 if (rettv->v_type == VAR_DICT)
22767 {
22768 selfdict = rettv->vval.v_dict;
22769 if (selfdict != NULL)
22770 ++selfdict->dv_refcount;
22771 }
22772 else
22773 selfdict = NULL;
22774 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22775 {
22776 clear_tv(rettv);
22777 ret = FAIL;
22778 }
22779 }
22780 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022781
Bram Moolenaar1d429612016-05-24 15:44:17 +020022782 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22783 * Don't do this when "Func" is already a partial that was bound
22784 * explicitly (pt_auto is FALSE). */
22785 if (selfdict != NULL
22786 && (rettv->v_type == VAR_FUNC
22787 || (rettv->v_type == VAR_PARTIAL
22788 && (rettv->vval.v_partial->pt_auto
22789 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022790 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022791 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22792 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022793 char_u *tofree = NULL;
22794 ufunc_T *fp;
22795 char_u fname_buf[FLEN_FIXED + 1];
22796 int error;
22797
22798 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022799 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022800 fp = find_func(fname);
22801 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022802
Bram Moolenaar65639032016-03-16 21:40:30 +010022803 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022804 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022805 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22806
22807 if (pt != NULL)
22808 {
22809 pt->pt_refcount = 1;
22810 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022811 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022812 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022813 if (rettv->v_type == VAR_FUNC)
22814 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022815 /* Just a function: Take over the function name and use
22816 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022817 pt->pt_name = rettv->vval.v_string;
22818 }
22819 else
22820 {
22821 partial_T *ret_pt = rettv->vval.v_partial;
22822 int i;
22823
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022824 /* Partial: copy the function name, use selfdict and copy
22825 * args. Can't take over name or args, the partial might
22826 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022827 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022828 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022829 if (ret_pt->pt_argc > 0)
22830 {
22831 pt->pt_argv = (typval_T *)alloc(
22832 sizeof(typval_T) * ret_pt->pt_argc);
22833 if (pt->pt_argv == NULL)
22834 /* out of memory: drop the arguments */
22835 pt->pt_argc = 0;
22836 else
22837 {
22838 pt->pt_argc = ret_pt->pt_argc;
22839 for (i = 0; i < pt->pt_argc; i++)
22840 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22841 }
22842 }
22843 partial_unref(ret_pt);
22844 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022845 rettv->v_type = VAR_PARTIAL;
22846 rettv->vval.v_partial = pt;
22847 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022848 }
22849 }
22850
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022851 dict_unref(selfdict);
22852 return ret;
22853}
22854
22855/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022856 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022857 * value).
22858 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022859 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022860alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022861{
Bram Moolenaar33570922005-01-25 22:26:29 +000022862 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022863}
22864
22865/*
22866 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 * The string "s" must have been allocated, it is consumed.
22868 * Return NULL for out of memory, the variable otherwise.
22869 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022870 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022871alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872{
Bram Moolenaar33570922005-01-25 22:26:29 +000022873 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022875 rettv = alloc_tv();
22876 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022878 rettv->v_type = VAR_STRING;
22879 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 }
22881 else
22882 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022883 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884}
22885
22886/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022887 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022890free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891{
22892 if (varp != NULL)
22893 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022894 switch (varp->v_type)
22895 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022896 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022897 func_unref(varp->vval.v_string);
22898 /*FALLTHROUGH*/
22899 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022900 vim_free(varp->vval.v_string);
22901 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022902 case VAR_PARTIAL:
22903 partial_unref(varp->vval.v_partial);
22904 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022905 case VAR_LIST:
22906 list_unref(varp->vval.v_list);
22907 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022908 case VAR_DICT:
22909 dict_unref(varp->vval.v_dict);
22910 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022911 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022912#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022913 job_unref(varp->vval.v_job);
22914 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022915#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022916 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022917#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022918 channel_unref(varp->vval.v_channel);
22919 break;
22920#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022921 case VAR_NUMBER:
22922 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022923 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022924 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022925 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 vim_free(varp);
22928 }
22929}
22930
22931/*
22932 * Free the memory for a variable value and set the value to NULL or 0.
22933 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022935clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936{
22937 if (varp != NULL)
22938 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022939 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022941 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022942 func_unref(varp->vval.v_string);
22943 /*FALLTHROUGH*/
22944 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022945 vim_free(varp->vval.v_string);
22946 varp->vval.v_string = NULL;
22947 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022948 case VAR_PARTIAL:
22949 partial_unref(varp->vval.v_partial);
22950 varp->vval.v_partial = NULL;
22951 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022952 case VAR_LIST:
22953 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022954 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022955 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022956 case VAR_DICT:
22957 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022958 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022959 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022960 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022961 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022962 varp->vval.v_number = 0;
22963 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022964 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022965#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022966 varp->vval.v_float = 0.0;
22967 break;
22968#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022969 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022970#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022971 job_unref(varp->vval.v_job);
22972 varp->vval.v_job = NULL;
22973#endif
22974 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022975 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022976#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022977 channel_unref(varp->vval.v_channel);
22978 varp->vval.v_channel = NULL;
22979#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022980 case VAR_UNKNOWN:
22981 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022983 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 }
22985}
22986
22987/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022988 * Set the value of a variable to NULL without freeing items.
22989 */
22990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022991init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022992{
22993 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022994 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022995}
22996
22997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998 * Get the number value of a variable.
22999 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023000 * For incompatible types, return 0.
23001 * get_tv_number_chk() is similar to get_tv_number(), but informs the
23002 * caller of incompatible types: it sets *denote to TRUE if "denote"
23003 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023005 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023006get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023008 int error = FALSE;
23009
23010 return get_tv_number_chk(varp, &error); /* return 0L on error */
23011}
23012
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023013 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023014get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023015{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023016 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023018 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023021 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023022 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023023#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000023024 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023025 break;
23026#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023027 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023028 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023029 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023030 break;
23031 case VAR_STRING:
23032 if (varp->vval.v_string != NULL)
23033 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010023034 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023035 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023036 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023037 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023038 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023039 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023040 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023041 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010023042 case VAR_SPECIAL:
23043 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
23044 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023045 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023046#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023047 EMSG(_("E910: Using a Job as a Number"));
23048 break;
23049#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023050 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023051#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023052 EMSG(_("E913: Using a Channel as a Number"));
23053 break;
23054#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023055 case VAR_UNKNOWN:
23056 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023057 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023059 if (denote == NULL) /* useful for values that must be unsigned */
23060 n = -1;
23061 else
23062 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023063 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064}
23065
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023066#ifdef FEAT_FLOAT
23067 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023068get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023069{
23070 switch (varp->v_type)
23071 {
23072 case VAR_NUMBER:
23073 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023074 case VAR_FLOAT:
23075 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023076 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023077 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023078 EMSG(_("E891: Using a Funcref as a Float"));
23079 break;
23080 case VAR_STRING:
23081 EMSG(_("E892: Using a String as a Float"));
23082 break;
23083 case VAR_LIST:
23084 EMSG(_("E893: Using a List as a Float"));
23085 break;
23086 case VAR_DICT:
23087 EMSG(_("E894: Using a Dictionary as a Float"));
23088 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023089 case VAR_SPECIAL:
23090 EMSG(_("E907: Using a special value as a Float"));
23091 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023092 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023093# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023094 EMSG(_("E911: Using a Job as a Float"));
23095 break;
23096# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023097 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023098# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023099 EMSG(_("E914: Using a Channel as a Float"));
23100 break;
23101# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023102 case VAR_UNKNOWN:
23103 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023104 break;
23105 }
23106 return 0;
23107}
23108#endif
23109
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023111 * Get the lnum from the first argument.
23112 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023113 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 */
23115 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023116get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117{
Bram Moolenaar33570922005-01-25 22:26:29 +000023118 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 linenr_T lnum;
23120
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023121 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 if (lnum == 0) /* no valid number, try using line() */
23123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023124 rettv.v_type = VAR_NUMBER;
23125 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023126 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023127 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 }
23129 return lnum;
23130}
23131
23132/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023133 * Get the lnum from the first argument.
23134 * Also accepts "$", then "buf" is used.
23135 * Returns 0 on error.
23136 */
23137 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023138get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023139{
23140 if (argvars[0].v_type == VAR_STRING
23141 && argvars[0].vval.v_string != NULL
23142 && argvars[0].vval.v_string[0] == '$'
23143 && buf != NULL)
23144 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023145 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023146}
23147
23148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 * Get the string value of a variable.
23150 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023151 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23152 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 * If the String variable has never been set, return an empty string.
23154 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023155 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23156 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023158 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023159get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160{
23161 static char_u mybuf[NUMBUFLEN];
23162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023163 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164}
23165
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023166 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023167get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023169 char_u *res = get_tv_string_buf_chk(varp, buf);
23170
23171 return res != NULL ? res : (char_u *)"";
23172}
23173
Bram Moolenaar7d647822014-04-05 21:28:56 +020023174/*
23175 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23176 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023177 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023178get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023179{
23180 static char_u mybuf[NUMBUFLEN];
23181
23182 return get_tv_string_buf_chk(varp, mybuf);
23183}
23184
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023185 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023186get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023187{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023188 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023190 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023191 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23192 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023193 return buf;
23194 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023195 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023196 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023197 break;
23198 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023199 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023200 break;
23201 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023202 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023203 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023204 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023205#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023206 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023207 break;
23208#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023209 case VAR_STRING:
23210 if (varp->vval.v_string != NULL)
23211 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023212 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023213 case VAR_SPECIAL:
23214 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23215 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023216 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023217#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023218 {
23219 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023220 char *status;
23221
23222 if (job == NULL)
23223 return (char_u *)"no process";
23224 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023225 : job->jv_status == JOB_ENDED ? "dead"
23226 : "run";
23227# ifdef UNIX
23228 vim_snprintf((char *)buf, NUMBUFLEN,
23229 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023230# elif defined(WIN32)
23231 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023232 "process %ld %s",
23233 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023234 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023235# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023236 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023237 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23238# endif
23239 return buf;
23240 }
23241#endif
23242 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023243 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023244#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023245 {
23246 channel_T *channel = varp->vval.v_channel;
23247 char *status = channel_status(channel);
23248
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023249 if (channel == NULL)
23250 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23251 else
23252 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023253 "channel %d %s", channel->ch_id, status);
23254 return buf;
23255 }
23256#endif
23257 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023258 case VAR_UNKNOWN:
23259 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023262 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263}
23264
23265/*
23266 * Find variable "name" in the list of variables.
23267 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023268 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023269 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023270 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023273find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277
Bram Moolenaara7043832005-01-21 11:56:39 +000023278 ht = find_var_ht(name, &varname);
23279 if (htp != NULL)
23280 *htp = ht;
23281 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023283 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284}
23285
23286/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023287 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023288 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023290 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023291find_var_in_ht(
23292 hashtab_T *ht,
23293 int htname,
23294 char_u *varname,
23295 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023296{
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 hashitem_T *hi;
23298
23299 if (*varname == NUL)
23300 {
23301 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023302 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023303 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023304 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023305 case 'g': return &globvars_var;
23306 case 'v': return &vimvars_var;
23307 case 'b': return &curbuf->b_bufvar;
23308 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023309#ifdef FEAT_WINDOWS
23310 case 't': return &curtab->tp_winvar;
23311#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023312 case 'l': return current_funccal == NULL
23313 ? NULL : &current_funccal->l_vars_var;
23314 case 'a': return current_funccal == NULL
23315 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023316 }
23317 return NULL;
23318 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023319
23320 hi = hash_find(ht, varname);
23321 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023322 {
23323 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023324 * worked find the variable again. Don't auto-load a script if it was
23325 * loaded already, otherwise it would be loaded every time when
23326 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023327 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023328 {
23329 /* Note: script_autoload() may make "hi" invalid. It must either
23330 * be obtained again or not used. */
23331 if (!script_autoload(varname, FALSE) || aborting())
23332 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023333 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023334 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023335 if (HASHITEM_EMPTY(hi))
23336 return NULL;
23337 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023338 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023339}
23340
23341/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023342 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023343 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023344 * Set "varname" to the start of name without ':'.
23345 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023346 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023347find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023349 hashitem_T *hi;
23350
Bram Moolenaar73627d02015-08-11 15:46:09 +020023351 if (name[0] == NUL)
23352 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 if (name[1] != ':')
23354 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023355 /* The name must not start with a colon or #. */
23356 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 return NULL;
23358 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023359
23360 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023361 hi = hash_find(&compat_hashtab, name);
23362 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023363 return &compat_hashtab;
23364
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023367 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 }
23369 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023370 if (*name == 'g') /* global variable */
23371 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023372 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23373 */
23374 if (vim_strchr(name + 2, ':') != NULL
23375 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023376 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023378 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023379 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023380 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023381#ifdef FEAT_WINDOWS
23382 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023383 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023384#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023385 if (*name == 'v') /* v: variable */
23386 return &vimvarht;
23387 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023388 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023389 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023390 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 if (*name == 's' /* script variable */
23392 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23393 return &SCRIPT_VARS(current_SID);
23394 return NULL;
23395}
23396
23397/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023398 * Get function call environment based on bactrace debug level
23399 */
23400 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023401get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023402{
23403 int i;
23404 funccall_T *funccal;
23405 funccall_T *temp_funccal;
23406
23407 funccal = current_funccal;
23408 if (debug_backtrace_level > 0)
23409 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023410 for (i = 0; i < debug_backtrace_level; i++)
23411 {
23412 temp_funccal = funccal->caller;
23413 if (temp_funccal)
23414 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023415 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023416 /* backtrace level overflow. reset to max */
23417 debug_backtrace_level = i;
23418 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023419 }
23420 return funccal;
23421}
23422
23423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023425 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 * Returns NULL when it doesn't exist.
23427 */
23428 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023429get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430{
Bram Moolenaar33570922005-01-25 22:26:29 +000023431 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023433 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 if (v == NULL)
23435 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023436 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437}
23438
23439/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023440 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 * sourcing this script and when executing functions defined in the script.
23442 */
23443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023444new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445{
Bram Moolenaara7043832005-01-21 11:56:39 +000023446 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023447 hashtab_T *ht;
23448 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023449
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23451 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023452 /* Re-allocating ga_data means that an ht_array pointing to
23453 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023454 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023455 for (i = 1; i <= ga_scripts.ga_len; ++i)
23456 {
23457 ht = &SCRIPT_VARS(i);
23458 if (ht->ht_mask == HT_INIT_SIZE - 1)
23459 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023460 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023461 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023462 }
23463
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 while (ga_scripts.ga_len < id)
23465 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023466 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023467 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023468 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 }
23471 }
23472}
23473
23474/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23476 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 */
23478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023479init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480{
Bram Moolenaar33570922005-01-25 22:26:29 +000023481 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023482 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023483 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023484 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023485 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023486 dict_var->di_tv.vval.v_dict = dict;
23487 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023488 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023489 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23490 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023491}
23492
23493/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023494 * Unreference a dictionary initialized by init_var_dict().
23495 */
23496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023497unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023498{
23499 /* Now the dict needs to be freed if no one else is using it, go back to
23500 * normal reference counting. */
23501 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23502 dict_unref(dict);
23503}
23504
23505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 * Frees all allocated variables and the value they contain.
23508 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 */
23510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023511vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023512{
23513 vars_clear_ext(ht, TRUE);
23514}
23515
23516/*
23517 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23518 */
23519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023520vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521{
Bram Moolenaara7043832005-01-21 11:56:39 +000023522 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023523 hashitem_T *hi;
23524 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525
Bram Moolenaar33570922005-01-25 22:26:29 +000023526 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023527 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023528 for (hi = ht->ht_array; todo > 0; ++hi)
23529 {
23530 if (!HASHITEM_EMPTY(hi))
23531 {
23532 --todo;
23533
Bram Moolenaar33570922005-01-25 22:26:29 +000023534 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023535 * ht_array might change then. hash_clear() takes care of it
23536 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023537 v = HI2DI(hi);
23538 if (free_val)
23539 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023540 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023541 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023542 }
23543 }
23544 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023545 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546}
23547
Bram Moolenaara7043832005-01-21 11:56:39 +000023548/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023549 * Delete a variable from hashtab "ht" at item "hi".
23550 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023553delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554{
Bram Moolenaar33570922005-01-25 22:26:29 +000023555 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023556
23557 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023558 clear_tv(&di->di_tv);
23559 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023560}
23561
23562/*
23563 * List the value of one internal variable.
23564 */
23565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023566list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023568 char_u *tofree;
23569 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023570 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023571
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023572 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023573 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023574 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023575 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576}
23577
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023579list_one_var_a(
23580 char_u *prefix,
23581 char_u *name,
23582 int type,
23583 char_u *string,
23584 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585{
Bram Moolenaar31859182007-08-14 20:41:13 +000023586 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23587 msg_start();
23588 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 if (name != NULL) /* "a:" vars don't have a name stored */
23590 msg_puts(name);
23591 msg_putchar(' ');
23592 msg_advance(22);
23593 if (type == VAR_NUMBER)
23594 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023595 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023596 msg_putchar('*');
23597 else if (type == VAR_LIST)
23598 {
23599 msg_putchar('[');
23600 if (*string == '[')
23601 ++string;
23602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023603 else if (type == VAR_DICT)
23604 {
23605 msg_putchar('{');
23606 if (*string == '{')
23607 ++string;
23608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023609 else
23610 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023611
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023613
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023614 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023615 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023616 if (*first)
23617 {
23618 msg_clr_eos();
23619 *first = FALSE;
23620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621}
23622
23623/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023624 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 * If the variable already exists, the value is updated.
23626 * Otherwise the variable is created.
23627 */
23628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023629set_var(
23630 char_u *name,
23631 typval_T *tv,
23632 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633{
Bram Moolenaar33570922005-01-25 22:26:29 +000023634 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023636 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023638 ht = find_var_ht(name, &varname);
23639 if (ht == NULL || *varname == NUL)
23640 {
23641 EMSG2(_(e_illvar), name);
23642 return;
23643 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023644 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023645
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023646 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23647 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023648 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023649
Bram Moolenaar33570922005-01-25 22:26:29 +000023650 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023652 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023653 if (var_check_ro(v->di_flags, name, FALSE)
23654 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023655 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023656
23657 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023658 * Handle setting internal v: variables separately where needed to
23659 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023660 */
23661 if (ht == &vimvarht)
23662 {
23663 if (v->di_tv.v_type == VAR_STRING)
23664 {
23665 vim_free(v->di_tv.vval.v_string);
23666 if (copy || tv->v_type != VAR_STRING)
23667 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23668 else
23669 {
23670 /* Take over the string to avoid an extra alloc/free. */
23671 v->di_tv.vval.v_string = tv->vval.v_string;
23672 tv->vval.v_string = NULL;
23673 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023674 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023675 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023676 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023677 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023678 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023679 if (STRCMP(varname, "searchforward") == 0)
23680 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023681#ifdef FEAT_SEARCH_EXTRA
23682 else if (STRCMP(varname, "hlsearch") == 0)
23683 {
23684 no_hlsearch = !v->di_tv.vval.v_number;
23685 redraw_all_later(SOME_VALID);
23686 }
23687#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023688 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023689 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023690 else if (v->di_tv.v_type != tv->v_type)
23691 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023692 }
23693
23694 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 }
23696 else /* add a new variable */
23697 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023698 /* Can't add "v:" variable. */
23699 if (ht == &vimvarht)
23700 {
23701 EMSG2(_(e_illvar), name);
23702 return;
23703 }
23704
Bram Moolenaar92124a32005-06-17 22:03:40 +000023705 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023706 if (!valid_varname(varname))
23707 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023708
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023709 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23710 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023711 if (v == NULL)
23712 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023713 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023714 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023716 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 return;
23718 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023719 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023721
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023722 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023723 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023724 else
23725 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023726 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023727 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023728 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730}
23731
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023732/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023733 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023734 * Also give an error message.
23735 */
23736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023737var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023738{
23739 if (flags & DI_FLAGS_RO)
23740 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023741 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023742 return TRUE;
23743 }
23744 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23745 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023746 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023747 return TRUE;
23748 }
23749 return FALSE;
23750}
23751
23752/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023753 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23754 * Also give an error message.
23755 */
23756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023757var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023758{
23759 if (flags & DI_FLAGS_FIX)
23760 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023761 EMSG2(_("E795: Cannot delete variable %s"),
23762 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023763 return TRUE;
23764 }
23765 return FALSE;
23766}
23767
23768/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023769 * Check if a funcref is assigned to a valid variable name.
23770 * Return TRUE and give an error if not.
23771 */
23772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023773var_check_func_name(
23774 char_u *name, /* points to start of variable name */
23775 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023776{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023777 /* Allow for w: b: s: and t:. */
23778 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023779 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23780 ? name[2] : name[0]))
23781 {
23782 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23783 name);
23784 return TRUE;
23785 }
23786 /* Don't allow hiding a function. When "v" is not NULL we might be
23787 * assigning another function to the same var, the type is checked
23788 * below. */
23789 if (new_var && function_exists(name))
23790 {
23791 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23792 name);
23793 return TRUE;
23794 }
23795 return FALSE;
23796}
23797
23798/*
23799 * Check if a variable name is valid.
23800 * Return FALSE and give an error if not.
23801 */
23802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023803valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023804{
23805 char_u *p;
23806
23807 for (p = varname; *p != NUL; ++p)
23808 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23809 && *p != AUTOLOAD_CHAR)
23810 {
23811 EMSG2(_(e_illvar), varname);
23812 return FALSE;
23813 }
23814 return TRUE;
23815}
23816
23817/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023818 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023819 * Also give an error message, using "name" or _("name") when use_gettext is
23820 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023821 */
23822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023823tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023824{
23825 if (lock & VAR_LOCKED)
23826 {
23827 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023828 name == NULL ? (char_u *)_("Unknown")
23829 : use_gettext ? (char_u *)_(name)
23830 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023831 return TRUE;
23832 }
23833 if (lock & VAR_FIXED)
23834 {
23835 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023836 name == NULL ? (char_u *)_("Unknown")
23837 : use_gettext ? (char_u *)_(name)
23838 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023839 return TRUE;
23840 }
23841 return FALSE;
23842}
23843
23844/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023845 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023846 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023847 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023848 * It is OK for "from" and "to" to point to the same item. This is used to
23849 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023850 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023852copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023854 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023855 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023856 switch (from->v_type)
23857 {
23858 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023859 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023860 to->vval.v_number = from->vval.v_number;
23861 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023862 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023863#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023864 to->vval.v_float = from->vval.v_float;
23865 break;
23866#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023867 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023868#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023869 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023870 if (to->vval.v_job != NULL)
23871 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023872 break;
23873#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023874 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023875#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023876 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023877 if (to->vval.v_channel != NULL)
23878 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023879 break;
23880#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023881 case VAR_STRING:
23882 case VAR_FUNC:
23883 if (from->vval.v_string == NULL)
23884 to->vval.v_string = NULL;
23885 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023887 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023888 if (from->v_type == VAR_FUNC)
23889 func_ref(to->vval.v_string);
23890 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023891 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023892 case VAR_PARTIAL:
23893 if (from->vval.v_partial == NULL)
23894 to->vval.v_partial = NULL;
23895 else
23896 {
23897 to->vval.v_partial = from->vval.v_partial;
23898 ++to->vval.v_partial->pt_refcount;
23899 }
23900 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023901 case VAR_LIST:
23902 if (from->vval.v_list == NULL)
23903 to->vval.v_list = NULL;
23904 else
23905 {
23906 to->vval.v_list = from->vval.v_list;
23907 ++to->vval.v_list->lv_refcount;
23908 }
23909 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023910 case VAR_DICT:
23911 if (from->vval.v_dict == NULL)
23912 to->vval.v_dict = NULL;
23913 else
23914 {
23915 to->vval.v_dict = from->vval.v_dict;
23916 ++to->vval.v_dict->dv_refcount;
23917 }
23918 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023919 case VAR_UNKNOWN:
23920 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023921 break;
23922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923}
23924
23925/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023926 * Make a copy of an item.
23927 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023928 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23929 * reference to an already copied list/dict can be used.
23930 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023931 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023933item_copy(
23934 typval_T *from,
23935 typval_T *to,
23936 int deep,
23937 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023938{
23939 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023940 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023941
Bram Moolenaar33570922005-01-25 22:26:29 +000023942 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023943 {
23944 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023945 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023946 }
23947 ++recurse;
23948
23949 switch (from->v_type)
23950 {
23951 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023952 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023953 case VAR_STRING:
23954 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023955 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023956 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023957 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023958 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023959 copy_tv(from, to);
23960 break;
23961 case VAR_LIST:
23962 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023963 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023964 if (from->vval.v_list == NULL)
23965 to->vval.v_list = NULL;
23966 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23967 {
23968 /* use the copy made earlier */
23969 to->vval.v_list = from->vval.v_list->lv_copylist;
23970 ++to->vval.v_list->lv_refcount;
23971 }
23972 else
23973 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23974 if (to->vval.v_list == NULL)
23975 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023976 break;
23977 case VAR_DICT:
23978 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023979 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023980 if (from->vval.v_dict == NULL)
23981 to->vval.v_dict = NULL;
23982 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23983 {
23984 /* use the copy made earlier */
23985 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23986 ++to->vval.v_dict->dv_refcount;
23987 }
23988 else
23989 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23990 if (to->vval.v_dict == NULL)
23991 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023992 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023993 case VAR_UNKNOWN:
23994 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023995 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023996 }
23997 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023998 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023999}
24000
24001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 * ":echo expr1 ..." print each argument separated with a space, add a
24003 * newline at the end.
24004 * ":echon expr1 ..." print each argument plain.
24005 */
24006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024007ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008{
24009 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024010 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024011 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 char_u *p;
24013 int needclr = TRUE;
24014 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024015 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016
24017 if (eap->skip)
24018 ++emsg_skip;
24019 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
24020 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024021 /* If eval1() causes an error message the text from the command may
24022 * still need to be cleared. E.g., "echo 22,44". */
24023 need_clr_eos = needclr;
24024
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024026 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 {
24028 /*
24029 * Report the invalid expression unless the expression evaluation
24030 * has been cancelled due to an aborting error, an interrupt, or an
24031 * exception.
24032 */
24033 if (!aborting())
24034 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024035 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 break;
24037 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024038 need_clr_eos = FALSE;
24039
Bram Moolenaar071d4272004-06-13 20:20:40 +000024040 if (!eap->skip)
24041 {
24042 if (atstart)
24043 {
24044 atstart = FALSE;
24045 /* Call msg_start() after eval1(), evaluating the expression
24046 * may cause a message to appear. */
24047 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010024048 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020024049 /* Mark the saved text as finishing the line, so that what
24050 * follows is displayed on a new line when scrolling back
24051 * at the more prompt. */
24052 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010024054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 }
24056 else if (eap->cmdidx == CMD_echo)
24057 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010024058 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024059 if (p != NULL)
24060 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024062 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024064 if (*p != TAB && needclr)
24065 {
24066 /* remove any text still there from the command */
24067 msg_clr_eos();
24068 needclr = FALSE;
24069 }
24070 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 }
24072 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024073 {
24074#ifdef FEAT_MBYTE
24075 if (has_mbyte)
24076 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000024077 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024078
24079 (void)msg_outtrans_len_attr(p, i, echo_attr);
24080 p += i - 1;
24081 }
24082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024084 (void)msg_outtrans_len_attr(p, 1, echo_attr);
24085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024087 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 arg = skipwhite(arg);
24091 }
24092 eap->nextcmd = check_nextcmd(arg);
24093
24094 if (eap->skip)
24095 --emsg_skip;
24096 else
24097 {
24098 /* remove text that may still be there from the command */
24099 if (needclr)
24100 msg_clr_eos();
24101 if (eap->cmdidx == CMD_echo)
24102 msg_end();
24103 }
24104}
24105
24106/*
24107 * ":echohl {name}".
24108 */
24109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024110ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111{
24112 int id;
24113
24114 id = syn_name2id(eap->arg);
24115 if (id == 0)
24116 echo_attr = 0;
24117 else
24118 echo_attr = syn_id2attr(id);
24119}
24120
24121/*
24122 * ":execute expr1 ..." execute the result of an expression.
24123 * ":echomsg expr1 ..." Print a message
24124 * ":echoerr expr1 ..." Print an error
24125 * Each gets spaces around each argument and a newline at the end for
24126 * echo commands
24127 */
24128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024129ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130{
24131 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024132 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 int ret = OK;
24134 char_u *p;
24135 garray_T ga;
24136 int len;
24137 int save_did_emsg;
24138
24139 ga_init2(&ga, 1, 80);
24140
24141 if (eap->skip)
24142 ++emsg_skip;
24143 while (*arg != NUL && *arg != '|' && *arg != '\n')
24144 {
24145 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024146 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 {
24148 /*
24149 * Report the invalid expression unless the expression evaluation
24150 * has been cancelled due to an aborting error, an interrupt, or an
24151 * exception.
24152 */
24153 if (!aborting())
24154 EMSG2(_(e_invexpr2), p);
24155 ret = FAIL;
24156 break;
24157 }
24158
24159 if (!eap->skip)
24160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024161 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 len = (int)STRLEN(p);
24163 if (ga_grow(&ga, len + 2) == FAIL)
24164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024165 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 ret = FAIL;
24167 break;
24168 }
24169 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024172 ga.ga_len += len;
24173 }
24174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024175 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 arg = skipwhite(arg);
24177 }
24178
24179 if (ret != FAIL && ga.ga_data != NULL)
24180 {
24181 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024184 out_flush();
24185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 else if (eap->cmdidx == CMD_echoerr)
24187 {
24188 /* We don't want to abort following commands, restore did_emsg. */
24189 save_did_emsg = did_emsg;
24190 EMSG((char_u *)ga.ga_data);
24191 if (!force_abort)
24192 did_emsg = save_did_emsg;
24193 }
24194 else if (eap->cmdidx == CMD_execute)
24195 do_cmdline((char_u *)ga.ga_data,
24196 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24197 }
24198
24199 ga_clear(&ga);
24200
24201 if (eap->skip)
24202 --emsg_skip;
24203
24204 eap->nextcmd = check_nextcmd(arg);
24205}
24206
24207/*
24208 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24209 * "arg" points to the "&" or '+' when called, to "option" when returning.
24210 * Returns NULL when no option name found. Otherwise pointer to the char
24211 * after the option name.
24212 */
24213 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024214find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215{
24216 char_u *p = *arg;
24217
24218 ++p;
24219 if (*p == 'g' && p[1] == ':')
24220 {
24221 *opt_flags = OPT_GLOBAL;
24222 p += 2;
24223 }
24224 else if (*p == 'l' && p[1] == ':')
24225 {
24226 *opt_flags = OPT_LOCAL;
24227 p += 2;
24228 }
24229 else
24230 *opt_flags = 0;
24231
24232 if (!ASCII_ISALPHA(*p))
24233 return NULL;
24234 *arg = p;
24235
24236 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24237 p += 4; /* termcap option */
24238 else
24239 while (ASCII_ISALPHA(*p))
24240 ++p;
24241 return p;
24242}
24243
24244/*
24245 * ":function"
24246 */
24247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024248ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249{
24250 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024251 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 int j;
24253 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024255 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256 char_u *name = NULL;
24257 char_u *p;
24258 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024259 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 garray_T newargs;
24261 garray_T newlines;
24262 int varargs = FALSE;
24263 int mustend = FALSE;
24264 int flags = 0;
24265 ufunc_T *fp;
24266 int indent;
24267 int nesting;
24268 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024269 dictitem_T *v;
24270 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024271 static int func_nr = 0; /* number for nameless function */
24272 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024273 hashtab_T *ht;
24274 int todo;
24275 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024276 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277
24278 /*
24279 * ":function" without argument: list functions.
24280 */
24281 if (ends_excmd(*eap->arg))
24282 {
24283 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024284 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024285 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024286 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024287 {
24288 if (!HASHITEM_EMPTY(hi))
24289 {
24290 --todo;
24291 fp = HI2UF(hi);
24292 if (!isdigit(*fp->uf_name))
24293 list_func_head(fp, FALSE);
24294 }
24295 }
24296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 eap->nextcmd = check_nextcmd(eap->arg);
24298 return;
24299 }
24300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024301 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024302 * ":function /pat": list functions matching pattern.
24303 */
24304 if (*eap->arg == '/')
24305 {
24306 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24307 if (!eap->skip)
24308 {
24309 regmatch_T regmatch;
24310
24311 c = *p;
24312 *p = NUL;
24313 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24314 *p = c;
24315 if (regmatch.regprog != NULL)
24316 {
24317 regmatch.rm_ic = p_ic;
24318
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024319 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024320 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24321 {
24322 if (!HASHITEM_EMPTY(hi))
24323 {
24324 --todo;
24325 fp = HI2UF(hi);
24326 if (!isdigit(*fp->uf_name)
24327 && vim_regexec(&regmatch, fp->uf_name, 0))
24328 list_func_head(fp, FALSE);
24329 }
24330 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024331 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024332 }
24333 }
24334 if (*p == '/')
24335 ++p;
24336 eap->nextcmd = check_nextcmd(p);
24337 return;
24338 }
24339
24340 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024341 * Get the function name. There are these situations:
24342 * func normal function name
24343 * "name" == func, "fudi.fd_dict" == NULL
24344 * dict.func new dictionary entry
24345 * "name" == NULL, "fudi.fd_dict" set,
24346 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24347 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024348 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024349 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24350 * dict.func existing dict entry that's not a Funcref
24351 * "name" == NULL, "fudi.fd_dict" set,
24352 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024353 * s:func script-local function name
24354 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024356 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024357 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024358 paren = (vim_strchr(p, '(') != NULL);
24359 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 {
24361 /*
24362 * Return on an invalid expression in braces, unless the expression
24363 * evaluation has been cancelled due to an aborting error, an
24364 * interrupt, or an exception.
24365 */
24366 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024368 if (!eap->skip && fudi.fd_newkey != NULL)
24369 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 else
24374 eap->skip = TRUE;
24375 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024376
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 /* An error in a function call during evaluation of an expression in magic
24378 * braces should not cause the function not to be defined. */
24379 saved_did_emsg = did_emsg;
24380 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381
24382 /*
24383 * ":function func" with only function name: list function.
24384 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024385 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 {
24387 if (!ends_excmd(*skipwhite(p)))
24388 {
24389 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024390 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 }
24392 eap->nextcmd = check_nextcmd(p);
24393 if (eap->nextcmd != NULL)
24394 *p = NUL;
24395 if (!eap->skip && !got_int)
24396 {
24397 fp = find_func(name);
24398 if (fp != NULL)
24399 {
24400 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024401 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024403 if (FUNCLINE(fp, j) == NULL)
24404 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 msg_putchar('\n');
24406 msg_outnum((long)(j + 1));
24407 if (j < 9)
24408 msg_putchar(' ');
24409 if (j < 99)
24410 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024411 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 out_flush(); /* show a line at a time */
24413 ui_breakcheck();
24414 }
24415 if (!got_int)
24416 {
24417 msg_putchar('\n');
24418 msg_puts((char_u *)" endfunction");
24419 }
24420 }
24421 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024422 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024424 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425 }
24426
24427 /*
24428 * ":function name(arg1, arg2)" Define function.
24429 */
24430 p = skipwhite(p);
24431 if (*p != '(')
24432 {
24433 if (!eap->skip)
24434 {
24435 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024436 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437 }
24438 /* attempt to continue by skipping some text */
24439 if (vim_strchr(p, '(') != NULL)
24440 p = vim_strchr(p, '(');
24441 }
24442 p = skipwhite(p + 1);
24443
24444 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24445 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24446
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024447 if (!eap->skip)
24448 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024449 /* Check the name of the function. Unless it's a dictionary function
24450 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024451 if (name != NULL)
24452 arg = name;
24453 else
24454 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024455 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024456 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24457 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024458 {
24459 if (*arg == K_SPECIAL)
24460 j = 3;
24461 else
24462 j = 0;
24463 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24464 : eval_isnamec(arg[j])))
24465 ++j;
24466 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024467 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024468 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024469 /* Disallow using the g: dict. */
24470 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24471 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024472 }
24473
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 /*
24475 * Isolate the arguments: "arg1, arg2, ...)"
24476 */
24477 while (*p != ')')
24478 {
24479 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24480 {
24481 varargs = TRUE;
24482 p += 3;
24483 mustend = TRUE;
24484 }
24485 else
24486 {
24487 arg = p;
24488 while (ASCII_ISALNUM(*p) || *p == '_')
24489 ++p;
24490 if (arg == p || isdigit(*arg)
24491 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24492 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24493 {
24494 if (!eap->skip)
24495 EMSG2(_("E125: Illegal argument: %s"), arg);
24496 break;
24497 }
24498 if (ga_grow(&newargs, 1) == FAIL)
24499 goto erret;
24500 c = *p;
24501 *p = NUL;
24502 arg = vim_strsave(arg);
24503 if (arg == NULL)
24504 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024505
24506 /* Check for duplicate argument name. */
24507 for (i = 0; i < newargs.ga_len; ++i)
24508 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24509 {
24510 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024511 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024512 goto erret;
24513 }
24514
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24516 *p = c;
24517 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 if (*p == ',')
24519 ++p;
24520 else
24521 mustend = TRUE;
24522 }
24523 p = skipwhite(p);
24524 if (mustend && *p != ')')
24525 {
24526 if (!eap->skip)
24527 EMSG2(_(e_invarg2), eap->arg);
24528 break;
24529 }
24530 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024531 if (*p != ')')
24532 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024533 ++p; /* skip the ')' */
24534
Bram Moolenaare9a41262005-01-15 22:18:47 +000024535 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024536 for (;;)
24537 {
24538 p = skipwhite(p);
24539 if (STRNCMP(p, "range", 5) == 0)
24540 {
24541 flags |= FC_RANGE;
24542 p += 5;
24543 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024544 else if (STRNCMP(p, "dict", 4) == 0)
24545 {
24546 flags |= FC_DICT;
24547 p += 4;
24548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024549 else if (STRNCMP(p, "abort", 5) == 0)
24550 {
24551 flags |= FC_ABORT;
24552 p += 5;
24553 }
24554 else
24555 break;
24556 }
24557
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024558 /* When there is a line break use what follows for the function body.
24559 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24560 if (*p == '\n')
24561 line_arg = p + 1;
24562 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 EMSG(_(e_trailing));
24564
24565 /*
24566 * Read the body of the function, until ":endfunction" is found.
24567 */
24568 if (KeyTyped)
24569 {
24570 /* Check if the function already exists, don't let the user type the
24571 * whole function before telling him it doesn't work! For a script we
24572 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024573 if (!eap->skip && !eap->forceit)
24574 {
24575 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24576 EMSG(_(e_funcdict));
24577 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024578 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024581 if (!eap->skip && did_emsg)
24582 goto erret;
24583
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 msg_putchar('\n'); /* don't overwrite the function name */
24585 cmdline_row = msg_row;
24586 }
24587
24588 indent = 2;
24589 nesting = 0;
24590 for (;;)
24591 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024592 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024593 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024594 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024595 saved_wait_return = FALSE;
24596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024598 sourcing_lnum_off = sourcing_lnum;
24599
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024600 if (line_arg != NULL)
24601 {
24602 /* Use eap->arg, split up in parts by line breaks. */
24603 theline = line_arg;
24604 p = vim_strchr(theline, '\n');
24605 if (p == NULL)
24606 line_arg += STRLEN(line_arg);
24607 else
24608 {
24609 *p = NUL;
24610 line_arg = p + 1;
24611 }
24612 }
24613 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 theline = getcmdline(':', 0L, indent);
24615 else
24616 theline = eap->getline(':', eap->cookie, indent);
24617 if (KeyTyped)
24618 lines_left = Rows - 1;
24619 if (theline == NULL)
24620 {
24621 EMSG(_("E126: Missing :endfunction"));
24622 goto erret;
24623 }
24624
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024625 /* Detect line continuation: sourcing_lnum increased more than one. */
24626 if (sourcing_lnum > sourcing_lnum_off + 1)
24627 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24628 else
24629 sourcing_lnum_off = 0;
24630
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 if (skip_until != NULL)
24632 {
24633 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24634 * don't check for ":endfunc". */
24635 if (STRCMP(theline, skip_until) == 0)
24636 {
24637 vim_free(skip_until);
24638 skip_until = NULL;
24639 }
24640 }
24641 else
24642 {
24643 /* skip ':' and blanks*/
24644 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24645 ;
24646
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024647 /* Check for "endfunction". */
24648 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024650 if (line_arg == NULL)
24651 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652 break;
24653 }
24654
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024655 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 * at "end". */
24657 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24658 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024659 else if (STRNCMP(p, "if", 2) == 0
24660 || STRNCMP(p, "wh", 2) == 0
24661 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 || STRNCMP(p, "try", 3) == 0)
24663 indent += 2;
24664
24665 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024666 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024668 if (*p == '!')
24669 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024671 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024672 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024674 ++nesting;
24675 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676 }
24677 }
24678
24679 /* Check for ":append" or ":insert". */
24680 p = skip_range(p, NULL);
24681 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24682 || (p[0] == 'i'
24683 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24684 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24685 skip_until = vim_strsave((char_u *)".");
24686
24687 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24688 arg = skipwhite(skiptowhite(p));
24689 if (arg[0] == '<' && arg[1] =='<'
24690 && ((p[0] == 'p' && p[1] == 'y'
24691 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24692 || (p[0] == 'p' && p[1] == 'e'
24693 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24694 || (p[0] == 't' && p[1] == 'c'
24695 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024696 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24697 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024698 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24699 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024700 || (p[0] == 'm' && p[1] == 'z'
24701 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702 ))
24703 {
24704 /* ":python <<" continues until a dot, like ":append" */
24705 p = skipwhite(arg + 2);
24706 if (*p == NUL)
24707 skip_until = vim_strsave((char_u *)".");
24708 else
24709 skip_until = vim_strsave(p);
24710 }
24711 }
24712
24713 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024714 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024715 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024716 if (line_arg == NULL)
24717 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024719 }
24720
24721 /* Copy the line to newly allocated memory. get_one_sourceline()
24722 * allocates 250 bytes per line, this saves 80% on average. The cost
24723 * is an extra alloc/free. */
24724 p = vim_strsave(theline);
24725 if (p != NULL)
24726 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024727 if (line_arg == NULL)
24728 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024729 theline = p;
24730 }
24731
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024732 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24733
24734 /* Add NULL lines for continuation lines, so that the line count is
24735 * equal to the index in the growarray. */
24736 while (sourcing_lnum_off-- > 0)
24737 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024738
24739 /* Check for end of eap->arg. */
24740 if (line_arg != NULL && *line_arg == NUL)
24741 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 }
24743
24744 /* Don't define the function when skipping commands or when an error was
24745 * detected. */
24746 if (eap->skip || did_emsg)
24747 goto erret;
24748
24749 /*
24750 * If there are no errors, add the function
24751 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024752 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024753 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024754 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024755 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024756 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024757 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024758 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024759 goto erret;
24760 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024761
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024762 fp = find_func(name);
24763 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024765 if (!eap->forceit)
24766 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024767 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024768 goto erret;
24769 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024771 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024772 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024773 name);
24774 goto erret;
24775 }
24776 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024777 ga_clear_strings(&(fp->uf_args));
24778 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024779 vim_free(name);
24780 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782 }
24783 else
24784 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024785 char numbuf[20];
24786
24787 fp = NULL;
24788 if (fudi.fd_newkey == NULL && !eap->forceit)
24789 {
24790 EMSG(_(e_funcdict));
24791 goto erret;
24792 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024793 if (fudi.fd_di == NULL)
24794 {
24795 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024796 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024797 goto erret;
24798 }
24799 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024800 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024801 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024802
24803 /* Give the function a sequential number. Can only be used with a
24804 * Funcref! */
24805 vim_free(name);
24806 sprintf(numbuf, "%d", ++func_nr);
24807 name = vim_strsave((char_u *)numbuf);
24808 if (name == NULL)
24809 goto erret;
24810 }
24811
24812 if (fp == NULL)
24813 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024814 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024815 {
24816 int slen, plen;
24817 char_u *scriptname;
24818
24819 /* Check that the autoload name matches the script name. */
24820 j = FAIL;
24821 if (sourcing_name != NULL)
24822 {
24823 scriptname = autoload_name(name);
24824 if (scriptname != NULL)
24825 {
24826 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024827 plen = (int)STRLEN(p);
24828 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024829 if (slen > plen && fnamecmp(p,
24830 sourcing_name + slen - plen) == 0)
24831 j = OK;
24832 vim_free(scriptname);
24833 }
24834 }
24835 if (j == FAIL)
24836 {
24837 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24838 goto erret;
24839 }
24840 }
24841
24842 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 if (fp == NULL)
24844 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024845
24846 if (fudi.fd_dict != NULL)
24847 {
24848 if (fudi.fd_di == NULL)
24849 {
24850 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024851 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024852 if (fudi.fd_di == NULL)
24853 {
24854 vim_free(fp);
24855 goto erret;
24856 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024857 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24858 {
24859 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024860 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024861 goto erret;
24862 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024863 }
24864 else
24865 /* overwrite existing dict entry */
24866 clear_tv(&fudi.fd_di->di_tv);
24867 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024868 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024869 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024870 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024871
24872 /* behave like "dict" was used */
24873 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024874 }
24875
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024877 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024878 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24879 {
24880 vim_free(fp);
24881 goto erret;
24882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024884 fp->uf_args = newargs;
24885 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024886#ifdef FEAT_PROFILE
24887 fp->uf_tml_count = NULL;
24888 fp->uf_tml_total = NULL;
24889 fp->uf_tml_self = NULL;
24890 fp->uf_profiling = FALSE;
24891 if (prof_def_func())
24892 func_do_profile(fp);
24893#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024894 fp->uf_varargs = varargs;
24895 fp->uf_flags = flags;
24896 fp->uf_calls = 0;
24897 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024898 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899
24900erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901 ga_clear_strings(&newargs);
24902 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024903ret_free:
24904 vim_free(skip_until);
24905 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024908 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909}
24910
24911/*
24912 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024913 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024915 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024916 * TFN_INT: internal function name OK
24917 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024918 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 * Advances "pp" to just after the function name (if no error).
24920 */
24921 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024922trans_function_name(
24923 char_u **pp,
24924 int skip, /* only find the end, don't evaluate */
24925 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024926 funcdict_T *fdp, /* return: info about dictionary used */
24927 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024929 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 char_u *start;
24931 char_u *end;
24932 int lead;
24933 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024935 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024936
24937 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024938 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024940
24941 /* Check for hard coded <SNR>: already translated function ID (from a user
24942 * command). */
24943 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24944 && (*pp)[2] == (int)KE_SNR)
24945 {
24946 *pp += 3;
24947 len = get_id_len(pp) + 3;
24948 return vim_strnsave(start, len);
24949 }
24950
24951 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24952 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024954 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024956
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024957 /* Note that TFN_ flags use the same values as GLV_ flags. */
24958 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024959 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024960 if (end == start)
24961 {
24962 if (!skip)
24963 EMSG(_("E129: Function name required"));
24964 goto theend;
24965 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024966 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024967 {
24968 /*
24969 * Report an invalid expression in braces, unless the expression
24970 * evaluation has been cancelled due to an aborting error, an
24971 * interrupt, or an exception.
24972 */
24973 if (!aborting())
24974 {
24975 if (end != NULL)
24976 EMSG2(_(e_invarg2), start);
24977 }
24978 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024979 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024980 goto theend;
24981 }
24982
24983 if (lv.ll_tv != NULL)
24984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024985 if (fdp != NULL)
24986 {
24987 fdp->fd_dict = lv.ll_dict;
24988 fdp->fd_newkey = lv.ll_newkey;
24989 lv.ll_newkey = NULL;
24990 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024991 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024992 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24993 {
24994 name = vim_strsave(lv.ll_tv->vval.v_string);
24995 *pp = end;
24996 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024997 else if (lv.ll_tv->v_type == VAR_PARTIAL
24998 && lv.ll_tv->vval.v_partial != NULL)
24999 {
25000 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
25001 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010025002 if (partial != NULL)
25003 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010025004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025005 else
25006 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025007 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
25008 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025009 EMSG(_(e_funcref));
25010 else
25011 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025012 name = NULL;
25013 }
25014 goto theend;
25015 }
25016
25017 if (lv.ll_name == NULL)
25018 {
25019 /* Error found, but continue after the function name. */
25020 *pp = end;
25021 goto theend;
25022 }
25023
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025024 /* Check if the name is a Funcref. If so, use the value. */
25025 if (lv.ll_exp_name != NULL)
25026 {
25027 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010025028 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025029 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025030 if (name == lv.ll_exp_name)
25031 name = NULL;
25032 }
25033 else
25034 {
25035 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010025036 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025037 if (name == *pp)
25038 name = NULL;
25039 }
25040 if (name != NULL)
25041 {
25042 name = vim_strsave(name);
25043 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020025044 if (STRNCMP(name, "<SNR>", 5) == 0)
25045 {
25046 /* Change "<SNR>" to the byte sequence. */
25047 name[0] = K_SPECIAL;
25048 name[1] = KS_EXTRA;
25049 name[2] = (int)KE_SNR;
25050 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
25051 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025052 goto theend;
25053 }
25054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025055 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025056 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025057 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025058 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
25059 && STRNCMP(lv.ll_name, "s:", 2) == 0)
25060 {
25061 /* When there was "s:" already or the name expanded to get a
25062 * leading "s:" then remove it. */
25063 lv.ll_name += 2;
25064 len -= 2;
25065 lead = 2;
25066 }
25067 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025068 else
Bram Moolenaara7043832005-01-21 11:56:39 +000025069 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025070 /* skip over "s:" and "g:" */
25071 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000025072 lv.ll_name += 2;
25073 len = (int)(end - lv.ll_name);
25074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025075
25076 /*
25077 * Copy the function name to allocated memory.
25078 * Accept <SID>name() inside a script, translate into <SNR>123_name().
25079 * Accept <SNR>123_name() outside a script.
25080 */
25081 if (skip)
25082 lead = 0; /* do nothing */
25083 else if (lead > 0)
25084 {
25085 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000025086 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
25087 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025088 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000025089 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025090 if (current_SID <= 0)
25091 {
25092 EMSG(_(e_usingsid));
25093 goto theend;
25094 }
25095 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
25096 lead += (int)STRLEN(sid_buf);
25097 }
25098 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025099 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025100 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025101 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025102 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025103 goto theend;
25104 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025105 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025106 {
25107 char_u *cp = vim_strchr(lv.ll_name, ':');
25108
25109 if (cp != NULL && cp < end)
25110 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025111 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025112 goto theend;
25113 }
25114 }
25115
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025116 name = alloc((unsigned)(len + lead + 1));
25117 if (name != NULL)
25118 {
25119 if (lead > 0)
25120 {
25121 name[0] = K_SPECIAL;
25122 name[1] = KS_EXTRA;
25123 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000025124 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025125 STRCPY(name + 3, sid_buf);
25126 }
25127 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025128 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025129 }
25130 *pp = end;
25131
25132theend:
25133 clear_lval(&lv);
25134 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025135}
25136
25137/*
25138 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25139 * Return 2 if "p" starts with "s:".
25140 * Return 0 otherwise.
25141 */
25142 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025143eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025145 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25146 * the standard library function. */
25147 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25148 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149 return 5;
25150 if (p[0] == 's' && p[1] == ':')
25151 return 2;
25152 return 0;
25153}
25154
25155/*
25156 * Return TRUE if "p" starts with "<SID>" or "s:".
25157 * Only works if eval_fname_script() returned non-zero for "p"!
25158 */
25159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025160eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025161{
25162 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25163}
25164
25165/*
25166 * List the head of the function: "name(arg1, arg2)".
25167 */
25168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025169list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170{
25171 int j;
25172
25173 msg_start();
25174 if (indent)
25175 MSG_PUTS(" ");
25176 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025177 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 {
25179 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025180 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 }
25182 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025183 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025185 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025186 {
25187 if (j)
25188 MSG_PUTS(", ");
25189 msg_puts(FUNCARG(fp, j));
25190 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025191 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025192 {
25193 if (j)
25194 MSG_PUTS(", ");
25195 MSG_PUTS("...");
25196 }
25197 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025198 if (fp->uf_flags & FC_ABORT)
25199 MSG_PUTS(" abort");
25200 if (fp->uf_flags & FC_RANGE)
25201 MSG_PUTS(" range");
25202 if (fp->uf_flags & FC_DICT)
25203 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025204 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025205 if (p_verbose > 0)
25206 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207}
25208
25209/*
25210 * Find a function by name, return pointer to it in ufuncs.
25211 * Return NULL for unknown function.
25212 */
25213 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025214find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025216 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025218 hi = hash_find(&func_hashtab, name);
25219 if (!HASHITEM_EMPTY(hi))
25220 return HI2UF(hi);
25221 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222}
25223
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025224#if defined(EXITFREE) || defined(PROTO)
25225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025226free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025227{
25228 hashitem_T *hi;
25229
25230 /* Need to start all over every time, because func_free() may change the
25231 * hash table. */
25232 while (func_hashtab.ht_used > 0)
25233 for (hi = func_hashtab.ht_array; ; ++hi)
25234 if (!HASHITEM_EMPTY(hi))
25235 {
25236 func_free(HI2UF(hi));
25237 break;
25238 }
25239}
25240#endif
25241
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025243translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025244{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025245 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025246 return find_internal_func(name) >= 0;
25247 return find_func(name) != NULL;
25248}
25249
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025250/*
25251 * Return TRUE if a function "name" exists.
25252 */
25253 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025254function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025255{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025256 char_u *nm = name;
25257 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025258 int n = FALSE;
25259
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025260 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025261 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025262 nm = skipwhite(nm);
25263
25264 /* Only accept "funcname", "funcname ", "funcname (..." and
25265 * "funcname(...", not "funcname!...". */
25266 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025267 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025268 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025269 return n;
25270}
25271
Bram Moolenaara1544c02013-05-30 12:35:52 +020025272 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025273get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025274{
25275 char_u *nm = name;
25276 char_u *p;
25277
Bram Moolenaar65639032016-03-16 21:40:30 +010025278 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025279
25280 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025281 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025282 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025283
Bram Moolenaara1544c02013-05-30 12:35:52 +020025284 vim_free(p);
25285 return NULL;
25286}
25287
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025288/*
25289 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025290 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25291 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025292 */
25293 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025294builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025295{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025296 char_u *p;
25297
25298 if (!ASCII_ISLOWER(name[0]))
25299 return FALSE;
25300 p = vim_strchr(name, AUTOLOAD_CHAR);
25301 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025302}
25303
Bram Moolenaar05159a02005-02-26 23:04:13 +000025304#if defined(FEAT_PROFILE) || defined(PROTO)
25305/*
25306 * Start profiling function "fp".
25307 */
25308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025309func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025310{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025311 int len = fp->uf_lines.ga_len;
25312
25313 if (len == 0)
25314 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025315 fp->uf_tm_count = 0;
25316 profile_zero(&fp->uf_tm_self);
25317 profile_zero(&fp->uf_tm_total);
25318 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025319 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025320 if (fp->uf_tml_total == NULL)
25321 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025322 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025323 if (fp->uf_tml_self == NULL)
25324 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025325 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025326 fp->uf_tml_idx = -1;
25327 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25328 || fp->uf_tml_self == NULL)
25329 return; /* out of memory */
25330
25331 fp->uf_profiling = TRUE;
25332}
25333
25334/*
25335 * Dump the profiling results for all functions in file "fd".
25336 */
25337 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025338func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025339{
25340 hashitem_T *hi;
25341 int todo;
25342 ufunc_T *fp;
25343 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025344 ufunc_T **sorttab;
25345 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025346
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025347 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025348 if (todo == 0)
25349 return; /* nothing to dump */
25350
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025351 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025352
Bram Moolenaar05159a02005-02-26 23:04:13 +000025353 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25354 {
25355 if (!HASHITEM_EMPTY(hi))
25356 {
25357 --todo;
25358 fp = HI2UF(hi);
25359 if (fp->uf_profiling)
25360 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025361 if (sorttab != NULL)
25362 sorttab[st_len++] = fp;
25363
Bram Moolenaar05159a02005-02-26 23:04:13 +000025364 if (fp->uf_name[0] == K_SPECIAL)
25365 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25366 else
25367 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25368 if (fp->uf_tm_count == 1)
25369 fprintf(fd, "Called 1 time\n");
25370 else
25371 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25372 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25373 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25374 fprintf(fd, "\n");
25375 fprintf(fd, "count total (s) self (s)\n");
25376
25377 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25378 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025379 if (FUNCLINE(fp, i) == NULL)
25380 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025381 prof_func_line(fd, fp->uf_tml_count[i],
25382 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025383 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25384 }
25385 fprintf(fd, "\n");
25386 }
25387 }
25388 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025389
25390 if (sorttab != NULL && st_len > 0)
25391 {
25392 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25393 prof_total_cmp);
25394 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25395 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25396 prof_self_cmp);
25397 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25398 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025399
25400 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025401}
Bram Moolenaar73830342005-02-28 22:48:19 +000025402
25403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025404prof_sort_list(
25405 FILE *fd,
25406 ufunc_T **sorttab,
25407 int st_len,
25408 char *title,
25409 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025410{
25411 int i;
25412 ufunc_T *fp;
25413
25414 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25415 fprintf(fd, "count total (s) self (s) function\n");
25416 for (i = 0; i < 20 && i < st_len; ++i)
25417 {
25418 fp = sorttab[i];
25419 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25420 prefer_self);
25421 if (fp->uf_name[0] == K_SPECIAL)
25422 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25423 else
25424 fprintf(fd, " %s()\n", fp->uf_name);
25425 }
25426 fprintf(fd, "\n");
25427}
25428
25429/*
25430 * Print the count and times for one function or function line.
25431 */
25432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025433prof_func_line(
25434 FILE *fd,
25435 int count,
25436 proftime_T *total,
25437 proftime_T *self,
25438 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025439{
25440 if (count > 0)
25441 {
25442 fprintf(fd, "%5d ", count);
25443 if (prefer_self && profile_equal(total, self))
25444 fprintf(fd, " ");
25445 else
25446 fprintf(fd, "%s ", profile_msg(total));
25447 if (!prefer_self && profile_equal(total, self))
25448 fprintf(fd, " ");
25449 else
25450 fprintf(fd, "%s ", profile_msg(self));
25451 }
25452 else
25453 fprintf(fd, " ");
25454}
25455
25456/*
25457 * Compare function for total time sorting.
25458 */
25459 static int
25460#ifdef __BORLANDC__
25461_RTLENTRYF
25462#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025463prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025464{
25465 ufunc_T *p1, *p2;
25466
25467 p1 = *(ufunc_T **)s1;
25468 p2 = *(ufunc_T **)s2;
25469 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25470}
25471
25472/*
25473 * Compare function for self time sorting.
25474 */
25475 static int
25476#ifdef __BORLANDC__
25477_RTLENTRYF
25478#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025479prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025480{
25481 ufunc_T *p1, *p2;
25482
25483 p1 = *(ufunc_T **)s1;
25484 p2 = *(ufunc_T **)s2;
25485 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25486}
25487
Bram Moolenaar05159a02005-02-26 23:04:13 +000025488#endif
25489
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025490/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025491 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025492 * Return TRUE if a package was loaded.
25493 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025495script_autoload(
25496 char_u *name,
25497 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025498{
25499 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025500 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025501 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025502 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025503
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025504 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025505 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025506 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025507 return FALSE;
25508
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025509 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025510
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025511 /* Find the name in the list of previously loaded package names. Skip
25512 * "autoload/", it's always the same. */
25513 for (i = 0; i < ga_loaded.ga_len; ++i)
25514 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25515 break;
25516 if (!reload && i < ga_loaded.ga_len)
25517 ret = FALSE; /* was loaded already */
25518 else
25519 {
25520 /* Remember the name if it wasn't loaded already. */
25521 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25522 {
25523 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25524 tofree = NULL;
25525 }
25526
25527 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025528 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025529 ret = TRUE;
25530 }
25531
25532 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025533 return ret;
25534}
25535
25536/*
25537 * Return the autoload script name for a function or variable name.
25538 * Returns NULL when out of memory.
25539 */
25540 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025541autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025542{
25543 char_u *p;
25544 char_u *scriptname;
25545
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025546 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025547 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25548 if (scriptname == NULL)
25549 return FALSE;
25550 STRCPY(scriptname, "autoload/");
25551 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025552 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025553 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025554 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025555 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025556 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025557}
25558
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25560
25561/*
25562 * Function given to ExpandGeneric() to obtain the list of user defined
25563 * function names.
25564 */
25565 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025566get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025568 static long_u done;
25569 static hashitem_T *hi;
25570 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025571
25572 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025574 done = 0;
25575 hi = func_hashtab.ht_array;
25576 }
25577 if (done < func_hashtab.ht_used)
25578 {
25579 if (done++ > 0)
25580 ++hi;
25581 while (HASHITEM_EMPTY(hi))
25582 ++hi;
25583 fp = HI2UF(hi);
25584
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025585 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025586 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025587
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025588 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25589 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590
25591 cat_func_name(IObuff, fp);
25592 if (xp->xp_context != EXPAND_USER_FUNC)
25593 {
25594 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025595 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 STRCAT(IObuff, ")");
25597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 return IObuff;
25599 }
25600 return NULL;
25601}
25602
25603#endif /* FEAT_CMDL_COMPL */
25604
25605/*
25606 * Copy the function name of "fp" to buffer "buf".
25607 * "buf" must be able to hold the function name plus three bytes.
25608 * Takes care of script-local function names.
25609 */
25610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025611cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025613 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 {
25615 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025616 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 }
25618 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025619 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620}
25621
25622/*
25623 * ":delfunction {name}"
25624 */
25625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025626ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025628 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629 char_u *p;
25630 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025631 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632
25633 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025634 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025635 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025637 {
25638 if (fudi.fd_dict != NULL && !eap->skip)
25639 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642 if (!ends_excmd(*skipwhite(p)))
25643 {
25644 vim_free(name);
25645 EMSG(_(e_trailing));
25646 return;
25647 }
25648 eap->nextcmd = check_nextcmd(p);
25649 if (eap->nextcmd != NULL)
25650 *p = NUL;
25651
25652 if (!eap->skip)
25653 fp = find_func(name);
25654 vim_free(name);
25655
25656 if (!eap->skip)
25657 {
25658 if (fp == NULL)
25659 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025660 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 return;
25662 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025663 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 {
25665 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25666 return;
25667 }
25668
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025669 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025671 /* Delete the dict item that refers to the function, it will
25672 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025673 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025675 else
25676 func_free(fp);
25677 }
25678}
25679
25680/*
25681 * Free a function and remove it from the list of functions.
25682 */
25683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025684func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025685{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025686 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025687
25688 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025689 ga_clear_strings(&(fp->uf_args));
25690 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025691#ifdef FEAT_PROFILE
25692 vim_free(fp->uf_tml_count);
25693 vim_free(fp->uf_tml_total);
25694 vim_free(fp->uf_tml_self);
25695#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025696
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025697 /* remove the function from the function hashtable */
25698 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25699 if (HASHITEM_EMPTY(hi))
25700 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025701 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025702 hash_remove(&func_hashtab, hi);
25703
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025704 vim_free(fp);
25705}
25706
25707/*
25708 * Unreference a Function: decrement the reference count and free it when it
25709 * becomes zero. Only for numbered functions.
25710 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025712func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025713{
25714 ufunc_T *fp;
25715
25716 if (name != NULL && isdigit(*name))
25717 {
25718 fp = find_func(name);
25719 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025720 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025721#ifdef EXITFREE
25722 if (!entered_free_all_mem)
25723#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025724 EMSG2(_(e_intern2), "func_unref()");
25725 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025726 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025727 {
25728 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025729 * when "uf_calls" becomes zero. */
25730 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025731 func_free(fp);
25732 }
25733 }
25734}
25735
25736/*
25737 * Count a reference to a Function.
25738 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025740func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025741{
25742 ufunc_T *fp;
25743
25744 if (name != NULL && isdigit(*name))
25745 {
25746 fp = find_func(name);
25747 if (fp == NULL)
25748 EMSG2(_(e_intern2), "func_ref()");
25749 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025750 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 }
25752}
25753
25754/*
25755 * Call a user function.
25756 */
25757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025758call_user_func(
25759 ufunc_T *fp, /* pointer to function */
25760 int argcount, /* nr of args */
25761 typval_T *argvars, /* arguments */
25762 typval_T *rettv, /* return value */
25763 linenr_T firstline, /* first line of range */
25764 linenr_T lastline, /* last line of range */
25765 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766{
Bram Moolenaar33570922005-01-25 22:26:29 +000025767 char_u *save_sourcing_name;
25768 linenr_T save_sourcing_lnum;
25769 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025770 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025771 int save_did_emsg;
25772 static int depth = 0;
25773 dictitem_T *v;
25774 int fixvar_idx = 0; /* index in fixvar[] */
25775 int i;
25776 int ai;
25777 char_u numbuf[NUMBUFLEN];
25778 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025779 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025780#ifdef FEAT_PROFILE
25781 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025782 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784
25785 /* If depth of calling is getting too high, don't execute the function */
25786 if (depth >= p_mfd)
25787 {
25788 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025789 rettv->v_type = VAR_NUMBER;
25790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791 return;
25792 }
25793 ++depth;
25794
25795 line_breakcheck(); /* check for CTRL-C hit */
25796
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025797 fc = (funccall_T *)alloc(sizeof(funccall_T));
25798 fc->caller = current_funccal;
25799 current_funccal = fc;
25800 fc->func = fp;
25801 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025802 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025803 fc->linenr = 0;
25804 fc->returned = FALSE;
25805 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025807 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25808 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809
Bram Moolenaar33570922005-01-25 22:26:29 +000025810 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025811 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025812 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25813 * each argument variable and saves a lot of time.
25814 */
25815 /*
25816 * Init l: variables.
25817 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025818 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025819 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025820 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025821 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25822 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025823 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025824 name = v->di_key;
25825 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025826 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025827 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025828 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025829 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025830 v->di_tv.vval.v_dict = selfdict;
25831 ++selfdict->dv_refcount;
25832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025833
Bram Moolenaar33570922005-01-25 22:26:29 +000025834 /*
25835 * Init a: variables.
25836 * Set a:0 to "argcount".
25837 * Set a:000 to a list with room for the "..." arguments.
25838 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025839 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025840 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025841 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025842 /* Use "name" to avoid a warning from some compiler that checks the
25843 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025844 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025845 name = v->di_key;
25846 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025847 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025848 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025849 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025850 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025851 v->di_tv.vval.v_list = &fc->l_varlist;
25852 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25853 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25854 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025855
25856 /*
25857 * Set a:firstline to "firstline" and a:lastline to "lastline".
25858 * Set a:name to named arguments.
25859 * Set a:N to the "..." arguments.
25860 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025861 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025862 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025863 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025864 (varnumber_T)lastline);
25865 for (i = 0; i < argcount; ++i)
25866 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025867 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025868 if (ai < 0)
25869 /* named argument a:name */
25870 name = FUNCARG(fp, i);
25871 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025872 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025873 /* "..." argument a:1, a:2, etc. */
25874 sprintf((char *)numbuf, "%d", ai + 1);
25875 name = numbuf;
25876 }
25877 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25878 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025879 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025880 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25881 }
25882 else
25883 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025884 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25885 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025886 if (v == NULL)
25887 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025888 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025889 }
25890 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025891 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025892
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025893 /* Note: the values are copied directly to avoid alloc/free.
25894 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025895 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025896 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025897
25898 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25899 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025900 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25901 fc->l_listitems[ai].li_tv = argvars[i];
25902 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025903 }
25904 }
25905
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906 /* Don't redraw while executing the function. */
25907 ++RedrawingDisabled;
25908 save_sourcing_name = sourcing_name;
25909 save_sourcing_lnum = sourcing_lnum;
25910 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025911 /* need space for function name + ("function " + 3) or "[number]" */
25912 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25913 + STRLEN(fp->uf_name) + 20;
25914 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915 if (sourcing_name != NULL)
25916 {
25917 if (save_sourcing_name != NULL
25918 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025919 sprintf((char *)sourcing_name, "%s[%d]..",
25920 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 else
25922 STRCPY(sourcing_name, "function ");
25923 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25924
25925 if (p_verbose >= 12)
25926 {
25927 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025928 verbose_enter_scroll();
25929
Bram Moolenaar555b2802005-05-19 21:08:39 +000025930 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931 if (p_verbose >= 14)
25932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025934 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025935 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025936 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937
25938 msg_puts((char_u *)"(");
25939 for (i = 0; i < argcount; ++i)
25940 {
25941 if (i > 0)
25942 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025943 if (argvars[i].v_type == VAR_NUMBER)
25944 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945 else
25946 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025947 /* Do not want errors such as E724 here. */
25948 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025949 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025950 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025951 if (s != NULL)
25952 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025953 if (vim_strsize(s) > MSG_BUF_CLEN)
25954 {
25955 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25956 s = buf;
25957 }
25958 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025959 vim_free(tofree);
25960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025961 }
25962 }
25963 msg_puts((char_u *)")");
25964 }
25965 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025966
25967 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 --no_wait_return;
25969 }
25970 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025971#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025972 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025973 {
25974 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25975 func_do_profile(fp);
25976 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025977 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025978 {
25979 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025980 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025981 profile_zero(&fp->uf_tm_children);
25982 }
25983 script_prof_save(&wait_start);
25984 }
25985#endif
25986
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025988 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 save_did_emsg = did_emsg;
25990 did_emsg = FALSE;
25991
25992 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025993 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25995
25996 --RedrawingDisabled;
25997
25998 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025999 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026001 clear_tv(rettv);
26002 rettv->v_type = VAR_NUMBER;
26003 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004 }
26005
Bram Moolenaar05159a02005-02-26 23:04:13 +000026006#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026007 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026008 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000026009 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000026010 profile_end(&call_start);
26011 profile_sub_wait(&wait_start, &call_start);
26012 profile_add(&fp->uf_tm_total, &call_start);
26013 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026014 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026015 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026016 profile_add(&fc->caller->func->uf_tm_children, &call_start);
26017 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026018 }
26019 }
26020#endif
26021
Bram Moolenaar071d4272004-06-13 20:20:40 +000026022 /* when being verbose, mention the return value */
26023 if (p_verbose >= 12)
26024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026026 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026027
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000026029 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026030 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000026031 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026032 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000026033 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000026035 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000026036 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000026037 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026038 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000026039
Bram Moolenaar555b2802005-05-19 21:08:39 +000026040 /* The value may be very long. Skip the middle part, so that we
26041 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020026042 * truncate it at the end. Don't want errors such as E724 here. */
26043 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026044 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020026045 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026046 if (s != NULL)
26047 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010026048 if (vim_strsize(s) > MSG_BUF_CLEN)
26049 {
26050 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
26051 s = buf;
26052 }
26053 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026054 vim_free(tofree);
26055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056 }
26057 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026058
26059 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 --no_wait_return;
26061 }
26062
26063 vim_free(sourcing_name);
26064 sourcing_name = save_sourcing_name;
26065 sourcing_lnum = save_sourcing_lnum;
26066 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026067#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026068 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026069 script_prof_restore(&wait_start);
26070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071
26072 if (p_verbose >= 12 && sourcing_name != NULL)
26073 {
26074 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026075 verbose_enter_scroll();
26076
Bram Moolenaar555b2802005-05-19 21:08:39 +000026077 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026079
26080 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081 --no_wait_return;
26082 }
26083
26084 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026085 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026087
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026088 /* If the a:000 list and the l: and a: dicts are not referenced we can
26089 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026090 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
26091 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
26092 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
26093 {
26094 free_funccal(fc, FALSE);
26095 }
26096 else
26097 {
26098 hashitem_T *hi;
26099 listitem_T *li;
26100 int todo;
26101
26102 /* "fc" is still in use. This can happen when returning "a:000" or
26103 * assigning "l:" to a global variable.
26104 * Link "fc" in the list for garbage collection later. */
26105 fc->caller = previous_funccal;
26106 previous_funccal = fc;
26107
26108 /* Make a copy of the a: variables, since we didn't do that above. */
26109 todo = (int)fc->l_avars.dv_hashtab.ht_used;
26110 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
26111 {
26112 if (!HASHITEM_EMPTY(hi))
26113 {
26114 --todo;
26115 v = HI2DI(hi);
26116 copy_tv(&v->di_tv, &v->di_tv);
26117 }
26118 }
26119
26120 /* Make a copy of the a:000 items, since we didn't do that above. */
26121 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26122 copy_tv(&li->li_tv, &li->li_tv);
26123 }
26124}
26125
26126/*
26127 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026128 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026129 */
26130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026131can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026132{
26133 return (fc->l_varlist.lv_copyID != copyID
26134 && fc->l_vars.dv_copyID != copyID
26135 && fc->l_avars.dv_copyID != copyID);
26136}
26137
26138/*
26139 * Free "fc" and what it contains.
26140 */
26141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026142free_funccal(
26143 funccall_T *fc,
26144 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026145{
26146 listitem_T *li;
26147
26148 /* The a: variables typevals may not have been allocated, only free the
26149 * allocated variables. */
26150 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26151
26152 /* free all l: variables */
26153 vars_clear(&fc->l_vars.dv_hashtab);
26154
26155 /* Free the a:000 variables if they were allocated. */
26156 if (free_val)
26157 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26158 clear_tv(&li->li_tv);
26159
26160 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161}
26162
26163/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026164 * Add a number variable "name" to dict "dp" with value "nr".
26165 */
26166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026167add_nr_var(
26168 dict_T *dp,
26169 dictitem_T *v,
26170 char *name,
26171 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026172{
26173 STRCPY(v->di_key, name);
26174 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26175 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26176 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026177 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026178 v->di_tv.vval.v_number = nr;
26179}
26180
26181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182 * ":return [expr]"
26183 */
26184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026185ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026186{
26187 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026188 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189 int returning = FALSE;
26190
26191 if (current_funccal == NULL)
26192 {
26193 EMSG(_("E133: :return not inside a function"));
26194 return;
26195 }
26196
26197 if (eap->skip)
26198 ++emsg_skip;
26199
26200 eap->nextcmd = NULL;
26201 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026202 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203 {
26204 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026205 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026207 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208 }
26209 /* It's safer to return also on error. */
26210 else if (!eap->skip)
26211 {
26212 /*
26213 * Return unless the expression evaluation has been cancelled due to an
26214 * aborting error, an interrupt, or an exception.
26215 */
26216 if (!aborting())
26217 returning = do_return(eap, FALSE, TRUE, NULL);
26218 }
26219
26220 /* When skipping or the return gets pending, advance to the next command
26221 * in this line (!returning). Otherwise, ignore the rest of the line.
26222 * Following lines will be ignored by get_func_line(). */
26223 if (returning)
26224 eap->nextcmd = NULL;
26225 else if (eap->nextcmd == NULL) /* no argument */
26226 eap->nextcmd = check_nextcmd(arg);
26227
26228 if (eap->skip)
26229 --emsg_skip;
26230}
26231
26232/*
26233 * Return from a function. Possibly makes the return pending. Also called
26234 * for a pending return at the ":endtry" or after returning from an extra
26235 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026236 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026237 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 * FALSE when the return gets pending.
26239 */
26240 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026241do_return(
26242 exarg_T *eap,
26243 int reanimate,
26244 int is_cmd,
26245 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246{
26247 int idx;
26248 struct condstack *cstack = eap->cstack;
26249
26250 if (reanimate)
26251 /* Undo the return. */
26252 current_funccal->returned = FALSE;
26253
26254 /*
26255 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26256 * not in its finally clause (which then is to be executed next) is found.
26257 * In this case, make the ":return" pending for execution at the ":endtry".
26258 * Otherwise, return normally.
26259 */
26260 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26261 if (idx >= 0)
26262 {
26263 cstack->cs_pending[idx] = CSTP_RETURN;
26264
26265 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026266 /* A pending return again gets pending. "rettv" points to an
26267 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026268 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026269 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026270 else
26271 {
26272 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026273 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026275 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026277 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026278 {
26279 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026280 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026281 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026282 else
26283 EMSG(_(e_outofmem));
26284 }
26285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026286 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287
26288 if (reanimate)
26289 {
26290 /* The pending return value could be overwritten by a ":return"
26291 * without argument in a finally clause; reset the default
26292 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026293 current_funccal->rettv->v_type = VAR_NUMBER;
26294 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 }
26296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026297 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298 }
26299 else
26300 {
26301 current_funccal->returned = TRUE;
26302
26303 /* If the return is carried out now, store the return value. For
26304 * a return immediately after reanimation, the value is already
26305 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026306 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026308 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026309 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026311 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312 }
26313 }
26314
26315 return idx < 0;
26316}
26317
26318/*
26319 * Free the variable with a pending return value.
26320 */
26321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026322discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026323{
Bram Moolenaar33570922005-01-25 22:26:29 +000026324 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026325}
26326
26327/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026328 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026329 * is an allocated string. Used by report_pending() for verbose messages.
26330 */
26331 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026332get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026334 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026335 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026336 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026337
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026338 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026339 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026340 if (s == NULL)
26341 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026342
26343 STRCPY(IObuff, ":return ");
26344 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26345 if (STRLEN(s) + 8 >= IOSIZE)
26346 STRCPY(IObuff + IOSIZE - 4, "...");
26347 vim_free(tofree);
26348 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349}
26350
26351/*
26352 * Get next function line.
26353 * Called by do_cmdline() to get the next line.
26354 * Returns allocated string, or NULL for end of function.
26355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026357get_func_line(
26358 int c UNUSED,
26359 void *cookie,
26360 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361{
Bram Moolenaar33570922005-01-25 22:26:29 +000026362 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026363 ufunc_T *fp = fcp->func;
26364 char_u *retval;
26365 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026366
26367 /* If breakpoints have been added/deleted need to check for it. */
26368 if (fcp->dbg_tick != debug_tick)
26369 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026370 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026371 sourcing_lnum);
26372 fcp->dbg_tick = debug_tick;
26373 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026374#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026375 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026376 func_line_end(cookie);
26377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378
Bram Moolenaar05159a02005-02-26 23:04:13 +000026379 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026380 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26381 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 retval = NULL;
26383 else
26384 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026385 /* Skip NULL lines (continuation lines). */
26386 while (fcp->linenr < gap->ga_len
26387 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26388 ++fcp->linenr;
26389 if (fcp->linenr >= gap->ga_len)
26390 retval = NULL;
26391 else
26392 {
26393 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26394 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026395#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026396 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026397 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026398#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400 }
26401
26402 /* Did we encounter a breakpoint? */
26403 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26404 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026405 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026406 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026407 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408 sourcing_lnum);
26409 fcp->dbg_tick = debug_tick;
26410 }
26411
26412 return retval;
26413}
26414
Bram Moolenaar05159a02005-02-26 23:04:13 +000026415#if defined(FEAT_PROFILE) || defined(PROTO)
26416/*
26417 * Called when starting to read a function line.
26418 * "sourcing_lnum" must be correct!
26419 * When skipping lines it may not actually be executed, but we won't find out
26420 * until later and we need to store the time now.
26421 */
26422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026423func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026424{
26425 funccall_T *fcp = (funccall_T *)cookie;
26426 ufunc_T *fp = fcp->func;
26427
26428 if (fp->uf_profiling && sourcing_lnum >= 1
26429 && sourcing_lnum <= fp->uf_lines.ga_len)
26430 {
26431 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026432 /* Skip continuation lines. */
26433 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26434 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026435 fp->uf_tml_execed = FALSE;
26436 profile_start(&fp->uf_tml_start);
26437 profile_zero(&fp->uf_tml_children);
26438 profile_get_wait(&fp->uf_tml_wait);
26439 }
26440}
26441
26442/*
26443 * Called when actually executing a function line.
26444 */
26445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026446func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026447{
26448 funccall_T *fcp = (funccall_T *)cookie;
26449 ufunc_T *fp = fcp->func;
26450
26451 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26452 fp->uf_tml_execed = TRUE;
26453}
26454
26455/*
26456 * Called when done with a function line.
26457 */
26458 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026459func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026460{
26461 funccall_T *fcp = (funccall_T *)cookie;
26462 ufunc_T *fp = fcp->func;
26463
26464 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26465 {
26466 if (fp->uf_tml_execed)
26467 {
26468 ++fp->uf_tml_count[fp->uf_tml_idx];
26469 profile_end(&fp->uf_tml_start);
26470 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026471 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026472 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26473 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026474 }
26475 fp->uf_tml_idx = -1;
26476 }
26477}
26478#endif
26479
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480/*
26481 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026482 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483 */
26484 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026485func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026486{
Bram Moolenaar33570922005-01-25 22:26:29 +000026487 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026488
26489 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26490 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026491 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026492 || fcp->returned);
26493}
26494
26495/*
26496 * return TRUE if cookie indicates a function which "abort"s on errors.
26497 */
26498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026499func_has_abort(
26500 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026502 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026503}
26504
26505#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26506typedef enum
26507{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026508 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26509 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26510 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026511} var_flavour_T;
26512
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026513static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026514
26515 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026516var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026517{
26518 char_u *p = varname;
26519
26520 if (ASCII_ISUPPER(*p))
26521 {
26522 while (*(++p))
26523 if (ASCII_ISLOWER(*p))
26524 return VAR_FLAVOUR_SESSION;
26525 return VAR_FLAVOUR_VIMINFO;
26526 }
26527 else
26528 return VAR_FLAVOUR_DEFAULT;
26529}
26530#endif
26531
26532#if defined(FEAT_VIMINFO) || defined(PROTO)
26533/*
26534 * Restore global vars that start with a capital from the viminfo file
26535 */
26536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026537read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538{
26539 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026540 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026541 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026542 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026543
26544 if (!writing && (find_viminfo_parameter('!') != NULL))
26545 {
26546 tab = vim_strchr(virp->vir_line + 1, '\t');
26547 if (tab != NULL)
26548 {
26549 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026550 switch (*tab)
26551 {
26552 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026553#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026554 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026555#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026556 case 'D': type = VAR_DICT; break;
26557 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026558 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026560
26561 tab = vim_strchr(tab, '\t');
26562 if (tab != NULL)
26563 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026564 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026565 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026566 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026568#ifdef FEAT_FLOAT
26569 else if (type == VAR_FLOAT)
26570 (void)string2float(tab + 1, &tv.vval.v_float);
26571#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026572 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026573 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026574 if (type == VAR_DICT || type == VAR_LIST)
26575 {
26576 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26577
26578 if (etv == NULL)
26579 /* Failed to parse back the dict or list, use it as a
26580 * string. */
26581 tv.v_type = VAR_STRING;
26582 else
26583 {
26584 vim_free(tv.vval.v_string);
26585 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026586 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026587 }
26588 }
26589
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026590 /* when in a function use global variables */
26591 save_funccal = current_funccal;
26592 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026593 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026594 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026595
26596 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026597 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026598 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26599 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026600 }
26601 }
26602 }
26603
26604 return viminfo_readline(virp);
26605}
26606
26607/*
26608 * Write global vars that start with a capital to the viminfo file
26609 */
26610 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026611write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026612{
Bram Moolenaar33570922005-01-25 22:26:29 +000026613 hashitem_T *hi;
26614 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026615 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026616 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026617 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026618 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026619 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026620
26621 if (find_viminfo_parameter('!') == NULL)
26622 return;
26623
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026624 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026625
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026626 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026627 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026628 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026629 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026630 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026631 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026632 this_var = HI2DI(hi);
26633 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026634 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026635 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026636 {
26637 case VAR_STRING: s = "STR"; break;
26638 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026639 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026640 case VAR_DICT: s = "DIC"; break;
26641 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026642 case VAR_SPECIAL: s = "XPL"; break;
26643
26644 case VAR_UNKNOWN:
26645 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026646 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026647 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026648 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026649 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026650 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026651 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026652 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026653 if (p != NULL)
26654 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026655 vim_free(tofree);
26656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657 }
26658 }
26659}
26660#endif
26661
26662#if defined(FEAT_SESSION) || defined(PROTO)
26663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026664store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026665{
Bram Moolenaar33570922005-01-25 22:26:29 +000026666 hashitem_T *hi;
26667 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026668 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026669 char_u *p, *t;
26670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026671 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026672 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026673 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026676 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026677 this_var = HI2DI(hi);
26678 if ((this_var->di_tv.v_type == VAR_NUMBER
26679 || this_var->di_tv.v_type == VAR_STRING)
26680 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026681 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026682 /* Escape special characters with a backslash. Turn a LF and
26683 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026684 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026685 (char_u *)"\\\"\n\r");
26686 if (p == NULL) /* out of memory */
26687 break;
26688 for (t = p; *t != NUL; ++t)
26689 if (*t == '\n')
26690 *t = 'n';
26691 else if (*t == '\r')
26692 *t = 'r';
26693 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026694 this_var->di_key,
26695 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26696 : ' ',
26697 p,
26698 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26699 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026700 || put_eol(fd) == FAIL)
26701 {
26702 vim_free(p);
26703 return FAIL;
26704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026705 vim_free(p);
26706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026707#ifdef FEAT_FLOAT
26708 else if (this_var->di_tv.v_type == VAR_FLOAT
26709 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26710 {
26711 float_T f = this_var->di_tv.vval.v_float;
26712 int sign = ' ';
26713
26714 if (f < 0)
26715 {
26716 f = -f;
26717 sign = '-';
26718 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026719 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026720 this_var->di_key, sign, f) < 0)
26721 || put_eol(fd) == FAIL)
26722 return FAIL;
26723 }
26724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725 }
26726 }
26727 return OK;
26728}
26729#endif
26730
Bram Moolenaar661b1822005-07-28 22:36:45 +000026731/*
26732 * Display script name where an item was last set.
26733 * Should only be invoked when 'verbose' is non-zero.
26734 */
26735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026736last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026737{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026738 char_u *p;
26739
Bram Moolenaar661b1822005-07-28 22:36:45 +000026740 if (scriptID != 0)
26741 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026742 p = home_replace_save(NULL, get_scriptname(scriptID));
26743 if (p != NULL)
26744 {
26745 verbose_enter();
26746 MSG_PUTS(_("\n\tLast set from "));
26747 MSG_PUTS(p);
26748 vim_free(p);
26749 verbose_leave();
26750 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026751 }
26752}
26753
Bram Moolenaard812df62008-11-09 12:46:09 +000026754/*
26755 * List v:oldfiles in a nice way.
26756 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026758ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026759{
26760 list_T *l = vimvars[VV_OLDFILES].vv_list;
26761 listitem_T *li;
26762 int nr = 0;
26763
26764 if (l == NULL)
26765 msg((char_u *)_("No old files"));
26766 else
26767 {
26768 msg_start();
26769 msg_scroll = TRUE;
26770 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26771 {
26772 msg_outnum((long)++nr);
26773 MSG_PUTS(": ");
26774 msg_outtrans(get_tv_string(&li->li_tv));
26775 msg_putchar('\n');
26776 out_flush(); /* output one line at a time */
26777 ui_breakcheck();
26778 }
26779 /* Assume "got_int" was set to truncate the listing. */
26780 got_int = FALSE;
26781
26782#ifdef FEAT_BROWSE_CMD
26783 if (cmdmod.browse)
26784 {
26785 quit_more = FALSE;
26786 nr = prompt_for_number(FALSE);
26787 msg_starthere();
26788 if (nr > 0)
26789 {
26790 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26791 (long)nr);
26792
26793 if (p != NULL)
26794 {
26795 p = expand_env_save(p);
26796 eap->arg = p;
26797 eap->cmdidx = CMD_edit;
26798 cmdmod.browse = FALSE;
26799 do_exedit(eap, NULL);
26800 vim_free(p);
26801 }
26802 }
26803 }
26804#endif
26805 }
26806}
26807
Bram Moolenaar53744302015-07-17 17:38:22 +020026808/* reset v:option_new, v:option_old and v:option_type */
26809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026810reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026811{
26812 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26813 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26814 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26815}
26816
26817
Bram Moolenaar071d4272004-06-13 20:20:40 +000026818#endif /* FEAT_EVAL */
26819
Bram Moolenaar071d4272004-06-13 20:20:40 +000026820
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026821#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026822
26823#ifdef WIN3264
26824/*
26825 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26826 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026827static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26828static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26829static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026830
26831/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026832 * Get the short path (8.3) for the filename in "fnamep".
26833 * Only works for a valid file name.
26834 * When the path gets longer "fnamep" is changed and the allocated buffer
26835 * is put in "bufp".
26836 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26837 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026838 */
26839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026840get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026842 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843 char_u *newbuf;
26844
26845 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026846 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026847 if (l > len - 1)
26848 {
26849 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026850 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026851 newbuf = vim_strnsave(*fnamep, l);
26852 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026853 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026854
26855 vim_free(*bufp);
26856 *fnamep = *bufp = newbuf;
26857
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026858 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026859 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026860 }
26861
26862 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026863 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864}
26865
26866/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026867 * Get the short path (8.3) for the filename in "fname". The converted
26868 * path is returned in "bufp".
26869 *
26870 * Some of the directories specified in "fname" may not exist. This function
26871 * will shorten the existing directories at the beginning of the path and then
26872 * append the remaining non-existing path.
26873 *
26874 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026875 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026876 * bufp - Pointer to an allocated buffer for the filename.
26877 * fnamelen - Length of the filename pointed to by fname
26878 *
26879 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026880 */
26881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026882shortpath_for_invalid_fname(
26883 char_u **fname,
26884 char_u **bufp,
26885 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026886{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026887 char_u *short_fname, *save_fname, *pbuf_unused;
26888 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026889 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026890 int old_len, len;
26891 int new_len, sfx_len;
26892 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026893
26894 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026895 old_len = *fnamelen;
26896 save_fname = vim_strnsave(*fname, old_len);
26897 pbuf_unused = NULL;
26898 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026900 endp = save_fname + old_len - 1; /* Find the end of the copy */
26901 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026902
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026903 /*
26904 * Try shortening the supplied path till it succeeds by removing one
26905 * directory at a time from the tail of the path.
26906 */
26907 len = 0;
26908 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026909 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026910 /* go back one path-separator */
26911 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26912 --endp;
26913 if (endp <= save_fname)
26914 break; /* processed the complete path */
26915
26916 /*
26917 * Replace the path separator with a NUL and try to shorten the
26918 * resulting path.
26919 */
26920 ch = *endp;
26921 *endp = 0;
26922 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026923 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026924 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26925 {
26926 retval = FAIL;
26927 goto theend;
26928 }
26929 *endp = ch; /* preserve the string */
26930
26931 if (len > 0)
26932 break; /* successfully shortened the path */
26933
26934 /* failed to shorten the path. Skip the path separator */
26935 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026936 }
26937
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026938 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026939 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026940 /*
26941 * Succeeded in shortening the path. Now concatenate the shortened
26942 * path with the remaining path at the tail.
26943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026944
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026945 /* Compute the length of the new path. */
26946 sfx_len = (int)(save_endp - endp) + 1;
26947 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026948
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026949 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026950 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026951 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026952 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026953 /* There is not enough space in the currently allocated string,
26954 * copy it to a buffer big enough. */
26955 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026956 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026957 {
26958 retval = FAIL;
26959 goto theend;
26960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026961 }
26962 else
26963 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026964 /* Transfer short_fname to the main buffer (it's big enough),
26965 * unless get_short_pathname() did its work in-place. */
26966 *fname = *bufp = save_fname;
26967 if (short_fname != save_fname)
26968 vim_strncpy(save_fname, short_fname, len);
26969 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026970 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026971
26972 /* concat the not-shortened part of the path */
26973 vim_strncpy(*fname + len, endp, sfx_len);
26974 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026975 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026976
26977theend:
26978 vim_free(pbuf_unused);
26979 vim_free(save_fname);
26980
26981 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026982}
26983
26984/*
26985 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026986 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026987 */
26988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026989shortpath_for_partial(
26990 char_u **fnamep,
26991 char_u **bufp,
26992 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026993{
26994 int sepcount, len, tflen;
26995 char_u *p;
26996 char_u *pbuf, *tfname;
26997 int hasTilde;
26998
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026999 /* Count up the path separators from the RHS.. so we know which part
27000 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027001 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027002 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027003 if (vim_ispathsep(*p))
27004 ++sepcount;
27005
27006 /* Need full path first (use expand_env() to remove a "~/") */
27007 hasTilde = (**fnamep == '~');
27008 if (hasTilde)
27009 pbuf = tfname = expand_env_save(*fnamep);
27010 else
27011 pbuf = tfname = FullName_save(*fnamep, FALSE);
27012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000027013 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027014
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027015 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
27016 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027017
27018 if (len == 0)
27019 {
27020 /* Don't have a valid filename, so shorten the rest of the
27021 * path if we can. This CAN give us invalid 8.3 filenames, but
27022 * there's not a lot of point in guessing what it might be.
27023 */
27024 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027025 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
27026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027027 }
27028
27029 /* Count the paths backward to find the beginning of the desired string. */
27030 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027031 {
27032#ifdef FEAT_MBYTE
27033 if (has_mbyte)
27034 p -= mb_head_off(tfname, p);
27035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027036 if (vim_ispathsep(*p))
27037 {
27038 if (sepcount == 0 || (hasTilde && sepcount == 1))
27039 break;
27040 else
27041 sepcount --;
27042 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027044 if (hasTilde)
27045 {
27046 --p;
27047 if (p >= tfname)
27048 *p = '~';
27049 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027050 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027051 }
27052 else
27053 ++p;
27054
27055 /* Copy in the string - p indexes into tfname - allocated at pbuf */
27056 vim_free(*bufp);
27057 *fnamelen = (int)STRLEN(p);
27058 *bufp = pbuf;
27059 *fnamep = p;
27060
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027061 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027062}
27063#endif /* WIN3264 */
27064
27065/*
27066 * Adjust a filename, according to a string of modifiers.
27067 * *fnamep must be NUL terminated when called. When returning, the length is
27068 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027069 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027070 * When there is an error, *fnamep is set to NULL.
27071 */
27072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027073modify_fname(
27074 char_u *src, /* string with modifiers */
27075 int *usedlen, /* characters after src that are used */
27076 char_u **fnamep, /* file name so far */
27077 char_u **bufp, /* buffer for allocated file name or NULL */
27078 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027079{
27080 int valid = 0;
27081 char_u *tail;
27082 char_u *s, *p, *pbuf;
27083 char_u dirname[MAXPATHL];
27084 int c;
27085 int has_fullname = 0;
27086#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027087 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027088 int has_shortname = 0;
27089#endif
27090
27091repeat:
27092 /* ":p" - full path/file_name */
27093 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
27094 {
27095 has_fullname = 1;
27096
27097 valid |= VALID_PATH;
27098 *usedlen += 2;
27099
27100 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
27101 if ((*fnamep)[0] == '~'
27102#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
27103 && ((*fnamep)[1] == '/'
27104# ifdef BACKSLASH_IN_FILENAME
27105 || (*fnamep)[1] == '\\'
27106# endif
27107 || (*fnamep)[1] == NUL)
27108
27109#endif
27110 )
27111 {
27112 *fnamep = expand_env_save(*fnamep);
27113 vim_free(*bufp); /* free any allocated file name */
27114 *bufp = *fnamep;
27115 if (*fnamep == NULL)
27116 return -1;
27117 }
27118
27119 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027120 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027121 {
27122 if (vim_ispathsep(*p)
27123 && p[1] == '.'
27124 && (p[2] == NUL
27125 || vim_ispathsep(p[2])
27126 || (p[2] == '.'
27127 && (p[3] == NUL || vim_ispathsep(p[3])))))
27128 break;
27129 }
27130
27131 /* FullName_save() is slow, don't use it when not needed. */
27132 if (*p != NUL || !vim_isAbsName(*fnamep))
27133 {
27134 *fnamep = FullName_save(*fnamep, *p != NUL);
27135 vim_free(*bufp); /* free any allocated file name */
27136 *bufp = *fnamep;
27137 if (*fnamep == NULL)
27138 return -1;
27139 }
27140
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027141#ifdef WIN3264
27142# if _WIN32_WINNT >= 0x0500
27143 if (vim_strchr(*fnamep, '~') != NULL)
27144 {
27145 /* Expand 8.3 filename to full path. Needed to make sure the same
27146 * file does not have two different names.
27147 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27148 p = alloc(_MAX_PATH + 1);
27149 if (p != NULL)
27150 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027151 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027152 {
27153 vim_free(*bufp);
27154 *bufp = *fnamep = p;
27155 }
27156 else
27157 vim_free(p);
27158 }
27159 }
27160# endif
27161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027162 /* Append a path separator to a directory. */
27163 if (mch_isdir(*fnamep))
27164 {
27165 /* Make room for one or two extra characters. */
27166 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27167 vim_free(*bufp); /* free any allocated file name */
27168 *bufp = *fnamep;
27169 if (*fnamep == NULL)
27170 return -1;
27171 add_pathsep(*fnamep);
27172 }
27173 }
27174
27175 /* ":." - path relative to the current directory */
27176 /* ":~" - path relative to the home directory */
27177 /* ":8" - shortname path - postponed till after */
27178 while (src[*usedlen] == ':'
27179 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27180 {
27181 *usedlen += 2;
27182 if (c == '8')
27183 {
27184#ifdef WIN3264
27185 has_shortname = 1; /* Postpone this. */
27186#endif
27187 continue;
27188 }
27189 pbuf = NULL;
27190 /* Need full path first (use expand_env() to remove a "~/") */
27191 if (!has_fullname)
27192 {
27193 if (c == '.' && **fnamep == '~')
27194 p = pbuf = expand_env_save(*fnamep);
27195 else
27196 p = pbuf = FullName_save(*fnamep, FALSE);
27197 }
27198 else
27199 p = *fnamep;
27200
27201 has_fullname = 0;
27202
27203 if (p != NULL)
27204 {
27205 if (c == '.')
27206 {
27207 mch_dirname(dirname, MAXPATHL);
27208 s = shorten_fname(p, dirname);
27209 if (s != NULL)
27210 {
27211 *fnamep = s;
27212 if (pbuf != NULL)
27213 {
27214 vim_free(*bufp); /* free any allocated file name */
27215 *bufp = pbuf;
27216 pbuf = NULL;
27217 }
27218 }
27219 }
27220 else
27221 {
27222 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27223 /* Only replace it when it starts with '~' */
27224 if (*dirname == '~')
27225 {
27226 s = vim_strsave(dirname);
27227 if (s != NULL)
27228 {
27229 *fnamep = s;
27230 vim_free(*bufp);
27231 *bufp = s;
27232 }
27233 }
27234 }
27235 vim_free(pbuf);
27236 }
27237 }
27238
27239 tail = gettail(*fnamep);
27240 *fnamelen = (int)STRLEN(*fnamep);
27241
27242 /* ":h" - head, remove "/file_name", can be repeated */
27243 /* Don't remove the first "/" or "c:\" */
27244 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27245 {
27246 valid |= VALID_HEAD;
27247 *usedlen += 2;
27248 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027249 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027250 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027251 *fnamelen = (int)(tail - *fnamep);
27252#ifdef VMS
27253 if (*fnamelen > 0)
27254 *fnamelen += 1; /* the path separator is part of the path */
27255#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027256 if (*fnamelen == 0)
27257 {
27258 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27259 p = vim_strsave((char_u *)".");
27260 if (p == NULL)
27261 return -1;
27262 vim_free(*bufp);
27263 *bufp = *fnamep = tail = p;
27264 *fnamelen = 1;
27265 }
27266 else
27267 {
27268 while (tail > s && !after_pathsep(s, tail))
27269 mb_ptr_back(*fnamep, tail);
27270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027271 }
27272
27273 /* ":8" - shortname */
27274 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27275 {
27276 *usedlen += 2;
27277#ifdef WIN3264
27278 has_shortname = 1;
27279#endif
27280 }
27281
27282#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027283 /*
27284 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027285 */
27286 if (has_shortname)
27287 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027288 /* Copy the string if it is shortened by :h and when it wasn't copied
27289 * yet, because we are going to change it in place. Avoids changing
27290 * the buffer name for "%:8". */
27291 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027292 {
27293 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027294 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027295 return -1;
27296 vim_free(*bufp);
27297 *bufp = *fnamep = p;
27298 }
27299
27300 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027301 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027302 if (!has_fullname && !vim_isAbsName(*fnamep))
27303 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027304 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027305 return -1;
27306 }
27307 else
27308 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027309 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027310
Bram Moolenaardc935552011-08-17 15:23:23 +020027311 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027312 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027313 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027314 return -1;
27315
27316 if (l == 0)
27317 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027318 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027319 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027320 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027321 return -1;
27322 }
27323 *fnamelen = l;
27324 }
27325 }
27326#endif /* WIN3264 */
27327
27328 /* ":t" - tail, just the basename */
27329 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27330 {
27331 *usedlen += 2;
27332 *fnamelen -= (int)(tail - *fnamep);
27333 *fnamep = tail;
27334 }
27335
27336 /* ":e" - extension, can be repeated */
27337 /* ":r" - root, without extension, can be repeated */
27338 while (src[*usedlen] == ':'
27339 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27340 {
27341 /* find a '.' in the tail:
27342 * - for second :e: before the current fname
27343 * - otherwise: The last '.'
27344 */
27345 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27346 s = *fnamep - 2;
27347 else
27348 s = *fnamep + *fnamelen - 1;
27349 for ( ; s > tail; --s)
27350 if (s[0] == '.')
27351 break;
27352 if (src[*usedlen + 1] == 'e') /* :e */
27353 {
27354 if (s > tail)
27355 {
27356 *fnamelen += (int)(*fnamep - (s + 1));
27357 *fnamep = s + 1;
27358#ifdef VMS
27359 /* cut version from the extension */
27360 s = *fnamep + *fnamelen - 1;
27361 for ( ; s > *fnamep; --s)
27362 if (s[0] == ';')
27363 break;
27364 if (s > *fnamep)
27365 *fnamelen = s - *fnamep;
27366#endif
27367 }
27368 else if (*fnamep <= tail)
27369 *fnamelen = 0;
27370 }
27371 else /* :r */
27372 {
27373 if (s > tail) /* remove one extension */
27374 *fnamelen = (int)(s - *fnamep);
27375 }
27376 *usedlen += 2;
27377 }
27378
27379 /* ":s?pat?foo?" - substitute */
27380 /* ":gs?pat?foo?" - global substitute */
27381 if (src[*usedlen] == ':'
27382 && (src[*usedlen + 1] == 's'
27383 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27384 {
27385 char_u *str;
27386 char_u *pat;
27387 char_u *sub;
27388 int sep;
27389 char_u *flags;
27390 int didit = FALSE;
27391
27392 flags = (char_u *)"";
27393 s = src + *usedlen + 2;
27394 if (src[*usedlen + 1] == 'g')
27395 {
27396 flags = (char_u *)"g";
27397 ++s;
27398 }
27399
27400 sep = *s++;
27401 if (sep)
27402 {
27403 /* find end of pattern */
27404 p = vim_strchr(s, sep);
27405 if (p != NULL)
27406 {
27407 pat = vim_strnsave(s, (int)(p - s));
27408 if (pat != NULL)
27409 {
27410 s = p + 1;
27411 /* find end of substitution */
27412 p = vim_strchr(s, sep);
27413 if (p != NULL)
27414 {
27415 sub = vim_strnsave(s, (int)(p - s));
27416 str = vim_strnsave(*fnamep, *fnamelen);
27417 if (sub != NULL && str != NULL)
27418 {
27419 *usedlen = (int)(p + 1 - src);
27420 s = do_string_sub(str, pat, sub, flags);
27421 if (s != NULL)
27422 {
27423 *fnamep = s;
27424 *fnamelen = (int)STRLEN(s);
27425 vim_free(*bufp);
27426 *bufp = s;
27427 didit = TRUE;
27428 }
27429 }
27430 vim_free(sub);
27431 vim_free(str);
27432 }
27433 vim_free(pat);
27434 }
27435 }
27436 /* after using ":s", repeat all the modifiers */
27437 if (didit)
27438 goto repeat;
27439 }
27440 }
27441
Bram Moolenaar26df0922014-02-23 23:39:13 +010027442 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27443 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027444 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027445 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027446 if (c != NUL)
27447 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027448 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027449 if (c != NUL)
27450 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027451 if (p == NULL)
27452 return -1;
27453 vim_free(*bufp);
27454 *bufp = *fnamep = p;
27455 *fnamelen = (int)STRLEN(p);
27456 *usedlen += 2;
27457 }
27458
Bram Moolenaar071d4272004-06-13 20:20:40 +000027459 return valid;
27460}
27461
27462/*
27463 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27464 * "flags" can be "g" to do a global substitute.
27465 * Returns an allocated string, NULL for error.
27466 */
27467 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027468do_string_sub(
27469 char_u *str,
27470 char_u *pat,
27471 char_u *sub,
27472 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027473{
27474 int sublen;
27475 regmatch_T regmatch;
27476 int i;
27477 int do_all;
27478 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027479 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027480 garray_T ga;
27481 char_u *ret;
27482 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027483 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027484
27485 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27486 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027487 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027488
27489 ga_init2(&ga, 1, 200);
27490
27491 do_all = (flags[0] == 'g');
27492
27493 regmatch.rm_ic = p_ic;
27494 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27495 if (regmatch.regprog != NULL)
27496 {
27497 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027498 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027499 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27500 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027501 /* Skip empty match except for first match. */
27502 if (regmatch.startp[0] == regmatch.endp[0])
27503 {
27504 if (zero_width == regmatch.startp[0])
27505 {
27506 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027507 i = MB_PTR2LEN(tail);
27508 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27509 (size_t)i);
27510 ga.ga_len += i;
27511 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027512 continue;
27513 }
27514 zero_width = regmatch.startp[0];
27515 }
27516
Bram Moolenaar071d4272004-06-13 20:20:40 +000027517 /*
27518 * Get some space for a temporary buffer to do the substitution
27519 * into. It will contain:
27520 * - The text up to where the match is.
27521 * - The substituted text.
27522 * - The text after the match.
27523 */
27524 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027525 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027526 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27527 {
27528 ga_clear(&ga);
27529 break;
27530 }
27531
27532 /* copy the text up to where the match is */
27533 i = (int)(regmatch.startp[0] - tail);
27534 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27535 /* add the substituted text */
27536 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27537 + ga.ga_len + i, TRUE, TRUE, FALSE);
27538 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027539 tail = regmatch.endp[0];
27540 if (*tail == NUL)
27541 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027542 if (!do_all)
27543 break;
27544 }
27545
27546 if (ga.ga_data != NULL)
27547 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27548
Bram Moolenaar473de612013-06-08 18:19:48 +020027549 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027550 }
27551
27552 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27553 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027554 if (p_cpo == empty_option)
27555 p_cpo = save_cpo;
27556 else
27557 /* Darn, evaluating {sub} expression changed the value. */
27558 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027559
27560 return ret;
27561}
27562
27563#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */