blob: 66cf3a0cc5bb9fc22bb4f0ac7ad24e187e39be22 [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},
362 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
363 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000364 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000365 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100366 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000367 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200368 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200369 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200370 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200371 {VV_NAME("option_new", VAR_STRING), VV_RO},
372 {VV_NAME("option_old", VAR_STRING), VV_RO},
373 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100374 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100375 {VV_NAME("false", VAR_SPECIAL), VV_RO},
376 {VV_NAME("true", VAR_SPECIAL), VV_RO},
377 {VV_NAME("null", VAR_SPECIAL), VV_RO},
378 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100379 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200380 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000381};
382
383/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000384#define vv_type vv_di.di_tv.v_type
385#define vv_nr vv_di.di_tv.vval.v_number
386#define vv_float vv_di.di_tv.vval.v_float
387#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000388#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200389#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000390#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000391
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200392static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000393#define vimvarht vimvardict.dv_hashtab
394
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100395static void prepare_vimvar(int idx, typval_T *save_tv);
396static void restore_vimvar(int idx, typval_T *save_tv);
397static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
398static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
399static char_u *skip_var_one(char_u *arg);
400static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
401static void list_glob_vars(int *first);
402static void list_buf_vars(int *first);
403static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000404#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100405static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000406#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100407static void list_vim_vars(int *first);
408static void list_script_vars(int *first);
409static void list_func_vars(int *first);
410static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
411static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
412static int check_changedtick(char_u *arg);
413static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
414static void clear_lval(lval_T *lp);
415static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
416static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
417static void list_fix_watch(list_T *l, listitem_T *item);
418static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
419static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
420static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
421static void item_lock(typval_T *tv, int deep, int lock);
422static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000423
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100424static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
425static int eval1(char_u **arg, typval_T *rettv, int evaluate);
426static int eval2(char_u **arg, typval_T *rettv, int evaluate);
427static int eval3(char_u **arg, typval_T *rettv, int evaluate);
428static int eval4(char_u **arg, typval_T *rettv, int evaluate);
429static int eval5(char_u **arg, typval_T *rettv, int evaluate);
430static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
431static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000432
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100433static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
434static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200438static void list_free_contents(list_T *l);
439static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100440static long list_len(list_T *l);
441static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
442static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
443static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
444static long list_find_nr(list_T *l, long idx, int *errorp);
445static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100446static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
447static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
448static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200449static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
450static 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);
451static 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 +0100452static int free_unref_items(int copyID);
453static dictitem_T *dictitem_copy(dictitem_T *org);
454static void dictitem_remove(dict_T *dict, dictitem_T *item);
455static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
456static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200457static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100458static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200459static 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 +0100460static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *string_quote(char_u *str, int function);
462static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
463static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100464static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
465static 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 +0100466static void emsg_funcname(char *ermsg, char_u *name);
467static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000468
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200469static void dict_free_contents(dict_T *d);
470static void dict_free_dict(dict_T *d);
471
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000472#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100473static void f_abs(typval_T *argvars, typval_T *rettv);
474static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100476static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_and(typval_T *argvars, typval_T *rettv);
478static void f_append(typval_T *argvars, typval_T *rettv);
479static void f_argc(typval_T *argvars, typval_T *rettv);
480static void f_argidx(typval_T *argvars, typval_T *rettv);
481static void f_arglistid(typval_T *argvars, typval_T *rettv);
482static void f_argv(typval_T *argvars, typval_T *rettv);
483static void f_assert_equal(typval_T *argvars, typval_T *rettv);
484static void f_assert_exception(typval_T *argvars, typval_T *rettv);
485static void f_assert_fails(typval_T *argvars, typval_T *rettv);
486static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200487static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200488static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
489static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100490static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100492static void f_asin(typval_T *argvars, typval_T *rettv);
493static void f_atan(typval_T *argvars, typval_T *rettv);
494static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_browse(typval_T *argvars, typval_T *rettv);
497static void f_browsedir(typval_T *argvars, typval_T *rettv);
498static void f_bufexists(typval_T *argvars, typval_T *rettv);
499static void f_buflisted(typval_T *argvars, typval_T *rettv);
500static void f_bufloaded(typval_T *argvars, typval_T *rettv);
501static void f_bufname(typval_T *argvars, typval_T *rettv);
502static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200503static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100504static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
505static void f_byte2line(typval_T *argvars, typval_T *rettv);
506static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
507static void f_byteidx(typval_T *argvars, typval_T *rettv);
508static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
509static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100511static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000512#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100513#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100515static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
516static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100517static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100518static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100519static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100520static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100521static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
522static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100523static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100524static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100525static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
526static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100527static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100528static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_changenr(typval_T *argvars, typval_T *rettv);
531static void f_char2nr(typval_T *argvars, typval_T *rettv);
532static void f_cindent(typval_T *argvars, typval_T *rettv);
533static void f_clearmatches(typval_T *argvars, typval_T *rettv);
534static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000535#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_complete(typval_T *argvars, typval_T *rettv);
537static void f_complete_add(typval_T *argvars, typval_T *rettv);
538static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000539#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_confirm(typval_T *argvars, typval_T *rettv);
541static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000542#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_cos(typval_T *argvars, typval_T *rettv);
544static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000545#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100546static void f_count(typval_T *argvars, typval_T *rettv);
547static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
548static void f_cursor(typval_T *argsvars, typval_T *rettv);
549static void f_deepcopy(typval_T *argvars, typval_T *rettv);
550static void f_delete(typval_T *argvars, typval_T *rettv);
551static void f_did_filetype(typval_T *argvars, typval_T *rettv);
552static void f_diff_filler(typval_T *argvars, typval_T *rettv);
553static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
554static void f_empty(typval_T *argvars, typval_T *rettv);
555static void f_escape(typval_T *argvars, typval_T *rettv);
556static void f_eval(typval_T *argvars, typval_T *rettv);
557static void f_eventhandler(typval_T *argvars, typval_T *rettv);
558static void f_executable(typval_T *argvars, typval_T *rettv);
559static void f_exepath(typval_T *argvars, typval_T *rettv);
560static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200561#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_expand(typval_T *argvars, typval_T *rettv);
565static void f_extend(typval_T *argvars, typval_T *rettv);
566static void f_feedkeys(typval_T *argvars, typval_T *rettv);
567static void f_filereadable(typval_T *argvars, typval_T *rettv);
568static void f_filewritable(typval_T *argvars, typval_T *rettv);
569static void f_filter(typval_T *argvars, typval_T *rettv);
570static void f_finddir(typval_T *argvars, typval_T *rettv);
571static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000572#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100573static void f_float2nr(typval_T *argvars, typval_T *rettv);
574static void f_floor(typval_T *argvars, typval_T *rettv);
575static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000576#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100577static void f_fnameescape(typval_T *argvars, typval_T *rettv);
578static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
579static void f_foldclosed(typval_T *argvars, typval_T *rettv);
580static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
581static void f_foldlevel(typval_T *argvars, typval_T *rettv);
582static void f_foldtext(typval_T *argvars, typval_T *rettv);
583static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
584static void f_foreground(typval_T *argvars, typval_T *rettv);
585static void f_function(typval_T *argvars, typval_T *rettv);
586static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
587static void f_get(typval_T *argvars, typval_T *rettv);
588static void f_getbufline(typval_T *argvars, typval_T *rettv);
589static void f_getbufvar(typval_T *argvars, typval_T *rettv);
590static void f_getchar(typval_T *argvars, typval_T *rettv);
591static void f_getcharmod(typval_T *argvars, typval_T *rettv);
592static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
593static void f_getcmdline(typval_T *argvars, typval_T *rettv);
594static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
595static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
596static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
597static void f_getcwd(typval_T *argvars, typval_T *rettv);
598static void f_getfontname(typval_T *argvars, typval_T *rettv);
599static void f_getfperm(typval_T *argvars, typval_T *rettv);
600static void f_getfsize(typval_T *argvars, typval_T *rettv);
601static void f_getftime(typval_T *argvars, typval_T *rettv);
602static void f_getftype(typval_T *argvars, typval_T *rettv);
603static void f_getline(typval_T *argvars, typval_T *rettv);
604static void f_getmatches(typval_T *argvars, typval_T *rettv);
605static void f_getpid(typval_T *argvars, typval_T *rettv);
606static void f_getcurpos(typval_T *argvars, typval_T *rettv);
607static void f_getpos(typval_T *argvars, typval_T *rettv);
608static void f_getqflist(typval_T *argvars, typval_T *rettv);
609static void f_getreg(typval_T *argvars, typval_T *rettv);
610static void f_getregtype(typval_T *argvars, typval_T *rettv);
611static void f_gettabvar(typval_T *argvars, typval_T *rettv);
612static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
613static void f_getwinposx(typval_T *argvars, typval_T *rettv);
614static void f_getwinposy(typval_T *argvars, typval_T *rettv);
615static void f_getwinvar(typval_T *argvars, typval_T *rettv);
616static void f_glob(typval_T *argvars, typval_T *rettv);
617static void f_globpath(typval_T *argvars, typval_T *rettv);
618static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
619static void f_has(typval_T *argvars, typval_T *rettv);
620static void f_has_key(typval_T *argvars, typval_T *rettv);
621static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
622static void f_hasmapto(typval_T *argvars, typval_T *rettv);
623static void f_histadd(typval_T *argvars, typval_T *rettv);
624static void f_histdel(typval_T *argvars, typval_T *rettv);
625static void f_histget(typval_T *argvars, typval_T *rettv);
626static void f_histnr(typval_T *argvars, typval_T *rettv);
627static void f_hlID(typval_T *argvars, typval_T *rettv);
628static void f_hlexists(typval_T *argvars, typval_T *rettv);
629static void f_hostname(typval_T *argvars, typval_T *rettv);
630static void f_iconv(typval_T *argvars, typval_T *rettv);
631static void f_indent(typval_T *argvars, typval_T *rettv);
632static void f_index(typval_T *argvars, typval_T *rettv);
633static void f_input(typval_T *argvars, typval_T *rettv);
634static void f_inputdialog(typval_T *argvars, typval_T *rettv);
635static void f_inputlist(typval_T *argvars, typval_T *rettv);
636static void f_inputrestore(typval_T *argvars, typval_T *rettv);
637static void f_inputsave(typval_T *argvars, typval_T *rettv);
638static void f_inputsecret(typval_T *argvars, typval_T *rettv);
639static void f_insert(typval_T *argvars, typval_T *rettv);
640static void f_invert(typval_T *argvars, typval_T *rettv);
641static void f_isdirectory(typval_T *argvars, typval_T *rettv);
642static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100643#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
644static void f_isnan(typval_T *argvars, typval_T *rettv);
645#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100648static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100649static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100650static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100651static void f_job_start(typval_T *argvars, typval_T *rettv);
652static void f_job_stop(typval_T *argvars, typval_T *rettv);
653static void f_job_status(typval_T *argvars, typval_T *rettv);
654#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100656static void f_js_decode(typval_T *argvars, typval_T *rettv);
657static void f_js_encode(typval_T *argvars, typval_T *rettv);
658static void f_json_decode(typval_T *argvars, typval_T *rettv);
659static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_keys(typval_T *argvars, typval_T *rettv);
661static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
662static void f_len(typval_T *argvars, typval_T *rettv);
663static void f_libcall(typval_T *argvars, typval_T *rettv);
664static void f_libcallnr(typval_T *argvars, typval_T *rettv);
665static void f_line(typval_T *argvars, typval_T *rettv);
666static void f_line2byte(typval_T *argvars, typval_T *rettv);
667static void f_lispindent(typval_T *argvars, typval_T *rettv);
668static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_log(typval_T *argvars, typval_T *rettv);
671static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000672#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_map(typval_T *argvars, typval_T *rettv);
677static void f_maparg(typval_T *argvars, typval_T *rettv);
678static void f_mapcheck(typval_T *argvars, typval_T *rettv);
679static void f_match(typval_T *argvars, typval_T *rettv);
680static void f_matchadd(typval_T *argvars, typval_T *rettv);
681static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
682static void f_matcharg(typval_T *argvars, typval_T *rettv);
683static void f_matchdelete(typval_T *argvars, typval_T *rettv);
684static void f_matchend(typval_T *argvars, typval_T *rettv);
685static void f_matchlist(typval_T *argvars, typval_T *rettv);
686static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200687static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_max(typval_T *argvars, typval_T *rettv);
689static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
698static void f_nr2char(typval_T *argvars, typval_T *rettv);
699static void f_or(typval_T *argvars, typval_T *rettv);
700static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
708static void f_printf(typval_T *argvars, typval_T *rettv);
709static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
713#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_range(typval_T *argvars, typval_T *rettv);
717static void f_readfile(typval_T *argvars, typval_T *rettv);
718static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100719#ifdef FEAT_FLOAT
720static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
721#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_reltimestr(typval_T *argvars, typval_T *rettv);
723static void f_remote_expr(typval_T *argvars, typval_T *rettv);
724static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
725static void f_remote_peek(typval_T *argvars, typval_T *rettv);
726static void f_remote_read(typval_T *argvars, typval_T *rettv);
727static void f_remote_send(typval_T *argvars, typval_T *rettv);
728static void f_remove(typval_T *argvars, typval_T *rettv);
729static void f_rename(typval_T *argvars, typval_T *rettv);
730static void f_repeat(typval_T *argvars, typval_T *rettv);
731static void f_resolve(typval_T *argvars, typval_T *rettv);
732static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_screenattr(typval_T *argvars, typval_T *rettv);
737static void f_screenchar(typval_T *argvars, typval_T *rettv);
738static void f_screencol(typval_T *argvars, typval_T *rettv);
739static void f_screenrow(typval_T *argvars, typval_T *rettv);
740static void f_search(typval_T *argvars, typval_T *rettv);
741static void f_searchdecl(typval_T *argvars, typval_T *rettv);
742static void f_searchpair(typval_T *argvars, typval_T *rettv);
743static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
744static void f_searchpos(typval_T *argvars, typval_T *rettv);
745static void f_server2client(typval_T *argvars, typval_T *rettv);
746static void f_serverlist(typval_T *argvars, typval_T *rettv);
747static void f_setbufvar(typval_T *argvars, typval_T *rettv);
748static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
749static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100750static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_setline(typval_T *argvars, typval_T *rettv);
752static void f_setloclist(typval_T *argvars, typval_T *rettv);
753static void f_setmatches(typval_T *argvars, typval_T *rettv);
754static void f_setpos(typval_T *argvars, typval_T *rettv);
755static void f_setqflist(typval_T *argvars, typval_T *rettv);
756static void f_setreg(typval_T *argvars, typval_T *rettv);
757static void f_settabvar(typval_T *argvars, typval_T *rettv);
758static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
759static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_shellescape(typval_T *argvars, typval_T *rettv);
764static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
765static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sin(typval_T *argvars, typval_T *rettv);
768static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_sort(typval_T *argvars, typval_T *rettv);
771static void f_soundfold(typval_T *argvars, typval_T *rettv);
772static void f_spellbadword(typval_T *argvars, typval_T *rettv);
773static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
774static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_sqrt(typval_T *argvars, typval_T *rettv);
777static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_str2nr(typval_T *argvars, typval_T *rettv);
780static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200784static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100785static void f_stridx(typval_T *argvars, typval_T *rettv);
786static void f_string(typval_T *argvars, typval_T *rettv);
787static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200788static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_strpart(typval_T *argvars, typval_T *rettv);
790static void f_strridx(typval_T *argvars, typval_T *rettv);
791static void f_strtrans(typval_T *argvars, typval_T *rettv);
792static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
793static void f_strwidth(typval_T *argvars, typval_T *rettv);
794static void f_submatch(typval_T *argvars, typval_T *rettv);
795static void f_substitute(typval_T *argvars, typval_T *rettv);
796static void f_synID(typval_T *argvars, typval_T *rettv);
797static void f_synIDattr(typval_T *argvars, typval_T *rettv);
798static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
799static void f_synstack(typval_T *argvars, typval_T *rettv);
800static void f_synconcealed(typval_T *argvars, typval_T *rettv);
801static void f_system(typval_T *argvars, typval_T *rettv);
802static void f_systemlist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
804static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
805static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
806static void f_taglist(typval_T *argvars, typval_T *rettv);
807static void f_tagfiles(typval_T *argvars, typval_T *rettv);
808static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200809static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
810static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200811static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
812#ifdef FEAT_JOB_CHANNEL
813static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
814#endif
815static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
816#ifdef FEAT_JOB_CHANNEL
817static void f_test_null_job(typval_T *argvars, typval_T *rettv);
818#endif
819static void f_test_null_list(typval_T *argvars, typval_T *rettv);
820static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
821static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200822#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static void f_tan(typval_T *argvars, typval_T *rettv);
824static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200825#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100826#ifdef FEAT_TIMERS
827static void f_timer_start(typval_T *argvars, typval_T *rettv);
828static void f_timer_stop(typval_T *argvars, typval_T *rettv);
829#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static void f_tolower(typval_T *argvars, typval_T *rettv);
831static void f_toupper(typval_T *argvars, typval_T *rettv);
832static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000833#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000835#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static void f_type(typval_T *argvars, typval_T *rettv);
837static void f_undofile(typval_T *argvars, typval_T *rettv);
838static void f_undotree(typval_T *argvars, typval_T *rettv);
839static void f_uniq(typval_T *argvars, typval_T *rettv);
840static void f_values(typval_T *argvars, typval_T *rettv);
841static void f_virtcol(typval_T *argvars, typval_T *rettv);
842static void f_visualmode(typval_T *argvars, typval_T *rettv);
843static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100844static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100845static void f_win_getid(typval_T *argvars, typval_T *rettv);
846static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
847static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
848static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100849static void f_winbufnr(typval_T *argvars, typval_T *rettv);
850static void f_wincol(typval_T *argvars, typval_T *rettv);
851static void f_winheight(typval_T *argvars, typval_T *rettv);
852static void f_winline(typval_T *argvars, typval_T *rettv);
853static void f_winnr(typval_T *argvars, typval_T *rettv);
854static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
855static void f_winrestview(typval_T *argvars, typval_T *rettv);
856static void f_winsaveview(typval_T *argvars, typval_T *rettv);
857static void f_winwidth(typval_T *argvars, typval_T *rettv);
858static void f_writefile(typval_T *argvars, typval_T *rettv);
859static void f_wordcount(typval_T *argvars, typval_T *rettv);
860static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000861
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100862static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
863static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
864static int get_env_len(char_u **arg);
865static int get_id_len(char_u **arg);
866static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
867static 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 +0000868#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
869#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
870 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
872static int eval_isnamec(int c);
873static int eval_isnamec1(int c);
874static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
875static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100876static typval_T *alloc_string_tv(char_u *string);
877static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100878#ifdef FEAT_FLOAT
879static float_T get_tv_float(typval_T *varp);
880#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static linenr_T get_tv_lnum(typval_T *argvars);
882static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
884static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
885static hashtab_T *find_var_ht(char_u *name, char_u **varname);
886static funccall_T *get_funccal(void);
887static void vars_clear_ext(hashtab_T *ht, int free_val);
888static void delete_var(hashtab_T *ht, hashitem_T *hi);
889static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
890static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
891static void set_var(char_u *name, typval_T *varp, int copy);
892static int var_check_ro(int flags, char_u *name, int use_gettext);
893static int var_check_fixed(int flags, char_u *name, int use_gettext);
894static int var_check_func_name(char_u *name, int new_var);
895static int valid_varname(char_u *varname);
896static int tv_check_lock(int lock, char_u *name, int use_gettext);
897static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
898static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100899static 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 +0100900static int eval_fname_script(char_u *p);
901static int eval_fname_sid(char_u *p);
902static void list_func_head(ufunc_T *fp, int indent);
903static ufunc_T *find_func(char_u *name);
904static int function_exists(char_u *name);
905static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static void func_do_profile(ufunc_T *fp);
908static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
909static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000910static int
911# ifdef __BORLANDC__
912 _RTLENTRYF
913# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100914 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000915static int
916# ifdef __BORLANDC__
917 _RTLENTRYF
918# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100919 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000920#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100921static int script_autoload(char_u *name, int reload);
922static char_u *autoload_name(char_u *name);
923static void cat_func_name(char_u *buf, ufunc_T *fp);
924static void func_free(ufunc_T *fp);
925static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
926static int can_free_funccal(funccall_T *fc, int copyID) ;
927static void free_funccal(funccall_T *fc, int free_val);
928static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
929static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
930static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
931static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
932static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
933static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
934static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
935static int write_list(FILE *fd, list_T *list, int binary);
936static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000937
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200938
939#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100940static int compare_func_name(const void *s1, const void *s2);
941static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200942#endif
943
Bram Moolenaar33570922005-01-25 22:26:29 +0000944/*
945 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000946 */
947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000949{
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 int i;
951 struct vimvar *p;
952
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200953 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
954 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200955 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000956 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000957 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000958
959 for (i = 0; i < VV_LEN; ++i)
960 {
961 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200962 if (STRLEN(p->vv_name) > 16)
963 {
964 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
965 getout(1);
966 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000967 STRCPY(p->vv_di.di_key, p->vv_name);
968 if (p->vv_flags & VV_RO)
969 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
970 else if (p->vv_flags & VV_RO_SBX)
971 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
972 else
973 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000974
975 /* add to v: scope dict, unless the value is not always available */
976 if (p->vv_type != VAR_UNKNOWN)
977 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000979 /* add to compat scope dict */
980 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100982 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
983
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000984 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100985 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200986 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100987 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100988
989 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
990 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
991 set_vim_var_nr(VV_NONE, VVAL_NONE);
992 set_vim_var_nr(VV_NULL, VVAL_NULL);
993
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200994 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200995
996#ifdef EBCDIC
997 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100998 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200999 */
1000 sortFunctions();
1001#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001002}
1003
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004#if defined(EXITFREE) || defined(PROTO)
1005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001007{
1008 int i;
1009 struct vimvar *p;
1010
1011 for (i = 0; i < VV_LEN; ++i)
1012 {
1013 p = &vimvars[i];
1014 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001015 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001016 vim_free(p->vv_str);
1017 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001018 }
1019 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1020 {
1021 list_unref(p->vv_list);
1022 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001023 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024 }
1025 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001026 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001027 hash_clear(&compat_hashtab);
1028
Bram Moolenaard9fba312005-06-26 22:34:35 +00001029 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001030# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001031 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001032# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001033
1034 /* global variables */
1035 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001036
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001037 /* autoloaded script names */
1038 ga_clear_strings(&ga_loaded);
1039
Bram Moolenaarcca74132013-09-25 21:00:28 +02001040 /* Script-local variables. First clear all the variables and in a second
1041 * loop free the scriptvar_T, because a variable in one script might hold
1042 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001045 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001047 ga_clear(&ga_scripts);
1048
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001049 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001050 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001051
1052 /* functions */
1053 free_all_functions();
1054 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001055}
1056#endif
1057
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 * Return the name of the executed function.
1060 */
1061 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001062func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065}
1066
1067/*
1068 * Return the address holding the next breakpoint line for a funccall cookie.
1069 */
1070 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001071func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
Bram Moolenaar33570922005-01-25 22:26:29 +00001073 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074}
1075
1076/*
1077 * Return the address holding the debug tick for a funccall cookie.
1078 */
1079 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001080func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
Bram Moolenaar33570922005-01-25 22:26:29 +00001082 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083}
1084
1085/*
1086 * Return the nesting level for a funccall cookie.
1087 */
1088 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001089func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar33570922005-01-25 22:26:29 +00001091 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092}
1093
1094/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001095funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001097/* pointer to list of previously used funccal, still around because some
1098 * item in it is still being used. */
1099funccall_T *previous_funccal = NULL;
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101/*
1102 * Return TRUE when a function was ended by a ":return" command.
1103 */
1104 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001105current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106{
1107 return current_funccal->returned;
1108}
1109
1110
1111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 * Set an internal variable to a string value. Creates the variable if it does
1113 * not already exist.
1114 */
1115 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001116set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001118 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001119 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
1121 val = vim_strsave(value);
1122 if (val != NULL)
1123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001127 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001128 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 }
1131}
1132
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135static char_u *redir_endp = NULL;
1136static char_u *redir_varname = NULL;
1137
1138/*
1139 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001140 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 * Returns OK if successfully completed the setup. FAIL otherwise.
1142 */
1143 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001144var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145{
1146 int save_emsg;
1147 int err;
1148 typval_T tv;
1149
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001151 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 {
1153 EMSG(_(e_invarg));
1154 return FAIL;
1155 }
1156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 redir_varname = vim_strsave(name);
1159 if (redir_varname == NULL)
1160 return FAIL;
1161
1162 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1163 if (redir_lval == NULL)
1164 {
1165 var_redir_stop();
1166 return FAIL;
1167 }
1168
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 /* The output is stored in growarray "redir_ga" until redirection ends. */
1170 ga_init2(&redir_ga, (int)sizeof(char), 500);
1171
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001173 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001174 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1176 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001177 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 if (redir_endp != NULL && *redir_endp != NUL)
1179 /* Trailing characters are present after the variable name */
1180 EMSG(_(e_trailing));
1181 else
1182 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001183 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 var_redir_stop();
1185 return FAIL;
1186 }
1187
1188 /* check if we can write to the variable: set it to or append an empty
1189 * string */
1190 save_emsg = did_emsg;
1191 did_emsg = FALSE;
1192 tv.v_type = VAR_STRING;
1193 tv.vval.v_string = (char_u *)"";
1194 if (append)
1195 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1196 else
1197 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001200 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 if (err)
1202 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 var_redir_stop();
1205 return FAIL;
1206 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207
1208 return OK;
1209}
1210
1211/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212 * Append "value[value_len]" to the variable set by var_redir_start().
1213 * The actual appending is postponed until redirection ends, because the value
1214 * appended may in fact be the string we write to, changing it may cause freed
1215 * memory to be used:
1216 * :redir => foo
1217 * :let foo
1218 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219 */
1220 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001221var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001223 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001224
1225 if (redir_lval == NULL)
1226 return;
1227
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001228 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001229 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 {
1235 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001236 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237 }
1238 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240}
1241
1242/*
1243 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001244 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245 */
1246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001247var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001249 typval_T tv;
1250
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001251 if (redir_lval != NULL)
1252 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001253 /* If there was no error: assign the text to the variable. */
1254 if (redir_endp != NULL)
1255 {
1256 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1257 tv.v_type = VAR_STRING;
1258 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001259 /* Call get_lval() again, if it's inside a Dict or List it may
1260 * have changed. */
1261 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001262 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001263 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1264 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1265 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001266 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001267
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001268 /* free the collected output */
1269 vim_free(redir_ga.ga_data);
1270 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001271
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001272 vim_free(redir_lval);
1273 redir_lval = NULL;
1274 }
1275 vim_free(redir_varname);
1276 redir_varname = NULL;
1277}
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279# if defined(FEAT_MBYTE) || defined(PROTO)
1280 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001281eval_charconvert(
1282 char_u *enc_from,
1283 char_u *enc_to,
1284 char_u *fname_from,
1285 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err = FALSE;
1288
1289 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1290 set_vim_var_string(VV_CC_TO, enc_to, -1);
1291 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1292 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1293 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1294 err = TRUE;
1295 set_vim_var_string(VV_CC_FROM, NULL, -1);
1296 set_vim_var_string(VV_CC_TO, NULL, -1);
1297 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1298 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1299
1300 if (err)
1301 return FAIL;
1302 return OK;
1303}
1304# endif
1305
1306# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001308eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 int err = FALSE;
1311
1312 set_vim_var_string(VV_FNAME_IN, fname, -1);
1313 set_vim_var_string(VV_CMDARG, args, -1);
1314 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1315 err = TRUE;
1316 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1317 set_vim_var_string(VV_CMDARG, NULL, -1);
1318
1319 if (err)
1320 {
1321 mch_remove(fname);
1322 return FAIL;
1323 }
1324 return OK;
1325}
1326# endif
1327
1328# if defined(FEAT_DIFF) || defined(PROTO)
1329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001330eval_diff(
1331 char_u *origfile,
1332 char_u *newfile,
1333 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
1335 int err = FALSE;
1336
1337 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1338 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1339 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1340 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1341 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1342 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1343 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1344}
1345
1346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347eval_patch(
1348 char_u *origfile,
1349 char_u *difffile,
1350 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351{
1352 int err;
1353
1354 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1355 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1356 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1357 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1358 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1359 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1360 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1361}
1362# endif
1363
1364/*
1365 * Top level evaluation function, returning a boolean.
1366 * Sets "error" to TRUE if there was an error.
1367 * Return TRUE or FALSE.
1368 */
1369 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001370eval_to_bool(
1371 char_u *arg,
1372 int *error,
1373 char_u **nextcmd,
1374 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375{
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 int retval = FALSE;
1378
1379 if (skip)
1380 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 else
1384 {
1385 *error = FALSE;
1386 if (!skip)
1387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001388 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 }
1392 if (skip)
1393 --emsg_skip;
1394
1395 return retval;
1396}
1397
1398/*
1399 * Top level evaluation function, returning a string. If "skip" is TRUE,
1400 * only parsing to "nextcmd" is done, without reporting errors. Return
1401 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1402 */
1403 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001404eval_to_string_skip(
1405 char_u *arg,
1406 char_u **nextcmd,
1407 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *retval;
1411
1412 if (skip)
1413 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 retval = NULL;
1416 else
1417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 retval = vim_strsave(get_tv_string(&tv));
1419 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
1421 if (skip)
1422 --emsg_skip;
1423
1424 return retval;
1425}
1426
1427/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001428 * Skip over an expression at "*pp".
1429 * Return FAIL for an error, OK otherwise.
1430 */
1431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001432skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001433{
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435
1436 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001437 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001438}
1439
1440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001442 * When "convert" is TRUE convert a List into a sequence of lines and convert
1443 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 * Return pointer to allocated memory, or NULL for failure.
1445 */
1446 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001447eval_to_string(
1448 char_u *arg,
1449 char_u **nextcmd,
1450 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
Bram Moolenaar33570922005-01-25 22:26:29 +00001452 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001454 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001455#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001456 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001459 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 retval = NULL;
1461 else
1462 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001463 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 {
1465 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001466 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001467 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001468 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001469 if (tv.vval.v_list->lv_len > 0)
1470 ga_append(&ga, NL);
1471 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001472 ga_append(&ga, NUL);
1473 retval = (char_u *)ga.ga_data;
1474 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001475#ifdef FEAT_FLOAT
1476 else if (convert && tv.v_type == VAR_FLOAT)
1477 {
1478 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1479 retval = vim_strsave(numbuf);
1480 }
1481#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 else
1483 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001484 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486
1487 return retval;
1488}
1489
1490/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001491 * Call eval_to_string() without using current local variables and using
1492 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 */
1494 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495eval_to_string_safe(
1496 char_u *arg,
1497 char_u **nextcmd,
1498 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 char_u *retval;
1501 void *save_funccalp;
1502
1503 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001504 if (use_sandbox)
1505 ++sandbox;
1506 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001507 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001508 if (use_sandbox)
1509 --sandbox;
1510 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 restore_funccal(save_funccalp);
1512 return retval;
1513}
1514
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515/*
1516 * Top level evaluation function, returning a number.
1517 * Evaluates "expr" silently.
1518 * Returns -1 for an error.
1519 */
1520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar33570922005-01-25 22:26:29 +00001523 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001525 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 ++emsg_off;
1528
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001529 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 retval = -1;
1531 else
1532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001533 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001534 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 --emsg_off;
1537
1538 return retval;
1539}
1540
Bram Moolenaara40058a2005-07-11 22:42:07 +00001541/*
1542 * Prepare v: variable "idx" to be used.
1543 * Save the current typeval in "save_tv".
1544 * When not used yet add the variable to the v: hashtable.
1545 */
1546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001548{
1549 *save_tv = vimvars[idx].vv_tv;
1550 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1551 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1552}
1553
1554/*
1555 * Restore v: variable "idx" to typeval "save_tv".
1556 * When no longer defined, remove the variable from the v: hashtable.
1557 */
1558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001559restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001560{
1561 hashitem_T *hi;
1562
Bram Moolenaara40058a2005-07-11 22:42:07 +00001563 vimvars[idx].vv_tv = *save_tv;
1564 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1565 {
1566 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1567 if (HASHITEM_EMPTY(hi))
1568 EMSG2(_(e_intern2), "restore_vimvar()");
1569 else
1570 hash_remove(&vimvarht, hi);
1571 }
1572}
1573
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001574#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001575/*
1576 * Evaluate an expression to a list with suggestions.
1577 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001578 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579 */
1580 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001581eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001582{
1583 typval_T save_val;
1584 typval_T rettv;
1585 list_T *list = NULL;
1586 char_u *p = skipwhite(expr);
1587
1588 /* Set "v:val" to the bad word. */
1589 prepare_vimvar(VV_VAL, &save_val);
1590 vimvars[VV_VAL].vv_type = VAR_STRING;
1591 vimvars[VV_VAL].vv_str = badword;
1592 if (p_verbose == 0)
1593 ++emsg_off;
1594
1595 if (eval1(&p, &rettv, TRUE) == OK)
1596 {
1597 if (rettv.v_type != VAR_LIST)
1598 clear_tv(&rettv);
1599 else
1600 list = rettv.vval.v_list;
1601 }
1602
1603 if (p_verbose == 0)
1604 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001605 restore_vimvar(VV_VAL, &save_val);
1606
1607 return list;
1608}
1609
1610/*
1611 * "list" is supposed to contain two items: a word and a number. Return the
1612 * word in "pp" and the number as the return value.
1613 * Return -1 if anything isn't right.
1614 * Used to get the good word and score from the eval_spell_expr() result.
1615 */
1616 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001617get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001618{
1619 listitem_T *li;
1620
1621 li = list->lv_first;
1622 if (li == NULL)
1623 return -1;
1624 *pp = get_tv_string(&li->li_tv);
1625
1626 li = li->li_next;
1627 if (li == NULL)
1628 return -1;
1629 return get_tv_number(&li->li_tv);
1630}
1631#endif
1632
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001634 * Top level evaluation function.
1635 * Returns an allocated typval_T with the result.
1636 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001637 */
1638 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001640{
1641 typval_T *tv;
1642
1643 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 {
1646 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001647 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001648 }
1649
1650 return tv;
1651}
1652
1653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001656 * Uses argv[argc] for the function arguments. Only Number and String
1657 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001658 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001660 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661call_vim_function(
1662 char_u *func,
1663 int argc,
1664 char_u **argv,
1665 int safe, /* use the sandbox */
1666 int str_arg_only, /* all arguments are strings */
1667 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaar33570922005-01-25 22:26:29 +00001669 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 long n;
1671 int len;
1672 int i;
1673 int doesrange;
1674 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001675 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001677 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681 for (i = 0; i < argc; i++)
1682 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001683 /* Pass a NULL or empty argument as an empty string */
1684 if (argv[i] == NULL || *argv[i] == NUL)
1685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001686 argvars[i].v_type = VAR_STRING;
1687 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001688 continue;
1689 }
1690
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001691 if (str_arg_only)
1692 len = 0;
1693 else
1694 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001695 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 if (len != 0 && len == (int)STRLEN(argv[i]))
1697 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001698 argvars[i].v_type = VAR_NUMBER;
1699 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
1701 else
1702 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001703 argvars[i].v_type = VAR_STRING;
1704 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 }
1706 }
1707
1708 if (safe)
1709 {
1710 save_funccalp = save_funccal();
1711 ++sandbox;
1712 }
1713
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001714 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1715 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001717 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 if (safe)
1719 {
1720 --sandbox;
1721 restore_funccal(save_funccalp);
1722 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001723 vim_free(argvars);
1724
1725 if (ret == FAIL)
1726 clear_tv(rettv);
1727
1728 return ret;
1729}
1730
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001731/*
1732 * Call vimL function "func" and return the result as a number.
1733 * Returns -1 when calling the function fails.
1734 * Uses argv[argc] for the function arguments.
1735 */
1736 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001737call_func_retnr(
1738 char_u *func,
1739 int argc,
1740 char_u **argv,
1741 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001742{
1743 typval_T rettv;
1744 long retval;
1745
1746 /* All arguments are passed as strings, no conversion to number. */
1747 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1748 return -1;
1749
1750 retval = get_tv_number_chk(&rettv, NULL);
1751 clear_tv(&rettv);
1752 return retval;
1753}
1754
1755#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1756 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1757
Bram Moolenaar4f688582007-07-24 12:34:30 +00001758# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001760 * Call vimL function "func" and return the result as a string.
1761 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 * Uses argv[argc] for the function arguments.
1763 */
1764 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765call_func_retstr(
1766 char_u *func,
1767 int argc,
1768 char_u **argv,
1769 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770{
1771 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001772 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001774 /* All arguments are passed as strings, no conversion to number. */
1775 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 return NULL;
1777
1778 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001779 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 return retval;
1781}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001782# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001784/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001785 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001787 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 */
1789 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790call_func_retlist(
1791 char_u *func,
1792 int argc,
1793 char_u **argv,
1794 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001795{
1796 typval_T rettv;
1797
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001798 /* All arguments are passed as strings, no conversion to number. */
1799 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001800 return NULL;
1801
1802 if (rettv.v_type != VAR_LIST)
1803 {
1804 clear_tv(&rettv);
1805 return NULL;
1806 }
1807
1808 return rettv.vval.v_list;
1809}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#endif
1811
1812/*
1813 * Save the current function call pointer, and set it to NULL.
1814 * Used when executing autocommands and for ":source".
1815 */
1816 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 current_funccal = NULL;
1822 return (void *)fc;
1823}
1824
1825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001828 funccall_T *fc = (funccall_T *)vfc;
1829
1830 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831}
1832
Bram Moolenaar05159a02005-02-26 23:04:13 +00001833#if defined(FEAT_PROFILE) || defined(PROTO)
1834/*
1835 * Prepare profiling for entering a child or something else that is not
1836 * counted for the script/function itself.
1837 * Should always be called in pair with prof_child_exit().
1838 */
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840prof_child_enter(
1841 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001842{
1843 funccall_T *fc = current_funccal;
1844
1845 if (fc != NULL && fc->func->uf_profiling)
1846 profile_start(&fc->prof_child);
1847 script_prof_save(tm);
1848}
1849
1850/*
1851 * Take care of time spent in a child.
1852 * Should always be called after prof_child_enter().
1853 */
1854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001855prof_child_exit(
1856 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001857{
1858 funccall_T *fc = current_funccal;
1859
1860 if (fc != NULL && fc->func->uf_profiling)
1861 {
1862 profile_end(&fc->prof_child);
1863 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1864 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1865 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1866 }
1867 script_prof_restore(tm);
1868}
1869#endif
1870
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#ifdef FEAT_FOLDING
1873/*
1874 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1875 * it in "*cp". Doesn't give error messages.
1876 */
1877 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001878eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879{
Bram Moolenaar33570922005-01-25 22:26:29 +00001880 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 int retval;
1882 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001883 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1884 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001887 if (use_sandbox)
1888 ++sandbox;
1889 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 retval = 0;
1893 else
1894 {
1895 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 if (tv.v_type == VAR_NUMBER)
1897 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001898 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 retval = 0;
1900 else
1901 {
1902 /* If the result is a string, check if there is a non-digit before
1903 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (!VIM_ISDIGIT(*s) && *s != '-')
1906 *cp = *s++;
1907 retval = atol((char *)s);
1908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001912 if (use_sandbox)
1913 --sandbox;
1914 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 return retval;
1917}
1918#endif
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 * ":let" list all variable values
1922 * ":let var1 var2" list variable values
1923 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 * ":let var += expr" assignment command.
1925 * ":let var -= expr" assignment command.
1926 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 */
1929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001930ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
1932 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001934 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 int var_count = 0;
1937 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001940 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
Bram Moolenaardb552d602006-03-23 22:59:57 +00001942 argend = skip_var_list(arg, &var_count, &semicolon);
1943 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001945 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1946 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001947 expr = skipwhite(argend);
1948 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1949 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001951 /*
1952 * ":let" without "=": list variables
1953 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 if (*arg == '[')
1955 EMSG(_(e_invarg));
1956 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001958 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001962 list_glob_vars(&first);
1963 list_buf_vars(&first);
1964 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001965#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001967#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 list_script_vars(&first);
1969 list_func_vars(&first);
1970 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 eap->nextcmd = check_nextcmd(arg);
1973 }
1974 else
1975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 op[0] = '=';
1977 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001978 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001979 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001980 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1981 op[0] = *expr; /* +=, -= or .= */
1982 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001984 else
1985 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (eap->skip)
1988 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (eap->skip)
1991 {
1992 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 --emsg_skip;
1995 }
1996 else if (i != FAIL)
1997 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
2002 }
2003}
2004
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005/*
2006 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2007 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 * When "nextchars" is not NULL it points to a string with characters that
2009 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2010 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 * Returns OK or FAIL;
2012 */
2013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002014ex_let_vars(
2015 char_u *arg_start,
2016 typval_T *tv,
2017 int copy, /* copy values from "tv", don't move */
2018 int semicolon, /* from skip_var_list() */
2019 int var_count, /* from skip_var_list() */
2020 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021{
2022 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002023 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002025 listitem_T *item;
2026 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027
2028 if (*arg != '[')
2029 {
2030 /*
2031 * ":let var = expr" or ":for var in list"
2032 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 return FAIL;
2035 return OK;
2036 }
2037
2038 /*
2039 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2040 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002041 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002042 {
2043 EMSG(_(e_listreq));
2044 return FAIL;
2045 }
2046
2047 i = list_len(l);
2048 if (semicolon == 0 && var_count < i)
2049 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002050 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 return FAIL;
2052 }
2053 if (var_count - semicolon > i)
2054 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002055 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 return FAIL;
2057 }
2058
2059 item = l->lv_first;
2060 while (*arg != ']')
2061 {
2062 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002063 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 item = item->li_next;
2065 if (arg == NULL)
2066 return FAIL;
2067
2068 arg = skipwhite(arg);
2069 if (*arg == ';')
2070 {
2071 /* Put the rest of the list (may be empty) in the var after ';'.
2072 * Create a new list for this. */
2073 l = list_alloc();
2074 if (l == NULL)
2075 return FAIL;
2076 while (item != NULL)
2077 {
2078 list_append_tv(l, &item->li_tv);
2079 item = item->li_next;
2080 }
2081
2082 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002083 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 ltv.vval.v_list = l;
2085 l->lv_refcount = 1;
2086
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002087 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2088 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 clear_tv(&ltv);
2090 if (arg == NULL)
2091 return FAIL;
2092 break;
2093 }
2094 else if (*arg != ',' && *arg != ']')
2095 {
2096 EMSG2(_(e_intern2), "ex_let_vars()");
2097 return FAIL;
2098 }
2099 }
2100
2101 return OK;
2102}
2103
2104/*
2105 * Skip over assignable variable "var" or list of variables "[var, var]".
2106 * Used for ":let varvar = expr" and ":for varvar in expr".
2107 * For "[var, var]" increment "*var_count" for each variable.
2108 * for "[var, var; var]" set "semicolon".
2109 * Return NULL for an error.
2110 */
2111 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002112skip_var_list(
2113 char_u *arg,
2114 int *var_count,
2115 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002116{
2117 char_u *p, *s;
2118
2119 if (*arg == '[')
2120 {
2121 /* "[var, var]": find the matching ']'. */
2122 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002123 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 {
2125 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2126 s = skip_var_one(p);
2127 if (s == p)
2128 {
2129 EMSG2(_(e_invarg2), p);
2130 return NULL;
2131 }
2132 ++*var_count;
2133
2134 p = skipwhite(s);
2135 if (*p == ']')
2136 break;
2137 else if (*p == ';')
2138 {
2139 if (*semicolon == 1)
2140 {
2141 EMSG(_("Double ; in list of variables"));
2142 return NULL;
2143 }
2144 *semicolon = 1;
2145 }
2146 else if (*p != ',')
2147 {
2148 EMSG2(_(e_invarg2), p);
2149 return NULL;
2150 }
2151 }
2152 return p + 1;
2153 }
2154 else
2155 return skip_var_one(arg);
2156}
2157
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002159 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002160 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002161 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002164{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002165 if (*arg == '@' && arg[1] != NUL)
2166 return arg + 2;
2167 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2168 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002169}
2170
Bram Moolenaara7043832005-01-21 11:56:39 +00002171/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002172 * List variables for hashtab "ht" with prefix "prefix".
2173 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002174 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176list_hashtable_vars(
2177 hashtab_T *ht,
2178 char_u *prefix,
2179 int empty,
2180 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002181{
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 hashitem_T *hi;
2183 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 int todo;
2185
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002187 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2188 {
2189 if (!HASHITEM_EMPTY(hi))
2190 {
2191 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 di = HI2DI(hi);
2193 if (empty || di->di_tv.v_type != VAR_STRING
2194 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002196 }
2197 }
2198}
2199
2200/*
2201 * List global variables.
2202 */
2203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002204list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002205{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002207}
2208
2209/*
2210 * List buffer variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002214{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002215 char_u numbuf[NUMBUFLEN];
2216
Bram Moolenaar429fa852013-04-15 12:27:36 +02002217 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219
2220 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2222 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002223}
2224
2225/*
2226 * List window variables.
2227 */
2228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002229list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002230{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002231 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002233}
2234
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235#ifdef FEAT_WINDOWS
2236/*
2237 * List tab page variables.
2238 */
2239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002242 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002243 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002244}
2245#endif
2246
Bram Moolenaara7043832005-01-21 11:56:39 +00002247/*
2248 * List Vim variables.
2249 */
2250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002251list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254}
2255
2256/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002257 * List script-local variables, if there is a script.
2258 */
2259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002260list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002261{
2262 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2264 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002265}
2266
2267/*
2268 * List function variables, if there is a function.
2269 */
2270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002272{
2273 if (current_funccal != NULL)
2274 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002275 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002276}
2277
2278/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 * List variables in "arg".
2280 */
2281 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002282list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283{
2284 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287 char_u *name_start;
2288 char_u *arg_subsc;
2289 char_u *tofree;
2290 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291
2292 while (!ends_excmd(*arg) && !got_int)
2293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002296 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2298 {
2299 emsg_severe = TRUE;
2300 EMSG(_(e_trailing));
2301 break;
2302 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 /* get_name_len() takes care of expanding curly braces */
2307 name_start = name = arg;
2308 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2309 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 /* This is mainly to keep test 49 working: when expanding
2312 * curly braces fails overrule the exception error message. */
2313 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 emsg_severe = TRUE;
2316 EMSG2(_(e_invarg2), arg);
2317 break;
2318 }
2319 error = TRUE;
2320 }
2321 else
2322 {
2323 if (tofree != NULL)
2324 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002325 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 else
2328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 /* handle d.key, l[idx], f(expr) */
2330 arg_subsc = arg;
2331 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002339 case 'g': list_glob_vars(first); break;
2340 case 'b': list_buf_vars(first); break;
2341 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002342#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002343 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002344#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002345 case 'v': list_vim_vars(first); break;
2346 case 's': list_script_vars(first); break;
2347 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002348 default:
2349 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002350 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002351 }
2352 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002353 {
2354 char_u numbuf[NUMBUFLEN];
2355 char_u *tf;
2356 int c;
2357 char_u *s;
2358
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002359 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002360 c = *arg;
2361 *arg = NUL;
2362 list_one_var_a((char_u *)"",
2363 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002364 tv.v_type,
2365 s == NULL ? (char_u *)"" : s,
2366 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002367 *arg = c;
2368 vim_free(tf);
2369 }
2370 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002371 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372 }
2373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374
2375 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002377
2378 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380
2381 return arg;
2382}
2383
2384/*
2385 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2386 * Returns a pointer to the char just after the var name.
2387 * Returns NULL if there is an error.
2388 */
2389 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002390ex_let_one(
2391 char_u *arg, /* points to variable name */
2392 typval_T *tv, /* value to assign to variable */
2393 int copy, /* copy value from "tv" */
2394 char_u *endchars, /* valid chars after variable name or NULL */
2395 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396{
2397 int c1;
2398 char_u *name;
2399 char_u *p;
2400 char_u *arg_end = NULL;
2401 int len;
2402 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404
2405 /*
2406 * ":let $VAR = expr": Set environment variable.
2407 */
2408 if (*arg == '$')
2409 {
2410 /* Find the end of the name. */
2411 ++arg;
2412 name = arg;
2413 len = get_env_len(&arg);
2414 if (len == 0)
2415 EMSG2(_(e_invarg2), name - 1);
2416 else
2417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 if (op != NULL && (*op == '+' || *op == '-'))
2419 EMSG2(_(e_letwrong), op);
2420 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002421 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002423 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 {
2425 c1 = name[len];
2426 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 p = get_tv_string_chk(tv);
2428 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 {
2430 int mustfree = FALSE;
2431 char_u *s = vim_getenv(name, &mustfree);
2432
2433 if (s != NULL)
2434 {
2435 p = tofree = concat_str(s, p);
2436 if (mustfree)
2437 vim_free(s);
2438 }
2439 }
2440 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002443 if (STRICMP(name, "HOME") == 0)
2444 init_homedir();
2445 else if (didset_vim && STRICMP(name, "VIM") == 0)
2446 didset_vim = FALSE;
2447 else if (didset_vimruntime
2448 && STRICMP(name, "VIMRUNTIME") == 0)
2449 didset_vimruntime = FALSE;
2450 arg_end = arg;
2451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 }
2455 }
2456 }
2457
2458 /*
2459 * ":let &option = expr": Set option value.
2460 * ":let &l:option = expr": Set local option value.
2461 * ":let &g:option = expr": Set global option value.
2462 */
2463 else if (*arg == '&')
2464 {
2465 /* Find the end of the name. */
2466 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002467 if (p == NULL || (endchars != NULL
2468 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 EMSG(_(e_letunexp));
2470 else
2471 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 long n;
2473 int opt_type;
2474 long numval;
2475 char_u *stringval = NULL;
2476 char_u *s;
2477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 c1 = *p;
2479 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480
2481 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 s = get_tv_string_chk(tv); /* != NULL if number or string */
2483 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002484 {
2485 opt_type = get_option_value(arg, &numval,
2486 &stringval, opt_flags);
2487 if ((opt_type == 1 && *op == '.')
2488 || (opt_type == 0 && *op != '.'))
2489 EMSG2(_(e_letwrong), op);
2490 else
2491 {
2492 if (opt_type == 1) /* number */
2493 {
2494 if (*op == '+')
2495 n = numval + n;
2496 else
2497 n = numval - n;
2498 }
2499 else if (opt_type == 0 && stringval != NULL) /* string */
2500 {
2501 s = concat_str(stringval, s);
2502 vim_free(stringval);
2503 stringval = s;
2504 }
2505 }
2506 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 if (s != NULL)
2508 {
2509 set_option_value(arg, n, s, opt_flags);
2510 arg_end = p;
2511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 }
2515 }
2516
2517 /*
2518 * ":let @r = expr": Set register contents.
2519 */
2520 else if (*arg == '@')
2521 {
2522 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 if (op != NULL && (*op == '+' || *op == '-'))
2524 EMSG2(_(e_letwrong), op);
2525 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002526 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 EMSG(_(e_letunexp));
2528 else
2529 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002530 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002531 char_u *s;
2532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002533 p = get_tv_string_chk(tv);
2534 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002536 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 if (s != NULL)
2538 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002539 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002540 vim_free(s);
2541 }
2542 }
2543 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002546 arg_end = arg + 1;
2547 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002548 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 }
2550 }
2551
2552 /*
2553 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002558 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002560 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002562 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2564 EMSG(_(e_letunexp));
2565 else
2566 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002567 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 arg_end = p;
2569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 }
2573
2574 else
2575 EMSG2(_(e_invarg2), arg);
2576
2577 return arg_end;
2578}
2579
2580/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002581 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2582 */
2583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002584check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002585{
2586 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2587 {
2588 EMSG2(_(e_readonlyvar), arg);
2589 return TRUE;
2590 }
2591 return FALSE;
2592}
2593
2594/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 * Get an lval: variable, Dict item or List item that can be assigned a value
2596 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2597 * "name.key", "name.key[expr]" etc.
2598 * Indexing only works if "name" is an existing List or Dictionary.
2599 * "name" points to the start of the name.
2600 * If "rettv" is not NULL it points to the value to be assigned.
2601 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2602 * wrong; must end in space or cmd separator.
2603 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002604 * flags:
2605 * GLV_QUIET: do not give error messages
2606 * GLV_NO_AUTOLOAD: do not use script autoloading
2607 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002609 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 */
2612 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002613get_lval(
2614 char_u *name,
2615 typval_T *rettv,
2616 lval_T *lp,
2617 int unlet,
2618 int skip,
2619 int flags, /* GLV_ values */
2620 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 char_u *expr_start, *expr_end;
2624 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002625 dictitem_T *v;
2626 typval_T var1;
2627 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002632 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002633 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002636 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637
2638 if (skip)
2639 {
2640 /* When skipping just find the end of the name. */
2641 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002642 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 }
2644
2645 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002646 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (expr_start != NULL)
2648 {
2649 /* Don't expand the name when we already know there is an error. */
2650 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2651 && *p != '[' && *p != '.')
2652 {
2653 EMSG(_(e_trailing));
2654 return NULL;
2655 }
2656
2657 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2658 if (lp->ll_exp_name == NULL)
2659 {
2660 /* Report an invalid expression in braces, unless the
2661 * expression evaluation has been cancelled due to an
2662 * aborting error, an interrupt, or an exception. */
2663 if (!aborting() && !quiet)
2664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002665 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 EMSG2(_(e_invarg2), name);
2667 return NULL;
2668 }
2669 }
2670 lp->ll_name = lp->ll_exp_name;
2671 }
2672 else
2673 lp->ll_name = name;
2674
2675 /* Without [idx] or .key we are done. */
2676 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2677 return p;
2678
2679 cc = *p;
2680 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002681 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (v == NULL && !quiet)
2683 EMSG2(_(e_undefvar), lp->ll_name);
2684 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 if (v == NULL)
2686 return NULL;
2687
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 /*
2689 * Loop until no more [idx] or .key is following.
2690 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002691 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2695 && !(lp->ll_tv->v_type == VAR_DICT
2696 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_("E689: Can only index a List or Dictionary"));
2700 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
2705 EMSG(_("E708: [:] must come last"));
2706 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 len = -1;
2710 if (*p == '.')
2711 {
2712 key = p + 1;
2713 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2714 ;
2715 if (len == 0)
2716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
2718 EMSG(_(e_emptykey));
2719 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
2721 p = key + len;
2722 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 else
2724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (*p == ':')
2728 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 else
2730 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 empty1 = FALSE;
2732 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 if (get_tv_string_chk(&var1) == NULL)
2735 {
2736 /* not a number or string */
2737 clear_tv(&var1);
2738 return NULL;
2739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 }
2741
2742 /* Optionally get the second index [ :expr]. */
2743 if (*p == ':')
2744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002748 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 if (!empty1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (rettv != NULL && (rettv->v_type != VAR_LIST
2754 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (!quiet)
2757 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (!empty1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
2762 p = skipwhite(p + 1);
2763 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 else
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (!empty1)
2771 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002774 if (get_tv_string_chk(&var2) == NULL)
2775 {
2776 /* not a number or string */
2777 if (!empty1)
2778 clear_tv(&var1);
2779 clear_tv(&var2);
2780 return NULL;
2781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002789 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (!quiet)
2791 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 if (!empty1)
2793 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 }
2798
2799 /* Skip to past ']'. */
2800 ++p;
2801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 {
2805 if (len == -1)
2806 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002807 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002808 key = get_tv_string_chk(&var1); /* is number or string */
2809 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 }
2814 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002815 lp->ll_list = NULL;
2816 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002817 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002819 /* When assigning to a scope dictionary check that a function and
2820 * variable name is valid (only variable name unless it is l: or
2821 * g: dictionary). Disallow overwriting a builtin function. */
2822 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002824 int prevval;
2825 int wrong;
2826
2827 if (len != -1)
2828 {
2829 prevval = key[len];
2830 key[len] = NUL;
2831 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002832 else
2833 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2835 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002837 || !valid_varname(key);
2838 if (len != -1)
2839 key[len] = prevval;
2840 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002841 return NULL;
2842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002846 /* Can't add "v:" variable. */
2847 if (lp->ll_dict == &vimvardict)
2848 {
2849 EMSG2(_(e_illvar), name);
2850 return NULL;
2851 }
2852
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002853 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 if (len == -1)
2859 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 }
2862 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 if (len == -1)
2867 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 p = NULL;
2870 break;
2871 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002872 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002873 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002874 return NULL;
2875
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (len == -1)
2877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 }
2880 else
2881 {
2882 /*
2883 * Get the number and item for the only or first index of the List.
2884 */
2885 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 else
2888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002889 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 clear_tv(&var1);
2891 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002892 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 lp->ll_list = lp->ll_tv->vval.v_list;
2894 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2895 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002897 if (lp->ll_n1 < 0)
2898 {
2899 lp->ll_n1 = 0;
2900 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2901 }
2902 }
2903 if (lp->ll_li == NULL)
2904 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002906 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 if (!quiet)
2908 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002910 }
2911
2912 /*
2913 * May need to find the item or absolute index for the second
2914 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 * When no index given: "lp->ll_empty2" is TRUE.
2916 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002920 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002926 {
2927 if (!quiet)
2928 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002930 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932 }
2933
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2935 if (lp->ll_n1 < 0)
2936 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2937 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002938 {
2939 if (!quiet)
2940 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
2944
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 }
2948
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 return p;
2950}
2951
2952/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 */
2955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957{
2958 vim_free(lp->ll_exp_name);
2959 vim_free(lp->ll_newkey);
2960}
2961
2962/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002963 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 */
2967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968set_var_lval(
2969 lval_T *lp,
2970 char_u *endp,
2971 typval_T *rettv,
2972 int copy,
2973 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974{
2975 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002976 listitem_T *ri;
2977 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978
2979 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002982 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 cc = *endp;
2984 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985 if (op != NULL && *op != '=')
2986 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002987 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002988
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002989 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002990 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002991 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002992 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002993 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002994 if ((di == NULL
2995 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2996 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2997 FALSE)))
2998 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 set_var(lp->ll_name, &tv, FALSE);
3000 clear_tv(&tv);
3001 }
3002 }
3003 else
3004 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003008 else if (tv_check_lock(lp->ll_newkey == NULL
3009 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003010 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003011 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 else if (lp->ll_range)
3013 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003014 listitem_T *ll_li = lp->ll_li;
3015 int ll_n1 = lp->ll_n1;
3016
3017 /*
3018 * Check whether any of the list items is locked
3019 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003020 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003021 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003022 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003023 return;
3024 ri = ri->li_next;
3025 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3026 break;
3027 ll_li = ll_li->li_next;
3028 ++ll_n1;
3029 }
3030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /*
3032 * Assign the List values to the list items.
3033 */
3034 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (op != NULL && *op != '=')
3037 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3038 else
3039 {
3040 clear_tv(&lp->ll_li->li_tv);
3041 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 ri = ri->li_next;
3044 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3045 break;
3046 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003049 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 ri = NULL;
3052 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003053 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 lp->ll_li = lp->ll_li->li_next;
3056 ++lp->ll_n1;
3057 }
3058 if (ri != NULL)
3059 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 else if (lp->ll_empty2
3061 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 : lp->ll_n1 != lp->ll_n2)
3063 EMSG(_("E711: List value has not enough items"));
3064 }
3065 else
3066 {
3067 /*
3068 * Assign to a List or Dictionary item.
3069 */
3070 if (lp->ll_newkey != NULL)
3071 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 if (op != NULL && *op != '=')
3073 {
3074 EMSG2(_(e_letwrong), op);
3075 return;
3076 }
3077
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003079 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003080 if (di == NULL)
3081 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003082 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3083 {
3084 vim_free(di);
3085 return;
3086 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003087 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 else if (op != NULL && *op != '=')
3090 {
3091 tv_op(lp->ll_tv, rettv, op);
3092 return;
3093 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003094 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003095 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003096
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003097 /*
3098 * Assign the value to the variable or list item.
3099 */
3100 if (copy)
3101 copy_tv(rettv, lp->ll_tv);
3102 else
3103 {
3104 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003105 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003106 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107 }
3108 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109}
3110
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003111/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003112 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3113 * Returns OK or FAIL.
3114 */
3115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117{
3118 long n;
3119 char_u numbuf[NUMBUFLEN];
3120 char_u *s;
3121
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003122 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3123 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3124 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003125 {
3126 switch (tv1->v_type)
3127 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003128 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 case VAR_DICT:
3130 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003131 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003132 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003133 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003134 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 break;
3136
3137 case VAR_LIST:
3138 if (*op != '+' || tv2->v_type != VAR_LIST)
3139 break;
3140 /* List += List */
3141 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3142 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3143 return OK;
3144
3145 case VAR_NUMBER:
3146 case VAR_STRING:
3147 if (tv2->v_type == VAR_LIST)
3148 break;
3149 if (*op == '+' || *op == '-')
3150 {
3151 /* nr += nr or nr -= nr*/
3152 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153#ifdef FEAT_FLOAT
3154 if (tv2->v_type == VAR_FLOAT)
3155 {
3156 float_T f = n;
3157
3158 if (*op == '+')
3159 f += tv2->vval.v_float;
3160 else
3161 f -= tv2->vval.v_float;
3162 clear_tv(tv1);
3163 tv1->v_type = VAR_FLOAT;
3164 tv1->vval.v_float = f;
3165 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003166 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003167#endif
3168 {
3169 if (*op == '+')
3170 n += get_tv_number(tv2);
3171 else
3172 n -= get_tv_number(tv2);
3173 clear_tv(tv1);
3174 tv1->v_type = VAR_NUMBER;
3175 tv1->vval.v_number = n;
3176 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003177 }
3178 else
3179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 if (tv2->v_type == VAR_FLOAT)
3181 break;
3182
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003183 /* str .= str */
3184 s = get_tv_string(tv1);
3185 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3186 clear_tv(tv1);
3187 tv1->v_type = VAR_STRING;
3188 tv1->vval.v_string = s;
3189 }
3190 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003193#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003194 {
3195 float_T f;
3196
3197 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3198 && tv2->v_type != VAR_NUMBER
3199 && tv2->v_type != VAR_STRING))
3200 break;
3201 if (tv2->v_type == VAR_FLOAT)
3202 f = tv2->vval.v_float;
3203 else
3204 f = get_tv_number(tv2);
3205 if (*op == '+')
3206 tv1->vval.v_float += f;
3207 else
3208 tv1->vval.v_float -= f;
3209 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003211 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003212 }
3213 }
3214
3215 EMSG2(_(e_letwrong), op);
3216 return FAIL;
3217}
3218
3219/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 * Add a watcher to a list.
3221 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
3225 lw->lw_next = l->lv_watch;
3226 l->lv_watch = lw;
3227}
3228
3229/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003230 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003231 * No warning when it isn't found...
3232 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235{
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 lwp = &l->lv_watch;
3239 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3240 {
3241 if (lw == lwrem)
3242 {
3243 *lwp = lw->lw_next;
3244 break;
3245 }
3246 lwp = &lw->lw_next;
3247 }
3248}
3249
3250/*
3251 * Just before removing an item from a list: advance watchers to the next
3252 * item.
3253 */
3254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003255list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256{
Bram Moolenaar33570922005-01-25 22:26:29 +00003257 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258
3259 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3260 if (lw->lw_item == item)
3261 lw->lw_item = item->li_next;
3262}
3263
3264/*
3265 * Evaluate the expression used in a ":for var in expr" command.
3266 * "arg" points to "var".
3267 * Set "*errp" to TRUE for an error, FALSE otherwise;
3268 * Return a pointer that holds the info. Null when there is an error.
3269 */
3270 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271eval_for_line(
3272 char_u *arg,
3273 int *errp,
3274 char_u **nextcmdp,
3275 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276{
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003279 typval_T tv;
3280 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281
3282 *errp = TRUE; /* default: there is an error */
3283
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 if (fi == NULL)
3286 return NULL;
3287
3288 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3289 if (expr == NULL)
3290 return fi;
3291
3292 expr = skipwhite(expr);
3293 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3294 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003295 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 return fi;
3297 }
3298
3299 if (skip)
3300 ++emsg_skip;
3301 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3302 {
3303 *errp = FALSE;
3304 if (!skip)
3305 {
3306 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003307 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003308 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003310 clear_tv(&tv);
3311 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003312 else if (l == NULL)
3313 {
3314 /* a null list is like an empty list: do nothing */
3315 clear_tv(&tv);
3316 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 else
3318 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003319 /* No need to increment the refcount, it's already set for the
3320 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 fi->fi_list = l;
3322 list_add_watch(l, &fi->fi_lw);
3323 fi->fi_lw.lw_item = l->lv_first;
3324 }
3325 }
3326 }
3327 if (skip)
3328 --emsg_skip;
3329
3330 return fi;
3331}
3332
3333/*
3334 * Use the first item in a ":for" list. Advance to the next.
3335 * Assign the values to the variable (list). "arg" points to the first one.
3336 * Return TRUE when a valid item was found, FALSE when at end of list or
3337 * something wrong.
3338 */
3339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003340next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003342 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003344 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345
3346 item = fi->fi_lw.lw_item;
3347 if (item == NULL)
3348 result = FALSE;
3349 else
3350 {
3351 fi->fi_lw.lw_item = item->li_next;
3352 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3353 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3354 }
3355 return result;
3356}
3357
3358/*
3359 * Free the structure used to store info used by ":for".
3360 */
3361 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363{
Bram Moolenaar33570922005-01-25 22:26:29 +00003364 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003366 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003367 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003369 list_unref(fi->fi_list);
3370 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003371 vim_free(fi);
3372}
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3375
3376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003377set_context_for_expression(
3378 expand_T *xp,
3379 char_u *arg,
3380 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 int got_eq = FALSE;
3383 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003386 if (cmdidx == CMD_let)
3387 {
3388 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003389 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 {
3391 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003392 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003393 {
3394 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003396 if (vim_iswhite(*p))
3397 break;
3398 }
3399 return;
3400 }
3401 }
3402 else
3403 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3404 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 while ((xp->xp_pattern = vim_strpbrk(arg,
3406 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3407 {
3408 c = *xp->xp_pattern;
3409 if (c == '&')
3410 {
3411 c = xp->xp_pattern[1];
3412 if (c == '&')
3413 {
3414 ++xp->xp_pattern;
3415 xp->xp_context = cmdidx != CMD_let || got_eq
3416 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3417 }
3418 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003421 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3422 xp->xp_pattern += 2;
3423
3424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 }
3426 else if (c == '$')
3427 {
3428 /* environment variable */
3429 xp->xp_context = EXPAND_ENV_VARS;
3430 }
3431 else if (c == '=')
3432 {
3433 got_eq = TRUE;
3434 xp->xp_context = EXPAND_EXPRESSION;
3435 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003436 else if (c == '#'
3437 && xp->xp_context == EXPAND_EXPRESSION)
3438 {
3439 /* Autoload function/variable contains '#'. */
3440 break;
3441 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003442 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 && xp->xp_context == EXPAND_FUNCTIONS
3444 && vim_strchr(xp->xp_pattern, '(') == NULL)
3445 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003446 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 break;
3448 }
3449 else if (cmdidx != CMD_let || got_eq)
3450 {
3451 if (c == '"') /* string */
3452 {
3453 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3454 if (c == '\\' && xp->xp_pattern[1] != NUL)
3455 ++xp->xp_pattern;
3456 xp->xp_context = EXPAND_NOTHING;
3457 }
3458 else if (c == '\'') /* literal string */
3459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003460 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3462 /* skip */ ;
3463 xp->xp_context = EXPAND_NOTHING;
3464 }
3465 else if (c == '|')
3466 {
3467 if (xp->xp_pattern[1] == '|')
3468 {
3469 ++xp->xp_pattern;
3470 xp->xp_context = EXPAND_EXPRESSION;
3471 }
3472 else
3473 xp->xp_context = EXPAND_COMMANDS;
3474 }
3475 else
3476 xp->xp_context = EXPAND_EXPRESSION;
3477 }
3478 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003479 /* Doesn't look like something valid, expand as an expression
3480 * anyway. */
3481 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 arg = xp->xp_pattern;
3483 if (*arg != NUL)
3484 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3485 /* skip */ ;
3486 }
3487 xp->xp_pattern = arg;
3488}
3489
3490#endif /* FEAT_CMDL_COMPL */
3491
3492/*
3493 * ":1,25call func(arg1, arg2)" function call.
3494 */
3495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003496ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 char_u *arg = eap->arg;
3499 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003501 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003503 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 linenr_T lnum;
3505 int doesrange;
3506 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003507 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003508 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003510 if (eap->skip)
3511 {
3512 /* trans_function_name() doesn't work well when skipping, use eval0()
3513 * instead to skip to any following command, e.g. for:
3514 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003515 ++emsg_skip;
3516 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3517 clear_tv(&rettv);
3518 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003519 return;
3520 }
3521
Bram Moolenaar65639032016-03-16 21:40:30 +01003522 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003523 if (fudi.fd_newkey != NULL)
3524 {
3525 /* Still need to give an error message for missing key. */
3526 EMSG2(_(e_dictkey), fudi.fd_newkey);
3527 vim_free(fudi.fd_newkey);
3528 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003529 if (tofree == NULL)
3530 return;
3531
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003532 /* Increase refcount on dictionary, it could get deleted when evaluating
3533 * the arguments. */
3534 if (fudi.fd_dict != NULL)
3535 ++fudi.fd_dict->dv_refcount;
3536
Bram Moolenaar65639032016-03-16 21:40:30 +01003537 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3538 * contents. For VAR_PARTIAL get its partial, unless we already have one
3539 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003540 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003541 name = deref_func_name(tofree, &len,
3542 partial != NULL ? NULL : &partial, FALSE);
3543
Bram Moolenaar532c7802005-01-27 14:44:31 +00003544 /* Skip white space to allow ":call func ()". Not good, but required for
3545 * backward compatibility. */
3546 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003547 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
3549 if (*startarg != '(')
3550 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003551 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 goto end;
3553 }
3554
3555 /*
3556 * When skipping, evaluate the function once, to find the end of the
3557 * arguments.
3558 * When the function takes a range, this is discovered after the first
3559 * call, and the loop is broken.
3560 */
3561 if (eap->skip)
3562 {
3563 ++emsg_skip;
3564 lnum = eap->line2; /* do it once, also with an invalid range */
3565 }
3566 else
3567 lnum = eap->line1;
3568 for ( ; lnum <= eap->line2; ++lnum)
3569 {
3570 if (!eap->skip && eap->addr_count > 0)
3571 {
3572 curwin->w_cursor.lnum = lnum;
3573 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003574#ifdef FEAT_VIRTUALEDIT
3575 curwin->w_cursor.coladd = 0;
3576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003579 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003580 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003581 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 failed = TRUE;
3584 break;
3585 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003586
3587 /* Handle a function returning a Funcref, Dictionary or List. */
3588 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3589 {
3590 failed = TRUE;
3591 break;
3592 }
3593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (doesrange || eap->skip)
3596 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003599 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (aborting())
3603 break;
3604 }
3605 if (eap->skip)
3606 --emsg_skip;
3607
3608 if (!failed)
3609 {
3610 /* Check for trailing illegal characters and a following command. */
3611 if (!ends_excmd(*arg))
3612 {
3613 emsg_severe = TRUE;
3614 EMSG(_(e_trailing));
3615 }
3616 else
3617 eap->nextcmd = check_nextcmd(arg);
3618 }
3619
3620end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003621 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003622 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
3624
3625/*
3626 * ":unlet[!] var1 ... " command.
3627 */
3628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003629ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003631 ex_unletlock(eap, eap->arg, 0);
3632}
3633
3634/*
3635 * ":lockvar" and ":unlockvar" commands
3636 */
3637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003638ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003641 int deep = 2;
3642
3643 if (eap->forceit)
3644 deep = -1;
3645 else if (vim_isdigit(*arg))
3646 {
3647 deep = getdigits(&arg);
3648 arg = skipwhite(arg);
3649 }
3650
3651 ex_unletlock(eap, arg, deep);
3652}
3653
3654/*
3655 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3656 */
3657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658ex_unletlock(
3659 exarg_T *eap,
3660 char_u *argstart,
3661 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003662{
3663 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003666 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 do
3669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003671 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003672 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 if (lv.ll_name == NULL)
3674 error = TRUE; /* error but continue parsing */
3675 if (name_end == NULL || (!vim_iswhite(*name_end)
3676 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 if (name_end != NULL)
3679 {
3680 emsg_severe = TRUE;
3681 EMSG(_(e_trailing));
3682 }
3683 if (!(eap->skip || error))
3684 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 break;
3686 }
3687
3688 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003689 {
3690 if (eap->cmdidx == CMD_unlet)
3691 {
3692 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3693 error = TRUE;
3694 }
3695 else
3696 {
3697 if (do_lock_var(&lv, name_end, deep,
3698 eap->cmdidx == CMD_lockvar) == FAIL)
3699 error = TRUE;
3700 }
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 if (!eap->skip)
3704 clear_lval(&lv);
3705
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 arg = skipwhite(name_end);
3707 } while (!ends_excmd(*arg));
3708
3709 eap->nextcmd = check_nextcmd(arg);
3710}
3711
Bram Moolenaar8c711452005-01-14 21:53:12 +00003712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003713do_unlet_var(
3714 lval_T *lp,
3715 char_u *name_end,
3716 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003717{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 int ret = OK;
3719 int cc;
3720
3721 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 cc = *name_end;
3724 *name_end = NUL;
3725
3726 /* Normal name or expanded name. */
3727 if (check_changedtick(lp->ll_name))
3728 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003729 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003731 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003734 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003735 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003736 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003737 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003738 else if (lp->ll_range)
3739 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003740 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003741 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003742 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003743
3744 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3745 {
3746 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003747 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003748 return FAIL;
3749 ll_li = li;
3750 ++ll_n1;
3751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003752
3753 /* Delete a range of List items. */
3754 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3755 {
3756 li = lp->ll_li->li_next;
3757 listitem_remove(lp->ll_list, lp->ll_li);
3758 lp->ll_li = li;
3759 ++lp->ll_n1;
3760 }
3761 }
3762 else
3763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 /* unlet a List item. */
3766 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003769 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003770 }
3771
3772 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003773}
3774
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775/*
3776 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 */
3779 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003780do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 hashtab_T *ht;
3783 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003784 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003785 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003786 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 ht = find_var_ht(name, &varname);
3789 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003791 if (ht == &globvarht)
3792 d = &globvardict;
3793 else if (current_funccal != NULL
3794 && ht == &current_funccal->l_vars.dv_hashtab)
3795 d = &current_funccal->l_vars;
3796 else if (ht == &compat_hashtab)
3797 d = &vimvardict;
3798 else
3799 {
3800 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3801 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3802 }
3803 if (d == NULL)
3804 {
3805 EMSG2(_(e_intern2), "do_unlet()");
3806 return FAIL;
3807 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003808 hi = hash_find(ht, varname);
3809 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003810 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003811 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003812 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003813 || var_check_ro(di->di_flags, name, FALSE)
3814 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003815 return FAIL;
3816
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003817 delete_var(ht, hi);
3818 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821 if (forceit)
3822 return OK;
3823 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 return FAIL;
3825}
3826
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003827/*
3828 * Lock or unlock variable indicated by "lp".
3829 * "deep" is the levels to go (-1 for unlimited);
3830 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3831 */
3832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003833do_lock_var(
3834 lval_T *lp,
3835 char_u *name_end,
3836 int deep,
3837 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003838{
3839 int ret = OK;
3840 int cc;
3841 dictitem_T *di;
3842
3843 if (deep == 0) /* nothing to do */
3844 return OK;
3845
3846 if (lp->ll_tv == NULL)
3847 {
3848 cc = *name_end;
3849 *name_end = NUL;
3850
3851 /* Normal name or expanded name. */
3852 if (check_changedtick(lp->ll_name))
3853 ret = FAIL;
3854 else
3855 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003856 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003857 if (di == NULL)
3858 ret = FAIL;
3859 else
3860 {
3861 if (lock)
3862 di->di_flags |= DI_FLAGS_LOCK;
3863 else
3864 di->di_flags &= ~DI_FLAGS_LOCK;
3865 item_lock(&di->di_tv, deep, lock);
3866 }
3867 }
3868 *name_end = cc;
3869 }
3870 else if (lp->ll_range)
3871 {
3872 listitem_T *li = lp->ll_li;
3873
3874 /* (un)lock a range of List items. */
3875 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3876 {
3877 item_lock(&li->li_tv, deep, lock);
3878 li = li->li_next;
3879 ++lp->ll_n1;
3880 }
3881 }
3882 else if (lp->ll_list != NULL)
3883 /* (un)lock a List item. */
3884 item_lock(&lp->ll_li->li_tv, deep, lock);
3885 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003886 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003887 item_lock(&lp->ll_di->di_tv, deep, lock);
3888
3889 return ret;
3890}
3891
3892/*
3893 * Lock or unlock an item. "deep" is nr of levels to go.
3894 */
3895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003896item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003897{
3898 static int recurse = 0;
3899 list_T *l;
3900 listitem_T *li;
3901 dict_T *d;
3902 hashitem_T *hi;
3903 int todo;
3904
3905 if (recurse >= DICT_MAXNEST)
3906 {
3907 EMSG(_("E743: variable nested too deep for (un)lock"));
3908 return;
3909 }
3910 if (deep == 0)
3911 return;
3912 ++recurse;
3913
3914 /* lock/unlock the item itself */
3915 if (lock)
3916 tv->v_lock |= VAR_LOCKED;
3917 else
3918 tv->v_lock &= ~VAR_LOCKED;
3919
3920 switch (tv->v_type)
3921 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003922 case VAR_UNKNOWN:
3923 case VAR_NUMBER:
3924 case VAR_STRING:
3925 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003926 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003927 case VAR_FLOAT:
3928 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003929 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003930 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003931 break;
3932
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003933 case VAR_LIST:
3934 if ((l = tv->vval.v_list) != NULL)
3935 {
3936 if (lock)
3937 l->lv_lock |= VAR_LOCKED;
3938 else
3939 l->lv_lock &= ~VAR_LOCKED;
3940 if (deep < 0 || deep > 1)
3941 /* recursive: lock/unlock the items the List contains */
3942 for (li = l->lv_first; li != NULL; li = li->li_next)
3943 item_lock(&li->li_tv, deep - 1, lock);
3944 }
3945 break;
3946 case VAR_DICT:
3947 if ((d = tv->vval.v_dict) != NULL)
3948 {
3949 if (lock)
3950 d->dv_lock |= VAR_LOCKED;
3951 else
3952 d->dv_lock &= ~VAR_LOCKED;
3953 if (deep < 0 || deep > 1)
3954 {
3955 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003956 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003957 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3958 {
3959 if (!HASHITEM_EMPTY(hi))
3960 {
3961 --todo;
3962 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3963 }
3964 }
3965 }
3966 }
3967 }
3968 --recurse;
3969}
3970
Bram Moolenaara40058a2005-07-11 22:42:07 +00003971/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003972 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3973 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003974 */
3975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003976tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003977{
3978 return (tv->v_lock & VAR_LOCKED)
3979 || (tv->v_type == VAR_LIST
3980 && tv->vval.v_list != NULL
3981 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3982 || (tv->v_type == VAR_DICT
3983 && tv->vval.v_dict != NULL
3984 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3985}
3986
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3988/*
3989 * Delete all "menutrans_" variables.
3990 */
3991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993{
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003995 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003998 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003999 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004000 {
4001 if (!HASHITEM_EMPTY(hi))
4002 {
4003 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004004 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4005 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004006 }
4007 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004008 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009}
4010#endif
4011
4012#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4013
4014/*
4015 * Local string buffer for the next two functions to store a variable name
4016 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4017 * get_user_var_name().
4018 */
4019
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004020static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022static char_u *varnamebuf = NULL;
4023static int varnamebuflen = 0;
4024
4025/*
4026 * Function to concatenate a prefix and a variable name.
4027 */
4028 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004029cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
4031 int len;
4032
4033 len = (int)STRLEN(name) + 3;
4034 if (len > varnamebuflen)
4035 {
4036 vim_free(varnamebuf);
4037 len += 10; /* some additional space */
4038 varnamebuf = alloc(len);
4039 if (varnamebuf == NULL)
4040 {
4041 varnamebuflen = 0;
4042 return NULL;
4043 }
4044 varnamebuflen = len;
4045 }
4046 *varnamebuf = prefix;
4047 varnamebuf[1] = ':';
4048 STRCPY(varnamebuf + 2, name);
4049 return varnamebuf;
4050}
4051
4052/*
4053 * Function given to ExpandGeneric() to obtain the list of user defined
4054 * (global/buffer/window/built-in) variable names.
4055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004057get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 static long_u gdone;
4060 static long_u bdone;
4061 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004062#ifdef FEAT_WINDOWS
4063 static long_u tdone;
4064#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004065 static int vidx;
4066 static hashitem_T *hi;
4067 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004071 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072#ifdef FEAT_WINDOWS
4073 tdone = 0;
4074#endif
4075 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004076
4077 /* Global variables */
4078 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004080 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004082 else
4083 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004084 while (HASHITEM_EMPTY(hi))
4085 ++hi;
4086 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4087 return cat_prefix_varname('g', hi->hi_key);
4088 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004090
4091 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004092 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004095 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004097 else
4098 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004099 while (HASHITEM_EMPTY(hi))
4100 ++hi;
4101 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004103 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004105 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 return (char_u *)"b:changedtick";
4107 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004108
4109 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004110 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004113 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004114 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004115 else
4116 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004117 while (HASHITEM_EMPTY(hi))
4118 ++hi;
4119 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004121
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122#ifdef FEAT_WINDOWS
4123 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004124 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004125 if (tdone < ht->ht_used)
4126 {
4127 if (tdone++ == 0)
4128 hi = ht->ht_array;
4129 else
4130 ++hi;
4131 while (HASHITEM_EMPTY(hi))
4132 ++hi;
4133 return cat_prefix_varname('t', hi->hi_key);
4134 }
4135#endif
4136
Bram Moolenaar33570922005-01-25 22:26:29 +00004137 /* v: variables */
4138 if (vidx < VV_LEN)
4139 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140
4141 vim_free(varnamebuf);
4142 varnamebuf = NULL;
4143 varnamebuflen = 0;
4144 return NULL;
4145}
4146
4147#endif /* FEAT_CMDL_COMPL */
4148
4149/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004150 * Return TRUE if "pat" matches "text".
4151 * Does not use 'cpo' and always uses 'magic'.
4152 */
4153 static int
4154pattern_match(char_u *pat, char_u *text, int ic)
4155{
4156 int matches = FALSE;
4157 char_u *save_cpo;
4158 regmatch_T regmatch;
4159
4160 /* avoid 'l' flag in 'cpoptions' */
4161 save_cpo = p_cpo;
4162 p_cpo = (char_u *)"";
4163 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4164 if (regmatch.regprog != NULL)
4165 {
4166 regmatch.rm_ic = ic;
4167 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4168 vim_regfree(regmatch.regprog);
4169 }
4170 p_cpo = save_cpo;
4171 return matches;
4172}
4173
4174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 * types for expressions.
4176 */
4177typedef enum
4178{
4179 TYPE_UNKNOWN = 0
4180 , TYPE_EQUAL /* == */
4181 , TYPE_NEQUAL /* != */
4182 , TYPE_GREATER /* > */
4183 , TYPE_GEQUAL /* >= */
4184 , TYPE_SMALLER /* < */
4185 , TYPE_SEQUAL /* <= */
4186 , TYPE_MATCH /* =~ */
4187 , TYPE_NOMATCH /* !~ */
4188} exptype_T;
4189
4190/*
4191 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4194 */
4195
4196/*
4197 * Handle zero level expression.
4198 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004200 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 * Return OK or FAIL.
4202 */
4203 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004204eval0(
4205 char_u *arg,
4206 typval_T *rettv,
4207 char_u **nextcmd,
4208 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 int ret;
4211 char_u *p;
4212
4213 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 if (ret == FAIL || !ends_excmd(*p))
4216 {
4217 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 /*
4220 * Report the invalid expression unless the expression evaluation has
4221 * been cancelled due to an aborting error, an interrupt, or an
4222 * exception.
4223 */
4224 if (!aborting())
4225 EMSG2(_(e_invexpr2), arg);
4226 ret = FAIL;
4227 }
4228 if (nextcmd != NULL)
4229 *nextcmd = check_nextcmd(p);
4230
4231 return ret;
4232}
4233
4234/*
4235 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004236 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 *
4238 * "arg" must point to the first non-white of the expression.
4239 * "arg" is advanced to the next non-white after the recognized expression.
4240 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004241 * Note: "rettv.v_lock" is not set.
4242 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 * Return OK or FAIL.
4244 */
4245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004249 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * Get the first variable.
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 if ((*arg)[0] == '?')
4258 {
4259 result = FALSE;
4260 if (evaluate)
4261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 int error = FALSE;
4263
4264 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004267 if (error)
4268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270
4271 /*
4272 * Get the second variable.
4273 */
4274 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 return FAIL;
4277
4278 /*
4279 * Check for the ":".
4280 */
4281 if ((*arg)[0] != ':')
4282 {
4283 EMSG(_("E109: Missing ':' after '?'"));
4284 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 return FAIL;
4287 }
4288
4289 /*
4290 * Get the third variable.
4291 */
4292 *arg = skipwhite(*arg + 1);
4293 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4294 {
4295 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 return FAIL;
4298 }
4299 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302
4303 return OK;
4304}
4305
4306/*
4307 * Handle first level expression:
4308 * expr2 || expr2 || expr2 logical OR
4309 *
4310 * "arg" must point to the first non-white of the expression.
4311 * "arg" is advanced to the next non-white after the recognized expression.
4312 *
4313 * Return OK or FAIL.
4314 */
4315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004316eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 long result;
4320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat until there is no following "||".
4331 */
4332 first = TRUE;
4333 result = FALSE;
4334 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4335 {
4336 if (evaluate && first)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 first = FALSE;
4344 }
4345
4346 /*
4347 * Get the second variable.
4348 */
4349 *arg = skipwhite(*arg + 2);
4350 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4351 return FAIL;
4352
4353 /*
4354 * Compute the result.
4355 */
4356 if (evaluate && !result)
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (error)
4362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 if (evaluate)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle second level expression:
4376 * expr3 && expr3 && expr3 logical AND
4377 *
4378 * "arg" must point to the first non-white of the expression.
4379 * "arg" is advanced to the next non-white after the recognized expression.
4380 *
4381 * Return OK or FAIL.
4382 */
4383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004384eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
Bram Moolenaar33570922005-01-25 22:26:29 +00004386 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 long result;
4388 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 /*
4392 * Get the first variable.
4393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 return FAIL;
4396
4397 /*
4398 * Repeat until there is no following "&&".
4399 */
4400 first = TRUE;
4401 result = TRUE;
4402 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4403 {
4404 if (evaluate && first)
4405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 if (error)
4410 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 first = FALSE;
4412 }
4413
4414 /*
4415 * Get the second variable.
4416 */
4417 *arg = skipwhite(*arg + 2);
4418 if (eval4(arg, &var2, evaluate && result) == FAIL)
4419 return FAIL;
4420
4421 /*
4422 * Compute the result.
4423 */
4424 if (evaluate && result)
4425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004428 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004429 if (error)
4430 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 if (evaluate)
4433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004434 rettv->v_type = VAR_NUMBER;
4435 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 }
4438
4439 return OK;
4440}
4441
4442/*
4443 * Handle third level expression:
4444 * var1 == var2
4445 * var1 =~ var2
4446 * var1 != var2
4447 * var1 !~ var2
4448 * var1 > var2
4449 * var1 >= var2
4450 * var1 < var2
4451 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 * var1 is var2
4453 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 *
4455 * "arg" must point to the first non-white of the expression.
4456 * "arg" is advanced to the next non-white after the recognized expression.
4457 *
4458 * Return OK or FAIL.
4459 */
4460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004461eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
Bram Moolenaar33570922005-01-25 22:26:29 +00004463 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 char_u *p;
4465 int i;
4466 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 int len = 2;
4469 long n1, n2;
4470 char_u *s1, *s2;
4471 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /*
4475 * Get the first variable.
4476 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004477 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return FAIL;
4479
4480 p = *arg;
4481 switch (p[0])
4482 {
4483 case '=': if (p[1] == '=')
4484 type = TYPE_EQUAL;
4485 else if (p[1] == '~')
4486 type = TYPE_MATCH;
4487 break;
4488 case '!': if (p[1] == '=')
4489 type = TYPE_NEQUAL;
4490 else if (p[1] == '~')
4491 type = TYPE_NOMATCH;
4492 break;
4493 case '>': if (p[1] != '=')
4494 {
4495 type = TYPE_GREATER;
4496 len = 1;
4497 }
4498 else
4499 type = TYPE_GEQUAL;
4500 break;
4501 case '<': if (p[1] != '=')
4502 {
4503 type = TYPE_SMALLER;
4504 len = 1;
4505 }
4506 else
4507 type = TYPE_SEQUAL;
4508 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 case 'i': if (p[1] == 's')
4510 {
4511 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4512 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004513 i = p[len];
4514 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 {
4516 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4517 type_is = TRUE;
4518 }
4519 }
4520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522
4523 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004524 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 */
4526 if (type != TYPE_UNKNOWN)
4527 {
4528 /* extra question mark appended: ignore case */
4529 if (p[len] == '?')
4530 {
4531 ic = TRUE;
4532 ++len;
4533 }
4534 /* extra '#' appended: match case */
4535 else if (p[len] == '#')
4536 {
4537 ic = FALSE;
4538 ++len;
4539 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 else
4542 ic = p_ic;
4543
4544 /*
4545 * Get the second variable.
4546 */
4547 *arg = skipwhite(p + len);
4548 if (eval5(arg, &var2, evaluate) == FAIL)
4549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004550 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 return FAIL;
4552 }
4553
4554 if (evaluate)
4555 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 if (type_is && rettv->v_type != var2.v_type)
4557 {
4558 /* For "is" a different type always means FALSE, for "notis"
4559 * it means TRUE. */
4560 n1 = (type == TYPE_NEQUAL);
4561 }
4562 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4563 {
4564 if (type_is)
4565 {
4566 n1 = (rettv->v_type == var2.v_type
4567 && rettv->vval.v_list == var2.vval.v_list);
4568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
4570 }
4571 else if (rettv->v_type != var2.v_type
4572 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4573 {
4574 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004575 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004577 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 clear_tv(rettv);
4579 clear_tv(&var2);
4580 return FAIL;
4581 }
4582 else
4583 {
4584 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004585 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4586 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004587 if (type == TYPE_NEQUAL)
4588 n1 = !n1;
4589 }
4590 }
4591
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004592 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4593 {
4594 if (type_is)
4595 {
4596 n1 = (rettv->v_type == var2.v_type
4597 && rettv->vval.v_dict == var2.vval.v_dict);
4598 if (type == TYPE_NEQUAL)
4599 n1 = !n1;
4600 }
4601 else if (rettv->v_type != var2.v_type
4602 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4603 {
4604 if (rettv->v_type != var2.v_type)
4605 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4606 else
4607 EMSG(_("E736: Invalid operation for Dictionary"));
4608 clear_tv(rettv);
4609 clear_tv(&var2);
4610 return FAIL;
4611 }
4612 else
4613 {
4614 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004615 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4616 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004617 if (type == TYPE_NEQUAL)
4618 n1 = !n1;
4619 }
4620 }
4621
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004622 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4623 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004625 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004626 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004627 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004628 clear_tv(rettv);
4629 clear_tv(&var2);
4630 return FAIL;
4631 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004632 if ((rettv->v_type == VAR_PARTIAL
4633 && rettv->vval.v_partial == NULL)
4634 || (var2.v_type == VAR_PARTIAL
4635 && var2.vval.v_partial == NULL))
4636 /* when a partial is NULL assume not equal */
4637 n1 = FALSE;
4638 else if (type_is)
4639 {
4640 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4641 /* strings are considered the same if their value is
4642 * the same */
4643 n1 = tv_equal(rettv, &var2, ic, FALSE);
4644 else if (rettv->v_type == VAR_PARTIAL
4645 && var2.v_type == VAR_PARTIAL)
4646 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4647 else
4648 n1 = FALSE;
4649 }
4650 else
4651 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004652 if (type == TYPE_NEQUAL)
4653 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004654 }
4655
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004656#ifdef FEAT_FLOAT
4657 /*
4658 * If one of the two variables is a float, compare as a float.
4659 * When using "=~" or "!~", always compare as string.
4660 */
4661 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4662 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4663 {
4664 float_T f1, f2;
4665
4666 if (rettv->v_type == VAR_FLOAT)
4667 f1 = rettv->vval.v_float;
4668 else
4669 f1 = get_tv_number(rettv);
4670 if (var2.v_type == VAR_FLOAT)
4671 f2 = var2.vval.v_float;
4672 else
4673 f2 = get_tv_number(&var2);
4674 n1 = FALSE;
4675 switch (type)
4676 {
4677 case TYPE_EQUAL: n1 = (f1 == f2); break;
4678 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4679 case TYPE_GREATER: n1 = (f1 > f2); break;
4680 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4681 case TYPE_SMALLER: n1 = (f1 < f2); break;
4682 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4683 case TYPE_UNKNOWN:
4684 case TYPE_MATCH:
4685 case TYPE_NOMATCH: break; /* avoid gcc warning */
4686 }
4687 }
4688#endif
4689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 /*
4691 * If one of the two variables is a number, compare as a number.
4692 * When using "=~" or "!~", always compare as string.
4693 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004694 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004697 n1 = get_tv_number(rettv);
4698 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 switch (type)
4700 {
4701 case TYPE_EQUAL: n1 = (n1 == n2); break;
4702 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4703 case TYPE_GREATER: n1 = (n1 > n2); break;
4704 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4705 case TYPE_SMALLER: n1 = (n1 < n2); break;
4706 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4707 case TYPE_UNKNOWN:
4708 case TYPE_MATCH:
4709 case TYPE_NOMATCH: break; /* avoid gcc warning */
4710 }
4711 }
4712 else
4713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004714 s1 = get_tv_string_buf(rettv, buf1);
4715 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4717 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4718 else
4719 i = 0;
4720 n1 = FALSE;
4721 switch (type)
4722 {
4723 case TYPE_EQUAL: n1 = (i == 0); break;
4724 case TYPE_NEQUAL: n1 = (i != 0); break;
4725 case TYPE_GREATER: n1 = (i > 0); break;
4726 case TYPE_GEQUAL: n1 = (i >= 0); break;
4727 case TYPE_SMALLER: n1 = (i < 0); break;
4728 case TYPE_SEQUAL: n1 = (i <= 0); break;
4729
4730 case TYPE_MATCH:
4731 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004732 n1 = pattern_match(s2, s1, ic);
4733 if (type == TYPE_NOMATCH)
4734 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 break;
4736
4737 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4738 }
4739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004740 clear_tv(rettv);
4741 clear_tv(&var2);
4742 rettv->v_type = VAR_NUMBER;
4743 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 }
4746
4747 return OK;
4748}
4749
4750/*
4751 * Handle fourth level expression:
4752 * + number addition
4753 * - number subtraction
4754 * . string concatenation
4755 *
4756 * "arg" must point to the first non-white of the expression.
4757 * "arg" is advanced to the next non-white after the recognized expression.
4758 *
4759 * Return OK or FAIL.
4760 */
4761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004762eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
Bram Moolenaar33570922005-01-25 22:26:29 +00004764 typval_T var2;
4765 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 int op;
4767 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768#ifdef FEAT_FLOAT
4769 float_T f1 = 0, f2 = 0;
4770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 char_u *s1, *s2;
4772 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4773 char_u *p;
4774
4775 /*
4776 * Get the first variable.
4777 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004778 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 return FAIL;
4780
4781 /*
4782 * Repeat computing, until no '+', '-' or '.' is following.
4783 */
4784 for (;;)
4785 {
4786 op = **arg;
4787 if (op != '+' && op != '-' && op != '.')
4788 break;
4789
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 if ((op != '+' || rettv->v_type != VAR_LIST)
4791#ifdef FEAT_FLOAT
4792 && (op == '.' || rettv->v_type != VAR_FLOAT)
4793#endif
4794 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 {
4796 /* For "list + ...", an illegal use of the first operand as
4797 * a number cannot be determined before evaluating the 2nd
4798 * operand: if this is also a list, all is ok.
4799 * For "something . ...", "something - ..." or "non-list + ...",
4800 * we know that the first operand needs to be a string or number
4801 * without evaluating the 2nd operand. So check before to avoid
4802 * side effects after an error. */
4803 if (evaluate && get_tv_string_chk(rettv) == NULL)
4804 {
4805 clear_tv(rettv);
4806 return FAIL;
4807 }
4808 }
4809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 /*
4811 * Get the second variable.
4812 */
4813 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004814 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004816 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return FAIL;
4818 }
4819
4820 if (evaluate)
4821 {
4822 /*
4823 * Compute the result.
4824 */
4825 if (op == '.')
4826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4828 s2 = get_tv_string_buf_chk(&var2, buf2);
4829 if (s2 == NULL) /* type error ? */
4830 {
4831 clear_tv(rettv);
4832 clear_tv(&var2);
4833 return FAIL;
4834 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004835 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
4837 rettv->v_type = VAR_STRING;
4838 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004840 else if (op == '+' && rettv->v_type == VAR_LIST
4841 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004842 {
4843 /* concatenate Lists */
4844 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4845 &var3) == FAIL)
4846 {
4847 clear_tv(rettv);
4848 clear_tv(&var2);
4849 return FAIL;
4850 }
4851 clear_tv(rettv);
4852 *rettv = var3;
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 else
4855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 int error = FALSE;
4857
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858#ifdef FEAT_FLOAT
4859 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004860 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861 f1 = rettv->vval.v_float;
4862 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004863 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004864 else
4865#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004866 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867 n1 = get_tv_number_chk(rettv, &error);
4868 if (error)
4869 {
4870 /* This can only happen for "list + non-list". For
4871 * "non-list + ..." or "something - ...", we returned
4872 * before evaluating the 2nd operand. */
4873 clear_tv(rettv);
4874 return FAIL;
4875 }
4876#ifdef FEAT_FLOAT
4877 if (var2.v_type == VAR_FLOAT)
4878 f1 = n1;
4879#endif
4880 }
4881#ifdef FEAT_FLOAT
4882 if (var2.v_type == VAR_FLOAT)
4883 {
4884 f2 = var2.vval.v_float;
4885 n2 = 0;
4886 }
4887 else
4888#endif
4889 {
4890 n2 = get_tv_number_chk(&var2, &error);
4891 if (error)
4892 {
4893 clear_tv(rettv);
4894 clear_tv(&var2);
4895 return FAIL;
4896 }
4897#ifdef FEAT_FLOAT
4898 if (rettv->v_type == VAR_FLOAT)
4899 f2 = n2;
4900#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004902 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903
4904#ifdef FEAT_FLOAT
4905 /* If there is a float on either side the result is a float. */
4906 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4907 {
4908 if (op == '+')
4909 f1 = f1 + f2;
4910 else
4911 f1 = f1 - f2;
4912 rettv->v_type = VAR_FLOAT;
4913 rettv->vval.v_float = f1;
4914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916#endif
4917 {
4918 if (op == '+')
4919 n1 = n1 + n2;
4920 else
4921 n1 = n1 - n2;
4922 rettv->v_type = VAR_NUMBER;
4923 rettv->vval.v_number = n1;
4924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 }
4929 return OK;
4930}
4931
4932/*
4933 * Handle fifth level expression:
4934 * * number multiplication
4935 * / number division
4936 * % number modulo
4937 *
4938 * "arg" must point to the first non-white of the expression.
4939 * "arg" is advanced to the next non-white after the recognized expression.
4940 *
4941 * Return OK or FAIL.
4942 */
4943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004944eval6(
4945 char_u **arg,
4946 typval_T *rettv,
4947 int evaluate,
4948 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
Bram Moolenaar33570922005-01-25 22:26:29 +00004950 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 int op;
4952 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#ifdef FEAT_FLOAT
4954 int use_float = FALSE;
4955 float_T f1 = 0, f2;
4956#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004957 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958
4959 /*
4960 * Get the first variable.
4961 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004962 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return FAIL;
4964
4965 /*
4966 * Repeat computing, until no '*', '/' or '%' is following.
4967 */
4968 for (;;)
4969 {
4970 op = **arg;
4971 if (op != '*' && op != '/' && op != '%')
4972 break;
4973
4974 if (evaluate)
4975 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976#ifdef FEAT_FLOAT
4977 if (rettv->v_type == VAR_FLOAT)
4978 {
4979 f1 = rettv->vval.v_float;
4980 use_float = TRUE;
4981 n1 = 0;
4982 }
4983 else
4984#endif
4985 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004987 if (error)
4988 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 else
4991 n1 = 0;
4992
4993 /*
4994 * Get the second variable.
4995 */
4996 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004997 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return FAIL;
4999
5000 if (evaluate)
5001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002#ifdef FEAT_FLOAT
5003 if (var2.v_type == VAR_FLOAT)
5004 {
5005 if (!use_float)
5006 {
5007 f1 = n1;
5008 use_float = TRUE;
5009 }
5010 f2 = var2.vval.v_float;
5011 n2 = 0;
5012 }
5013 else
5014#endif
5015 {
5016 n2 = get_tv_number_chk(&var2, &error);
5017 clear_tv(&var2);
5018 if (error)
5019 return FAIL;
5020#ifdef FEAT_FLOAT
5021 if (use_float)
5022 f2 = n2;
5023#endif
5024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
5026 /*
5027 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030#ifdef FEAT_FLOAT
5031 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 if (op == '*')
5034 f1 = f1 * f2;
5035 else if (op == '/')
5036 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005037# ifdef VMS
5038 /* VMS crashes on divide by zero, work around it */
5039 if (f2 == 0.0)
5040 {
5041 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005042 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005043 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005044 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005045 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005046 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005047 }
5048 else
5049 f1 = f1 / f2;
5050# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005051 /* We rely on the floating point library to handle divide
5052 * by zero to result in "inf" and not a crash. */
5053 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005054# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005057 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005058 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005059 return FAIL;
5060 }
5061 rettv->v_type = VAR_FLOAT;
5062 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 if (op == '*')
5068 n1 = n1 * n2;
5069 else if (op == '/')
5070 {
5071 if (n2 == 0) /* give an error message? */
5072 {
5073 if (n1 == 0)
5074 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5075 else if (n1 < 0)
5076 n1 = -0x7fffffffL;
5077 else
5078 n1 = 0x7fffffffL;
5079 }
5080 else
5081 n1 = n1 / n2;
5082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005084 {
5085 if (n2 == 0) /* give an error message? */
5086 n1 = 0;
5087 else
5088 n1 = n1 % n2;
5089 }
5090 rettv->v_type = VAR_NUMBER;
5091 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 }
5095
5096 return OK;
5097}
5098
5099/*
5100 * Handle sixth level expression:
5101 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005102 * "string" string constant
5103 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 * &option-name option value
5105 * @r register contents
5106 * identifier variable value
5107 * function() function call
5108 * $VAR environment variable
5109 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005110 * [expr, expr] List
5111 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 *
5113 * Also handle:
5114 * ! in front logical NOT
5115 * - in front unary minus
5116 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005117 * trailing [] subscript in String or List
5118 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 *
5120 * "arg" must point to the first non-white of the expression.
5121 * "arg" is advanced to the next non-white after the recognized expression.
5122 *
5123 * Return OK or FAIL.
5124 */
5125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005126eval7(
5127 char_u **arg,
5128 typval_T *rettv,
5129 int evaluate,
5130 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 long n;
5133 int len;
5134 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 char_u *start_leader, *end_leader;
5136 int ret = OK;
5137 char_u *alias;
5138
5139 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005140 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005141 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * Skip '!' and '-' characters. They are handled later.
5147 */
5148 start_leader = *arg;
5149 while (**arg == '!' || **arg == '-' || **arg == '+')
5150 *arg = skipwhite(*arg + 1);
5151 end_leader = *arg;
5152
5153 switch (**arg)
5154 {
5155 /*
5156 * Number constant.
5157 */
5158 case '0':
5159 case '1':
5160 case '2':
5161 case '3':
5162 case '4':
5163 case '5':
5164 case '6':
5165 case '7':
5166 case '8':
5167 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 {
5169#ifdef FEAT_FLOAT
5170 char_u *p = skipdigits(*arg + 1);
5171 int get_float = FALSE;
5172
5173 /* We accept a float when the format matches
5174 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005175 * strict to avoid backwards compatibility problems.
5176 * Don't look for a float after the "." operator, so that
5177 * ":let vers = 1.2.3" doesn't fail. */
5178 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005180 get_float = TRUE;
5181 p = skipdigits(p + 2);
5182 if (*p == 'e' || *p == 'E')
5183 {
5184 ++p;
5185 if (*p == '-' || *p == '+')
5186 ++p;
5187 if (!vim_isdigit(*p))
5188 get_float = FALSE;
5189 else
5190 p = skipdigits(p + 1);
5191 }
5192 if (ASCII_ISALPHA(*p) || *p == '.')
5193 get_float = FALSE;
5194 }
5195 if (get_float)
5196 {
5197 float_T f;
5198
5199 *arg += string2float(*arg, &f);
5200 if (evaluate)
5201 {
5202 rettv->v_type = VAR_FLOAT;
5203 rettv->vval.v_float = f;
5204 }
5205 }
5206 else
5207#endif
5208 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005209 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005210 *arg += len;
5211 if (evaluate)
5212 {
5213 rettv->v_type = VAR_NUMBER;
5214 rettv->vval.v_number = n;
5215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220 /*
5221 * String constant: "string".
5222 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005223 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 break;
5225
5226 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005229 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005230 break;
5231
5232 /*
5233 * List: [expr, expr]
5234 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 break;
5237
5238 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 * Dictionary: {key: val, key: val}
5240 */
5241 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5242 break;
5243
5244 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005245 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005247 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 break;
5249
5250 /*
5251 * Environment variable: $VAR.
5252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 break;
5255
5256 /*
5257 * Register contents: @r.
5258 */
5259 case '@': ++*arg;
5260 if (evaluate)
5261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005263 rettv->vval.v_string = get_reg_contents(**arg,
5264 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 }
5266 if (**arg != NUL)
5267 ++*arg;
5268 break;
5269
5270 /*
5271 * nested expression: (expression).
5272 */
5273 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (**arg == ')')
5276 ++*arg;
5277 else if (ret == OK)
5278 {
5279 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 ret = FAIL;
5282 }
5283 break;
5284
Bram Moolenaar8c711452005-01-14 21:53:12 +00005285 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 break;
5287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005288
5289 if (ret == NOTDONE)
5290 {
5291 /*
5292 * Must be a variable or function name.
5293 * Can also be a curly-braces kind of name: {expr}.
5294 */
5295 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 if (alias != NULL)
5298 s = alias;
5299
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005300 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 ret = FAIL;
5302 else
5303 {
5304 if (**arg == '(') /* recursive! */
5305 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005306 partial_T *partial;
5307
Bram Moolenaar8c711452005-01-14 21:53:12 +00005308 /* If "s" is the name of a variable of type VAR_FUNC
5309 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005310 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311
5312 /* Invoke the function. */
5313 ret = get_func_tv(s, len, rettv, arg,
5314 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005315 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005316
5317 /* If evaluate is FALSE rettv->v_type was not set in
5318 * get_func_tv, but it's needed in handle_subscript() to parse
5319 * what follows. So set it here. */
5320 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5321 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005322 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005323 rettv->v_type = VAR_FUNC;
5324 }
5325
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 /* Stop the expression evaluation when immediately
5327 * aborting on error, or when an interrupt occurred or
5328 * an exception was thrown but not caught. */
5329 if (aborting())
5330 {
5331 if (ret == OK)
5332 clear_tv(rettv);
5333 ret = FAIL;
5334 }
5335 }
5336 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005337 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005338 else
5339 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005341 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 }
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 *arg = skipwhite(*arg);
5345
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005346 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5347 * expr(expr). */
5348 if (ret == OK)
5349 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 /*
5352 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5353 */
5354 if (ret == OK && evaluate && end_leader > start_leader)
5355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005357 int val = 0;
5358#ifdef FEAT_FLOAT
5359 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005360
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005361 if (rettv->v_type == VAR_FLOAT)
5362 f = rettv->vval.v_float;
5363 else
5364#endif
5365 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005366 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005368 clear_tv(rettv);
5369 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005371 else
5372 {
5373 while (end_leader > start_leader)
5374 {
5375 --end_leader;
5376 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005377 {
5378#ifdef FEAT_FLOAT
5379 if (rettv->v_type == VAR_FLOAT)
5380 f = !f;
5381 else
5382#endif
5383 val = !val;
5384 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005386 {
5387#ifdef FEAT_FLOAT
5388 if (rettv->v_type == VAR_FLOAT)
5389 f = -f;
5390 else
5391#endif
5392 val = -val;
5393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005394 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005395#ifdef FEAT_FLOAT
5396 if (rettv->v_type == VAR_FLOAT)
5397 {
5398 clear_tv(rettv);
5399 rettv->vval.v_float = f;
5400 }
5401 else
5402#endif
5403 {
5404 clear_tv(rettv);
5405 rettv->v_type = VAR_NUMBER;
5406 rettv->vval.v_number = val;
5407 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410
5411 return ret;
5412}
5413
5414/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005415 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5416 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5418 */
5419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005420eval_index(
5421 char_u **arg,
5422 typval_T *rettv,
5423 int evaluate,
5424 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425{
5426 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005427 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005429 long len = -1;
5430 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433
Bram Moolenaara03f2332016-02-06 18:09:59 +01005434 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005436 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005437 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005438 if (verbose)
5439 EMSG(_("E695: Cannot index a Funcref"));
5440 return FAIL;
5441 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005442#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005443 if (verbose)
5444 EMSG(_(e_float_as_string));
5445 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005446#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005447 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005448 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005449 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005450 if (verbose)
5451 EMSG(_("E909: Cannot index a special variable"));
5452 return FAIL;
5453 case VAR_UNKNOWN:
5454 if (evaluate)
5455 return FAIL;
5456 /* FALLTHROUGH */
5457
5458 case VAR_STRING:
5459 case VAR_NUMBER:
5460 case VAR_LIST:
5461 case VAR_DICT:
5462 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005463 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005465 init_tv(&var1);
5466 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 /*
5470 * dict.name
5471 */
5472 key = *arg + 1;
5473 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5474 ;
5475 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 /*
5482 * something[idx]
5483 *
5484 * Get the (first) variable from inside the [].
5485 */
5486 *arg = skipwhite(*arg + 1);
5487 if (**arg == ':')
5488 empty1 = TRUE;
5489 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5490 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005491 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5492 {
5493 /* not a number or string */
5494 clear_tv(&var1);
5495 return FAIL;
5496 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497
5498 /*
5499 * Get the second variable from inside the [:].
5500 */
5501 if (**arg == ':')
5502 {
5503 range = TRUE;
5504 *arg = skipwhite(*arg + 1);
5505 if (**arg == ']')
5506 empty2 = TRUE;
5507 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005509 if (!empty1)
5510 clear_tv(&var1);
5511 return FAIL;
5512 }
5513 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5514 {
5515 /* not a number or string */
5516 if (!empty1)
5517 clear_tv(&var1);
5518 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005519 return FAIL;
5520 }
5521 }
5522
5523 /* Check for the ']'. */
5524 if (**arg != ']')
5525 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005526 if (verbose)
5527 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 clear_tv(&var1);
5529 if (range)
5530 clear_tv(&var2);
5531 return FAIL;
5532 }
5533 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 }
5535
5536 if (evaluate)
5537 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005538 n1 = 0;
5539 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 n1 = get_tv_number(&var1);
5542 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544 if (range)
5545 {
5546 if (empty2)
5547 n2 = -1;
5548 else
5549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005550 n2 = get_tv_number(&var2);
5551 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 }
5553 }
5554
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005557 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005558 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005559 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005560 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005561 case VAR_SPECIAL:
5562 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005563 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005564 break; /* not evaluating, skipping over subscript */
5565
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 case VAR_NUMBER:
5567 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 len = (long)STRLEN(s);
5570 if (range)
5571 {
5572 /* The resulting variable is a substring. If the indexes
5573 * are out of range the result is empty. */
5574 if (n1 < 0)
5575 {
5576 n1 = len + n1;
5577 if (n1 < 0)
5578 n1 = 0;
5579 }
5580 if (n2 < 0)
5581 n2 = len + n2;
5582 else if (n2 >= len)
5583 n2 = len;
5584 if (n1 >= len || n2 < 0 || n1 > n2)
5585 s = NULL;
5586 else
5587 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5588 }
5589 else
5590 {
5591 /* The resulting variable is a string of a single
5592 * character. If the index is too big or negative the
5593 * result is empty. */
5594 if (n1 >= len || n1 < 0)
5595 s = NULL;
5596 else
5597 s = vim_strnsave(s + n1, 1);
5598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 clear_tv(rettv);
5600 rettv->v_type = VAR_STRING;
5601 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602 break;
5603
5604 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 if (n1 < 0)
5607 n1 = len + n1;
5608 if (!empty1 && (n1 < 0 || n1 >= len))
5609 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005610 /* For a range we allow invalid values and return an empty
5611 * list. A list index out of range is an error. */
5612 if (!range)
5613 {
5614 if (verbose)
5615 EMSGN(_(e_listidx), n1);
5616 return FAIL;
5617 }
5618 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 }
5620 if (range)
5621 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 list_T *l;
5623 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624
5625 if (n2 < 0)
5626 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005627 else if (n2 >= len)
5628 n2 = len - 1;
5629 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005630 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 l = list_alloc();
5632 if (l == NULL)
5633 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 n1 <= n2; ++n1)
5636 {
5637 if (list_append_tv(l, &item->li_tv) == FAIL)
5638 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005639 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 return FAIL;
5641 }
5642 item = item->li_next;
5643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 clear_tv(rettv);
5645 rettv->v_type = VAR_LIST;
5646 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005647 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 }
5649 else
5650 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005651 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 clear_tv(rettv);
5653 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 }
5655 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656
5657 case VAR_DICT:
5658 if (range)
5659 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005660 if (verbose)
5661 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 if (len == -1)
5663 clear_tv(&var1);
5664 return FAIL;
5665 }
5666 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668
5669 if (len == -1)
5670 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005671 key = get_tv_string_chk(&var1);
5672 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 clear_tv(&var1);
5675 return FAIL;
5676 }
5677 }
5678
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005679 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005682 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 if (len == -1)
5684 clear_tv(&var1);
5685 if (item == NULL)
5686 return FAIL;
5687
5688 copy_tv(&item->di_tv, &var1);
5689 clear_tv(rettv);
5690 *rettv = var1;
5691 }
5692 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 }
5694 }
5695
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005696 return OK;
5697}
5698
5699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 * Get an option value.
5701 * "arg" points to the '&' or '+' before the option name.
5702 * "arg" is advanced to character after the option name.
5703 * Return OK or FAIL.
5704 */
5705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706get_option_tv(
5707 char_u **arg,
5708 typval_T *rettv, /* when NULL, only check if option exists */
5709 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 char_u *option_end;
5712 long numval;
5713 char_u *stringval;
5714 int opt_type;
5715 int c;
5716 int working = (**arg == '+'); /* has("+option") */
5717 int ret = OK;
5718 int opt_flags;
5719
5720 /*
5721 * Isolate the option name and find its value.
5722 */
5723 option_end = find_option_end(arg, &opt_flags);
5724 if (option_end == NULL)
5725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 EMSG2(_("E112: Option name missing: %s"), *arg);
5728 return FAIL;
5729 }
5730
5731 if (!evaluate)
5732 {
5733 *arg = option_end;
5734 return OK;
5735 }
5736
5737 c = *option_end;
5738 *option_end = NUL;
5739 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 if (opt_type == -3) /* invalid name */
5743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005744 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 EMSG2(_("E113: Unknown option: %s"), *arg);
5746 ret = FAIL;
5747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005748 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 {
5750 if (opt_type == -2) /* hidden string option */
5751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752 rettv->v_type = VAR_STRING;
5753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755 else if (opt_type == -1) /* hidden number option */
5756 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005757 rettv->v_type = VAR_NUMBER;
5758 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760 else if (opt_type == 1) /* number option */
5761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005762 rettv->v_type = VAR_NUMBER;
5763 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 else /* string option */
5766 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005767 rettv->v_type = VAR_STRING;
5768 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
5770 }
5771 else if (working && (opt_type == -2 || opt_type == -1))
5772 ret = FAIL;
5773
5774 *option_end = c; /* put back for error messages */
5775 *arg = option_end;
5776
5777 return ret;
5778}
5779
5780/*
5781 * Allocate a variable for a string constant.
5782 * Return OK or FAIL.
5783 */
5784 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005785get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 char_u *p;
5788 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 int extra = 0;
5790
5791 /*
5792 * Find the end of the string, skipping backslashed characters.
5793 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
5796 if (*p == '\\' && p[1] != NUL)
5797 {
5798 ++p;
5799 /* A "\<x>" form occupies at least 4 characters, and produces up
5800 * to 6 characters: reserve space for 2 extra */
5801 if (*p == '<')
5802 extra += 2;
5803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
5805
5806 if (*p != '"')
5807 {
5808 EMSG2(_("E114: Missing quote: %s"), *arg);
5809 return FAIL;
5810 }
5811
5812 /* If only parsing, set *arg and return here */
5813 if (!evaluate)
5814 {
5815 *arg = p + 1;
5816 return OK;
5817 }
5818
5819 /*
5820 * Copy the string into allocated memory, handling backslashed
5821 * characters.
5822 */
5823 name = alloc((unsigned)(p - *arg + extra));
5824 if (name == NULL)
5825 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 rettv->v_type = VAR_STRING;
5827 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
5831 if (*p == '\\')
5832 {
5833 switch (*++p)
5834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 case 'b': *name++ = BS; ++p; break;
5836 case 'e': *name++ = ESC; ++p; break;
5837 case 'f': *name++ = FF; ++p; break;
5838 case 'n': *name++ = NL; ++p; break;
5839 case 'r': *name++ = CAR; ++p; break;
5840 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 case 'X': /* hex: "\x1", "\x12" */
5843 case 'x':
5844 case 'u': /* Unicode: "\u0023" */
5845 case 'U':
5846 if (vim_isxdigit(p[1]))
5847 {
5848 int n, nr;
5849 int c = toupper(*p);
5850
5851 if (c == 'X')
5852 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005853 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005855 else
5856 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 nr = 0;
5858 while (--n >= 0 && vim_isxdigit(p[1]))
5859 {
5860 ++p;
5861 nr = (nr << 4) + hex2nr(*p);
5862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#ifdef FEAT_MBYTE
5865 /* For "\u" store the number according to
5866 * 'encoding'. */
5867 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 else
5870#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 break;
5874
5875 /* octal: "\1", "\12", "\123" */
5876 case '0':
5877 case '1':
5878 case '2':
5879 case '3':
5880 case '4':
5881 case '5':
5882 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 case '7': *name = *p++ - '0';
5884 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 *name = (*name << 3) + *p++ - '0';
5887 if (*p >= '0' && *p <= '7')
5888 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 break;
5892
5893 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (extra != 0)
5896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 break;
5899 }
5900 /* FALLTHROUGH */
5901
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 break;
5904 }
5905 }
5906 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 *arg = p + 1;
5912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 return OK;
5914}
5915
5916/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 * Return OK or FAIL.
5919 */
5920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 int reduce = 0;
5926
5927 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 */
5930 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5931 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 break;
5936 ++reduce;
5937 ++p;
5938 }
5939 }
5940
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 return FAIL;
5945 }
5946
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005948 if (!evaluate)
5949 {
5950 *arg = p + 1;
5951 return OK;
5952 }
5953
5954 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956 */
5957 str = alloc((unsigned)((p - *arg) - reduce));
5958 if (str == NULL)
5959 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960 rettv->v_type = VAR_STRING;
5961 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962
Bram Moolenaar8c711452005-01-14 21:53:12 +00005963 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005967 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005968 break;
5969 ++p;
5970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005973 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005974 *arg = p + 1;
5975
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005976 return OK;
5977}
5978
Bram Moolenaarddecc252016-04-06 22:59:37 +02005979 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005980partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005981{
5982 int i;
5983
5984 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005985 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005986 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005987 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005988 func_unref(pt->pt_name);
5989 vim_free(pt->pt_name);
5990 vim_free(pt);
5991}
5992
5993/*
5994 * Unreference a closure: decrement the reference count and free it when it
5995 * becomes zero.
5996 */
5997 void
5998partial_unref(partial_T *pt)
5999{
6000 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006001 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006002}
6003
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006004/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 * Allocate a variable for a List and fill it from "*arg".
6006 * Return OK or FAIL.
6007 */
6008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006009get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010{
Bram Moolenaar33570922005-01-25 22:26:29 +00006011 list_T *l = NULL;
6012 typval_T tv;
6013 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014
6015 if (evaluate)
6016 {
6017 l = list_alloc();
6018 if (l == NULL)
6019 return FAIL;
6020 }
6021
6022 *arg = skipwhite(*arg + 1);
6023 while (**arg != ']' && **arg != NUL)
6024 {
6025 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6026 goto failret;
6027 if (evaluate)
6028 {
6029 item = listitem_alloc();
6030 if (item != NULL)
6031 {
6032 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006033 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 list_append(l, item);
6035 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006036 else
6037 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 }
6039
6040 if (**arg == ']')
6041 break;
6042 if (**arg != ',')
6043 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006044 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 goto failret;
6046 }
6047 *arg = skipwhite(*arg + 1);
6048 }
6049
6050 if (**arg != ']')
6051 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053failret:
6054 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006055 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 return FAIL;
6057 }
6058
6059 *arg = skipwhite(*arg + 1);
6060 if (evaluate)
6061 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006062 rettv->v_type = VAR_LIST;
6063 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 ++l->lv_refcount;
6065 }
6066
6067 return OK;
6068}
6069
6070/*
6071 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006072 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006074 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006077 list_T *l;
6078
6079 l = (list_T *)alloc_clear(sizeof(list_T));
6080 if (l != NULL)
6081 {
6082 /* Prepend the list to the list of lists for garbage collection. */
6083 if (first_list != NULL)
6084 first_list->lv_used_prev = l;
6085 l->lv_used_prev = NULL;
6086 l->lv_used_next = first_list;
6087 first_list = l;
6088 }
6089 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090}
6091
6092/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006093 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006094 * Returns OK or FAIL.
6095 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006096 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006098{
6099 list_T *l = list_alloc();
6100
6101 if (l == NULL)
6102 return FAIL;
6103
6104 rettv->vval.v_list = l;
6105 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006106 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006107 ++l->lv_refcount;
6108 return OK;
6109}
6110
6111/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112 * Unreference a list: decrement the reference count and free it when it
6113 * becomes zero.
6114 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006118 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006119 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120}
6121
6122/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006123 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124 * Ignores the reference count.
6125 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006126 static void
6127list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128{
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006131 for (item = l->lv_first; item != NULL; item = l->lv_first)
6132 {
6133 /* Remove the item before deleting it. */
6134 l->lv_first = item->li_next;
6135 clear_tv(&item->li_tv);
6136 vim_free(item);
6137 }
6138}
6139
6140 static void
6141list_free_list(list_T *l)
6142{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006143 /* Remove the list from the list of lists for garbage collection. */
6144 if (l->lv_used_prev == NULL)
6145 first_list = l->lv_used_next;
6146 else
6147 l->lv_used_prev->lv_used_next = l->lv_used_next;
6148 if (l->lv_used_next != NULL)
6149 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6150
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151 vim_free(l);
6152}
6153
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006154 void
6155list_free(list_T *l)
6156{
6157 if (!in_free_unref_items)
6158 {
6159 list_free_contents(l);
6160 list_free_list(l);
6161 }
6162}
6163
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164/*
6165 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006166 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006167 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006168 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006169listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170{
Bram Moolenaar33570922005-01-25 22:26:29 +00006171 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172}
6173
6174/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006175 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006180 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181 vim_free(item);
6182}
6183
6184/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006185 * Remove a list item from a List and free it. Also clears the value.
6186 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006189{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006190 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006191 listitem_free(item);
6192}
6193
6194/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195 * Get the number of items in a list.
6196 */
6197 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200 if (l == NULL)
6201 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006202 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203}
6204
6205/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 * Return TRUE when two lists have exactly the same values.
6207 */
6208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209list_equal(
6210 list_T *l1,
6211 list_T *l2,
6212 int ic, /* ignore case for strings */
6213 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006214{
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006216
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006217 if (l1 == NULL || l2 == NULL)
6218 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006219 if (l1 == l2)
6220 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006221 if (list_len(l1) != list_len(l2))
6222 return FALSE;
6223
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006224 for (item1 = l1->lv_first, item2 = l2->lv_first;
6225 item1 != NULL && item2 != NULL;
6226 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006228 return FALSE;
6229 return item1 == NULL && item2 == NULL;
6230}
6231
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006232/*
6233 * Return the dictitem that an entry in a hashtable points to.
6234 */
6235 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006236dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006237{
6238 return HI2DI(hi);
6239}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006240
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 * Return TRUE when two dictionaries have exactly the same key/values.
6243 */
6244 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006245dict_equal(
6246 dict_T *d1,
6247 dict_T *d2,
6248 int ic, /* ignore case for strings */
6249 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006250{
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 hashitem_T *hi;
6252 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006253 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006254
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006255 if (d1 == NULL && d2 == NULL)
6256 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006257 if (d1 == NULL || d2 == NULL)
6258 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006259 if (d1 == d2)
6260 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006261 if (dict_len(d1) != dict_len(d2))
6262 return FALSE;
6263
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006264 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006265 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006266 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006267 if (!HASHITEM_EMPTY(hi))
6268 {
6269 item2 = dict_find(d2, hi->hi_key, -1);
6270 if (item2 == NULL)
6271 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006272 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006273 return FALSE;
6274 --todo;
6275 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006276 }
6277 return TRUE;
6278}
6279
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006280static int tv_equal_recurse_limit;
6281
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006282 static int
6283func_equal(
6284 typval_T *tv1,
6285 typval_T *tv2,
6286 int ic) /* ignore case */
6287{
6288 char_u *s1, *s2;
6289 dict_T *d1, *d2;
6290 int a1, a2;
6291 int i;
6292
6293 /* empty and NULL function name considered the same */
6294 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6295 : tv1->vval.v_partial->pt_name;
6296 if (s1 != NULL && *s1 == NUL)
6297 s1 = NULL;
6298 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6299 : tv2->vval.v_partial->pt_name;
6300 if (s2 != NULL && *s2 == NUL)
6301 s2 = NULL;
6302 if (s1 == NULL || s2 == NULL)
6303 {
6304 if (s1 != s2)
6305 return FALSE;
6306 }
6307 else if (STRCMP(s1, s2) != 0)
6308 return FALSE;
6309
6310 /* empty dict and NULL dict is different */
6311 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6312 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6313 if (d1 == NULL || d2 == NULL)
6314 {
6315 if (d1 != d2)
6316 return FALSE;
6317 }
6318 else if (!dict_equal(d1, d2, ic, TRUE))
6319 return FALSE;
6320
6321 /* empty list and no list considered the same */
6322 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6323 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6324 if (a1 != a2)
6325 return FALSE;
6326 for (i = 0; i < a1; ++i)
6327 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6328 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6329 return FALSE;
6330
6331 return TRUE;
6332}
6333
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006334/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006335 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006336 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006337 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 */
6339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006340tv_equal(
6341 typval_T *tv1,
6342 typval_T *tv2,
6343 int ic, /* ignore case */
6344 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006345{
6346 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006347 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006348 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006349 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006350
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006351 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006352 * recursiveness to a limit. We guess they are equal then.
6353 * A fixed limit has the problem of still taking an awful long time.
6354 * Reduce the limit every time running into it. That should work fine for
6355 * deeply linked structures that are not recursively linked and catch
6356 * recursiveness quickly. */
6357 if (!recursive)
6358 tv_equal_recurse_limit = 1000;
6359 if (recursive_cnt >= tv_equal_recurse_limit)
6360 {
6361 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006362 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006363 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006364
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006365 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6366 if ((tv1->v_type == VAR_FUNC
6367 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6368 && (tv2->v_type == VAR_FUNC
6369 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6370 {
6371 ++recursive_cnt;
6372 r = func_equal(tv1, tv2, ic);
6373 --recursive_cnt;
6374 return r;
6375 }
6376
6377 if (tv1->v_type != tv2->v_type)
6378 return FALSE;
6379
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006380 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006382 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006383 ++recursive_cnt;
6384 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6385 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006386 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006387
6388 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006389 ++recursive_cnt;
6390 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6391 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006392 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006393
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006394 case VAR_NUMBER:
6395 return tv1->vval.v_number == tv2->vval.v_number;
6396
6397 case VAR_STRING:
6398 s1 = get_tv_string_buf(tv1, buf1);
6399 s2 = get_tv_string_buf(tv2, buf2);
6400 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006401
6402 case VAR_SPECIAL:
6403 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006404
6405 case VAR_FLOAT:
6406#ifdef FEAT_FLOAT
6407 return tv1->vval.v_float == tv2->vval.v_float;
6408#endif
6409 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006410#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006411 return tv1->vval.v_job == tv2->vval.v_job;
6412#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006413 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006414#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006415 return tv1->vval.v_channel == tv2->vval.v_channel;
6416#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006417 case VAR_FUNC:
6418 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006419 case VAR_UNKNOWN:
6420 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006421 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006422
Bram Moolenaara03f2332016-02-06 18:09:59 +01006423 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6424 * does not equal anything, not even itself. */
6425 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426}
6427
6428/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429 * Locate item with index "n" in list "l" and return it.
6430 * A negative index is counted from the end; -1 is the last item.
6431 * Returns NULL when "n" is out of range.
6432 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006433 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435{
Bram Moolenaar33570922005-01-25 22:26:29 +00006436 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437 long idx;
6438
6439 if (l == NULL)
6440 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006441
6442 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006444 n = l->lv_len + n;
6445
6446 /* Check for index out of range. */
6447 if (n < 0 || n >= l->lv_len)
6448 return NULL;
6449
6450 /* When there is a cached index may start search from there. */
6451 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 if (n < l->lv_idx / 2)
6454 {
6455 /* closest to the start of the list */
6456 item = l->lv_first;
6457 idx = 0;
6458 }
6459 else if (n > (l->lv_idx + l->lv_len) / 2)
6460 {
6461 /* closest to the end of the list */
6462 item = l->lv_last;
6463 idx = l->lv_len - 1;
6464 }
6465 else
6466 {
6467 /* closest to the cached index */
6468 item = l->lv_idx_item;
6469 idx = l->lv_idx;
6470 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471 }
6472 else
6473 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006474 if (n < l->lv_len / 2)
6475 {
6476 /* closest to the start of the list */
6477 item = l->lv_first;
6478 idx = 0;
6479 }
6480 else
6481 {
6482 /* closest to the end of the list */
6483 item = l->lv_last;
6484 idx = l->lv_len - 1;
6485 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006486 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006487
6488 while (n > idx)
6489 {
6490 /* search forward */
6491 item = item->li_next;
6492 ++idx;
6493 }
6494 while (n < idx)
6495 {
6496 /* search backward */
6497 item = item->li_prev;
6498 --idx;
6499 }
6500
6501 /* cache the used index */
6502 l->lv_idx = idx;
6503 l->lv_idx_item = item;
6504
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006505 return item;
6506}
6507
6508/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006509 * Get list item "l[idx]" as a number.
6510 */
6511 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512list_find_nr(
6513 list_T *l,
6514 long idx,
6515 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006516{
6517 listitem_T *li;
6518
6519 li = list_find(l, idx);
6520 if (li == NULL)
6521 {
6522 if (errorp != NULL)
6523 *errorp = TRUE;
6524 return -1L;
6525 }
6526 return get_tv_number_chk(&li->li_tv, errorp);
6527}
6528
6529/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006530 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6531 */
6532 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006533list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006534{
6535 listitem_T *li;
6536
6537 li = list_find(l, idx - 1);
6538 if (li == NULL)
6539 {
6540 EMSGN(_(e_listidx), idx);
6541 return NULL;
6542 }
6543 return get_tv_string(&li->li_tv);
6544}
6545
6546/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006547 * Locate "item" list "l" and return its index.
6548 * Returns -1 when "item" is not in the list.
6549 */
6550 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006551list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006552{
6553 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006554 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006555
6556 if (l == NULL)
6557 return -1;
6558 idx = 0;
6559 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6560 ++idx;
6561 if (li == NULL)
6562 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006563 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006564}
6565
6566/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006567 * Append item "item" to the end of list "l".
6568 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006570list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006571{
6572 if (l->lv_last == NULL)
6573 {
6574 /* empty list */
6575 l->lv_first = item;
6576 l->lv_last = item;
6577 item->li_prev = NULL;
6578 }
6579 else
6580 {
6581 l->lv_last->li_next = item;
6582 item->li_prev = l->lv_last;
6583 l->lv_last = item;
6584 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006585 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 item->li_next = NULL;
6587}
6588
6589/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006593 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006594list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006596 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597
Bram Moolenaar05159a02005-02-26 23:04:13 +00006598 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006600 copy_tv(tv, &li->li_tv);
6601 list_append(l, li);
6602 return OK;
6603}
6604
6605/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006606 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006607 * Return FAIL when out of memory.
6608 */
6609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006611{
6612 listitem_T *li = listitem_alloc();
6613
6614 if (li == NULL)
6615 return FAIL;
6616 li->li_tv.v_type = VAR_DICT;
6617 li->li_tv.v_lock = 0;
6618 li->li_tv.vval.v_dict = dict;
6619 list_append(list, li);
6620 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 return OK;
6622}
6623
6624/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006625 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006626 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006627 * Returns FAIL when out of memory.
6628 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006630list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006631{
6632 listitem_T *li = listitem_alloc();
6633
6634 if (li == NULL)
6635 return FAIL;
6636 list_append(l, li);
6637 li->li_tv.v_type = VAR_STRING;
6638 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006639 if (str == NULL)
6640 li->li_tv.vval.v_string = NULL;
6641 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006642 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006643 return FAIL;
6644 return OK;
6645}
6646
6647/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006648 * Append "n" to list "l".
6649 * Returns FAIL when out of memory.
6650 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006652list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006653{
6654 listitem_T *li;
6655
6656 li = listitem_alloc();
6657 if (li == NULL)
6658 return FAIL;
6659 li->li_tv.v_type = VAR_NUMBER;
6660 li->li_tv.v_lock = 0;
6661 li->li_tv.vval.v_number = n;
6662 list_append(l, li);
6663 return OK;
6664}
6665
6666/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006667 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668 * If "item" is NULL append at the end.
6669 * Return FAIL when out of memory.
6670 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006672list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673{
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006675
6676 if (ni == NULL)
6677 return FAIL;
6678 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006679 list_insert(l, ni, item);
6680 return OK;
6681}
6682
6683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006684list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006685{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006686 if (item == NULL)
6687 /* Append new item at end of list. */
6688 list_append(l, ni);
6689 else
6690 {
6691 /* Insert new item before existing item. */
6692 ni->li_prev = item->li_prev;
6693 ni->li_next = item;
6694 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006695 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006696 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006697 ++l->lv_idx;
6698 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006699 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006700 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006701 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006702 l->lv_idx_item = NULL;
6703 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006704 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006705 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006706 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006707}
6708
6709/*
6710 * Extend "l1" with "l2".
6711 * If "bef" is NULL append at the end, otherwise insert before this item.
6712 * Returns FAIL when out of memory.
6713 */
6714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006715list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006716{
Bram Moolenaar33570922005-01-25 22:26:29 +00006717 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006718 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006719
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006720 /* We also quit the loop when we have inserted the original item count of
6721 * the list, avoid a hang when we extend a list with itself. */
6722 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6724 return FAIL;
6725 return OK;
6726}
6727
6728/*
6729 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6730 * Return FAIL when out of memory.
6731 */
6732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006733list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006734{
Bram Moolenaar33570922005-01-25 22:26:29 +00006735 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006736
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006737 if (l1 == NULL || l2 == NULL)
6738 return FAIL;
6739
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006740 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006741 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 if (l == NULL)
6743 return FAIL;
6744 tv->v_type = VAR_LIST;
6745 tv->vval.v_list = l;
6746
6747 /* append all items from the second list */
6748 return list_extend(l, l2, NULL);
6749}
6750
6751/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006752 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006753 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006754 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006755 * Returns NULL when out of memory.
6756 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759{
Bram Moolenaar33570922005-01-25 22:26:29 +00006760 list_T *copy;
6761 listitem_T *item;
6762 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763
6764 if (orig == NULL)
6765 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006766
6767 copy = list_alloc();
6768 if (copy != NULL)
6769 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006770 if (copyID != 0)
6771 {
6772 /* Do this before adding the items, because one of the items may
6773 * refer back to this list. */
6774 orig->lv_copyID = copyID;
6775 orig->lv_copylist = copy;
6776 }
6777 for (item = orig->lv_first; item != NULL && !got_int;
6778 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779 {
6780 ni = listitem_alloc();
6781 if (ni == NULL)
6782 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006783 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006784 {
6785 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6786 {
6787 vim_free(ni);
6788 break;
6789 }
6790 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006792 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006793 list_append(copy, ni);
6794 }
6795 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006796 if (item != NULL)
6797 {
6798 list_unref(copy);
6799 copy = NULL;
6800 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 }
6802
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006803 return copy;
6804}
6805
6806/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006807 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006808 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006809 * This used to be called list_remove, but that conflicts with a Sun header
6810 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006811 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006814{
Bram Moolenaar33570922005-01-25 22:26:29 +00006815 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006816
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006817 /* notify watchers */
6818 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006820 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006821 list_fix_watch(l, ip);
6822 if (ip == item2)
6823 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006825
6826 if (item2->li_next == NULL)
6827 l->lv_last = item->li_prev;
6828 else
6829 item2->li_next->li_prev = item->li_prev;
6830 if (item->li_prev == NULL)
6831 l->lv_first = item2->li_next;
6832 else
6833 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006834 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006835}
6836
6837/*
6838 * Return an allocated string with the string representation of a list.
6839 * May return NULL.
6840 */
6841 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006842list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006843{
6844 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845
6846 if (tv->vval.v_list == NULL)
6847 return NULL;
6848 ga_init2(&ga, (int)sizeof(char), 80);
6849 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006850 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6851 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006852 {
6853 vim_free(ga.ga_data);
6854 return NULL;
6855 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006856 ga_append(&ga, ']');
6857 ga_append(&ga, NUL);
6858 return (char_u *)ga.ga_data;
6859}
6860
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006861typedef struct join_S {
6862 char_u *s;
6863 char_u *tofree;
6864} join_T;
6865
6866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006867list_join_inner(
6868 garray_T *gap, /* to store the result in */
6869 list_T *l,
6870 char_u *sep,
6871 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006872 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006873 int copyID,
6874 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006875{
6876 int i;
6877 join_T *p;
6878 int len;
6879 int sumlen = 0;
6880 int first = TRUE;
6881 char_u *tofree;
6882 char_u numbuf[NUMBUFLEN];
6883 listitem_T *item;
6884 char_u *s;
6885
6886 /* Stringify each item in the list. */
6887 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6888 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006889 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6890 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006891 if (s == NULL)
6892 return FAIL;
6893
6894 len = (int)STRLEN(s);
6895 sumlen += len;
6896
Bram Moolenaarcde88542015-08-11 19:14:00 +02006897 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006898 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6899 if (tofree != NULL || s != numbuf)
6900 {
6901 p->s = s;
6902 p->tofree = tofree;
6903 }
6904 else
6905 {
6906 p->s = vim_strnsave(s, len);
6907 p->tofree = p->s;
6908 }
6909
6910 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006911 if (did_echo_string_emsg) /* recursion error, bail out */
6912 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006913 }
6914
6915 /* Allocate result buffer with its total size, avoid re-allocation and
6916 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6917 if (join_gap->ga_len >= 2)
6918 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6919 if (ga_grow(gap, sumlen + 2) == FAIL)
6920 return FAIL;
6921
6922 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6923 {
6924 if (first)
6925 first = FALSE;
6926 else
6927 ga_concat(gap, sep);
6928 p = ((join_T *)join_gap->ga_data) + i;
6929
6930 if (p->s != NULL)
6931 ga_concat(gap, p->s);
6932 line_breakcheck();
6933 }
6934
6935 return OK;
6936}
6937
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006938/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006939 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006940 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006941 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006942 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006944list_join(
6945 garray_T *gap,
6946 list_T *l,
6947 char_u *sep,
6948 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006949 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006950 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006951{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006952 garray_T join_ga;
6953 int retval;
6954 join_T *p;
6955 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006956
Bram Moolenaard39a7512015-04-16 22:51:22 +02006957 if (l->lv_len < 1)
6958 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006959 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006960 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6961 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006962
6963 /* Dispose each item in join_ga. */
6964 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006966 p = (join_T *)join_ga.ga_data;
6967 for (i = 0; i < join_ga.ga_len; ++i)
6968 {
6969 vim_free(p->tofree);
6970 ++p;
6971 }
6972 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006974
6975 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006976}
6977
6978/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006979 * Return the next (unique) copy ID.
6980 * Used for serializing nested structures.
6981 */
6982 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006983get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006984{
6985 current_copyID += COPYID_INC;
6986 return current_copyID;
6987}
6988
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006989/* Used by get_func_tv() */
6990static garray_T funcargs = GA_EMPTY;
6991
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006992/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 * Garbage collection for lists and dictionaries.
6994 *
6995 * We use reference counts to be able to free most items right away when they
6996 * are no longer used. But for composite items it's possible that it becomes
6997 * unused while the reference count is > 0: When there is a recursive
6998 * reference. Example:
6999 * :let l = [1, 2, 3]
7000 * :let d = {9: l}
7001 * :let l[1] = d
7002 *
7003 * Since this is quite unusual we handle this with garbage collection: every
7004 * once in a while find out which lists and dicts are not referenced from any
7005 * variable.
7006 *
7007 * Here is a good reference text about garbage collection (refers to Python
7008 * but it applies to all reference-counting mechanisms):
7009 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007010 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007011
7012/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007014 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007016 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007018garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007019{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007020 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 buf_T *buf;
7023 win_T *wp;
7024 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007025 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007026 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007027 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007028#ifdef FEAT_WINDOWS
7029 tabpage_T *tp;
7030#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007032 if (!testing)
7033 {
7034 /* Only do this once. */
7035 want_garbage_collect = FALSE;
7036 may_garbage_collect = FALSE;
7037 garbage_collect_at_exit = FALSE;
7038 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007039
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007040 /* We advance by two because we add one for items referenced through
7041 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007042 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007043
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 /*
7045 * 1. Go through all accessible variables and mark all lists and dicts
7046 * with copyID.
7047 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007048
7049 /* Don't free variables in the previous_funccal list unless they are only
7050 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007051 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007052 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7053 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7055 NULL);
7056 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7057 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007058 }
7059
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 /* script-local variables */
7061 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063
7064 /* buffer-local variables */
7065 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7067 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068
7069 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007070 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7072 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007073#ifdef FEAT_AUTOCMD
7074 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7076 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007077#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007079#ifdef FEAT_WINDOWS
7080 /* tabpage-local variables */
7081 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7083 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007084#endif
7085
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088
7089 /* function-local variables */
7090 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7091 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7093 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 }
7095
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007096 /* function call arguments, if v:testing is set. */
7097 for (i = 0; i < funcargs.ga_len; ++i)
7098 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7099 copyID, NULL, NULL);
7100
Bram Moolenaard812df62008-11-09 12:46:09 +00007101 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007103
Bram Moolenaar1dced572012-04-05 16:54:08 +02007104#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007106#endif
7107
Bram Moolenaardb913952012-06-29 12:54:53 +02007108#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007110#endif
7111
7112#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007114#endif
7115
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007116#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007117 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007118 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007119#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007120#ifdef FEAT_NETBEANS_INTG
7121 abort = abort || set_ref_in_nb_channel(copyID);
7122#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007123
Bram Moolenaare3188e22016-05-31 21:13:04 +02007124#ifdef FEAT_TIMERS
7125 abort = abort || set_ref_in_timer(copyID);
7126#endif
7127
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007129 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007130 /*
7131 * 2. Free lists and dictionaries that are not referenced.
7132 */
7133 did_free = free_unref_items(copyID);
7134
7135 /*
7136 * 3. Check if any funccal can be freed now.
7137 */
7138 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007139 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 if (can_free_funccal(*pfc, copyID))
7141 {
7142 fc = *pfc;
7143 *pfc = fc->caller;
7144 free_funccal(fc, TRUE);
7145 did_free = TRUE;
7146 did_free_funccal = TRUE;
7147 }
7148 else
7149 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007150 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007151 if (did_free_funccal)
7152 /* When a funccal was freed some more items might be garbage
7153 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007154 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 else if (p_verbose > 0)
7157 {
7158 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7159 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007160
7161 return did_free;
7162}
7163
7164/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007165 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007166 */
7167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007168free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007169{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007170 dict_T *dd, *dd_next;
7171 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007172 int did_free = FALSE;
7173
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007174 /* Let all "free" functions know that we are here. This means no
7175 * dictionaries, lists, channels or jobs are to be freed, because we will
7176 * do that here. */
7177 in_free_unref_items = TRUE;
7178
7179 /*
7180 * PASS 1: free the contents of the items. We don't free the items
7181 * themselves yet, so that it is possible to decrement refcount counters
7182 */
7183
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007185 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007187 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007188 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007190 /* Free the Dictionary and ordinary items it contains, but don't
7191 * recurse into Lists and Dictionaries, they will be in the list
7192 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007193 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196
7197 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007198 * Go through the list of lists and free items without the copyID.
7199 * But don't free a list that has a watcher (used in a for loop), these
7200 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007201 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007202 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7203 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7204 && ll->lv_watch == NULL)
7205 {
7206 /* Free the List and ordinary items it contains, but don't recurse
7207 * into Lists and Dictionaries, they will be in the list of dicts
7208 * or list of lists. */
7209 list_free_contents(ll);
7210 did_free = TRUE;
7211 }
7212
7213#ifdef FEAT_JOB_CHANNEL
7214 /* Go through the list of jobs and free items without the copyID. This
7215 * must happen before doing channels, because jobs refer to channels, but
7216 * the reference from the channel to the job isn't tracked. */
7217 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7218
7219 /* Go through the list of channels and free items without the copyID. */
7220 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7221#endif
7222
7223 /*
7224 * PASS 2: free the items themselves.
7225 */
7226 for (dd = first_dict; dd != NULL; dd = dd_next)
7227 {
7228 dd_next = dd->dv_used_next;
7229 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7230 dict_free_dict(dd);
7231 }
7232
7233 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007234 {
7235 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007236 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7237 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007238 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007239 /* Free the List and ordinary items it contains, but don't recurse
7240 * into Lists and Dictionaries, they will be in the list of dicts
7241 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007242 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007244 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007245
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007246#ifdef FEAT_JOB_CHANNEL
7247 /* Go through the list of jobs and free items without the copyID. This
7248 * must happen before doing channels, because jobs refer to channels, but
7249 * the reference from the channel to the job isn't tracked. */
7250 free_unused_jobs(copyID, COPYID_MASK);
7251
7252 /* Go through the list of channels and free items without the copyID. */
7253 free_unused_channels(copyID, COPYID_MASK);
7254#endif
7255
7256 in_free_unref_items = FALSE;
7257
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007258 return did_free;
7259}
7260
7261/*
7262 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007263 * "list_stack" is used to add lists to be marked. Can be NULL.
7264 *
7265 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007266 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007268set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269{
7270 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007271 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007272 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007273 hashtab_T *cur_ht;
7274 ht_stack_T *ht_stack = NULL;
7275 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007276
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007277 cur_ht = ht;
7278 for (;;)
7279 {
7280 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007281 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007282 /* Mark each item in the hashtab. If the item contains a hashtab
7283 * it is added to ht_stack, if it contains a list it is added to
7284 * list_stack. */
7285 todo = (int)cur_ht->ht_used;
7286 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7287 if (!HASHITEM_EMPTY(hi))
7288 {
7289 --todo;
7290 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7291 &ht_stack, list_stack);
7292 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007293 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007294
7295 if (ht_stack == NULL)
7296 break;
7297
7298 /* take an item from the stack */
7299 cur_ht = ht_stack->ht;
7300 tempitem = ht_stack;
7301 ht_stack = ht_stack->prev;
7302 free(tempitem);
7303 }
7304
7305 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007306}
7307
7308/*
7309 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007310 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7311 *
7312 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007313 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007316{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007317 listitem_T *li;
7318 int abort = FALSE;
7319 list_T *cur_l;
7320 list_stack_T *list_stack = NULL;
7321 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007322
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007323 cur_l = l;
7324 for (;;)
7325 {
7326 if (!abort)
7327 /* Mark each item in the list. If the item contains a hashtab
7328 * it is added to ht_stack, if it contains a list it is added to
7329 * list_stack. */
7330 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7331 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7332 ht_stack, &list_stack);
7333 if (list_stack == NULL)
7334 break;
7335
7336 /* take an item from the stack */
7337 cur_l = list_stack->list;
7338 tempitem = list_stack;
7339 list_stack = list_stack->prev;
7340 free(tempitem);
7341 }
7342
7343 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007344}
7345
7346/*
7347 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007348 * "list_stack" is used to add lists to be marked. Can be NULL.
7349 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7350 *
7351 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007352 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354set_ref_in_item(
7355 typval_T *tv,
7356 int copyID,
7357 ht_stack_T **ht_stack,
7358 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007359{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007360 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007361
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007362 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007363 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007364 dict_T *dd = tv->vval.v_dict;
7365
Bram Moolenaara03f2332016-02-06 18:09:59 +01007366 if (dd != NULL && dd->dv_copyID != copyID)
7367 {
7368 /* Didn't see this dict yet. */
7369 dd->dv_copyID = copyID;
7370 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007371 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007372 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7373 }
7374 else
7375 {
7376 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7377 if (newitem == NULL)
7378 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007379 else
7380 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007381 newitem->ht = &dd->dv_hashtab;
7382 newitem->prev = *ht_stack;
7383 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007384 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007385 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007386 }
7387 }
7388 else if (tv->v_type == VAR_LIST)
7389 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007390 list_T *ll = tv->vval.v_list;
7391
Bram Moolenaara03f2332016-02-06 18:09:59 +01007392 if (ll != NULL && ll->lv_copyID != copyID)
7393 {
7394 /* Didn't see this list yet. */
7395 ll->lv_copyID = copyID;
7396 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007397 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007398 abort = set_ref_in_list(ll, copyID, ht_stack);
7399 }
7400 else
7401 {
7402 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007403 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007404 if (newitem == NULL)
7405 abort = TRUE;
7406 else
7407 {
7408 newitem->list = ll;
7409 newitem->prev = *list_stack;
7410 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007411 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007412 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007413 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007414 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007415 else if (tv->v_type == VAR_PARTIAL)
7416 {
7417 partial_T *pt = tv->vval.v_partial;
7418 int i;
7419
7420 /* A partial does not have a copyID, because it cannot contain itself.
7421 */
7422 if (pt != NULL)
7423 {
7424 if (pt->pt_dict != NULL)
7425 {
7426 typval_T dtv;
7427
7428 dtv.v_type = VAR_DICT;
7429 dtv.vval.v_dict = pt->pt_dict;
7430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7431 }
7432
7433 for (i = 0; i < pt->pt_argc; ++i)
7434 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7435 ht_stack, list_stack);
7436 }
7437 }
7438#ifdef FEAT_JOB_CHANNEL
7439 else if (tv->v_type == VAR_JOB)
7440 {
7441 job_T *job = tv->vval.v_job;
7442 typval_T dtv;
7443
7444 if (job != NULL && job->jv_copyID != copyID)
7445 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007446 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007447 if (job->jv_channel != NULL)
7448 {
7449 dtv.v_type = VAR_CHANNEL;
7450 dtv.vval.v_channel = job->jv_channel;
7451 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7452 }
7453 if (job->jv_exit_partial != NULL)
7454 {
7455 dtv.v_type = VAR_PARTIAL;
7456 dtv.vval.v_partial = job->jv_exit_partial;
7457 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7458 }
7459 }
7460 }
7461 else if (tv->v_type == VAR_CHANNEL)
7462 {
7463 channel_T *ch =tv->vval.v_channel;
7464 int part;
7465 typval_T dtv;
7466 jsonq_T *jq;
7467 cbq_T *cq;
7468
7469 if (ch != NULL && ch->ch_copyID != copyID)
7470 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007471 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007472 for (part = PART_SOCK; part <= PART_IN; ++part)
7473 {
7474 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7475 jq = jq->jq_next)
7476 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7477 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7478 cq = cq->cq_next)
7479 if (cq->cq_partial != NULL)
7480 {
7481 dtv.v_type = VAR_PARTIAL;
7482 dtv.vval.v_partial = cq->cq_partial;
7483 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7484 }
7485 if (ch->ch_part[part].ch_partial != NULL)
7486 {
7487 dtv.v_type = VAR_PARTIAL;
7488 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7489 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7490 }
7491 }
7492 if (ch->ch_partial != NULL)
7493 {
7494 dtv.v_type = VAR_PARTIAL;
7495 dtv.vval.v_partial = ch->ch_partial;
7496 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7497 }
7498 if (ch->ch_close_partial != NULL)
7499 {
7500 dtv.v_type = VAR_PARTIAL;
7501 dtv.vval.v_partial = ch->ch_close_partial;
7502 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7503 }
7504 }
7505 }
7506#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007507 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007508}
7509
7510/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 * Allocate an empty header for a dictionary.
7512 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007513 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007514dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515{
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007520 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007521 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007522 if (first_dict != NULL)
7523 first_dict->dv_used_prev = d;
7524 d->dv_used_next = first_dict;
7525 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007526 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007527
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007529 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007530 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007532 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007533 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535}
7536
7537/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007538 * Allocate an empty dict for a return value.
7539 * Returns OK or FAIL.
7540 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007543{
7544 dict_T *d = dict_alloc();
7545
7546 if (d == NULL)
7547 return FAIL;
7548
7549 rettv->vval.v_dict = d;
7550 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007551 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007552 ++d->dv_refcount;
7553 return OK;
7554}
7555
7556
7557/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558 * Unreference a Dictionary: decrement the reference count and free it when it
7559 * becomes zero.
7560 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007561 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007562dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007564 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007565 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566}
7567
7568/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007569 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 * Ignores the reference count.
7571 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007572 static void
7573dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007577 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007579 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007580 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007581 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 if (!HASHITEM_EMPTY(hi))
7585 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007586 /* Remove the item before deleting it, just in case there is
7587 * something recursive causing trouble. */
7588 di = HI2DI(hi);
7589 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007590 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007591 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 --todo;
7593 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007595 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007596}
7597
7598 static void
7599dict_free_dict(dict_T *d)
7600{
7601 /* Remove the dict from the list of dicts for garbage collection. */
7602 if (d->dv_used_prev == NULL)
7603 first_dict = d->dv_used_next;
7604 else
7605 d->dv_used_prev->dv_used_next = d->dv_used_next;
7606 if (d->dv_used_next != NULL)
7607 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 vim_free(d);
7609}
7610
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007611 void
7612dict_free(dict_T *d)
7613{
7614 if (!in_free_unref_items)
7615 {
7616 dict_free_contents(d);
7617 dict_free_dict(d);
7618 }
7619}
7620
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621/*
7622 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623 * The "key" is copied to the new item.
7624 * Note that the value of the item "di_tv" still needs to be initialized!
7625 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007627 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007628dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629{
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007632 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007634 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007635 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007636 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007637 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639}
7640
7641/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 * Make a copy of a Dictionary item.
7643 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646{
Bram Moolenaar33570922005-01-25 22:26:29 +00007647 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007649 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7650 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 if (di != NULL)
7652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007654 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 copy_tv(&org->di_tv, &di->di_tv);
7656 }
7657 return di;
7658}
7659
7660/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 * Remove item "item" from Dictionary "dict" and free it.
7662 */
7663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007664dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665{
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667
Bram Moolenaar33570922005-01-25 22:26:29 +00007668 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007669 if (HASHITEM_EMPTY(hi))
7670 EMSG2(_(e_intern2), "dictitem_remove()");
7671 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 dictitem_free(item);
7674}
7675
7676/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 * Free a dict item. Also clears the value.
7678 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007683 if (item->di_flags & DI_FLAGS_ALLOC)
7684 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685}
7686
7687/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007688 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7689 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007690 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007691 * Returns NULL when out of memory.
7692 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007693 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007694dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695{
Bram Moolenaar33570922005-01-25 22:26:29 +00007696 dict_T *copy;
7697 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007698 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007700
7701 if (orig == NULL)
7702 return NULL;
7703
7704 copy = dict_alloc();
7705 if (copy != NULL)
7706 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007707 if (copyID != 0)
7708 {
7709 orig->dv_copyID = copyID;
7710 orig->dv_copydict = copy;
7711 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007712 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007713 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 --todo;
7718
7719 di = dictitem_alloc(hi->hi_key);
7720 if (di == NULL)
7721 break;
7722 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007723 {
7724 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7725 copyID) == FAIL)
7726 {
7727 vim_free(di);
7728 break;
7729 }
7730 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007731 else
7732 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7733 if (dict_add(copy, di) == FAIL)
7734 {
7735 dictitem_free(di);
7736 break;
7737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007742 if (todo > 0)
7743 {
7744 dict_unref(copy);
7745 copy = NULL;
7746 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007747 }
7748
7749 return copy;
7750}
7751
7752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007754 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007756 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007757dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758{
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760}
7761
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007763 * Add a number or string entry to dictionary "d".
7764 * When "str" is NULL use number "nr", otherwise use "str".
7765 * Returns FAIL when out of memory and when key already exists.
7766 */
7767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007768dict_add_nr_str(
7769 dict_T *d,
7770 char *key,
7771 long nr,
7772 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007773{
7774 dictitem_T *item;
7775
7776 item = dictitem_alloc((char_u *)key);
7777 if (item == NULL)
7778 return FAIL;
7779 item->di_tv.v_lock = 0;
7780 if (str == NULL)
7781 {
7782 item->di_tv.v_type = VAR_NUMBER;
7783 item->di_tv.vval.v_number = nr;
7784 }
7785 else
7786 {
7787 item->di_tv.v_type = VAR_STRING;
7788 item->di_tv.vval.v_string = vim_strsave(str);
7789 }
7790 if (dict_add(d, item) == FAIL)
7791 {
7792 dictitem_free(item);
7793 return FAIL;
7794 }
7795 return OK;
7796}
7797
7798/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007799 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007800 * Returns FAIL when out of memory and when key already exists.
7801 */
7802 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007803dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007804{
7805 dictitem_T *item;
7806
7807 item = dictitem_alloc((char_u *)key);
7808 if (item == NULL)
7809 return FAIL;
7810 item->di_tv.v_lock = 0;
7811 item->di_tv.v_type = VAR_LIST;
7812 item->di_tv.vval.v_list = list;
7813 if (dict_add(d, item) == FAIL)
7814 {
7815 dictitem_free(item);
7816 return FAIL;
7817 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007818 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007819 return OK;
7820}
7821
7822/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007823 * Get the number of items in a Dictionary.
7824 */
7825 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007826dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 if (d == NULL)
7829 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007830 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831}
7832
7833/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834 * Find item "key[len]" in Dictionary "d".
7835 * If "len" is negative use strlen(key).
7836 * Returns NULL when not found.
7837 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007838 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007839dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007841#define AKEYLEN 200
7842 char_u buf[AKEYLEN];
7843 char_u *akey;
7844 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007847 if (d == NULL)
7848 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007849 if (len < 0)
7850 akey = key;
7851 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007852 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007853 tofree = akey = vim_strnsave(key, len);
7854 if (akey == NULL)
7855 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007856 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007857 else
7858 {
7859 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007860 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007861 akey = buf;
7862 }
7863
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007865 vim_free(tofree);
7866 if (HASHITEM_EMPTY(hi))
7867 return NULL;
7868 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007869}
7870
7871/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007872 * Get a string item from a dictionary.
7873 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007874 * Returns NULL if the entry doesn't exist or out of memory.
7875 */
7876 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007877get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007878{
7879 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007880 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007881
7882 di = dict_find(d, key, -1);
7883 if (di == NULL)
7884 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007885 s = get_tv_string(&di->di_tv);
7886 if (save && s != NULL)
7887 s = vim_strsave(s);
7888 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007889}
7890
7891/*
7892 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007893 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007894 */
7895 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007897{
7898 dictitem_T *di;
7899
7900 di = dict_find(d, key, -1);
7901 if (di == NULL)
7902 return 0;
7903 return get_tv_number(&di->di_tv);
7904}
7905
7906/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007907 * Return an allocated string with the string representation of a Dictionary.
7908 * May return NULL.
7909 */
7910 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007911dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007912{
7913 garray_T ga;
7914 int first = TRUE;
7915 char_u *tofree;
7916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007918 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007919 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007920 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007922 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 return NULL;
7924 ga_init2(&ga, (int)sizeof(char), 80);
7925 ga_append(&ga, '{');
7926
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007927 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007928 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007930 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007931 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007932 --todo;
7933
7934 if (first)
7935 first = FALSE;
7936 else
7937 ga_concat(&ga, (char_u *)", ");
7938
7939 tofree = string_quote(hi->hi_key, FALSE);
7940 if (tofree != NULL)
7941 {
7942 ga_concat(&ga, tofree);
7943 vim_free(tofree);
7944 }
7945 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007946 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7947 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007948 if (s != NULL)
7949 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007951 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007952 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007953 line_breakcheck();
7954
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007957 if (todo > 0)
7958 {
7959 vim_free(ga.ga_data);
7960 return NULL;
7961 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007962
7963 ga_append(&ga, '}');
7964 ga_append(&ga, NUL);
7965 return (char_u *)ga.ga_data;
7966}
7967
7968/*
7969 * Allocate a variable for a Dictionary and fill it from "*arg".
7970 * Return OK or FAIL. Returns NOTDONE for {expr}.
7971 */
7972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007973get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007974{
Bram Moolenaar33570922005-01-25 22:26:29 +00007975 dict_T *d = NULL;
7976 typval_T tvkey;
7977 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007978 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007981 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982
7983 /*
7984 * First check if it's not a curly-braces thing: {expr}.
7985 * Must do this without evaluating, otherwise a function may be called
7986 * twice. Unfortunately this means we need to call eval1() twice for the
7987 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 if (*start != '}')
7991 {
7992 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7993 return FAIL;
7994 if (*start == '}')
7995 return NOTDONE;
7996 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007997
7998 if (evaluate)
7999 {
8000 d = dict_alloc();
8001 if (d == NULL)
8002 return FAIL;
8003 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008004 tvkey.v_type = VAR_UNKNOWN;
8005 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006
8007 *arg = skipwhite(*arg + 1);
8008 while (**arg != '}' && **arg != NUL)
8009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008010 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 goto failret;
8012 if (**arg != ':')
8013 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008014 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008015 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008016 goto failret;
8017 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008018 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008020 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008021 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008022 {
8023 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008024 clear_tv(&tvkey);
8025 goto failret;
8026 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008027 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028
8029 *arg = skipwhite(*arg + 1);
8030 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8031 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008032 if (evaluate)
8033 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008034 goto failret;
8035 }
8036 if (evaluate)
8037 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008038 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008039 if (item != NULL)
8040 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008041 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008042 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008043 clear_tv(&tv);
8044 goto failret;
8045 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008046 item = dictitem_alloc(key);
8047 clear_tv(&tvkey);
8048 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008050 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008051 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008052 if (dict_add(d, item) == FAIL)
8053 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 }
8055 }
8056
8057 if (**arg == '}')
8058 break;
8059 if (**arg != ',')
8060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008061 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008062 goto failret;
8063 }
8064 *arg = skipwhite(*arg + 1);
8065 }
8066
8067 if (**arg != '}')
8068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008069 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008070failret:
8071 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008072 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008073 return FAIL;
8074 }
8075
8076 *arg = skipwhite(*arg + 1);
8077 if (evaluate)
8078 {
8079 rettv->v_type = VAR_DICT;
8080 rettv->vval.v_dict = d;
8081 ++d->dv_refcount;
8082 }
8083
8084 return OK;
8085}
8086
Bram Moolenaar17a13432016-01-24 14:22:10 +01008087 static char *
8088get_var_special_name(int nr)
8089{
8090 switch (nr)
8091 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008092 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008093 case VVAL_TRUE: return "v:true";
8094 case VVAL_NONE: return "v:none";
8095 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008096 }
8097 EMSG2(_(e_intern2), "get_var_special_name()");
8098 return "42";
8099}
8100
Bram Moolenaar8c711452005-01-14 21:53:12 +00008101/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 * Return a string with the string representation of a variable.
8103 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008104 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008105 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008106 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8107 * "string()", otherwise does not put quotes around strings, as ":echo"
8108 * displays values.
8109 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8110 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008111 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112 */
8113 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008114echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008115 typval_T *tv,
8116 char_u **tofree,
8117 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008118 int copyID,
8119 int echo_style,
8120 int restore_copyID,
8121 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 static int recurse = 0;
8124 char_u *r = NULL;
8125
Bram Moolenaar33570922005-01-25 22:26:29 +00008126 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008127 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008128 if (!did_echo_string_emsg)
8129 {
8130 /* Only give this message once for a recursive call to avoid
8131 * flooding the user with errors. And stop iterating over lists
8132 * and dicts. */
8133 did_echo_string_emsg = TRUE;
8134 EMSG(_("E724: variable nested too deep for displaying"));
8135 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008136 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008137 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008138 }
8139 ++recurse;
8140
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008141 switch (tv->v_type)
8142 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008143 case VAR_STRING:
8144 if (echo_style && !dict_val)
8145 {
8146 *tofree = NULL;
8147 r = get_tv_string_buf(tv, numbuf);
8148 }
8149 else
8150 {
8151 *tofree = string_quote(tv->vval.v_string, FALSE);
8152 r = *tofree;
8153 }
8154 break;
8155
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008156 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008157 if (echo_style)
8158 {
8159 *tofree = NULL;
8160 r = tv->vval.v_string;
8161 }
8162 else
8163 {
8164 *tofree = string_quote(tv->vval.v_string, TRUE);
8165 r = *tofree;
8166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008168
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008169 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008170 {
8171 partial_T *pt = tv->vval.v_partial;
8172 char_u *fname = string_quote(pt == NULL ? NULL
8173 : pt->pt_name, FALSE);
8174 garray_T ga;
8175 int i;
8176 char_u *tf;
8177
8178 ga_init2(&ga, 1, 100);
8179 ga_concat(&ga, (char_u *)"function(");
8180 if (fname != NULL)
8181 {
8182 ga_concat(&ga, fname);
8183 vim_free(fname);
8184 }
8185 if (pt != NULL && pt->pt_argc > 0)
8186 {
8187 ga_concat(&ga, (char_u *)", [");
8188 for (i = 0; i < pt->pt_argc; ++i)
8189 {
8190 if (i > 0)
8191 ga_concat(&ga, (char_u *)", ");
8192 ga_concat(&ga,
8193 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8194 vim_free(tf);
8195 }
8196 ga_concat(&ga, (char_u *)"]");
8197 }
8198 if (pt != NULL && pt->pt_dict != NULL)
8199 {
8200 typval_T dtv;
8201
8202 ga_concat(&ga, (char_u *)", ");
8203 dtv.v_type = VAR_DICT;
8204 dtv.vval.v_dict = pt->pt_dict;
8205 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8206 vim_free(tf);
8207 }
8208 ga_concat(&ga, (char_u *)")");
8209
8210 *tofree = ga.ga_data;
8211 r = *tofree;
8212 break;
8213 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008214
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008216 if (tv->vval.v_list == NULL)
8217 {
8218 *tofree = NULL;
8219 r = NULL;
8220 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008221 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8222 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008223 {
8224 *tofree = NULL;
8225 r = (char_u *)"[...]";
8226 }
8227 else
8228 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008229 int old_copyID = tv->vval.v_list->lv_copyID;
8230
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008231 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008232 *tofree = list2string(tv, copyID, restore_copyID);
8233 if (restore_copyID)
8234 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008235 r = *tofree;
8236 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008237 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008238
Bram Moolenaar8c711452005-01-14 21:53:12 +00008239 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008240 if (tv->vval.v_dict == NULL)
8241 {
8242 *tofree = NULL;
8243 r = NULL;
8244 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008245 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8246 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008247 {
8248 *tofree = NULL;
8249 r = (char_u *)"{...}";
8250 }
8251 else
8252 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008253 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008254 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008255 *tofree = dict2string(tv, copyID, restore_copyID);
8256 if (restore_copyID)
8257 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008258 r = *tofree;
8259 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008260 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008261
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008263 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008264 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008265 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008266 *tofree = NULL;
8267 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008269
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008270 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008271#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008272 *tofree = NULL;
8273 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8274 r = numbuf;
8275 break;
8276#endif
8277
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008278 case VAR_SPECIAL:
8279 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008280 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008281 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008282 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008283
Bram Moolenaar8502c702014-06-17 12:51:16 +02008284 if (--recurse == 0)
8285 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008287}
8288
8289/*
8290 * Return a string with the string representation of a variable.
8291 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8292 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008293 * Does not put quotes around strings, as ":echo" displays values.
8294 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8295 * May return NULL.
8296 */
8297 static char_u *
8298echo_string(
8299 typval_T *tv,
8300 char_u **tofree,
8301 char_u *numbuf,
8302 int copyID)
8303{
8304 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8305}
8306
8307/*
8308 * Return a string with the string representation of a variable.
8309 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8310 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008311 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008312 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008313 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008314 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008315tv2string(
8316 typval_T *tv,
8317 char_u **tofree,
8318 char_u *numbuf,
8319 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008320{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008321 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322}
8323
8324/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008325 * Return string "str" in ' quotes, doubling ' characters.
8326 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008327 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008328 */
8329 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008330string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008331{
Bram Moolenaar33570922005-01-25 22:26:29 +00008332 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 char_u *p, *r, *s;
8334
Bram Moolenaar33570922005-01-25 22:26:29 +00008335 len = (function ? 13 : 3);
8336 if (str != NULL)
8337 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008338 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008339 for (p = str; *p != NUL; mb_ptr_adv(p))
8340 if (*p == '\'')
8341 ++len;
8342 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008343 s = r = alloc(len);
8344 if (r != NULL)
8345 {
8346 if (function)
8347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008348 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008349 r += 10;
8350 }
8351 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008352 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 if (str != NULL)
8354 for (p = str; *p != NUL; )
8355 {
8356 if (*p == '\'')
8357 *r++ = '\'';
8358 MB_COPY_CHAR(p, r);
8359 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008361 if (function)
8362 *r++ = ')';
8363 *r++ = NUL;
8364 }
8365 return s;
8366}
8367
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008368#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369/*
8370 * Convert the string "text" to a floating point number.
8371 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8372 * this always uses a decimal point.
8373 * Returns the length of the text that was consumed.
8374 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008376string2float(
8377 char_u *text,
8378 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008379{
8380 char *s = (char *)text;
8381 float_T f;
8382
8383 f = strtod(s, &s);
8384 *value = f;
8385 return (int)((char_u *)s - text);
8386}
8387#endif
8388
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 * Get the value of an environment variable.
8391 * "arg" is pointing to the '$'. It is advanced to after the name.
8392 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008393 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 */
8395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008396get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
8398 char_u *string = NULL;
8399 int len;
8400 int cc;
8401 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008402 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 ++*arg;
8405 name = *arg;
8406 len = get_env_len(arg);
8407 if (evaluate)
8408 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008409 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008410 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008411
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008412 cc = name[len];
8413 name[len] = NUL;
8414 /* first try vim_getenv(), fast for normal environment vars */
8415 string = vim_getenv(name, &mustfree);
8416 if (string != NULL && *string != NUL)
8417 {
8418 if (!mustfree)
8419 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008421 else
8422 {
8423 if (mustfree)
8424 vim_free(string);
8425
8426 /* next try expanding things like $VIM and ${HOME} */
8427 string = expand_env_save(name - 1);
8428 if (string != NULL && *string == '$')
8429 {
8430 vim_free(string);
8431 string = NULL;
8432 }
8433 }
8434 name[len] = cc;
8435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008436 rettv->v_type = VAR_STRING;
8437 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
8439
8440 return OK;
8441}
8442
8443/*
8444 * Array with names and number of arguments of all internal functions
8445 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8446 */
8447static struct fst
8448{
8449 char *f_name; /* function name */
8450 char f_min_argc; /* minimal number of arguments */
8451 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008452 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008453 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454} functions[] =
8455{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008456#ifdef FEAT_FLOAT
8457 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008458 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008459#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008460 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008461 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {"append", 2, 2, f_append},
8463 {"argc", 0, 0, f_argc},
8464 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008465 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008466 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008467#ifdef FEAT_FLOAT
8468 {"asin", 1, 1, f_asin}, /* WJMc */
8469#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008470 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008471 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008472 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008473 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008474 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008475 {"assert_notequal", 2, 3, f_assert_notequal},
8476 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008477 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478#ifdef FEAT_FLOAT
8479 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008483 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"bufexists", 1, 1, f_bufexists},
8485 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8486 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8487 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8488 {"buflisted", 1, 1, f_buflisted},
8489 {"bufloaded", 1, 1, f_bufloaded},
8490 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008491 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008492 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"bufwinnr", 1, 1, f_bufwinnr},
8494 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008495 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008496 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008498#ifdef FEAT_FLOAT
8499 {"ceil", 1, 1, f_ceil},
8500#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008501#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008502 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008503 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8504 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008505 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008506 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008507 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008508 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008509 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008510 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008511 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008512 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008513 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8514 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008515 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008516 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008517#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008518 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008519 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008521 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008523#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008524 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008525 {"complete_add", 1, 1, f_complete_add},
8526 {"complete_check", 0, 0, f_complete_check},
8527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008530#ifdef FEAT_FLOAT
8531 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008532 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008533#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008534 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008536 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008537 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008538 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008540 {"diff_filler", 1, 1, f_diff_filler},
8541 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008542 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008544 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {"eventhandler", 0, 0, f_eventhandler},
8546 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008547 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008549#ifdef FEAT_FLOAT
8550 {"exp", 1, 1, f_exp},
8551#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008552 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008553 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008554 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8556 {"filereadable", 1, 1, f_filereadable},
8557 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008558 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008559 {"finddir", 1, 3, f_finddir},
8560 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008561#ifdef FEAT_FLOAT
8562 {"float2nr", 1, 1, f_float2nr},
8563 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008564 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008565#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008566 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 {"fnamemodify", 2, 2, f_fnamemodify},
8568 {"foldclosed", 1, 1, f_foldclosed},
8569 {"foldclosedend", 1, 1, f_foldclosedend},
8570 {"foldlevel", 1, 1, f_foldlevel},
8571 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008572 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008574 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008575 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008576 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008577 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008578 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 {"getchar", 0, 1, f_getchar},
8580 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008581 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"getcmdline", 0, 0, f_getcmdline},
8583 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008584 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008585 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008586 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008587 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008588 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008589 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 {"getfsize", 1, 1, f_getfsize},
8591 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008592 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008593 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008594 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008595 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008596 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008597 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008598 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008599 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008601 {"gettabvar", 2, 3, f_gettabvar},
8602 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 {"getwinposx", 0, 0, f_getwinposx},
8604 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008605 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008606 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008607 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008608 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008610 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008611 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008612 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8614 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8615 {"histadd", 2, 2, f_histadd},
8616 {"histdel", 1, 2, f_histdel},
8617 {"histget", 1, 2, f_histget},
8618 {"histnr", 1, 1, f_histnr},
8619 {"hlID", 1, 1, f_hlID},
8620 {"hlexists", 1, 1, f_hlexists},
8621 {"hostname", 0, 0, f_hostname},
8622 {"iconv", 3, 3, f_iconv},
8623 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008624 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008625 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008627 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {"inputrestore", 0, 0, f_inputrestore},
8629 {"inputsave", 0, 0, f_inputsave},
8630 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008631 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008632 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008634 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008635#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8636 {"isnan", 1, 1, f_isnan},
8637#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008638 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008639#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008640 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008641 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008642 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008643 {"job_start", 1, 2, f_job_start},
8644 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008645 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008646#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008648 {"js_decode", 1, 1, f_js_decode},
8649 {"js_encode", 1, 1, f_js_encode},
8650 {"json_decode", 1, 1, f_json_decode},
8651 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008652 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {"libcall", 3, 3, f_libcall},
8656 {"libcallnr", 3, 3, f_libcallnr},
8657 {"line", 1, 1, f_line},
8658 {"line2byte", 1, 1, f_line2byte},
8659 {"lispindent", 1, 1, f_lispindent},
8660 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008661#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008662 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008663 {"log10", 1, 1, f_log10},
8664#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008665#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008666 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008667#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008668 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008669 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008670 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008671 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008672 {"matchadd", 2, 5, f_matchadd},
8673 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008674 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008675 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008676 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008677 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008678 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008679 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008680 {"max", 1, 1, f_max},
8681 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008682#ifdef vim_mkdir
8683 {"mkdir", 1, 3, f_mkdir},
8684#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008686#ifdef FEAT_MZSCHEME
8687 {"mzeval", 1, 1, f_mzeval},
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008690 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008691 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008692 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008693#ifdef FEAT_PERL
8694 {"perleval", 1, 1, f_perleval},
8695#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008696#ifdef FEAT_FLOAT
8697 {"pow", 2, 2, f_pow},
8698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008700 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008701 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008702#ifdef FEAT_PYTHON3
8703 {"py3eval", 1, 1, f_py3eval},
8704#endif
8705#ifdef FEAT_PYTHON
8706 {"pyeval", 1, 1, f_pyeval},
8707#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008708 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008709 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008710 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008711#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008712 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008713#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008714 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 {"remote_expr", 2, 3, f_remote_expr},
8716 {"remote_foreground", 1, 1, f_remote_foreground},
8717 {"remote_peek", 1, 2, f_remote_peek},
8718 {"remote_read", 1, 1, f_remote_read},
8719 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008720 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008722 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008724 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008725#ifdef FEAT_FLOAT
8726 {"round", 1, 1, f_round},
8727#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008728 {"screenattr", 2, 2, f_screenattr},
8729 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008730 {"screencol", 0, 0, f_screencol},
8731 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008732 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008733 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008734 {"searchpair", 3, 7, f_searchpair},
8735 {"searchpairpos", 3, 7, f_searchpairpos},
8736 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 {"server2client", 2, 2, f_server2client},
8738 {"serverlist", 0, 0, f_serverlist},
8739 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008740 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008742 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008744 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008745 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008746 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008747 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008749 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008750 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008752#ifdef FEAT_CRYPT
8753 {"sha256", 1, 1, f_sha256},
8754#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008755 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008756 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008758#ifdef FEAT_FLOAT
8759 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008760 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008761#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008762 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008763 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008764 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008765 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008766 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008767#ifdef FEAT_FLOAT
8768 {"sqrt", 1, 1, f_sqrt},
8769 {"str2float", 1, 1, f_str2float},
8770#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008771 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008772 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008773 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008774 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775#ifdef HAVE_STRFTIME
8776 {"strftime", 1, 2, f_strftime},
8777#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008778 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008779 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008780 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {"strlen", 1, 1, f_strlen},
8782 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008783 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008785 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008786 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {"substitute", 4, 4, f_substitute},
8788 {"synID", 3, 3, f_synID},
8789 {"synIDattr", 2, 3, f_synIDattr},
8790 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008791 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008792 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008793 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008794 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008795 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008796 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008797 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008798 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008799 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008800#ifdef FEAT_FLOAT
8801 {"tan", 1, 1, f_tan},
8802 {"tanh", 1, 1, f_tanh},
8803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008805 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8806 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008807 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8808#ifdef FEAT_JOB_CHANNEL
8809 {"test_null_channel", 0, 0, f_test_null_channel},
8810#endif
8811 {"test_null_dict", 0, 0, f_test_null_dict},
8812#ifdef FEAT_JOB_CHANNEL
8813 {"test_null_job", 0, 0, f_test_null_job},
8814#endif
8815 {"test_null_list", 0, 0, f_test_null_list},
8816 {"test_null_partial", 0, 0, f_test_null_partial},
8817 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008818#ifdef FEAT_TIMERS
8819 {"timer_start", 2, 3, f_timer_start},
8820 {"timer_stop", 1, 1, f_timer_stop},
8821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {"tolower", 1, 1, f_tolower},
8823 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008824 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008825#ifdef FEAT_FLOAT
8826 {"trunc", 1, 1, f_trunc},
8827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008829 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008830 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008831 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008832 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {"virtcol", 1, 1, f_virtcol},
8834 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008835 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008836 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008837 {"win_getid", 0, 2, f_win_getid},
8838 {"win_gotoid", 1, 1, f_win_gotoid},
8839 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8840 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {"winbufnr", 1, 1, f_winbufnr},
8842 {"wincol", 0, 0, f_wincol},
8843 {"winheight", 1, 1, f_winheight},
8844 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008845 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008847 {"winrestview", 1, 1, f_winrestview},
8848 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008850 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008851 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008852 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853};
8854
8855#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8856
8857/*
8858 * Function given to ExpandGeneric() to obtain the list of internal
8859 * or user defined function names.
8860 */
8861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008862get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
8864 static int intidx = -1;
8865 char_u *name;
8866
8867 if (idx == 0)
8868 intidx = -1;
8869 if (intidx < 0)
8870 {
8871 name = get_user_func_name(xp, idx);
8872 if (name != NULL)
8873 return name;
8874 }
8875 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8876 {
8877 STRCPY(IObuff, functions[intidx].f_name);
8878 STRCAT(IObuff, "(");
8879 if (functions[intidx].f_max_argc == 0)
8880 STRCAT(IObuff, ")");
8881 return IObuff;
8882 }
8883
8884 return NULL;
8885}
8886
8887/*
8888 * Function given to ExpandGeneric() to obtain the list of internal or
8889 * user defined variable or function names.
8890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008892get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893{
8894 static int intidx = -1;
8895 char_u *name;
8896
8897 if (idx == 0)
8898 intidx = -1;
8899 if (intidx < 0)
8900 {
8901 name = get_function_name(xp, idx);
8902 if (name != NULL)
8903 return name;
8904 }
8905 return get_user_var_name(xp, ++intidx);
8906}
8907
8908#endif /* FEAT_CMDL_COMPL */
8909
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008910#if defined(EBCDIC) || defined(PROTO)
8911/*
8912 * Compare struct fst by function name.
8913 */
8914 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008915compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008916{
8917 struct fst *p1 = (struct fst *)s1;
8918 struct fst *p2 = (struct fst *)s2;
8919
8920 return STRCMP(p1->f_name, p2->f_name);
8921}
8922
8923/*
8924 * Sort the function table by function name.
8925 * The sorting of the table above is ASCII dependant.
8926 * On machines using EBCDIC we have to sort it.
8927 */
8928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008929sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008930{
8931 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8932
8933 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8934}
8935#endif
8936
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938/*
8939 * Find internal function in table above.
8940 * Return index, or -1 if not found
8941 */
8942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008943find_internal_func(
8944 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945{
8946 int first = 0;
8947 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8948 int cmp;
8949 int x;
8950
8951 /*
8952 * Find the function name in the table. Binary search.
8953 */
8954 while (first <= last)
8955 {
8956 x = first + ((unsigned)(last - first) >> 1);
8957 cmp = STRCMP(name, functions[x].f_name);
8958 if (cmp < 0)
8959 last = x - 1;
8960 else if (cmp > 0)
8961 first = x + 1;
8962 else
8963 return x;
8964 }
8965 return -1;
8966}
8967
8968/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8970 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008971 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8972 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008973 */
8974 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008975deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976{
Bram Moolenaar33570922005-01-25 22:26:29 +00008977 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008978 int cc;
8979
Bram Moolenaar65639032016-03-16 21:40:30 +01008980 if (partialp != NULL)
8981 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008982
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 cc = name[*lenp];
8984 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008985 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008990 {
8991 *lenp = 0;
8992 return (char_u *)""; /* just in case */
8993 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008994 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008996 }
8997
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008998 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8999 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009000 partial_T *pt = v->di_tv.vval.v_partial;
9001
9002 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009003 {
9004 *lenp = 0;
9005 return (char_u *)""; /* just in case */
9006 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009007 if (partialp != NULL)
9008 *partialp = pt;
9009 *lenp = (int)STRLEN(pt->pt_name);
9010 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009011 }
9012
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009013 return name;
9014}
9015
9016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 * Allocate a variable for the result of a function.
9018 * Return OK or FAIL.
9019 */
9020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009021get_func_tv(
9022 char_u *name, /* name of the function */
9023 int len, /* length of "name" */
9024 typval_T *rettv,
9025 char_u **arg, /* argument, pointing to the '(' */
9026 linenr_T firstline, /* first line of range */
9027 linenr_T lastline, /* last line of range */
9028 int *doesrange, /* return: function handled range */
9029 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009030 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009031 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 char_u *argp;
9034 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009035 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 int argcount = 0; /* number of arguments found */
9037
9038 /*
9039 * Get the arguments.
9040 */
9041 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009042 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 {
9044 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9045 if (*argp == ')' || *argp == ',' || *argp == NUL)
9046 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9048 {
9049 ret = FAIL;
9050 break;
9051 }
9052 ++argcount;
9053 if (*argp != ',')
9054 break;
9055 }
9056 if (*argp == ')')
9057 ++argp;
9058 else
9059 ret = FAIL;
9060
9061 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009062 {
9063 int i = 0;
9064
9065 if (get_vim_var_nr(VV_TESTING))
9066 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009067 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009068 * what variables are used on the call stack. */
9069 if (funcargs.ga_itemsize == 0)
9070 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9071 for (i = 0; i < argcount; ++i)
9072 if (ga_grow(&funcargs, 1) == OK)
9073 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9074 &argvars[i];
9075 }
9076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009078 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009079
9080 funcargs.ga_len -= i;
9081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 {
9084 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009085 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009087 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
9093 *arg = skipwhite(argp);
9094 return ret;
9095}
9096
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009097#define ERROR_UNKNOWN 0
9098#define ERROR_TOOMANY 1
9099#define ERROR_TOOFEW 2
9100#define ERROR_SCRIPT 3
9101#define ERROR_DICT 4
9102#define ERROR_NONE 5
9103#define ERROR_OTHER 6
9104#define FLEN_FIXED 40
9105
9106/*
9107 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9108 * Change <SNR>123_name() to K_SNR 123_name().
9109 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9110 * (slow).
9111 */
9112 static char_u *
9113fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9114{
9115 int llen;
9116 char_u *fname;
9117 int i;
9118
9119 llen = eval_fname_script(name);
9120 if (llen > 0)
9121 {
9122 fname_buf[0] = K_SPECIAL;
9123 fname_buf[1] = KS_EXTRA;
9124 fname_buf[2] = (int)KE_SNR;
9125 i = 3;
9126 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9127 {
9128 if (current_SID <= 0)
9129 *error = ERROR_SCRIPT;
9130 else
9131 {
9132 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9133 i = (int)STRLEN(fname_buf);
9134 }
9135 }
9136 if (i + STRLEN(name + llen) < FLEN_FIXED)
9137 {
9138 STRCPY(fname_buf + i, name + llen);
9139 fname = fname_buf;
9140 }
9141 else
9142 {
9143 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9144 if (fname == NULL)
9145 *error = ERROR_OTHER;
9146 else
9147 {
9148 *tofree = fname;
9149 mch_memmove(fname, fname_buf, (size_t)i);
9150 STRCPY(fname + i, name + llen);
9151 }
9152 }
9153 }
9154 else
9155 fname = name;
9156 return fname;
9157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158
9159/*
9160 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009161 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009162 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009165call_func(
9166 char_u *funcname, /* name of the function */
9167 int len, /* length of "name" */
9168 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009169 int argcount_in, /* number of "argvars" */
9170 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009171 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009172 linenr_T firstline, /* first line of range */
9173 linenr_T lastline, /* last line of range */
9174 int *doesrange, /* return: function handled range */
9175 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009176 partial_T *partial, /* optional, can be NULL */
9177 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 int error = ERROR_NONE;
9181 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009184 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009186 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009187 int argcount = argcount_in;
9188 typval_T *argvars = argvars_in;
9189 dict_T *selfdict = selfdict_in;
9190 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9191 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009192
9193 /* Make a copy of the name, if it comes from a funcref variable it could
9194 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009195 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009196 if (name == NULL)
9197 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009199 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
9201 *doesrange = FALSE;
9202
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009203 if (partial != NULL)
9204 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009205 /* When the function has a partial with a dict and there is a dict
9206 * argument, use the dict argument. That is backwards compatible.
9207 * When the dict was bound explicitly use the one from the partial. */
9208 if (partial->pt_dict != NULL
9209 && (selfdict_in == NULL || !partial->pt_auto))
9210 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009211 if (error == ERROR_NONE && partial->pt_argc > 0)
9212 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009213 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9214 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9215 for (i = 0; i < argcount_in; ++i)
9216 argv[i + argv_clear] = argvars_in[i];
9217 argvars = argv;
9218 argcount = partial->pt_argc + argcount_in;
9219 }
9220 }
9221
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
9223 /* execute the function if no errors detected and executing */
9224 if (evaluate && error == ERROR_NONE)
9225 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009226 char_u *rfname = fname;
9227
9228 /* Ignore "g:" before a function name. */
9229 if (fname[0] == 'g' && fname[1] == ':')
9230 rfname = fname + 2;
9231
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009232 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9233 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 error = ERROR_UNKNOWN;
9235
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009236 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 {
9238 /*
9239 * User defined function.
9240 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009241 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009242
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009244 /* Trigger FuncUndefined event, may load the function. */
9245 if (fp == NULL
9246 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009247 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009248 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009250 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009251 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 }
9253#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009254 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009255 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009256 {
9257 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009258 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009259 }
9260
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 if (fp != NULL)
9262 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009263 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009265 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009267 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009269 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009270 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 else
9272 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009273 int did_save_redo = FALSE;
9274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 /*
9276 * Call the user function.
9277 * Save and restore search patterns, script variables and
9278 * redo buffer.
9279 */
9280 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009281#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009282 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009283#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009284 {
9285 saveRedobuff();
9286 did_save_redo = TRUE;
9287 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009288 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009289 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009291 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9292 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9293 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009294 /* Function was unreferenced while being used, free it
9295 * now. */
9296 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009297 if (did_save_redo)
9298 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 restore_search_patterns();
9300 error = ERROR_NONE;
9301 }
9302 }
9303 }
9304 else
9305 {
9306 /*
9307 * Find the function name in the table, call its implementation.
9308 */
9309 i = find_internal_func(fname);
9310 if (i >= 0)
9311 {
9312 if (argcount < functions[i].f_min_argc)
9313 error = ERROR_TOOFEW;
9314 else if (argcount > functions[i].f_max_argc)
9315 error = ERROR_TOOMANY;
9316 else
9317 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009318 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009319 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 error = ERROR_NONE;
9321 }
9322 }
9323 }
9324 /*
9325 * The function call (or "FuncUndefined" autocommand sequence) might
9326 * have been aborted by an error, an interrupt, or an explicitly thrown
9327 * exception that has not been caught so far. This situation can be
9328 * tested for by calling aborting(). For an error in an internal
9329 * function or for the "E132" error in call_user_func(), however, the
9330 * throw point at which the "force_abort" flag (temporarily reset by
9331 * emsg()) is normally updated has not been reached yet. We need to
9332 * update that flag first to make aborting() reliable.
9333 */
9334 update_force_abort();
9335 }
9336 if (error == ERROR_NONE)
9337 ret = OK;
9338
9339 /*
9340 * Report an error unless the argument evaluation or function call has been
9341 * cancelled due to an aborting error, an interrupt, or an exception.
9342 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009343 if (!aborting())
9344 {
9345 switch (error)
9346 {
9347 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009348 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009349 break;
9350 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009351 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009352 break;
9353 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009354 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009355 name);
9356 break;
9357 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009358 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009359 name);
9360 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009361 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009362 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009363 name);
9364 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009365 }
9366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009368 while (argv_clear > 0)
9369 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009370 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009371 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372
9373 return ret;
9374}
9375
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009376/*
9377 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009378 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009379 */
9380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009381emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009382{
9383 char_u *p;
9384
9385 if (*name == K_SPECIAL)
9386 p = concat_str((char_u *)"<SNR>", name + 3);
9387 else
9388 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009389 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009390 if (p != name)
9391 vim_free(p);
9392}
9393
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009394/*
9395 * Return TRUE for a non-zero Number and a non-empty String.
9396 */
9397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009398non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009399{
9400 return ((argvars[0].v_type == VAR_NUMBER
9401 && argvars[0].vval.v_number != 0)
9402 || (argvars[0].v_type == VAR_STRING
9403 && argvars[0].vval.v_string != NULL
9404 && *argvars[0].vval.v_string != NUL));
9405}
9406
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407/*********************************************
9408 * Implementation of the built-in functions
9409 */
9410
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009411#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009412static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009413
9414/*
9415 * Get the float value of "argvars[0]" into "f".
9416 * Returns FAIL when the argument is not a Number or Float.
9417 */
9418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009419get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009420{
9421 if (argvars[0].v_type == VAR_FLOAT)
9422 {
9423 *f = argvars[0].vval.v_float;
9424 return OK;
9425 }
9426 if (argvars[0].v_type == VAR_NUMBER)
9427 {
9428 *f = (float_T)argvars[0].vval.v_number;
9429 return OK;
9430 }
9431 EMSG(_("E808: Number or Float required"));
9432 return FAIL;
9433}
9434
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009435/*
9436 * "abs(expr)" function
9437 */
9438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009439f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009440{
9441 if (argvars[0].v_type == VAR_FLOAT)
9442 {
9443 rettv->v_type = VAR_FLOAT;
9444 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9445 }
9446 else
9447 {
9448 varnumber_T n;
9449 int error = FALSE;
9450
9451 n = get_tv_number_chk(&argvars[0], &error);
9452 if (error)
9453 rettv->vval.v_number = -1;
9454 else if (n > 0)
9455 rettv->vval.v_number = n;
9456 else
9457 rettv->vval.v_number = -n;
9458 }
9459}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009460
9461/*
9462 * "acos()" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009466{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009467 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009468
9469 rettv->v_type = VAR_FLOAT;
9470 if (get_float_arg(argvars, &f) == OK)
9471 rettv->vval.v_float = acos(f);
9472 else
9473 rettv->vval.v_float = 0.0;
9474}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009475#endif
9476
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009478 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479 */
9480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009481f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
Bram Moolenaar33570922005-01-25 22:26:29 +00009483 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009486 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009488 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009489 && !tv_check_lock(l->lv_lock,
9490 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009491 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009492 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009493 }
9494 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009495 EMSG(_(e_listreq));
9496}
9497
9498/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009499 * "and(expr, expr)" function
9500 */
9501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009502f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009503{
9504 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9505 & get_tv_number_chk(&argvars[1], NULL);
9506}
9507
9508/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009509 * "append(lnum, string/list)" function
9510 */
9511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009512f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009513{
9514 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009515 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009516 list_T *l = NULL;
9517 listitem_T *li = NULL;
9518 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009519 long added = 0;
9520
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009521 /* When coming here from Insert mode, sync undo, so that this can be
9522 * undone separately from what was previously inserted. */
9523 if (u_sync_once == 2)
9524 {
9525 u_sync_once = 1; /* notify that u_sync() was called */
9526 u_sync(TRUE);
9527 }
9528
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529 lnum = get_tv_lnum(argvars);
9530 if (lnum >= 0
9531 && lnum <= curbuf->b_ml.ml_line_count
9532 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009534 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009535 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009536 l = argvars[1].vval.v_list;
9537 if (l == NULL)
9538 return;
9539 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009540 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009541 for (;;)
9542 {
9543 if (l == NULL)
9544 tv = &argvars[1]; /* append a string */
9545 else if (li == NULL)
9546 break; /* end of list */
9547 else
9548 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 line = get_tv_string_chk(tv);
9550 if (line == NULL) /* type error */
9551 {
9552 rettv->vval.v_number = 1; /* Failed */
9553 break;
9554 }
9555 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009556 ++added;
9557 if (l == NULL)
9558 break;
9559 li = li->li_next;
9560 }
9561
9562 appended_lines_mark(lnum, added);
9563 if (curwin->w_cursor.lnum > lnum)
9564 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 else
9567 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568}
9569
9570/*
9571 * "argc()" function
9572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009574f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577}
9578
9579/*
9580 * "argidx()" function
9581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009583f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586}
9587
9588/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009589 * "arglistid()" function
9590 */
9591 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009592f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009593{
9594 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009595
9596 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009597 wp = find_tabwin(&argvars[0], &argvars[1]);
9598 if (wp != NULL)
9599 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009600}
9601
9602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 * "argv(nr)" function
9604 */
9605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009606f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
9608 int idx;
9609
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009610 if (argvars[0].v_type != VAR_UNKNOWN)
9611 {
9612 idx = get_tv_number_chk(&argvars[0], NULL);
9613 if (idx >= 0 && idx < ARGCOUNT)
9614 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9615 else
9616 rettv->vval.v_string = NULL;
9617 rettv->v_type = VAR_STRING;
9618 }
9619 else if (rettv_list_alloc(rettv) == OK)
9620 for (idx = 0; idx < ARGCOUNT; ++idx)
9621 list_append_string(rettv->vval.v_list,
9622 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623}
9624
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009625typedef enum
9626{
9627 ASSERT_EQUAL,
9628 ASSERT_NOTEQUAL,
9629 ASSERT_MATCH,
9630 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009631 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009632} assert_type_T;
9633
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009634static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009635static 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 +01009636static void assert_error(garray_T *gap);
9637static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009638
9639/*
9640 * Prepare "gap" for an assert error and add the sourcing position.
9641 */
9642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009644{
9645 char buf[NUMBUFLEN];
9646
9647 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009648 if (sourcing_name != NULL)
9649 {
9650 ga_concat(gap, sourcing_name);
9651 if (sourcing_lnum > 0)
9652 ga_concat(gap, (char_u *)" ");
9653 }
9654 if (sourcing_lnum > 0)
9655 {
9656 sprintf(buf, "line %ld", (long)sourcing_lnum);
9657 ga_concat(gap, (char_u *)buf);
9658 }
9659 if (sourcing_name != NULL || sourcing_lnum > 0)
9660 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009661}
9662
9663/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009664 * Append "str" to "gap", escaping unprintable characters.
9665 * Changes NL to \n, CR to \r, etc.
9666 */
9667 static void
9668ga_concat_esc(garray_T *gap, char_u *str)
9669{
9670 char_u *p;
9671 char_u buf[NUMBUFLEN];
9672
Bram Moolenaarf1551962016-03-15 12:55:58 +01009673 if (str == NULL)
9674 {
9675 ga_concat(gap, (char_u *)"NULL");
9676 return;
9677 }
9678
Bram Moolenaar23689172016-02-15 22:37:37 +01009679 for (p = str; *p != NUL; ++p)
9680 switch (*p)
9681 {
9682 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9683 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9684 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9685 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9686 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9687 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9688 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9689 default:
9690 if (*p < ' ')
9691 {
9692 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9693 ga_concat(gap, buf);
9694 }
9695 else
9696 ga_append(gap, *p);
9697 break;
9698 }
9699}
9700
9701/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009702 * Fill "gap" with information about an assert error.
9703 */
9704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009705fill_assert_error(
9706 garray_T *gap,
9707 typval_T *opt_msg_tv,
9708 char_u *exp_str,
9709 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009710 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009711 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009712{
9713 char_u numbuf[NUMBUFLEN];
9714 char_u *tofree;
9715
9716 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9717 {
9718 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9719 vim_free(tofree);
9720 }
9721 else
9722 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009723 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009724 ga_concat(gap, (char_u *)"Pattern ");
9725 else
9726 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009727 if (exp_str == NULL)
9728 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009729 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009730 vim_free(tofree);
9731 }
9732 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009733 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009734 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009735 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009736 else if (atype == ASSERT_NOTMATCH)
9737 ga_concat(gap, (char_u *)" does match ");
9738 else if (atype == ASSERT_NOTEQUAL)
9739 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009740 else
9741 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009742 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009743 vim_free(tofree);
9744 }
9745}
Bram Moolenaar43345542015-11-29 17:35:35 +01009746
9747/*
9748 * Add an assert error to v:errors.
9749 */
9750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009751assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009752{
9753 struct vimvar *vp = &vimvars[VV_ERRORS];
9754
9755 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9756 /* Make sure v:errors is a list. */
9757 set_vim_var_list(VV_ERRORS, list_alloc());
9758 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9759}
9760
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009761 static void
9762assert_equal_common(typval_T *argvars, assert_type_T atype)
9763{
9764 garray_T ga;
9765
9766 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9767 != (atype == ASSERT_EQUAL))
9768 {
9769 prepare_assert_error(&ga);
9770 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9771 atype);
9772 assert_error(&ga);
9773 ga_clear(&ga);
9774 }
9775}
9776
Bram Moolenaar43345542015-11-29 17:35:35 +01009777/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009778 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009779 */
9780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009781f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009782{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009783 assert_equal_common(argvars, ASSERT_EQUAL);
9784}
Bram Moolenaar43345542015-11-29 17:35:35 +01009785
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009786/*
9787 * "assert_notequal(expected, actual[, msg])" function
9788 */
9789 static void
9790f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9791{
9792 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009793}
9794
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009795/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009796 * "assert_exception(string[, msg])" function
9797 */
9798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009799f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009800{
9801 garray_T ga;
9802 char *error;
9803
9804 error = (char *)get_tv_string_chk(&argvars[0]);
9805 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9806 {
9807 prepare_assert_error(&ga);
9808 ga_concat(&ga, (char_u *)"v:exception is not set");
9809 assert_error(&ga);
9810 ga_clear(&ga);
9811 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009812 else if (error != NULL
9813 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009814 {
9815 prepare_assert_error(&ga);
9816 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009817 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009818 assert_error(&ga);
9819 ga_clear(&ga);
9820 }
9821}
9822
9823/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009824 * "assert_fails(cmd [, error])" function
9825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009828{
9829 char_u *cmd = get_tv_string_chk(&argvars[0]);
9830 garray_T ga;
9831
9832 called_emsg = FALSE;
9833 suppress_errthrow = TRUE;
9834 emsg_silent = TRUE;
9835 do_cmdline_cmd(cmd);
9836 if (!called_emsg)
9837 {
9838 prepare_assert_error(&ga);
9839 ga_concat(&ga, (char_u *)"command did not fail: ");
9840 ga_concat(&ga, cmd);
9841 assert_error(&ga);
9842 ga_clear(&ga);
9843 }
9844 else if (argvars[1].v_type != VAR_UNKNOWN)
9845 {
9846 char_u buf[NUMBUFLEN];
9847 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9848
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009849 if (error == NULL
9850 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009851 {
9852 prepare_assert_error(&ga);
9853 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009854 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009855 assert_error(&ga);
9856 ga_clear(&ga);
9857 }
9858 }
9859
9860 called_emsg = FALSE;
9861 suppress_errthrow = FALSE;
9862 emsg_silent = FALSE;
9863 emsg_on_display = FALSE;
9864 set_vim_var_string(VV_ERRMSG, NULL, 0);
9865}
9866
9867/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009868 * Common for assert_true() and assert_false().
9869 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009871assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009872{
9873 int error = FALSE;
9874 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009875
Bram Moolenaar37127922016-02-06 20:29:28 +01009876 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009877 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009878 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009879 if (argvars[0].v_type != VAR_NUMBER
9880 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9881 || error)
9882 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009883 prepare_assert_error(&ga);
9884 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009885 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009886 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009887 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009888 ga_clear(&ga);
9889 }
9890}
9891
9892/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009893 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009894 */
9895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009896f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009897{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009898 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009899}
9900
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009901 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009902assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009903{
9904 garray_T ga;
9905 char_u buf1[NUMBUFLEN];
9906 char_u buf2[NUMBUFLEN];
9907 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9908 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9909
Bram Moolenaar72188e92016-03-28 22:48:29 +02009910 if (pat == NULL || text == NULL)
9911 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009912 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009913 {
9914 prepare_assert_error(&ga);
9915 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009916 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009917 assert_error(&ga);
9918 ga_clear(&ga);
9919 }
9920}
9921
9922/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009923 * "assert_match(pattern, actual[, msg])" function
9924 */
9925 static void
9926f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9927{
9928 assert_match_common(argvars, ASSERT_MATCH);
9929}
9930
9931/*
9932 * "assert_notmatch(pattern, actual[, msg])" function
9933 */
9934 static void
9935f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9936{
9937 assert_match_common(argvars, ASSERT_NOTMATCH);
9938}
9939
9940/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009941 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009942 */
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009945{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009946 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009947}
9948
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009949#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009950/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009951 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009952 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009954f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009955{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009956 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009957
9958 rettv->v_type = VAR_FLOAT;
9959 if (get_float_arg(argvars, &f) == OK)
9960 rettv->vval.v_float = asin(f);
9961 else
9962 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009963}
9964
9965/*
9966 * "atan()" function
9967 */
9968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009969f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009970{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009971 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009972
9973 rettv->v_type = VAR_FLOAT;
9974 if (get_float_arg(argvars, &f) == OK)
9975 rettv->vval.v_float = atan(f);
9976 else
9977 rettv->vval.v_float = 0.0;
9978}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009979
9980/*
9981 * "atan2()" function
9982 */
9983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009984f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009985{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009986 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009987
9988 rettv->v_type = VAR_FLOAT;
9989 if (get_float_arg(argvars, &fx) == OK
9990 && get_float_arg(&argvars[1], &fy) == OK)
9991 rettv->vval.v_float = atan2(fx, fy);
9992 else
9993 rettv->vval.v_float = 0.0;
9994}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009995#endif
9996
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997/*
9998 * "browse(save, title, initdir, default)" function
9999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010001f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002{
10003#ifdef FEAT_BROWSE
10004 int save;
10005 char_u *title;
10006 char_u *initdir;
10007 char_u *defname;
10008 char_u buf[NUMBUFLEN];
10009 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 save = get_tv_number_chk(&argvars[0], &error);
10013 title = get_tv_string_chk(&argvars[1]);
10014 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10015 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010017 if (error || title == NULL || initdir == NULL || defname == NULL)
10018 rettv->vval.v_string = NULL;
10019 else
10020 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010021 do_browse(save ? BROWSE_SAVE : 0,
10022 title, defname, NULL, initdir, NULL, curbuf);
10023#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010025#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010026 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010027}
10028
10029/*
10030 * "browsedir(title, initdir)" function
10031 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010033f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010034{
10035#ifdef FEAT_BROWSE
10036 char_u *title;
10037 char_u *initdir;
10038 char_u buf[NUMBUFLEN];
10039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010040 title = get_tv_string_chk(&argvars[0]);
10041 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 if (title == NULL || initdir == NULL)
10044 rettv->vval.v_string = NULL;
10045 else
10046 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010047 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010051 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052}
10053
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010054static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056/*
10057 * Find a buffer by number or exact name.
10058 */
10059 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010060find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061{
10062 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010064 if (avar->v_type == VAR_NUMBER)
10065 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010066 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010068 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010069 if (buf == NULL)
10070 {
10071 /* No full path name match, try a match with a URL or a "nofile"
10072 * buffer, these don't use the full path. */
10073 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10074 if (buf->b_fname != NULL
10075 && (path_with_url(buf->b_fname)
10076#ifdef FEAT_QUICKFIX
10077 || bt_nofile(buf)
10078#endif
10079 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010080 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010081 break;
10082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083 }
10084 return buf;
10085}
10086
10087/*
10088 * "bufexists(expr)" function
10089 */
10090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010091f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010093 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094}
10095
10096/*
10097 * "buflisted(expr)" function
10098 */
10099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010100f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101{
10102 buf_T *buf;
10103
10104 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106}
10107
10108/*
10109 * "bufloaded(expr)" function
10110 */
10111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010112f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113{
10114 buf_T *buf;
10115
10116 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010117 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118}
10119
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010120 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010121buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 int save_magic;
10124 char_u *save_cpo;
10125 buf_T *buf;
10126
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10128 save_magic = p_magic;
10129 p_magic = TRUE;
10130 save_cpo = p_cpo;
10131 p_cpo = (char_u *)"";
10132
10133 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010134 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135
10136 p_magic = save_magic;
10137 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010138 return buf;
10139}
10140
10141/*
10142 * Get buffer by number or pattern.
10143 */
10144 static buf_T *
10145get_buf_tv(typval_T *tv, int curtab_only)
10146{
10147 char_u *name = tv->vval.v_string;
10148 buf_T *buf;
10149
10150 if (tv->v_type == VAR_NUMBER)
10151 return buflist_findnr((int)tv->vval.v_number);
10152 if (tv->v_type != VAR_STRING)
10153 return NULL;
10154 if (name == NULL || *name == NUL)
10155 return curbuf;
10156 if (name[0] == '$' && name[1] == NUL)
10157 return lastbuf;
10158
10159 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160
10161 /* If not found, try expanding the name, like done for bufexists(). */
10162 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010163 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164
10165 return buf;
10166}
10167
10168/*
10169 * "bufname(expr)" function
10170 */
10171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010172f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 buf_T *buf;
10175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010176 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010178 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010183 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 --emsg_off;
10185}
10186
10187/*
10188 * "bufnr(expr)" function
10189 */
10190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010191f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010192{
10193 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010194 int error = FALSE;
10195 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010199 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010200 --emsg_off;
10201
10202 /* If the buffer isn't found and the second argument is not zero create a
10203 * new buffer. */
10204 if (buf == NULL
10205 && argvars[1].v_type != VAR_UNKNOWN
10206 && get_tv_number_chk(&argvars[1], &error) != 0
10207 && !error
10208 && (name = get_tv_string_chk(&argvars[0])) != NULL
10209 && !error)
10210 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10211
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216}
10217
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010219buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221#ifdef FEAT_WINDOWS
10222 win_T *wp;
10223 int winnr = 0;
10224#endif
10225 buf_T *buf;
10226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010229 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230#ifdef FEAT_WINDOWS
10231 for (wp = firstwin; wp; wp = wp->w_next)
10232 {
10233 ++winnr;
10234 if (wp->w_buffer == buf)
10235 break;
10236 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010237 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010239 rettv->vval.v_number = (curwin->w_buffer == buf
10240 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241#endif
10242 --emsg_off;
10243}
10244
10245/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010246 * "bufwinid(nr)" function
10247 */
10248 static void
10249f_bufwinid(typval_T *argvars, typval_T *rettv)
10250{
10251 buf_win_common(argvars, rettv, FALSE);
10252}
10253
10254/*
10255 * "bufwinnr(nr)" function
10256 */
10257 static void
10258f_bufwinnr(typval_T *argvars, typval_T *rettv)
10259{
10260 buf_win_common(argvars, rettv, TRUE);
10261}
10262
10263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 * "byte2line(byte)" function
10265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010267f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
10269#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010271#else
10272 long boff = 0;
10273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 (linenr_T)0, &boff);
10280#endif
10281}
10282
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010284byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010285{
10286#ifdef FEAT_MBYTE
10287 char_u *t;
10288#endif
10289 char_u *str;
10290 long idx;
10291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010292 str = get_tv_string_chk(&argvars[0]);
10293 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010296 return;
10297
10298#ifdef FEAT_MBYTE
10299 t = str;
10300 for ( ; idx > 0; idx--)
10301 {
10302 if (*t == NUL) /* EOL reached */
10303 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010304 if (enc_utf8 && comp)
10305 t += utf_ptr2len(t);
10306 else
10307 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010308 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010309 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010310#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010311 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010313#endif
10314}
10315
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010316/*
10317 * "byteidx()" function
10318 */
10319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010321{
10322 byteidx(argvars, rettv, FALSE);
10323}
10324
10325/*
10326 * "byteidxcomp()" function
10327 */
10328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010329f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010330{
10331 byteidx(argvars, rettv, TRUE);
10332}
10333
Bram Moolenaardb913952012-06-29 12:54:53 +020010334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010335func_call(
10336 char_u *name,
10337 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010338 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339 dict_T *selfdict,
10340 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010341{
10342 listitem_T *item;
10343 typval_T argv[MAX_FUNC_ARGS + 1];
10344 int argc = 0;
10345 int dummy;
10346 int r = 0;
10347
10348 for (item = args->vval.v_list->lv_first; item != NULL;
10349 item = item->li_next)
10350 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010351 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010352 {
10353 EMSG(_("E699: Too many arguments"));
10354 break;
10355 }
10356 /* Make a copy of each argument. This is needed to be able to set
10357 * v_lock to VAR_FIXED in the copy without changing the original list.
10358 */
10359 copy_tv(&item->li_tv, &argv[argc++]);
10360 }
10361
10362 if (item == NULL)
10363 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10364 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010365 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010366
10367 /* Free the arguments. */
10368 while (argc > 0)
10369 clear_tv(&argv[--argc]);
10370
10371 return r;
10372}
10373
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010374/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010375 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010376 */
10377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010378f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010379{
10380 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010381 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010382 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010383
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010384 if (argvars[1].v_type != VAR_LIST)
10385 {
10386 EMSG(_(e_listreq));
10387 return;
10388 }
10389 if (argvars[1].vval.v_list == NULL)
10390 return;
10391
10392 if (argvars[0].v_type == VAR_FUNC)
10393 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010394 else if (argvars[0].v_type == VAR_PARTIAL)
10395 {
10396 partial = argvars[0].vval.v_partial;
10397 func = partial->pt_name;
10398 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010399 else
10400 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401 if (*func == NUL)
10402 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010403
Bram Moolenaare9a41262005-01-15 22:18:47 +000010404 if (argvars[2].v_type != VAR_UNKNOWN)
10405 {
10406 if (argvars[2].v_type != VAR_DICT)
10407 {
10408 EMSG(_(e_dictreq));
10409 return;
10410 }
10411 selfdict = argvars[2].vval.v_dict;
10412 }
10413
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010414 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010415}
10416
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010417#ifdef FEAT_FLOAT
10418/*
10419 * "ceil({float})" function
10420 */
10421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010422f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010423{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010424 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010425
10426 rettv->v_type = VAR_FLOAT;
10427 if (get_float_arg(argvars, &f) == OK)
10428 rettv->vval.v_float = ceil(f);
10429 else
10430 rettv->vval.v_float = 0.0;
10431}
10432#endif
10433
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010434#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010435/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010436 * "ch_close()" function
10437 */
10438 static void
10439f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10440{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010441 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010442
10443 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010444 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010445 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010446 channel_clear(channel);
10447 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010448}
10449
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010450/*
10451 * "ch_getbufnr()" function
10452 */
10453 static void
10454f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10455{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010456 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010457
10458 rettv->vval.v_number = -1;
10459 if (channel != NULL)
10460 {
10461 char_u *what = get_tv_string(&argvars[1]);
10462 int part;
10463
10464 if (STRCMP(what, "err") == 0)
10465 part = PART_ERR;
10466 else if (STRCMP(what, "out") == 0)
10467 part = PART_OUT;
10468 else if (STRCMP(what, "in") == 0)
10469 part = PART_IN;
10470 else
10471 part = PART_SOCK;
10472 if (channel->ch_part[part].ch_buffer != NULL)
10473 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10474 }
10475}
10476
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010477/*
10478 * "ch_getjob()" function
10479 */
10480 static void
10481f_ch_getjob(typval_T *argvars, typval_T *rettv)
10482{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010483 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010484
10485 if (channel != NULL)
10486 {
10487 rettv->v_type = VAR_JOB;
10488 rettv->vval.v_job = channel->ch_job;
10489 if (channel->ch_job != NULL)
10490 ++channel->ch_job->jv_refcount;
10491 }
10492}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010493
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010494/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010495 * "ch_info()" function
10496 */
10497 static void
10498f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10499{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010500 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010501
10502 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10503 channel_info(channel, rettv->vval.v_dict);
10504}
10505
10506/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010507 * "ch_log()" function
10508 */
10509 static void
10510f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10511{
10512 char_u *msg = get_tv_string(&argvars[0]);
10513 channel_T *channel = NULL;
10514
10515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010516 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010517
10518 ch_log(channel, (char *)msg);
10519}
10520
10521/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010522 * "ch_logfile()" function
10523 */
10524 static void
10525f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10526{
10527 char_u *fname;
10528 char_u *opt = (char_u *)"";
10529 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010530
10531 fname = get_tv_string(&argvars[0]);
10532 if (argvars[1].v_type == VAR_STRING)
10533 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010534 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010535}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010536
10537/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010538 * "ch_open()" function
10539 */
10540 static void
10541f_ch_open(typval_T *argvars, typval_T *rettv)
10542{
Bram Moolenaar77073442016-02-13 23:23:53 +010010543 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010544 if (check_restricted() || check_secure())
10545 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010546 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010547}
10548
10549/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010550 * "ch_read()" function
10551 */
10552 static void
10553f_ch_read(typval_T *argvars, typval_T *rettv)
10554{
10555 common_channel_read(argvars, rettv, FALSE);
10556}
10557
10558/*
10559 * "ch_readraw()" function
10560 */
10561 static void
10562f_ch_readraw(typval_T *argvars, typval_T *rettv)
10563{
10564 common_channel_read(argvars, rettv, TRUE);
10565}
10566
10567/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010568 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010569 */
10570 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010571f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10572{
10573 ch_expr_common(argvars, rettv, TRUE);
10574}
10575
10576/*
10577 * "ch_sendexpr()" function
10578 */
10579 static void
10580f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10581{
10582 ch_expr_common(argvars, rettv, FALSE);
10583}
10584
10585/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010586 * "ch_evalraw()" function
10587 */
10588 static void
10589f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10590{
10591 ch_raw_common(argvars, rettv, TRUE);
10592}
10593
10594/*
10595 * "ch_sendraw()" function
10596 */
10597 static void
10598f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10599{
10600 ch_raw_common(argvars, rettv, FALSE);
10601}
10602
10603/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010604 * "ch_setoptions()" function
10605 */
10606 static void
10607f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10608{
10609 channel_T *channel;
10610 jobopt_T opt;
10611
Bram Moolenaar437905c2016-04-26 19:01:05 +020010612 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010613 if (channel == NULL)
10614 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010615 clear_job_options(&opt);
10616 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010617 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10618 channel_set_options(channel, &opt);
10619 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010620}
10621
10622/*
10623 * "ch_status()" function
10624 */
10625 static void
10626f_ch_status(typval_T *argvars, typval_T *rettv)
10627{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010628 channel_T *channel;
10629
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010630 /* return an empty string by default */
10631 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010632 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010633
Bram Moolenaar437905c2016-04-26 19:01:05 +020010634 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010635 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010636}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010637#endif
10638
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010639/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010640 * "changenr()" function
10641 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010643f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010644{
10645 rettv->vval.v_number = curbuf->b_u_seq_cur;
10646}
10647
10648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 * "char2nr(string)" function
10650 */
10651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010652f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
10654#ifdef FEAT_MBYTE
10655 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010656 {
10657 int utf8 = 0;
10658
10659 if (argvars[1].v_type != VAR_UNKNOWN)
10660 utf8 = get_tv_number_chk(&argvars[1], NULL);
10661
10662 if (utf8)
10663 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10664 else
10665 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 else
10668#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670}
10671
10672/*
10673 * "cindent(lnum)" function
10674 */
10675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010676f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677{
10678#ifdef FEAT_CINDENT
10679 pos_T pos;
10680 linenr_T lnum;
10681
10682 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10685 {
10686 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010687 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 curwin->w_cursor = pos;
10689 }
10690 else
10691#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010692 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693}
10694
10695/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010696 * "clearmatches()" function
10697 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010699f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010700{
10701#ifdef FEAT_SEARCH_EXTRA
10702 clear_matches(curwin);
10703#endif
10704}
10705
10706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 * "col(string)" function
10708 */
10709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712 colnr_T col = 0;
10713 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010714 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010716 fp = var2fpos(&argvars[0], FALSE, &fnum);
10717 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 {
10719 if (fp->col == MAXCOL)
10720 {
10721 /* '> can be MAXCOL, get the length of the line then */
10722 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010723 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 else
10725 col = MAXCOL;
10726 }
10727 else
10728 {
10729 col = fp->col + 1;
10730#ifdef FEAT_VIRTUALEDIT
10731 /* col(".") when the cursor is on the NUL at the end of the line
10732 * because of "coladd" can be seen as an extra column. */
10733 if (virtual_active() && fp == &curwin->w_cursor)
10734 {
10735 char_u *p = ml_get_cursor();
10736
10737 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10738 curwin->w_virtcol - curwin->w_cursor.coladd))
10739 {
10740# ifdef FEAT_MBYTE
10741 int l;
10742
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010743 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 col += l;
10745# else
10746 if (*p != NUL && p[1] == NUL)
10747 ++col;
10748# endif
10749 }
10750 }
10751#endif
10752 }
10753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010754 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755}
10756
Bram Moolenaar572cb562005-08-05 21:35:02 +000010757#if defined(FEAT_INS_EXPAND)
10758/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010759 * "complete()" function
10760 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010762f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010763{
10764 int startcol;
10765
10766 if ((State & INSERT) == 0)
10767 {
10768 EMSG(_("E785: complete() can only be used in Insert mode"));
10769 return;
10770 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010771
10772 /* Check for undo allowed here, because if something was already inserted
10773 * the line was already saved for undo and this check isn't done. */
10774 if (!undo_allowed())
10775 return;
10776
Bram Moolenaarade00832006-03-10 21:46:58 +000010777 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10778 {
10779 EMSG(_(e_invarg));
10780 return;
10781 }
10782
10783 startcol = get_tv_number_chk(&argvars[0], NULL);
10784 if (startcol <= 0)
10785 return;
10786
10787 set_completion(startcol - 1, argvars[1].vval.v_list);
10788}
10789
10790/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010791 * "complete_add()" function
10792 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010794f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010795{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010796 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010797}
10798
10799/*
10800 * "complete_check()" function
10801 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010803f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010804{
10805 int saved = RedrawingDisabled;
10806
10807 RedrawingDisabled = 0;
10808 ins_compl_check_keys(0);
10809 rettv->vval.v_number = compl_interrupted;
10810 RedrawingDisabled = saved;
10811}
10812#endif
10813
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814/*
10815 * "confirm(message, buttons[, default [, type]])" function
10816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010818f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819{
10820#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10821 char_u *message;
10822 char_u *buttons = NULL;
10823 char_u buf[NUMBUFLEN];
10824 char_u buf2[NUMBUFLEN];
10825 int def = 1;
10826 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 char_u *typestr;
10828 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 message = get_tv_string_chk(&argvars[0]);
10831 if (message == NULL)
10832 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010835 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10836 if (buttons == NULL)
10837 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010838 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010841 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10844 if (typestr == NULL)
10845 error = TRUE;
10846 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010848 switch (TOUPPER_ASC(*typestr))
10849 {
10850 case 'E': type = VIM_ERROR; break;
10851 case 'Q': type = VIM_QUESTION; break;
10852 case 'I': type = VIM_INFO; break;
10853 case 'W': type = VIM_WARNING; break;
10854 case 'G': type = VIM_GENERIC; break;
10855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 }
10857 }
10858 }
10859 }
10860
10861 if (buttons == NULL || *buttons == NUL)
10862 buttons = (char_u *)_("&Ok");
10863
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010864 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010866 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867#endif
10868}
10869
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010870/*
10871 * "copy()" function
10872 */
10873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010874f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010876 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010877}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010879#ifdef FEAT_FLOAT
10880/*
10881 * "cos()" function
10882 */
10883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010884f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010885{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010886 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010887
10888 rettv->v_type = VAR_FLOAT;
10889 if (get_float_arg(argvars, &f) == OK)
10890 rettv->vval.v_float = cos(f);
10891 else
10892 rettv->vval.v_float = 0.0;
10893}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010894
10895/*
10896 * "cosh()" function
10897 */
10898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010899f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010900{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010901 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010902
10903 rettv->v_type = VAR_FLOAT;
10904 if (get_float_arg(argvars, &f) == OK)
10905 rettv->vval.v_float = cosh(f);
10906 else
10907 rettv->vval.v_float = 0.0;
10908}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010909#endif
10910
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010912 * "count()" function
10913 */
10914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010915f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010916{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010917 long n = 0;
10918 int ic = FALSE;
10919
Bram Moolenaare9a41262005-01-15 22:18:47 +000010920 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010921 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010922 listitem_T *li;
10923 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010925
Bram Moolenaare9a41262005-01-15 22:18:47 +000010926 if ((l = argvars[0].vval.v_list) != NULL)
10927 {
10928 li = l->lv_first;
10929 if (argvars[2].v_type != VAR_UNKNOWN)
10930 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 int error = FALSE;
10932
10933 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934 if (argvars[3].v_type != VAR_UNKNOWN)
10935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010936 idx = get_tv_number_chk(&argvars[3], &error);
10937 if (!error)
10938 {
10939 li = list_find(l, idx);
10940 if (li == NULL)
10941 EMSGN(_(e_listidx), idx);
10942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 if (error)
10945 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 }
10947
10948 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010949 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950 ++n;
10951 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010952 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010953 else if (argvars[0].v_type == VAR_DICT)
10954 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010955 int todo;
10956 dict_T *d;
10957 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010958
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010959 if ((d = argvars[0].vval.v_dict) != NULL)
10960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 int error = FALSE;
10962
Bram Moolenaare9a41262005-01-15 22:18:47 +000010963 if (argvars[2].v_type != VAR_UNKNOWN)
10964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010966 if (argvars[3].v_type != VAR_UNKNOWN)
10967 EMSG(_(e_invarg));
10968 }
10969
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010970 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010971 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010972 {
10973 if (!HASHITEM_EMPTY(hi))
10974 {
10975 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010976 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010977 ++n;
10978 }
10979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010980 }
10981 }
10982 else
10983 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010984 rettv->vval.v_number = n;
10985}
10986
10987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10989 *
10990 * Checks the existence of a cscope connection.
10991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010993f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994{
10995#ifdef FEAT_CSCOPE
10996 int num = 0;
10997 char_u *dbpath = NULL;
10998 char_u *prepend = NULL;
10999 char_u buf[NUMBUFLEN];
11000
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011001 if (argvars[0].v_type != VAR_UNKNOWN
11002 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 num = (int)get_tv_number(&argvars[0]);
11005 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 }
11009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011#endif
11012}
11013
11014/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011015 * "cursor(lnum, col)" function, or
11016 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011018 * Moves the cursor to the specified line and column.
11019 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011022f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
11024 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011025#ifdef FEAT_VIRTUALEDIT
11026 long coladd = 0;
11027#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011028 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011030 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011031 if (argvars[1].v_type == VAR_UNKNOWN)
11032 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011033 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011034 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011035
Bram Moolenaar493c1782014-05-28 14:34:46 +020011036 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011037 {
11038 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011039 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011040 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011041 line = pos.lnum;
11042 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011043#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011044 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011045#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011046 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011047 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011048 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011049 set_curswant = FALSE;
11050 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011051 }
11052 else
11053 {
11054 line = get_tv_lnum(argvars);
11055 col = get_tv_number_chk(&argvars[1], NULL);
11056#ifdef FEAT_VIRTUALEDIT
11057 if (argvars[2].v_type != VAR_UNKNOWN)
11058 coladd = get_tv_number_chk(&argvars[2], NULL);
11059#endif
11060 }
11061 if (line < 0 || col < 0
11062#ifdef FEAT_VIRTUALEDIT
11063 || coladd < 0
11064#endif
11065 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 if (line > 0)
11068 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069 if (col > 0)
11070 curwin->w_cursor.col = col - 1;
11071#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011072 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073#endif
11074
11075 /* Make sure the cursor is in a valid position. */
11076 check_cursor();
11077#ifdef FEAT_MBYTE
11078 /* Correct cursor for multi-byte character. */
11079 if (has_mbyte)
11080 mb_adjust_cursor();
11081#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011082
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011083 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011084 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085}
11086
11087/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011088 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 */
11090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011091f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011093 int noref = 0;
11094
11095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011097 if (noref < 0 || noref > 1)
11098 EMSG(_(e_invarg));
11099 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011100 {
11101 current_copyID += COPYID_INC;
11102 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104}
11105
11106/*
11107 * "delete()" function
11108 */
11109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011110f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011112 char_u nbuf[NUMBUFLEN];
11113 char_u *name;
11114 char_u *flags;
11115
11116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011118 return;
11119
11120 name = get_tv_string(&argvars[0]);
11121 if (name == NULL || *name == NUL)
11122 {
11123 EMSG(_(e_invarg));
11124 return;
11125 }
11126
11127 if (argvars[1].v_type != VAR_UNKNOWN)
11128 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011130 flags = (char_u *)"";
11131
11132 if (*flags == NUL)
11133 /* delete a file */
11134 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11135 else if (STRCMP(flags, "d") == 0)
11136 /* delete an empty directory */
11137 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11138 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011139 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011140 rettv->vval.v_number = delete_recursive(name);
11141 else
11142 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143}
11144
11145/*
11146 * "did_filetype()" function
11147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011149f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150{
11151#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153#endif
11154}
11155
11156/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011157 * "diff_filler()" function
11158 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011160f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011161{
11162#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011164#endif
11165}
11166
11167/*
11168 * "diff_hlID()" function
11169 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011171f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011172{
11173#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011175 static linenr_T prev_lnum = 0;
11176 static int changedtick = 0;
11177 static int fnum = 0;
11178 static int change_start = 0;
11179 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011180 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011181 int filler_lines;
11182 int col;
11183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 if (lnum < 0) /* ignore type error in {lnum} arg */
11185 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011186 if (lnum != prev_lnum
11187 || changedtick != curbuf->b_changedtick
11188 || fnum != curbuf->b_fnum)
11189 {
11190 /* New line, buffer, change: need to get the values. */
11191 filler_lines = diff_check(curwin, lnum);
11192 if (filler_lines < 0)
11193 {
11194 if (filler_lines == -1)
11195 {
11196 change_start = MAXCOL;
11197 change_end = -1;
11198 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11199 hlID = HLF_ADD; /* added line */
11200 else
11201 hlID = HLF_CHD; /* changed line */
11202 }
11203 else
11204 hlID = HLF_ADD; /* added line */
11205 }
11206 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011207 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011208 prev_lnum = lnum;
11209 changedtick = curbuf->b_changedtick;
11210 fnum = curbuf->b_fnum;
11211 }
11212
11213 if (hlID == HLF_CHD || hlID == HLF_TXD)
11214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011216 if (col >= change_start && col <= change_end)
11217 hlID = HLF_TXD; /* changed text */
11218 else
11219 hlID = HLF_CHD; /* changed line */
11220 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011221 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011222#endif
11223}
11224
11225/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011226 * "empty({expr})" function
11227 */
11228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011229f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011230{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011231 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011232
11233 switch (argvars[0].v_type)
11234 {
11235 case VAR_STRING:
11236 case VAR_FUNC:
11237 n = argvars[0].vval.v_string == NULL
11238 || *argvars[0].vval.v_string == NUL;
11239 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011240 case VAR_PARTIAL:
11241 n = FALSE;
11242 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011243 case VAR_NUMBER:
11244 n = argvars[0].vval.v_number == 0;
11245 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011246 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011247#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011248 n = argvars[0].vval.v_float == 0.0;
11249 break;
11250#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011251 case VAR_LIST:
11252 n = argvars[0].vval.v_list == NULL
11253 || argvars[0].vval.v_list->lv_first == NULL;
11254 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 case VAR_DICT:
11256 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011257 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011258 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011259 case VAR_SPECIAL:
11260 n = argvars[0].vval.v_number != VVAL_TRUE;
11261 break;
11262
Bram Moolenaar835dc632016-02-07 14:27:38 +010011263 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011264#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011265 n = argvars[0].vval.v_job == NULL
11266 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11267 break;
11268#endif
11269 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011270#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011271 n = argvars[0].vval.v_channel == NULL
11272 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011273 break;
11274#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011275 case VAR_UNKNOWN:
11276 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11277 n = TRUE;
11278 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011279 }
11280
11281 rettv->vval.v_number = n;
11282}
11283
11284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 * "escape({string}, {chars})" function
11286 */
11287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011288f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289{
11290 char_u buf[NUMBUFLEN];
11291
Bram Moolenaar758711c2005-02-02 23:11:38 +000011292 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11293 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295}
11296
11297/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011298 * "eval()" function
11299 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011301f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011302{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011303 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 s = get_tv_string_chk(&argvars[0]);
11306 if (s != NULL)
11307 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011308
Bram Moolenaar615b9972015-01-14 17:15:05 +010011309 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11311 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011312 if (p != NULL && !aborting())
11313 EMSG2(_(e_invexpr2), p);
11314 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011315 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011316 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011317 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011318 else if (*s != NUL)
11319 EMSG(_(e_trailing));
11320}
11321
11322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 * "eventhandler()" function
11324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011326f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329}
11330
11331/*
11332 * "executable()" function
11333 */
11334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011335f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011337 char_u *name = get_tv_string(&argvars[0]);
11338
11339 /* Check in $PATH and also check directly if there is a directory name. */
11340 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11341 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011342}
11343
11344/*
11345 * "exepath()" function
11346 */
11347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011348f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011349{
11350 char_u *p = NULL;
11351
Bram Moolenaarb5971142015-03-21 17:32:19 +010011352 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011353 rettv->v_type = VAR_STRING;
11354 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355}
11356
11357/*
11358 * "exists()" function
11359 */
11360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
11363 char_u *p;
11364 char_u *name;
11365 int n = FALSE;
11366 int len = 0;
11367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 if (*p == '$') /* environment variable */
11370 {
11371 /* first try "normal" environment variables (fast) */
11372 if (mch_getenv(p + 1) != NULL)
11373 n = TRUE;
11374 else
11375 {
11376 /* try expanding things like $VIM and ${HOME} */
11377 p = expand_env_save(p);
11378 if (p != NULL && *p != '$')
11379 n = TRUE;
11380 vim_free(p);
11381 }
11382 }
11383 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011386 if (*skipwhite(p) != NUL)
11387 n = FALSE; /* trailing garbage */
11388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 else if (*p == '*') /* internal or user defined function */
11390 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011391 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 }
11393 else if (*p == ':')
11394 {
11395 n = cmd_exists(p + 1);
11396 }
11397 else if (*p == '#')
11398 {
11399#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011400 if (p[1] == '#')
11401 n = autocmd_supported(p + 2);
11402 else
11403 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404#endif
11405 }
11406 else /* internal variable */
11407 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011408 char_u *tofree;
11409 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011411 /* get_name_len() takes care of expanding curly braces */
11412 name = p;
11413 len = get_name_len(&p, &tofree, TRUE, FALSE);
11414 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011416 if (tofree != NULL)
11417 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011418 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011419 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011421 /* handle d.key, l[idx], f(expr) */
11422 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11423 if (n)
11424 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 }
11426 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011427 if (*p != NUL)
11428 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011430 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 }
11432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011433 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434}
11435
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011436#ifdef FEAT_FLOAT
11437/*
11438 * "exp()" function
11439 */
11440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011441f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011442{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011443 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011444
11445 rettv->v_type = VAR_FLOAT;
11446 if (get_float_arg(argvars, &f) == OK)
11447 rettv->vval.v_float = exp(f);
11448 else
11449 rettv->vval.v_float = 0.0;
11450}
11451#endif
11452
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453/*
11454 * "expand()" function
11455 */
11456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011457f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458{
11459 char_u *s;
11460 int len;
11461 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011462 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011465 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011468 if (argvars[1].v_type != VAR_UNKNOWN
11469 && argvars[2].v_type != VAR_UNKNOWN
11470 && get_tv_number_chk(&argvars[2], &error)
11471 && !error)
11472 {
11473 rettv->v_type = VAR_LIST;
11474 rettv->vval.v_list = NULL;
11475 }
11476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 if (*s == '%' || *s == '#' || *s == '<')
11479 {
11480 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011481 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011483 if (rettv->v_type == VAR_LIST)
11484 {
11485 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11486 list_append_string(rettv->vval.v_list, result, -1);
11487 else
11488 vim_free(result);
11489 }
11490 else
11491 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 }
11493 else
11494 {
11495 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011496 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011497 if (argvars[1].v_type != VAR_UNKNOWN
11498 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011499 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 if (!error)
11501 {
11502 ExpandInit(&xpc);
11503 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011504 if (p_wic)
11505 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011506 if (rettv->v_type == VAR_STRING)
11507 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11508 options, WILD_ALL);
11509 else if (rettv_list_alloc(rettv) != FAIL)
11510 {
11511 int i;
11512
11513 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11514 for (i = 0; i < xpc.xp_numfiles; i++)
11515 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11516 ExpandCleanup(&xpc);
11517 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011518 }
11519 else
11520 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 }
11522}
11523
11524/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011525 * Go over all entries in "d2" and add them to "d1".
11526 * When "action" is "error" then a duplicate key is an error.
11527 * When "action" is "force" then a duplicate key is overwritten.
11528 * Otherwise duplicate keys are ignored ("action" is "keep").
11529 */
11530 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011531dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011532{
11533 dictitem_T *di1;
11534 hashitem_T *hi2;
11535 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011536 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011537
11538 todo = (int)d2->dv_hashtab.ht_used;
11539 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11540 {
11541 if (!HASHITEM_EMPTY(hi2))
11542 {
11543 --todo;
11544 di1 = dict_find(d1, hi2->hi_key, -1);
11545 if (d1->dv_scope != 0)
11546 {
11547 /* Disallow replacing a builtin function in l: and g:.
11548 * Check the key to be valid when adding to any
11549 * scope. */
11550 if (d1->dv_scope == VAR_DEF_SCOPE
11551 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11552 && var_check_func_name(hi2->hi_key,
11553 di1 == NULL))
11554 break;
11555 if (!valid_varname(hi2->hi_key))
11556 break;
11557 }
11558 if (di1 == NULL)
11559 {
11560 di1 = dictitem_copy(HI2DI(hi2));
11561 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11562 dictitem_free(di1);
11563 }
11564 else if (*action == 'e')
11565 {
11566 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11567 break;
11568 }
11569 else if (*action == 'f' && HI2DI(hi2) != di1)
11570 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011571 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11572 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011573 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011574 clear_tv(&di1->di_tv);
11575 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11576 }
11577 }
11578 }
11579}
11580
11581/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011582 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011584 */
11585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011586f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011587{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011588 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011589
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011591 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011592 list_T *l1, *l2;
11593 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011595 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011596
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597 l1 = argvars[0].vval.v_list;
11598 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011599 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011600 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601 {
11602 if (argvars[2].v_type != VAR_UNKNOWN)
11603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 before = get_tv_number_chk(&argvars[2], &error);
11605 if (error)
11606 return; /* type error; errmsg already given */
11607
Bram Moolenaar758711c2005-02-02 23:11:38 +000011608 if (before == l1->lv_len)
11609 item = NULL;
11610 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011611 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011612 item = list_find(l1, before);
11613 if (item == NULL)
11614 {
11615 EMSGN(_(e_listidx), before);
11616 return;
11617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011618 }
11619 }
11620 else
11621 item = NULL;
11622 list_extend(l1, l2, item);
11623
Bram Moolenaare9a41262005-01-15 22:18:47 +000011624 copy_tv(&argvars[0], rettv);
11625 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011626 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011627 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11628 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011629 dict_T *d1, *d2;
11630 char_u *action;
11631 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011632
11633 d1 = argvars[0].vval.v_dict;
11634 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011635 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011636 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011637 {
11638 /* Check the third argument. */
11639 if (argvars[2].v_type != VAR_UNKNOWN)
11640 {
11641 static char *(av[]) = {"keep", "force", "error"};
11642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643 action = get_tv_string_chk(&argvars[2]);
11644 if (action == NULL)
11645 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011646 for (i = 0; i < 3; ++i)
11647 if (STRCMP(action, av[i]) == 0)
11648 break;
11649 if (i == 3)
11650 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011651 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011652 return;
11653 }
11654 }
11655 else
11656 action = (char_u *)"force";
11657
Bram Moolenaara9922d62013-05-30 13:01:18 +020011658 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011659
Bram Moolenaare9a41262005-01-15 22:18:47 +000011660 copy_tv(&argvars[0], rettv);
11661 }
11662 }
11663 else
11664 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011665}
11666
11667/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011668 * "feedkeys()" function
11669 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011671f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011672{
11673 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011674 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011675 char_u *keys, *flags;
11676 char_u nbuf[NUMBUFLEN];
11677 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011678 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011679 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011680 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011681
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011682 /* This is not allowed in the sandbox. If the commands would still be
11683 * executed in the sandbox it would be OK, but it probably happens later,
11684 * when "sandbox" is no longer set. */
11685 if (check_secure())
11686 return;
11687
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011688 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011689
11690 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011691 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011692 flags = get_tv_string_buf(&argvars[1], nbuf);
11693 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011694 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011695 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011696 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011697 case 'n': remap = FALSE; break;
11698 case 'm': remap = TRUE; break;
11699 case 't': typed = TRUE; break;
11700 case 'i': insert = TRUE; break;
11701 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011702 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011703 }
11704 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011705 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011706
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011707 if (*keys != NUL || execute)
11708 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011709 /* Need to escape K_SPECIAL and CSI before putting the string in the
11710 * typeahead buffer. */
11711 keys_esc = vim_strsave_escape_csi(keys);
11712 if (keys_esc != NULL)
11713 {
11714 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011715 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011716 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011717 if (vgetc_busy)
11718 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011719 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011720 {
11721 int save_msg_scroll = msg_scroll;
11722
11723 /* Avoid a 1 second delay when the keys start Insert mode. */
11724 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011725
Bram Moolenaar245c4102016-04-20 17:37:41 +020011726 if (!dangerous)
11727 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011728 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011729 if (!dangerous)
11730 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011731 msg_scroll |= save_msg_scroll;
11732 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011733 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011734 }
11735}
11736
11737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 * "filereadable()" function
11739 */
11740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011741f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011743 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 char_u *p;
11745 int n;
11746
Bram Moolenaarc236c162008-07-13 17:41:49 +000011747#ifndef O_NONBLOCK
11748# define O_NONBLOCK 0
11749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011751 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11752 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 {
11754 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011755 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 }
11757 else
11758 n = FALSE;
11759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011760 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011761}
11762
11763/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011764 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 * rights to write into.
11766 */
11767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011768f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011769{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011770 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771}
11772
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011773 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011774findfilendir(
11775 typval_T *argvars UNUSED,
11776 typval_T *rettv,
11777 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011778{
11779#ifdef FEAT_SEARCHPATH
11780 char_u *fname;
11781 char_u *fresult = NULL;
11782 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11783 char_u *p;
11784 char_u pathbuf[NUMBUFLEN];
11785 int count = 1;
11786 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011787 int error = FALSE;
11788#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011789
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011790 rettv->vval.v_string = NULL;
11791 rettv->v_type = VAR_STRING;
11792
11793#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011794 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011795
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011796 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11799 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011800 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 else
11802 {
11803 if (*p != NUL)
11804 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011805
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011807 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011809 }
11810
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011811 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11812 error = TRUE;
11813
11814 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 do
11817 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011818 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011819 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 fresult = find_file_in_path_option(first ? fname : NULL,
11821 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011822 0, first, path,
11823 find_what,
11824 curbuf->b_ffname,
11825 find_what == FINDFILE_DIR
11826 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011827 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011828
11829 if (fresult != NULL && rettv->v_type == VAR_LIST)
11830 list_append_string(rettv->vval.v_list, fresult, -1);
11831
11832 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011833 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011834
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011835 if (rettv->v_type == VAR_STRING)
11836 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011837#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011838}
11839
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011840static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11841static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011842
11843/*
11844 * Implementation of map() and filter().
11845 */
11846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011847filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011848{
11849 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 listitem_T *li, *nli;
11852 list_T *l = NULL;
11853 dictitem_T *di;
11854 hashtab_T *ht;
11855 hashitem_T *hi;
11856 dict_T *d = NULL;
11857 typval_T save_val;
11858 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011859 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011860 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011861 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011862 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011863 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011864 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011865 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011866
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011868 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011869 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011870 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011871 return;
11872 }
11873 else if (argvars[0].v_type == VAR_DICT)
11874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011875 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011876 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011877 return;
11878 }
11879 else
11880 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011881 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 return;
11883 }
11884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011885 expr = get_tv_string_buf_chk(&argvars[1], buf);
11886 /* On type errors, the preceding call has already displayed an error
11887 * message. Avoid a misleading error message for an empty string that
11888 * was not passed as argument. */
11889 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 prepare_vimvar(VV_VAL, &save_val);
11892 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011893
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011894 /* We reset "did_emsg" to be able to detect whether an error
11895 * occurred during evaluation of the expression. */
11896 save_did_emsg = did_emsg;
11897 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011898
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011899 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 vimvars[VV_KEY].vv_type = VAR_STRING;
11903
11904 ht = &d->dv_hashtab;
11905 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011906 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011909 if (!HASHITEM_EMPTY(hi))
11910 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011911 int r;
11912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913 --todo;
11914 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011915 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011916 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11917 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 break;
11919 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011920 r = filter_map_one(&di->di_tv, expr, map, &rem);
11921 clear_tv(&vimvars[VV_KEY].vv_tv);
11922 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923 break;
11924 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011925 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011926 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11927 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011928 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011929 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 }
11932 }
11933 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 }
11935 else
11936 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011937 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011939 for (li = l->lv_first; li != NULL; li = nli)
11940 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011941 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011942 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011943 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011944 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011945 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011946 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011947 break;
11948 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011950 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011951 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011953
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011954 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011955 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011956
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011957 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011958 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011959
11960 copy_tv(&argvars[0], rettv);
11961}
11962
11963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011964filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011965{
Bram Moolenaar33570922005-01-25 22:26:29 +000011966 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011967 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011968 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011969
Bram Moolenaar33570922005-01-25 22:26:29 +000011970 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011971 s = expr;
11972 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011973 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974 if (*s != NUL) /* check for trailing chars after expr */
11975 {
11976 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011977 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011978 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011979 }
11980 if (map)
11981 {
11982 /* map(): replace the list item value */
11983 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011984 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011985 *tv = rettv;
11986 }
11987 else
11988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011989 int error = FALSE;
11990
Bram Moolenaare9a41262005-01-15 22:18:47 +000011991 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 /* On type error, nothing has been removed; return FAIL to stop the
11995 * loop. The error message was given by get_tv_number_chk(). */
11996 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011997 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011998 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011999 retval = OK;
12000theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012002 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012003}
12004
12005/*
12006 * "filter()" function
12007 */
12008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012009f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012010{
12011 filter_map(argvars, rettv, FALSE);
12012}
12013
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012014/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015 * "finddir({fname}[, {path}[, {count}]])" function
12016 */
12017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012018f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012020 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012021}
12022
12023/*
12024 * "findfile({fname}[, {path}[, {count}]])" function
12025 */
12026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012027f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012028{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012029 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012030}
12031
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012032#ifdef FEAT_FLOAT
12033/*
12034 * "float2nr({float})" function
12035 */
12036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012037f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012038{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012039 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012040
12041 if (get_float_arg(argvars, &f) == OK)
12042 {
12043 if (f < -0x7fffffff)
12044 rettv->vval.v_number = -0x7fffffff;
12045 else if (f > 0x7fffffff)
12046 rettv->vval.v_number = 0x7fffffff;
12047 else
12048 rettv->vval.v_number = (varnumber_T)f;
12049 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012050}
12051
12052/*
12053 * "floor({float})" function
12054 */
12055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012056f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012057{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012058 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012059
12060 rettv->v_type = VAR_FLOAT;
12061 if (get_float_arg(argvars, &f) == OK)
12062 rettv->vval.v_float = floor(f);
12063 else
12064 rettv->vval.v_float = 0.0;
12065}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012066
12067/*
12068 * "fmod()" function
12069 */
12070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012071f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012072{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012073 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012074
12075 rettv->v_type = VAR_FLOAT;
12076 if (get_float_arg(argvars, &fx) == OK
12077 && get_float_arg(&argvars[1], &fy) == OK)
12078 rettv->vval.v_float = fmod(fx, fy);
12079 else
12080 rettv->vval.v_float = 0.0;
12081}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012082#endif
12083
Bram Moolenaar0d660222005-01-07 21:51:51 +000012084/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012085 * "fnameescape({string})" function
12086 */
12087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012088f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012089{
12090 rettv->vval.v_string = vim_strsave_fnameescape(
12091 get_tv_string(&argvars[0]), FALSE);
12092 rettv->v_type = VAR_STRING;
12093}
12094
12095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 * "fnamemodify({fname}, {mods})" function
12097 */
12098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012099f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100{
12101 char_u *fname;
12102 char_u *mods;
12103 int usedlen = 0;
12104 int len;
12105 char_u *fbuf = NULL;
12106 char_u buf[NUMBUFLEN];
12107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108 fname = get_tv_string_chk(&argvars[0]);
12109 mods = get_tv_string_buf_chk(&argvars[1], buf);
12110 if (fname == NULL || mods == NULL)
12111 fname = NULL;
12112 else
12113 {
12114 len = (int)STRLEN(fname);
12115 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012120 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 vim_free(fbuf);
12124}
12125
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012126static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127
12128/*
12129 * "foldclosed()" function
12130 */
12131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012132foldclosed_both(
12133 typval_T *argvars UNUSED,
12134 typval_T *rettv,
12135 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136{
12137#ifdef FEAT_FOLDING
12138 linenr_T lnum;
12139 linenr_T first, last;
12140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12143 {
12144 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12145 {
12146 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 return;
12151 }
12152 }
12153#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155}
12156
12157/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158 * "foldclosed()" function
12159 */
12160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012161f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012162{
12163 foldclosed_both(argvars, rettv, FALSE);
12164}
12165
12166/*
12167 * "foldclosedend()" function
12168 */
12169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012171{
12172 foldclosed_both(argvars, rettv, TRUE);
12173}
12174
12175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 * "foldlevel()" function
12177 */
12178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012179f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012180{
12181#ifdef FEAT_FOLDING
12182 linenr_T lnum;
12183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188}
12189
12190/*
12191 * "foldtext()" function
12192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012194f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195{
12196#ifdef FEAT_FOLDING
12197 linenr_T lnum;
12198 char_u *s;
12199 char_u *r;
12200 int len;
12201 char *txt;
12202#endif
12203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->v_type = VAR_STRING;
12205 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012207 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12208 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12209 <= curbuf->b_ml.ml_line_count
12210 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 {
12212 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012213 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12214 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 {
12216 if (!linewhite(lnum))
12217 break;
12218 ++lnum;
12219 }
12220
12221 /* Find interesting text in this line. */
12222 s = skipwhite(ml_get(lnum));
12223 /* skip C comment-start */
12224 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012227 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012228 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012229 {
12230 s = skipwhite(ml_get(lnum + 1));
12231 if (*s == '*')
12232 s = skipwhite(s + 1);
12233 }
12234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 txt = _("+-%s%3ld lines: ");
12236 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012237 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 + 20 /* for %3ld */
12239 + STRLEN(s))); /* concatenated */
12240 if (r != NULL)
12241 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012242 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12243 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12244 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245 len = (int)STRLEN(r);
12246 STRCAT(r, s);
12247 /* remove 'foldmarker' and 'commentstring' */
12248 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250 }
12251 }
12252#endif
12253}
12254
12255/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012256 * "foldtextresult(lnum)" function
12257 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012259f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012260{
12261#ifdef FEAT_FOLDING
12262 linenr_T lnum;
12263 char_u *text;
12264 char_u buf[51];
12265 foldinfo_T foldinfo;
12266 int fold_count;
12267#endif
12268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012269 rettv->v_type = VAR_STRING;
12270 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012271#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 /* treat illegal types and illegal string values for {lnum} the same */
12274 if (lnum < 0)
12275 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012276 fold_count = foldedCount(curwin, lnum, &foldinfo);
12277 if (fold_count > 0)
12278 {
12279 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12280 &foldinfo, buf);
12281 if (text == buf)
12282 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012284 }
12285#endif
12286}
12287
12288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 * "foreground()" function
12290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012292f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294#ifdef FEAT_GUI
12295 if (gui.in_use)
12296 gui_mch_set_foreground();
12297#else
12298# ifdef WIN32
12299 win32_set_foreground();
12300# endif
12301#endif
12302}
12303
12304/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012305 * "function()" function
12306 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012308f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012309{
12310 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012311 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012312 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012313 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012314
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012315 if (argvars[0].v_type == VAR_FUNC)
12316 {
12317 /* function(MyFunc, [arg], dict) */
12318 s = argvars[0].vval.v_string;
12319 }
12320 else if (argvars[0].v_type == VAR_PARTIAL
12321 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012322 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012323 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012324 arg_pt = argvars[0].vval.v_partial;
12325 s = arg_pt->pt_name;
12326 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012327 else
12328 {
12329 /* function('MyFunc', [arg], dict) */
12330 s = get_tv_string(&argvars[0]);
12331 use_string = TRUE;
12332 }
12333
12334 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012335 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012336 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012337 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12338 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012339 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012340 else
12341 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012342 int dict_idx = 0;
12343 int arg_idx = 0;
12344 list_T *list = NULL;
12345
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012346 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012347 {
12348 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012349 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012350
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012351 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12352 * also be called from another script. Using trans_function_name()
12353 * would also work, but some plugins depend on the name being
12354 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012355 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012356 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12357 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012358 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012359 STRCPY(name, sid_buf);
12360 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012361 }
12362 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012363 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012364 name = vim_strsave(s);
12365
12366 if (argvars[1].v_type != VAR_UNKNOWN)
12367 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012368 if (argvars[2].v_type != VAR_UNKNOWN)
12369 {
12370 /* function(name, [args], dict) */
12371 arg_idx = 1;
12372 dict_idx = 2;
12373 }
12374 else if (argvars[1].v_type == VAR_DICT)
12375 /* function(name, dict) */
12376 dict_idx = 1;
12377 else
12378 /* function(name, [args]) */
12379 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012380 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012381 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012382 if (argvars[dict_idx].v_type != VAR_DICT)
12383 {
12384 EMSG(_("E922: expected a dict"));
12385 vim_free(name);
12386 return;
12387 }
12388 if (argvars[dict_idx].vval.v_dict == NULL)
12389 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012390 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012391 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012392 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012393 if (argvars[arg_idx].v_type != VAR_LIST)
12394 {
12395 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12396 vim_free(name);
12397 return;
12398 }
12399 list = argvars[arg_idx].vval.v_list;
12400 if (list == NULL || list->lv_len == 0)
12401 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012402 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012403 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012404 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012405 {
12406 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012407
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012408 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012409 if (pt == NULL)
12410 vim_free(name);
12411 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012412 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012413 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012414 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012415 listitem_T *li;
12416 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012417 int arg_len = 0;
12418 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012419
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012420 if (arg_pt != NULL)
12421 arg_len = arg_pt->pt_argc;
12422 if (list != NULL)
12423 lv_len = list->lv_len;
12424 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012425 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012426 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012427 if (pt->pt_argv == NULL)
12428 {
12429 vim_free(pt);
12430 vim_free(name);
12431 return;
12432 }
12433 else
12434 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012435 for (i = 0; i < arg_len; i++)
12436 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12437 if (lv_len > 0)
12438 for (li = list->lv_first; li != NULL;
12439 li = li->li_next)
12440 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012441 }
12442 }
12443
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012444 /* For "function(dict.func, [], dict)" and "func" is a partial
12445 * use "dict". That is backwards compatible. */
12446 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012447 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012448 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012449 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12450 ++pt->pt_dict->dv_refcount;
12451 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012452 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012453 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012454 /* If the dict was bound automatically the result is also
12455 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012456 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012457 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012458 if (pt->pt_dict != NULL)
12459 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012460 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012461
12462 pt->pt_refcount = 1;
12463 pt->pt_name = name;
12464 func_ref(pt->pt_name);
12465 }
12466 rettv->v_type = VAR_PARTIAL;
12467 rettv->vval.v_partial = pt;
12468 }
12469 else
12470 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012471 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012472 rettv->v_type = VAR_FUNC;
12473 rettv->vval.v_string = name;
12474 func_ref(name);
12475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 }
12477}
12478
12479/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012480 * "garbagecollect()" function
12481 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012484{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012485 /* This is postponed until we are back at the toplevel, because we may be
12486 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12487 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012488
12489 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12490 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012491}
12492
12493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012494 * "get()" function
12495 */
12496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012497f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498{
Bram Moolenaar33570922005-01-25 22:26:29 +000012499 listitem_T *li;
12500 list_T *l;
12501 dictitem_T *di;
12502 dict_T *d;
12503 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012504
Bram Moolenaare9a41262005-01-15 22:18:47 +000012505 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012506 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012507 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 int error = FALSE;
12510
12511 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12512 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012513 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012514 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012516 else if (argvars[0].v_type == VAR_DICT)
12517 {
12518 if ((d = argvars[0].vval.v_dict) != NULL)
12519 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012520 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012521 if (di != NULL)
12522 tv = &di->di_tv;
12523 }
12524 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012525 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012526 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012527 partial_T *pt;
12528 partial_T fref_pt;
12529
12530 if (argvars[0].v_type == VAR_PARTIAL)
12531 pt = argvars[0].vval.v_partial;
12532 else
12533 {
12534 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12535 fref_pt.pt_name = argvars[0].vval.v_string;
12536 pt = &fref_pt;
12537 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012538
12539 if (pt != NULL)
12540 {
12541 char_u *what = get_tv_string(&argvars[1]);
12542
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012543 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012544 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012545 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012546 if (pt->pt_name == NULL)
12547 rettv->vval.v_string = NULL;
12548 else
12549 rettv->vval.v_string = vim_strsave(pt->pt_name);
12550 }
12551 else if (STRCMP(what, "dict") == 0)
12552 {
12553 rettv->v_type = VAR_DICT;
12554 rettv->vval.v_dict = pt->pt_dict;
12555 if (pt->pt_dict != NULL)
12556 ++pt->pt_dict->dv_refcount;
12557 }
12558 else if (STRCMP(what, "args") == 0)
12559 {
12560 rettv->v_type = VAR_LIST;
12561 if (rettv_list_alloc(rettv) == OK)
12562 {
12563 int i;
12564
12565 for (i = 0; i < pt->pt_argc; ++i)
12566 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12567 }
12568 }
12569 else
12570 EMSG2(_(e_invarg2), what);
12571 return;
12572 }
12573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012574 else
12575 EMSG2(_(e_listdictarg), "get()");
12576
12577 if (tv == NULL)
12578 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012580 copy_tv(&argvars[2], rettv);
12581 }
12582 else
12583 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584}
12585
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012586static 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 +000012587
12588/*
12589 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012590 * Return a range (from start to end) of lines in rettv from the specified
12591 * buffer.
12592 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012593 */
12594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012595get_buffer_lines(
12596 buf_T *buf,
12597 linenr_T start,
12598 linenr_T end,
12599 int retlist,
12600 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012601{
12602 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012603
Bram Moolenaar959a1432013-12-14 12:17:38 +010012604 rettv->v_type = VAR_STRING;
12605 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012606 if (retlist && rettv_list_alloc(rettv) == FAIL)
12607 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012608
12609 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12610 return;
12611
12612 if (!retlist)
12613 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012614 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12615 p = ml_get_buf(buf, start, FALSE);
12616 else
12617 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012618 rettv->vval.v_string = vim_strsave(p);
12619 }
12620 else
12621 {
12622 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012623 return;
12624
12625 if (start < 1)
12626 start = 1;
12627 if (end > buf->b_ml.ml_line_count)
12628 end = buf->b_ml.ml_line_count;
12629 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012630 if (list_append_string(rettv->vval.v_list,
12631 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012632 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012633 }
12634}
12635
12636/*
12637 * "getbufline()" function
12638 */
12639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012640f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012641{
12642 linenr_T lnum;
12643 linenr_T end;
12644 buf_T *buf;
12645
12646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12647 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012648 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012649 --emsg_off;
12650
Bram Moolenaar661b1822005-07-28 22:36:45 +000012651 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012652 if (argvars[2].v_type == VAR_UNKNOWN)
12653 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012654 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012655 end = get_tv_lnum_buf(&argvars[2], buf);
12656
Bram Moolenaar342337a2005-07-21 21:11:17 +000012657 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012658}
12659
Bram Moolenaar0d660222005-01-07 21:51:51 +000012660/*
12661 * "getbufvar()" function
12662 */
12663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012664f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665{
12666 buf_T *buf;
12667 buf_T *save_curbuf;
12668 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012670 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12673 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012674 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012675 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012676
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679
12680 if (buf != NULL && varname != NULL)
12681 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012682 /* set curbuf to be our buf, temporarily */
12683 save_curbuf = curbuf;
12684 curbuf = buf;
12685
Bram Moolenaar0d660222005-01-07 21:51:51 +000012686 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012687 {
12688 if (get_option_tv(&varname, rettv, TRUE) == OK)
12689 done = TRUE;
12690 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012691 else if (STRCMP(varname, "changedtick") == 0)
12692 {
12693 rettv->v_type = VAR_NUMBER;
12694 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012695 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012696 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012697 else
12698 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012699 /* Look up the variable. */
12700 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12701 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12702 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012704 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012705 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012706 done = TRUE;
12707 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012708 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012709
12710 /* restore previous notion of curbuf */
12711 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012712 }
12713
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012714 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12715 /* use the default value */
12716 copy_tv(&argvars[2], rettv);
12717
Bram Moolenaar0d660222005-01-07 21:51:51 +000012718 --emsg_off;
12719}
12720
12721/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 * "getchar()" function
12723 */
12724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012725f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726{
12727 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012728 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012730 /* Position the cursor. Needed after a message that ends in a space. */
12731 windgoto(msg_row, msg_col);
12732
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 ++no_mapping;
12734 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012735 for (;;)
12736 {
12737 if (argvars[0].v_type == VAR_UNKNOWN)
12738 /* getchar(): blocking wait. */
12739 n = safe_vgetc();
12740 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12741 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012742 n = vpeekc_any();
12743 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012744 /* illegal argument or getchar(0) and no char avail: return zero */
12745 n = 0;
12746 else
12747 /* getchar(0) and char avail: return char */
12748 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012749
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012750 if (n == K_IGNORE)
12751 continue;
12752 break;
12753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 --no_mapping;
12755 --allow_keys;
12756
Bram Moolenaar219b8702006-11-01 14:32:36 +000012757 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12758 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12759 vimvars[VV_MOUSE_COL].vv_nr = 0;
12760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 if (IS_SPECIAL(n) || mod_mask != 0)
12763 {
12764 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12765 int i = 0;
12766
12767 /* Turn a special key into three bytes, plus modifier. */
12768 if (mod_mask != 0)
12769 {
12770 temp[i++] = K_SPECIAL;
12771 temp[i++] = KS_MODIFIER;
12772 temp[i++] = mod_mask;
12773 }
12774 if (IS_SPECIAL(n))
12775 {
12776 temp[i++] = K_SPECIAL;
12777 temp[i++] = K_SECOND(n);
12778 temp[i++] = K_THIRD(n);
12779 }
12780#ifdef FEAT_MBYTE
12781 else if (has_mbyte)
12782 i += (*mb_char2bytes)(n, temp + i);
12783#endif
12784 else
12785 temp[i++] = n;
12786 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012787 rettv->v_type = VAR_STRING;
12788 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012789
12790#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012791 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012792 {
12793 int row = mouse_row;
12794 int col = mouse_col;
12795 win_T *win;
12796 linenr_T lnum;
12797# ifdef FEAT_WINDOWS
12798 win_T *wp;
12799# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012800 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012801
12802 if (row >= 0 && col >= 0)
12803 {
12804 /* Find the window at the mouse coordinates and compute the
12805 * text position. */
12806 win = mouse_find_win(&row, &col);
12807 (void)mouse_comp_pos(win, &row, &col, &lnum);
12808# ifdef FEAT_WINDOWS
12809 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012810 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012811# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012812 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012813 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12814 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12815 }
12816 }
12817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 }
12819}
12820
12821/*
12822 * "getcharmod()" function
12823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012825f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012831 * "getcharsearch()" function
12832 */
12833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012834f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012835{
12836 if (rettv_dict_alloc(rettv) != FAIL)
12837 {
12838 dict_T *dict = rettv->vval.v_dict;
12839
12840 dict_add_nr_str(dict, "char", 0L, last_csearch());
12841 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12842 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12843 }
12844}
12845
12846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847 * "getcmdline()" function
12848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012850f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->v_type = VAR_STRING;
12853 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854}
12855
12856/*
12857 * "getcmdpos()" function
12858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012860f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863}
12864
12865/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012866 * "getcmdtype()" function
12867 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012869f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012870{
12871 rettv->v_type = VAR_STRING;
12872 rettv->vval.v_string = alloc(2);
12873 if (rettv->vval.v_string != NULL)
12874 {
12875 rettv->vval.v_string[0] = get_cmdline_type();
12876 rettv->vval.v_string[1] = NUL;
12877 }
12878}
12879
12880/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012881 * "getcmdwintype()" function
12882 */
12883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012884f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012885{
12886 rettv->v_type = VAR_STRING;
12887 rettv->vval.v_string = NULL;
12888#ifdef FEAT_CMDWIN
12889 rettv->vval.v_string = alloc(2);
12890 if (rettv->vval.v_string != NULL)
12891 {
12892 rettv->vval.v_string[0] = cmdwin_type;
12893 rettv->vval.v_string[1] = NUL;
12894 }
12895#endif
12896}
12897
12898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 * "getcwd()" function
12900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012902f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012904 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012905 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012908 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012909
12910 wp = find_tabwin(&argvars[0], &argvars[1]);
12911 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012913 if (wp->w_localdir != NULL)
12914 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12915 else if(globaldir != NULL)
12916 rettv->vval.v_string = vim_strsave(globaldir);
12917 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012918 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012919 cwd = alloc(MAXPATHL);
12920 if (cwd != NULL)
12921 {
12922 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12923 rettv->vval.v_string = vim_strsave(cwd);
12924 vim_free(cwd);
12925 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012926 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012927#ifdef BACKSLASH_IN_FILENAME
12928 if (rettv->vval.v_string != NULL)
12929 slash_adjust(rettv->vval.v_string);
12930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 }
12932}
12933
12934/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012935 * "getfontname()" function
12936 */
12937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012938f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->v_type = VAR_STRING;
12941 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012942#ifdef FEAT_GUI
12943 if (gui.in_use)
12944 {
12945 GuiFont font;
12946 char_u *name = NULL;
12947
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012948 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012949 {
12950 /* Get the "Normal" font. Either the name saved by
12951 * hl_set_font_name() or from the font ID. */
12952 font = gui.norm_font;
12953 name = hl_get_font_name();
12954 }
12955 else
12956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012958 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12959 return;
12960 font = gui_mch_get_font(name, FALSE);
12961 if (font == NOFONT)
12962 return; /* Invalid font name, return empty string. */
12963 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012965 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012966 gui_mch_free_font(font);
12967 }
12968#endif
12969}
12970
12971/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012972 * "getfperm({fname})" function
12973 */
12974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012975f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012976{
12977 char_u *fname;
12978 struct stat st;
12979 char_u *perm = NULL;
12980 char_u flags[] = "rwx";
12981 int i;
12982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012986 if (mch_stat((char *)fname, &st) >= 0)
12987 {
12988 perm = vim_strsave((char_u *)"---------");
12989 if (perm != NULL)
12990 {
12991 for (i = 0; i < 9; i++)
12992 {
12993 if (st.st_mode & (1 << (8 - i)))
12994 perm[i] = flags[i % 3];
12995 }
12996 }
12997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012999}
13000
13001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002 * "getfsize({fname})" function
13003 */
13004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006{
13007 char_u *fname;
13008 struct stat st;
13009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013
13014 if (mch_stat((char *)fname, &st) >= 0)
13015 {
13016 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013017 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013021
13022 /* non-perfect check for overflow */
13023 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
13024 rettv->vval.v_number = -2;
13025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 }
13027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029}
13030
13031/*
13032 * "getftime({fname})" function
13033 */
13034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013035f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036{
13037 char_u *fname;
13038 struct stat st;
13039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041
13042 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046}
13047
13048/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013049 * "getftype({fname})" function
13050 */
13051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013052f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013053{
13054 char_u *fname;
13055 struct stat st;
13056 char_u *type = NULL;
13057 char *t;
13058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013062 if (mch_lstat((char *)fname, &st) >= 0)
13063 {
13064#ifdef S_ISREG
13065 if (S_ISREG(st.st_mode))
13066 t = "file";
13067 else if (S_ISDIR(st.st_mode))
13068 t = "dir";
13069# ifdef S_ISLNK
13070 else if (S_ISLNK(st.st_mode))
13071 t = "link";
13072# endif
13073# ifdef S_ISBLK
13074 else if (S_ISBLK(st.st_mode))
13075 t = "bdev";
13076# endif
13077# ifdef S_ISCHR
13078 else if (S_ISCHR(st.st_mode))
13079 t = "cdev";
13080# endif
13081# ifdef S_ISFIFO
13082 else if (S_ISFIFO(st.st_mode))
13083 t = "fifo";
13084# endif
13085# ifdef S_ISSOCK
13086 else if (S_ISSOCK(st.st_mode))
13087 t = "fifo";
13088# endif
13089 else
13090 t = "other";
13091#else
13092# ifdef S_IFMT
13093 switch (st.st_mode & S_IFMT)
13094 {
13095 case S_IFREG: t = "file"; break;
13096 case S_IFDIR: t = "dir"; break;
13097# ifdef S_IFLNK
13098 case S_IFLNK: t = "link"; break;
13099# endif
13100# ifdef S_IFBLK
13101 case S_IFBLK: t = "bdev"; break;
13102# endif
13103# ifdef S_IFCHR
13104 case S_IFCHR: t = "cdev"; break;
13105# endif
13106# ifdef S_IFIFO
13107 case S_IFIFO: t = "fifo"; break;
13108# endif
13109# ifdef S_IFSOCK
13110 case S_IFSOCK: t = "socket"; break;
13111# endif
13112 default: t = "other";
13113 }
13114# else
13115 if (mch_isdir(fname))
13116 t = "dir";
13117 else
13118 t = "file";
13119# endif
13120#endif
13121 type = vim_strsave((char_u *)t);
13122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013124}
13125
13126/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013127 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013128 */
13129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013130f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131{
13132 linenr_T lnum;
13133 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013134 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013135
13136 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013137 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013138 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013139 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013140 retlist = FALSE;
13141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013142 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013144 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013145 retlist = TRUE;
13146 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013147
Bram Moolenaar342337a2005-07-21 21:11:17 +000013148 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013149}
13150
13151/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013152 * "getmatches()" function
13153 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013155f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013156{
13157#ifdef FEAT_SEARCH_EXTRA
13158 dict_T *dict;
13159 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013160 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013161
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013162 if (rettv_list_alloc(rettv) == OK)
13163 {
13164 while (cur != NULL)
13165 {
13166 dict = dict_alloc();
13167 if (dict == NULL)
13168 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013169 if (cur->match.regprog == NULL)
13170 {
13171 /* match added with matchaddpos() */
13172 for (i = 0; i < MAXPOSMATCH; ++i)
13173 {
13174 llpos_T *llpos;
13175 char buf[6];
13176 list_T *l;
13177
13178 llpos = &cur->pos.pos[i];
13179 if (llpos->lnum == 0)
13180 break;
13181 l = list_alloc();
13182 if (l == NULL)
13183 break;
13184 list_append_number(l, (varnumber_T)llpos->lnum);
13185 if (llpos->col > 0)
13186 {
13187 list_append_number(l, (varnumber_T)llpos->col);
13188 list_append_number(l, (varnumber_T)llpos->len);
13189 }
13190 sprintf(buf, "pos%d", i + 1);
13191 dict_add_list(dict, buf, l);
13192 }
13193 }
13194 else
13195 {
13196 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13197 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013198 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013199 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13200 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013201# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013202 if (cur->conceal_char)
13203 {
13204 char_u buf[MB_MAXBYTES + 1];
13205
13206 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13207 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13208 }
13209# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013210 list_append_dict(rettv->vval.v_list, dict);
13211 cur = cur->next;
13212 }
13213 }
13214#endif
13215}
13216
13217/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013218 * "getpid()" function
13219 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013221f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013222{
13223 rettv->vval.v_number = mch_get_pid();
13224}
13225
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013227getpos_both(
13228 typval_T *argvars,
13229 typval_T *rettv,
13230 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013231{
Bram Moolenaara5525202006-03-02 22:52:09 +000013232 pos_T *fp;
13233 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013234 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013235
13236 if (rettv_list_alloc(rettv) == OK)
13237 {
13238 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013239 if (getcurpos)
13240 fp = &curwin->w_cursor;
13241 else
13242 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013243 if (fnum != -1)
13244 list_append_number(l, (varnumber_T)fnum);
13245 else
13246 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013247 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13248 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013249 list_append_number(l, (fp != NULL)
13250 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013251 : (varnumber_T)0);
13252 list_append_number(l,
13253#ifdef FEAT_VIRTUALEDIT
13254 (fp != NULL) ? (varnumber_T)fp->coladd :
13255#endif
13256 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013257 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013258 {
13259 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013260 list_append_number(l, curwin->w_curswant == MAXCOL ?
13261 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013262 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013263 }
13264 else
13265 rettv->vval.v_number = FALSE;
13266}
13267
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013268
13269/*
13270 * "getcurpos()" function
13271 */
13272 static void
13273f_getcurpos(typval_T *argvars, typval_T *rettv)
13274{
13275 getpos_both(argvars, rettv, TRUE);
13276}
13277
13278/*
13279 * "getpos(string)" function
13280 */
13281 static void
13282f_getpos(typval_T *argvars, typval_T *rettv)
13283{
13284 getpos_both(argvars, rettv, FALSE);
13285}
13286
Bram Moolenaara5525202006-03-02 22:52:09 +000013287/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013288 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013289 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013291f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013292{
13293#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013294 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013295#endif
13296
Bram Moolenaar2641f772005-03-25 21:58:17 +000013297#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013298 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013299 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013300 wp = NULL;
13301 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13302 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013303 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013304 if (wp == NULL)
13305 return;
13306 }
13307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013308 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013309 }
13310#endif
13311}
13312
13313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 * "getreg()" function
13315 */
13316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013317f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318{
13319 char_u *strregname;
13320 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013321 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013322 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013323 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013325 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 strregname = get_tv_string_chk(&argvars[0]);
13328 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013329 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013331 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013332 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13333 return_list = get_tv_number_chk(&argvars[2], &error);
13334 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013337 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013338
13339 if (error)
13340 return;
13341
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 regname = (strregname == NULL ? '"' : *strregname);
13343 if (regname == 0)
13344 regname = '"';
13345
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013346 if (return_list)
13347 {
13348 rettv->v_type = VAR_LIST;
13349 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13350 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013351 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013352 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013353 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013354 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013355 }
13356 else
13357 {
13358 rettv->v_type = VAR_STRING;
13359 rettv->vval.v_string = get_reg_contents(regname,
13360 arg2 ? GREG_EXPR_SRC : 0);
13361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362}
13363
13364/*
13365 * "getregtype()" function
13366 */
13367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013368f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
13370 char_u *strregname;
13371 int regname;
13372 char_u buf[NUMBUFLEN + 2];
13373 long reglen = 0;
13374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013375 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013376 {
13377 strregname = get_tv_string_chk(&argvars[0]);
13378 if (strregname == NULL) /* type error; errmsg already given */
13379 {
13380 rettv->v_type = VAR_STRING;
13381 rettv->vval.v_string = NULL;
13382 return;
13383 }
13384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 else
13386 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013387 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388
13389 regname = (strregname == NULL ? '"' : *strregname);
13390 if (regname == 0)
13391 regname = '"';
13392
13393 buf[0] = NUL;
13394 buf[1] = NUL;
13395 switch (get_reg_type(regname, &reglen))
13396 {
13397 case MLINE: buf[0] = 'V'; break;
13398 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399 case MBLOCK:
13400 buf[0] = Ctrl_V;
13401 sprintf((char *)buf + 1, "%ld", reglen + 1);
13402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013404 rettv->v_type = VAR_STRING;
13405 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406}
13407
13408/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013409 * "gettabvar()" function
13410 */
13411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013412f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013413{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013414 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013415 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013416 dictitem_T *v;
13417 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013418 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013419
13420 rettv->v_type = VAR_STRING;
13421 rettv->vval.v_string = NULL;
13422
13423 varname = get_tv_string_chk(&argvars[1]);
13424 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13425 if (tp != NULL && varname != NULL)
13426 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013427 /* Set tp to be our tabpage, temporarily. Also set the window to the
13428 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013429 if (switch_win(&oldcurwin, &oldtabpage,
13430 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013431 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013432 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013433 /* look up the variable */
13434 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13435 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13436 if (v != NULL)
13437 {
13438 copy_tv(&v->di_tv, rettv);
13439 done = TRUE;
13440 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013441 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013442
13443 /* restore previous notion of curwin */
13444 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013445 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013446
13447 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013448 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013449}
13450
13451/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013452 * "gettabwinvar()" function
13453 */
13454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013455f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013456{
13457 getwinvar(argvars, rettv, 1);
13458}
13459
13460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 * "getwinposx()" function
13462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013464f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467#ifdef FEAT_GUI
13468 if (gui.in_use)
13469 {
13470 int x, y;
13471
13472 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013473 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474 }
13475#endif
13476}
13477
13478/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013479 * "win_findbuf()" function
13480 */
13481 static void
13482f_win_findbuf(typval_T *argvars, typval_T *rettv)
13483{
13484 if (rettv_list_alloc(rettv) != FAIL)
13485 win_findbuf(argvars, rettv->vval.v_list);
13486}
13487
13488/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013489 * "win_getid()" function
13490 */
13491 static void
13492f_win_getid(typval_T *argvars, typval_T *rettv)
13493{
13494 rettv->vval.v_number = win_getid(argvars);
13495}
13496
13497/*
13498 * "win_gotoid()" function
13499 */
13500 static void
13501f_win_gotoid(typval_T *argvars, typval_T *rettv)
13502{
13503 rettv->vval.v_number = win_gotoid(argvars);
13504}
13505
13506/*
13507 * "win_id2tabwin()" function
13508 */
13509 static void
13510f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13511{
13512 if (rettv_list_alloc(rettv) != FAIL)
13513 win_id2tabwin(argvars, rettv->vval.v_list);
13514}
13515
13516/*
13517 * "win_id2win()" function
13518 */
13519 static void
13520f_win_id2win(typval_T *argvars, typval_T *rettv)
13521{
13522 rettv->vval.v_number = win_id2win(argvars);
13523}
13524
13525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 * "getwinposy()" function
13527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013529f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532#ifdef FEAT_GUI
13533 if (gui.in_use)
13534 {
13535 int x, y;
13536
13537 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 }
13540#endif
13541}
13542
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013543/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013544 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013545 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013546 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013547find_win_by_nr(
13548 typval_T *vp,
13549 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013550{
13551#ifdef FEAT_WINDOWS
13552 win_T *wp;
13553#endif
13554 int nr;
13555
13556 nr = get_tv_number_chk(vp, NULL);
13557
13558#ifdef FEAT_WINDOWS
13559 if (nr < 0)
13560 return NULL;
13561 if (nr == 0)
13562 return curwin;
13563
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013564 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13565 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013566 if (--nr <= 0)
13567 break;
13568 return wp;
13569#else
13570 if (nr == 0 || nr == 1)
13571 return curwin;
13572 return NULL;
13573#endif
13574}
13575
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013577 * Find window specified by "wvp" in tabpage "tvp".
13578 */
13579 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013580find_tabwin(
13581 typval_T *wvp, /* VAR_UNKNOWN for current window */
13582 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013583{
13584 win_T *wp = NULL;
13585 tabpage_T *tp = NULL;
13586 long n;
13587
13588 if (wvp->v_type != VAR_UNKNOWN)
13589 {
13590 if (tvp->v_type != VAR_UNKNOWN)
13591 {
13592 n = get_tv_number(tvp);
13593 if (n >= 0)
13594 tp = find_tabpage(n);
13595 }
13596 else
13597 tp = curtab;
13598
13599 if (tp != NULL)
13600 wp = find_win_by_nr(wvp, tp);
13601 }
13602 else
13603 wp = curwin;
13604
13605 return wp;
13606}
13607
13608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 * "getwinvar()" function
13610 */
13611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013612f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013614 getwinvar(argvars, rettv, 0);
13615}
13616
13617/*
13618 * getwinvar() and gettabwinvar()
13619 */
13620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013621getwinvar(
13622 typval_T *argvars,
13623 typval_T *rettv,
13624 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013625{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013626 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013628 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013629 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013630 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013631#ifdef FEAT_WINDOWS
13632 win_T *oldcurwin;
13633 tabpage_T *oldtabpage;
13634 int need_switch_win;
13635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013637#ifdef FEAT_WINDOWS
13638 if (off == 1)
13639 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13640 else
13641 tp = curtab;
13642#endif
13643 win = find_win_by_nr(&argvars[off], tp);
13644 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013645 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013647 rettv->v_type = VAR_STRING;
13648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
13650 if (win != NULL && varname != NULL)
13651 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013652#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013653 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013654 * otherwise the window is not valid. Only do this when needed,
13655 * autocommands get blocked. */
13656 need_switch_win = !(tp == curtab && win == curwin);
13657 if (!need_switch_win
13658 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13659#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013660 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013661 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013662 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013663 if (get_option_tv(&varname, rettv, 1) == OK)
13664 done = TRUE;
13665 }
13666 else
13667 {
13668 /* Look up the variable. */
13669 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13670 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13671 varname, FALSE);
13672 if (v != NULL)
13673 {
13674 copy_tv(&v->di_tv, rettv);
13675 done = TRUE;
13676 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013679
Bram Moolenaarba117c22015-09-29 16:53:22 +020013680#ifdef FEAT_WINDOWS
13681 if (need_switch_win)
13682 /* restore previous notion of curwin */
13683 restore_win(oldcurwin, oldtabpage, TRUE);
13684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685 }
13686
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013687 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13688 /* use the default return value */
13689 copy_tv(&argvars[off + 2], rettv);
13690
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 --emsg_off;
13692}
13693
13694/*
13695 * "glob()" function
13696 */
13697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013698f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013700 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013702 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013704 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013705 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013707 if (argvars[1].v_type != VAR_UNKNOWN)
13708 {
13709 if (get_tv_number_chk(&argvars[1], &error))
13710 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013712 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013713 if (get_tv_number_chk(&argvars[2], &error))
13714 {
13715 rettv->v_type = VAR_LIST;
13716 rettv->vval.v_list = NULL;
13717 }
13718 if (argvars[3].v_type != VAR_UNKNOWN
13719 && get_tv_number_chk(&argvars[3], &error))
13720 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013721 }
13722 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013723 if (!error)
13724 {
13725 ExpandInit(&xpc);
13726 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013727 if (p_wic)
13728 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013729 if (rettv->v_type == VAR_STRING)
13730 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013731 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013732 else if (rettv_list_alloc(rettv) != FAIL)
13733 {
13734 int i;
13735
13736 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13737 NULL, options, WILD_ALL_KEEP);
13738 for (i = 0; i < xpc.xp_numfiles; i++)
13739 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13740
13741 ExpandCleanup(&xpc);
13742 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013743 }
13744 else
13745 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746}
13747
13748/*
13749 * "globpath()" function
13750 */
13751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013752f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013754 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013756 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013757 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013758 garray_T ga;
13759 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013761 /* When the optional second argument is non-zero, don't remove matches
13762 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013763 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013764 if (argvars[2].v_type != VAR_UNKNOWN)
13765 {
13766 if (get_tv_number_chk(&argvars[2], &error))
13767 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013768 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013769 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013770 if (get_tv_number_chk(&argvars[3], &error))
13771 {
13772 rettv->v_type = VAR_LIST;
13773 rettv->vval.v_list = NULL;
13774 }
13775 if (argvars[4].v_type != VAR_UNKNOWN
13776 && get_tv_number_chk(&argvars[4], &error))
13777 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013778 }
13779 }
13780 if (file != NULL && !error)
13781 {
13782 ga_init2(&ga, (int)sizeof(char_u *), 10);
13783 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13784 if (rettv->v_type == VAR_STRING)
13785 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13786 else if (rettv_list_alloc(rettv) != FAIL)
13787 for (i = 0; i < ga.ga_len; ++i)
13788 list_append_string(rettv->vval.v_list,
13789 ((char_u **)(ga.ga_data))[i], -1);
13790 ga_clear_strings(&ga);
13791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794}
13795
13796/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013797 * "glob2regpat()" function
13798 */
13799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013800f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013801{
13802 char_u *pat = get_tv_string_chk(&argvars[0]);
13803
13804 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013805 rettv->vval.v_string = (pat == NULL)
13806 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013807}
13808
13809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810 * "has()" function
13811 */
13812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013813f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814{
13815 int i;
13816 char_u *name;
13817 int n = FALSE;
13818 static char *(has_list[]) =
13819 {
13820#ifdef AMIGA
13821 "amiga",
13822# ifdef FEAT_ARP
13823 "arp",
13824# endif
13825#endif
13826#ifdef __BEOS__
13827 "beos",
13828#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013829#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 "mac",
13831#endif
13832#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013833 "macunix", /* built with 'darwin' enabled */
13834#endif
13835#if defined(__APPLE__) && __APPLE__ == 1
13836 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#ifdef __QNX__
13839 "qnx",
13840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#ifdef UNIX
13842 "unix",
13843#endif
13844#ifdef VMS
13845 "vms",
13846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#ifdef WIN32
13848 "win32",
13849#endif
13850#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13851 "win32unix",
13852#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013853#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 "win64",
13855#endif
13856#ifdef EBCDIC
13857 "ebcdic",
13858#endif
13859#ifndef CASE_INSENSITIVE_FILENAME
13860 "fname_case",
13861#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013862#ifdef HAVE_ACL
13863 "acl",
13864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865#ifdef FEAT_ARABIC
13866 "arabic",
13867#endif
13868#ifdef FEAT_AUTOCMD
13869 "autocmd",
13870#endif
13871#ifdef FEAT_BEVAL
13872 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013873# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13874 "balloon_multiline",
13875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876#endif
13877#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13878 "builtin_terms",
13879# ifdef ALL_BUILTIN_TCAPS
13880 "all_builtin_terms",
13881# endif
13882#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013883#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13884 || defined(FEAT_GUI_W32) \
13885 || defined(FEAT_GUI_MOTIF))
13886 "browsefilter",
13887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888#ifdef FEAT_BYTEOFF
13889 "byte_offset",
13890#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013891#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013892 "channel",
13893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894#ifdef FEAT_CINDENT
13895 "cindent",
13896#endif
13897#ifdef FEAT_CLIENTSERVER
13898 "clientserver",
13899#endif
13900#ifdef FEAT_CLIPBOARD
13901 "clipboard",
13902#endif
13903#ifdef FEAT_CMDL_COMPL
13904 "cmdline_compl",
13905#endif
13906#ifdef FEAT_CMDHIST
13907 "cmdline_hist",
13908#endif
13909#ifdef FEAT_COMMENTS
13910 "comments",
13911#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013912#ifdef FEAT_CONCEAL
13913 "conceal",
13914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915#ifdef FEAT_CRYPT
13916 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013917 "crypt-blowfish",
13918 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919#endif
13920#ifdef FEAT_CSCOPE
13921 "cscope",
13922#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013923#ifdef FEAT_CURSORBIND
13924 "cursorbind",
13925#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013926#ifdef CURSOR_SHAPE
13927 "cursorshape",
13928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929#ifdef DEBUG
13930 "debug",
13931#endif
13932#ifdef FEAT_CON_DIALOG
13933 "dialog_con",
13934#endif
13935#ifdef FEAT_GUI_DIALOG
13936 "dialog_gui",
13937#endif
13938#ifdef FEAT_DIFF
13939 "diff",
13940#endif
13941#ifdef FEAT_DIGRAPHS
13942 "digraphs",
13943#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013944#ifdef FEAT_DIRECTX
13945 "directx",
13946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947#ifdef FEAT_DND
13948 "dnd",
13949#endif
13950#ifdef FEAT_EMACS_TAGS
13951 "emacs_tags",
13952#endif
13953 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013954 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955#ifdef FEAT_SEARCH_EXTRA
13956 "extra_search",
13957#endif
13958#ifdef FEAT_FKMAP
13959 "farsi",
13960#endif
13961#ifdef FEAT_SEARCHPATH
13962 "file_in_path",
13963#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013964#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013965 "filterpipe",
13966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef FEAT_FIND_ID
13968 "find_in_path",
13969#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013970#ifdef FEAT_FLOAT
13971 "float",
13972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973#ifdef FEAT_FOLDING
13974 "folding",
13975#endif
13976#ifdef FEAT_FOOTER
13977 "footer",
13978#endif
13979#if !defined(USE_SYSTEM) && defined(UNIX)
13980 "fork",
13981#endif
13982#ifdef FEAT_GETTEXT
13983 "gettext",
13984#endif
13985#ifdef FEAT_GUI
13986 "gui",
13987#endif
13988#ifdef FEAT_GUI_ATHENA
13989# ifdef FEAT_GUI_NEXTAW
13990 "gui_neXtaw",
13991# else
13992 "gui_athena",
13993# endif
13994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995#ifdef FEAT_GUI_GTK
13996 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013997# ifdef USE_GTK3
13998 "gui_gtk3",
13999# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014003#ifdef FEAT_GUI_GNOME
14004 "gui_gnome",
14005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006#ifdef FEAT_GUI_MAC
14007 "gui_mac",
14008#endif
14009#ifdef FEAT_GUI_MOTIF
14010 "gui_motif",
14011#endif
14012#ifdef FEAT_GUI_PHOTON
14013 "gui_photon",
14014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015#ifdef FEAT_GUI_W32
14016 "gui_win32",
14017#endif
14018#ifdef FEAT_HANGULIN
14019 "hangul_input",
14020#endif
14021#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14022 "iconv",
14023#endif
14024#ifdef FEAT_INS_EXPAND
14025 "insert_expand",
14026#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014027#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014028 "job",
14029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030#ifdef FEAT_JUMPLIST
14031 "jumplist",
14032#endif
14033#ifdef FEAT_KEYMAP
14034 "keymap",
14035#endif
14036#ifdef FEAT_LANGMAP
14037 "langmap",
14038#endif
14039#ifdef FEAT_LIBCALL
14040 "libcall",
14041#endif
14042#ifdef FEAT_LINEBREAK
14043 "linebreak",
14044#endif
14045#ifdef FEAT_LISP
14046 "lispindent",
14047#endif
14048#ifdef FEAT_LISTCMDS
14049 "listcmds",
14050#endif
14051#ifdef FEAT_LOCALMAP
14052 "localmap",
14053#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014054#ifdef FEAT_LUA
14055# ifndef DYNAMIC_LUA
14056 "lua",
14057# endif
14058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059#ifdef FEAT_MENU
14060 "menu",
14061#endif
14062#ifdef FEAT_SESSION
14063 "mksession",
14064#endif
14065#ifdef FEAT_MODIFY_FNAME
14066 "modify_fname",
14067#endif
14068#ifdef FEAT_MOUSE
14069 "mouse",
14070#endif
14071#ifdef FEAT_MOUSESHAPE
14072 "mouseshape",
14073#endif
14074#if defined(UNIX) || defined(VMS)
14075# ifdef FEAT_MOUSE_DEC
14076 "mouse_dec",
14077# endif
14078# ifdef FEAT_MOUSE_GPM
14079 "mouse_gpm",
14080# endif
14081# ifdef FEAT_MOUSE_JSB
14082 "mouse_jsbterm",
14083# endif
14084# ifdef FEAT_MOUSE_NET
14085 "mouse_netterm",
14086# endif
14087# ifdef FEAT_MOUSE_PTERM
14088 "mouse_pterm",
14089# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014090# ifdef FEAT_MOUSE_SGR
14091 "mouse_sgr",
14092# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014093# ifdef FEAT_SYSMOUSE
14094 "mouse_sysmouse",
14095# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014096# ifdef FEAT_MOUSE_URXVT
14097 "mouse_urxvt",
14098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099# ifdef FEAT_MOUSE_XTERM
14100 "mouse_xterm",
14101# endif
14102#endif
14103#ifdef FEAT_MBYTE
14104 "multi_byte",
14105#endif
14106#ifdef FEAT_MBYTE_IME
14107 "multi_byte_ime",
14108#endif
14109#ifdef FEAT_MULTI_LANG
14110 "multi_lang",
14111#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014112#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014113#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014114 "mzscheme",
14115#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014117#ifdef FEAT_OLE
14118 "ole",
14119#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014120 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121#ifdef FEAT_PATH_EXTRA
14122 "path_extra",
14123#endif
14124#ifdef FEAT_PERL
14125#ifndef DYNAMIC_PERL
14126 "perl",
14127#endif
14128#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014129#ifdef FEAT_PERSISTENT_UNDO
14130 "persistent_undo",
14131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132#ifdef FEAT_PYTHON
14133#ifndef DYNAMIC_PYTHON
14134 "python",
14135#endif
14136#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014137#ifdef FEAT_PYTHON3
14138#ifndef DYNAMIC_PYTHON3
14139 "python3",
14140#endif
14141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142#ifdef FEAT_POSTSCRIPT
14143 "postscript",
14144#endif
14145#ifdef FEAT_PRINTER
14146 "printer",
14147#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014148#ifdef FEAT_PROFILE
14149 "profile",
14150#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014151#ifdef FEAT_RELTIME
14152 "reltime",
14153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154#ifdef FEAT_QUICKFIX
14155 "quickfix",
14156#endif
14157#ifdef FEAT_RIGHTLEFT
14158 "rightleft",
14159#endif
14160#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14161 "ruby",
14162#endif
14163#ifdef FEAT_SCROLLBIND
14164 "scrollbind",
14165#endif
14166#ifdef FEAT_CMDL_INFO
14167 "showcmd",
14168 "cmdline_info",
14169#endif
14170#ifdef FEAT_SIGNS
14171 "signs",
14172#endif
14173#ifdef FEAT_SMARTINDENT
14174 "smartindent",
14175#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014176#ifdef STARTUPTIME
14177 "startuptime",
14178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179#ifdef FEAT_STL_OPT
14180 "statusline",
14181#endif
14182#ifdef FEAT_SUN_WORKSHOP
14183 "sun_workshop",
14184#endif
14185#ifdef FEAT_NETBEANS_INTG
14186 "netbeans_intg",
14187#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014188#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014189 "spell",
14190#endif
14191#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192 "syntax",
14193#endif
14194#if defined(USE_SYSTEM) || !defined(UNIX)
14195 "system",
14196#endif
14197#ifdef FEAT_TAG_BINS
14198 "tag_binary",
14199#endif
14200#ifdef FEAT_TAG_OLDSTATIC
14201 "tag_old_static",
14202#endif
14203#ifdef FEAT_TAG_ANYWHITE
14204 "tag_any_white",
14205#endif
14206#ifdef FEAT_TCL
14207# ifndef DYNAMIC_TCL
14208 "tcl",
14209# endif
14210#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014211#ifdef FEAT_TERMGUICOLORS
14212 "termguicolors",
14213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214#ifdef TERMINFO
14215 "terminfo",
14216#endif
14217#ifdef FEAT_TERMRESPONSE
14218 "termresponse",
14219#endif
14220#ifdef FEAT_TEXTOBJ
14221 "textobjects",
14222#endif
14223#ifdef HAVE_TGETENT
14224 "tgetent",
14225#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014226#ifdef FEAT_TIMERS
14227 "timers",
14228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229#ifdef FEAT_TITLE
14230 "title",
14231#endif
14232#ifdef FEAT_TOOLBAR
14233 "toolbar",
14234#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014235#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14236 "unnamedplus",
14237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238#ifdef FEAT_USR_CMDS
14239 "user-commands", /* was accidentally included in 5.4 */
14240 "user_commands",
14241#endif
14242#ifdef FEAT_VIMINFO
14243 "viminfo",
14244#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014245#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 "vertsplit",
14247#endif
14248#ifdef FEAT_VIRTUALEDIT
14249 "virtualedit",
14250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252#ifdef FEAT_VISUALEXTRA
14253 "visualextra",
14254#endif
14255#ifdef FEAT_VREPLACE
14256 "vreplace",
14257#endif
14258#ifdef FEAT_WILDIGN
14259 "wildignore",
14260#endif
14261#ifdef FEAT_WILDMENU
14262 "wildmenu",
14263#endif
14264#ifdef FEAT_WINDOWS
14265 "windows",
14266#endif
14267#ifdef FEAT_WAK
14268 "winaltkeys",
14269#endif
14270#ifdef FEAT_WRITEBACKUP
14271 "writebackup",
14272#endif
14273#ifdef FEAT_XIM
14274 "xim",
14275#endif
14276#ifdef FEAT_XFONTSET
14277 "xfontset",
14278#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014279#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014280 "xpm",
14281 "xpm_w32", /* for backward compatibility */
14282#else
14283# if defined(HAVE_XPM)
14284 "xpm",
14285# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287#ifdef USE_XSMP
14288 "xsmp",
14289#endif
14290#ifdef USE_XSMP_INTERACT
14291 "xsmp_interact",
14292#endif
14293#ifdef FEAT_XCLIPBOARD
14294 "xterm_clipboard",
14295#endif
14296#ifdef FEAT_XTERM_SAVE
14297 "xterm_save",
14298#endif
14299#if defined(UNIX) && defined(FEAT_X11)
14300 "X11",
14301#endif
14302 NULL
14303 };
14304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014305 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 for (i = 0; has_list[i] != NULL; ++i)
14307 if (STRICMP(name, has_list[i]) == 0)
14308 {
14309 n = TRUE;
14310 break;
14311 }
14312
14313 if (n == FALSE)
14314 {
14315 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014316 {
14317 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014318 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014319 && vim_isdigit(name[6])
14320 && vim_isdigit(name[8])
14321 && vim_isdigit(name[10]))
14322 {
14323 int major = atoi((char *)name + 6);
14324 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014325
14326 /* Expect "patch-9.9.01234". */
14327 n = (major < VIM_VERSION_MAJOR
14328 || (major == VIM_VERSION_MAJOR
14329 && (minor < VIM_VERSION_MINOR
14330 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014331 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014332 }
14333 else
14334 n = has_patch(atoi((char *)name + 5));
14335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 else if (STRICMP(name, "vim_starting") == 0)
14337 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014338#ifdef FEAT_MBYTE
14339 else if (STRICMP(name, "multi_byte_encoding") == 0)
14340 n = has_mbyte;
14341#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014342#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14343 else if (STRICMP(name, "balloon_multiline") == 0)
14344 n = multiline_balloon_available();
14345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346#ifdef DYNAMIC_TCL
14347 else if (STRICMP(name, "tcl") == 0)
14348 n = tcl_enabled(FALSE);
14349#endif
14350#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14351 else if (STRICMP(name, "iconv") == 0)
14352 n = iconv_enabled(FALSE);
14353#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014354#ifdef DYNAMIC_LUA
14355 else if (STRICMP(name, "lua") == 0)
14356 n = lua_enabled(FALSE);
14357#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014358#ifdef DYNAMIC_MZSCHEME
14359 else if (STRICMP(name, "mzscheme") == 0)
14360 n = mzscheme_enabled(FALSE);
14361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362#ifdef DYNAMIC_RUBY
14363 else if (STRICMP(name, "ruby") == 0)
14364 n = ruby_enabled(FALSE);
14365#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014366#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367#ifdef DYNAMIC_PYTHON
14368 else if (STRICMP(name, "python") == 0)
14369 n = python_enabled(FALSE);
14370#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014371#endif
14372#ifdef FEAT_PYTHON3
14373#ifdef DYNAMIC_PYTHON3
14374 else if (STRICMP(name, "python3") == 0)
14375 n = python3_enabled(FALSE);
14376#endif
14377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378#ifdef DYNAMIC_PERL
14379 else if (STRICMP(name, "perl") == 0)
14380 n = perl_enabled(FALSE);
14381#endif
14382#ifdef FEAT_GUI
14383 else if (STRICMP(name, "gui_running") == 0)
14384 n = (gui.in_use || gui.starting);
14385# ifdef FEAT_GUI_W32
14386 else if (STRICMP(name, "gui_win32s") == 0)
14387 n = gui_is_win32s();
14388# endif
14389# ifdef FEAT_BROWSE
14390 else if (STRICMP(name, "browse") == 0)
14391 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14392# endif
14393#endif
14394#ifdef FEAT_SYN_HL
14395 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014396 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397#endif
14398#if defined(WIN3264)
14399 else if (STRICMP(name, "win95") == 0)
14400 n = mch_windows95();
14401#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014402#ifdef FEAT_NETBEANS_INTG
14403 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014404 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 }
14407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409}
14410
14411/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014412 * "has_key()" function
14413 */
14414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014416{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014417 if (argvars[0].v_type != VAR_DICT)
14418 {
14419 EMSG(_(e_dictreq));
14420 return;
14421 }
14422 if (argvars[0].vval.v_dict == NULL)
14423 return;
14424
14425 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014426 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014427}
14428
14429/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014430 * "haslocaldir()" function
14431 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014433f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014434{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014435 win_T *wp = NULL;
14436
14437 wp = find_tabwin(&argvars[0], &argvars[1]);
14438 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014439}
14440
14441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442 * "hasmapto()" function
14443 */
14444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014445f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446{
14447 char_u *name;
14448 char_u *mode;
14449 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014450 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014452 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014453 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 mode = (char_u *)"nvo";
14455 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014458 if (argvars[2].v_type != VAR_UNKNOWN)
14459 abbr = get_tv_number(&argvars[2]);
14460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461
Bram Moolenaar2c932302006-03-18 21:42:09 +000014462 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466}
14467
14468/*
14469 * "histadd()" function
14470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014472f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473{
14474#ifdef FEAT_CMDHIST
14475 int histype;
14476 char_u *str;
14477 char_u buf[NUMBUFLEN];
14478#endif
14479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 if (check_restricted() || check_secure())
14482 return;
14483#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14485 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 if (histype >= 0)
14487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014488 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 if (*str != NUL)
14490 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014491 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014493 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494 return;
14495 }
14496 }
14497#endif
14498}
14499
14500/*
14501 * "histdel()" function
14502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014504f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505{
14506#ifdef FEAT_CMDHIST
14507 int n;
14508 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014509 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14512 if (str == NULL)
14513 n = 0;
14514 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014516 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014517 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 else
14522 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014523 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 get_tv_string_buf(&argvars[1], buf));
14525 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526#endif
14527}
14528
14529/*
14530 * "histget()" function
14531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014533f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534{
14535#ifdef FEAT_CMDHIST
14536 int type;
14537 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014538 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014540 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14541 if (str == NULL)
14542 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014544 {
14545 type = get_histtype(str);
14546 if (argvars[1].v_type == VAR_UNKNOWN)
14547 idx = get_history_idx(type);
14548 else
14549 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14550 /* -1 on type error */
14551 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557}
14558
14559/*
14560 * "histnr()" function
14561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014563f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564{
14565 int i;
14566
14567#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 char_u *history = get_tv_string_chk(&argvars[0]);
14569
14570 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571 if (i >= HIST_CMD && i < HIST_COUNT)
14572 i = get_history_idx(i);
14573 else
14574#endif
14575 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
14579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 * "highlightID(name)" function
14581 */
14582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014583f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586}
14587
14588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589 * "highlight_exists()" function
14590 */
14591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014592f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593{
14594 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14595}
14596
14597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 * "hostname()" function
14599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014601f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602{
14603 char_u hostname[256];
14604
14605 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->v_type = VAR_STRING;
14607 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608}
14609
14610/*
14611 * iconv() function
14612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014614f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615{
14616#ifdef FEAT_MBYTE
14617 char_u buf1[NUMBUFLEN];
14618 char_u buf2[NUMBUFLEN];
14619 char_u *from, *to, *str;
14620 vimconv_T vimconv;
14621#endif
14622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014623 rettv->v_type = VAR_STRING;
14624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625
14626#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 str = get_tv_string(&argvars[0]);
14628 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14629 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 vimconv.vc_type = CONV_NONE;
14631 convert_setup(&vimconv, from, to);
14632
14633 /* If the encodings are equal, no conversion needed. */
14634 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014637 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638
14639 convert_setup(&vimconv, NULL, NULL);
14640 vim_free(from);
14641 vim_free(to);
14642#endif
14643}
14644
14645/*
14646 * "indent()" function
14647 */
14648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014649f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014650{
14651 linenr_T lnum;
14652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014653 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014657 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658}
14659
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014660/*
14661 * "index()" function
14662 */
14663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014664f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014665{
Bram Moolenaar33570922005-01-25 22:26:29 +000014666 list_T *l;
14667 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014668 long idx = 0;
14669 int ic = FALSE;
14670
14671 rettv->vval.v_number = -1;
14672 if (argvars[0].v_type != VAR_LIST)
14673 {
14674 EMSG(_(e_listreq));
14675 return;
14676 }
14677 l = argvars[0].vval.v_list;
14678 if (l != NULL)
14679 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014680 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014681 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014683 int error = FALSE;
14684
Bram Moolenaar758711c2005-02-02 23:11:38 +000014685 /* Start at specified item. Use the cached index that list_find()
14686 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014687 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014688 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014689 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 ic = get_tv_number_chk(&argvars[3], &error);
14691 if (error)
14692 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014693 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014694
Bram Moolenaar758711c2005-02-02 23:11:38 +000014695 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014696 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014697 {
14698 rettv->vval.v_number = idx;
14699 break;
14700 }
14701 }
14702}
14703
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704static int inputsecret_flag = 0;
14705
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014706static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014707
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014709 * This function is used by f_input() and f_inputdialog() functions. The third
14710 * argument to f_input() specifies the type of completion to use at the
14711 * prompt. The third argument to f_inputdialog() specifies the value to return
14712 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 */
14714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014715get_user_input(
14716 typval_T *argvars,
14717 typval_T *rettv,
14718 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014720 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014721 char_u *p = NULL;
14722 int c;
14723 char_u buf[NUMBUFLEN];
14724 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014725 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014726 int xp_type = EXPAND_NOTHING;
14727 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014729 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731
14732#ifdef NO_CONSOLE_INPUT
14733 /* While starting up, there is no place to enter text. */
14734 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736#endif
14737
14738 cmd_silent = FALSE; /* Want to see the prompt. */
14739 if (prompt != NULL)
14740 {
14741 /* Only the part of the message after the last NL is considered as
14742 * prompt for the command line */
14743 p = vim_strrchr(prompt, '\n');
14744 if (p == NULL)
14745 p = prompt;
14746 else
14747 {
14748 ++p;
14749 c = *p;
14750 *p = NUL;
14751 msg_start();
14752 msg_clr_eos();
14753 msg_puts_attr(prompt, echo_attr);
14754 msg_didout = FALSE;
14755 msg_starthere();
14756 *p = c;
14757 }
14758 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 if (argvars[1].v_type != VAR_UNKNOWN)
14761 {
14762 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14763 if (defstr != NULL)
14764 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014766 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014767 {
14768 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014769 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014770 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014771
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014772 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014773 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014774
Bram Moolenaar4463f292005-09-25 22:20:24 +000014775 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14776 if (xp_name == NULL)
14777 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014778
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014779 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014780
Bram Moolenaar4463f292005-09-25 22:20:24 +000014781 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14782 &xp_arg) == FAIL)
14783 return;
14784 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014785 }
14786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014787 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014788 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014789 int save_ex_normal_busy = ex_normal_busy;
14790 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014792 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14793 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014794 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014795 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014796 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014797 && argvars[1].v_type != VAR_UNKNOWN
14798 && argvars[2].v_type != VAR_UNKNOWN)
14799 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14800 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014801
14802 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014804 /* since the user typed this, no need to wait for return */
14805 need_wait_return = FALSE;
14806 msg_didout = FALSE;
14807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808 cmd_silent = cmd_silent_save;
14809}
14810
14811/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014812 * "input()" function
14813 * Also handles inputsecret() when inputsecret is set.
14814 */
14815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014816f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014817{
14818 get_user_input(argvars, rettv, FALSE);
14819}
14820
14821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822 * "inputdialog()" function
14823 */
14824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014825f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826{
14827#if defined(FEAT_GUI_TEXTDIALOG)
14828 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14829 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14830 {
14831 char_u *message;
14832 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 message = get_tv_string_chk(&argvars[0]);
14836 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014837 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014838 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 else
14840 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014841 if (message != NULL && defstr != NULL
14842 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014843 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014844 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 else
14846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 if (message != NULL && defstr != NULL
14848 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014849 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014850 rettv->vval.v_string = vim_strsave(
14851 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 }
14857 else
14858#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014859 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860}
14861
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014862/*
14863 * "inputlist()" function
14864 */
14865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014866f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014867{
14868 listitem_T *li;
14869 int selected;
14870 int mouse_used;
14871
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014872#ifdef NO_CONSOLE_INPUT
14873 /* While starting up, there is no place to enter text. */
14874 if (no_console_input())
14875 return;
14876#endif
14877 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14878 {
14879 EMSG2(_(e_listarg), "inputlist()");
14880 return;
14881 }
14882
14883 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014884 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014885 lines_left = Rows; /* avoid more prompt */
14886 msg_scroll = TRUE;
14887 msg_clr_eos();
14888
14889 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14890 {
14891 msg_puts(get_tv_string(&li->li_tv));
14892 msg_putchar('\n');
14893 }
14894
14895 /* Ask for choice. */
14896 selected = prompt_for_number(&mouse_used);
14897 if (mouse_used)
14898 selected -= lines_left;
14899
14900 rettv->vval.v_number = selected;
14901}
14902
14903
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14905
14906/*
14907 * "inputrestore()" function
14908 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014910f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911{
14912 if (ga_userinput.ga_len > 0)
14913 {
14914 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14916 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014917 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 }
14919 else if (p_verbose > 1)
14920 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014921 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 }
14924}
14925
14926/*
14927 * "inputsave()" function
14928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014932 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 if (ga_grow(&ga_userinput, 1) == OK)
14934 {
14935 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14936 + ga_userinput.ga_len);
14937 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014938 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 }
14940 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014941 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942}
14943
14944/*
14945 * "inputsecret()" function
14946 */
14947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014948f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949{
14950 ++cmdline_star;
14951 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014952 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 --cmdline_star;
14954 --inputsecret_flag;
14955}
14956
14957/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014958 * "insert()" function
14959 */
14960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014961f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014962{
14963 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 listitem_T *item;
14965 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014967
14968 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014970 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014971 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014972 {
14973 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 before = get_tv_number_chk(&argvars[2], &error);
14975 if (error)
14976 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014977
Bram Moolenaar758711c2005-02-02 23:11:38 +000014978 if (before == l->lv_len)
14979 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014980 else
14981 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014982 item = list_find(l, before);
14983 if (item == NULL)
14984 {
14985 EMSGN(_(e_listidx), before);
14986 l = NULL;
14987 }
14988 }
14989 if (l != NULL)
14990 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014991 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014992 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014993 }
14994 }
14995}
14996
14997/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014998 * "invert(expr)" function
14999 */
15000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015001f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015002{
15003 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15004}
15005
15006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 * "isdirectory()" function
15008 */
15009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015010f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013}
15014
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015015/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015016 * "islocked()" function
15017 */
15018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015019f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015020{
15021 lval_T lv;
15022 char_u *end;
15023 dictitem_T *di;
15024
15025 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015026 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15027 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015028 if (end != NULL && lv.ll_name != NULL)
15029 {
15030 if (*end != NUL)
15031 EMSG(_(e_trailing));
15032 else
15033 {
15034 if (lv.ll_tv == NULL)
15035 {
15036 if (check_changedtick(lv.ll_name))
15037 rettv->vval.v_number = 1; /* always locked */
15038 else
15039 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015040 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015041 if (di != NULL)
15042 {
15043 /* Consider a variable locked when:
15044 * 1. the variable itself is locked
15045 * 2. the value of the variable is locked.
15046 * 3. the List or Dict value is locked.
15047 */
15048 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15049 || tv_islocked(&di->di_tv));
15050 }
15051 }
15052 }
15053 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015054 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015055 else if (lv.ll_newkey != NULL)
15056 EMSG2(_(e_dictkey), lv.ll_newkey);
15057 else if (lv.ll_list != NULL)
15058 /* List item. */
15059 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15060 else
15061 /* Dictionary item. */
15062 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15063 }
15064 }
15065
15066 clear_lval(&lv);
15067}
15068
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015069#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15070/*
15071 * "isnan()" function
15072 */
15073 static void
15074f_isnan(typval_T *argvars, typval_T *rettv)
15075{
15076 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15077 && isnan(argvars[0].vval.v_float);
15078}
15079#endif
15080
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015081static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015082
15083/*
15084 * Turn a dict into a list:
15085 * "what" == 0: list of keys
15086 * "what" == 1: list of values
15087 * "what" == 2: list of items
15088 */
15089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015090dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015091{
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 list_T *l2;
15093 dictitem_T *di;
15094 hashitem_T *hi;
15095 listitem_T *li;
15096 listitem_T *li2;
15097 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015098 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015099
Bram Moolenaar8c711452005-01-14 21:53:12 +000015100 if (argvars[0].v_type != VAR_DICT)
15101 {
15102 EMSG(_(e_dictreq));
15103 return;
15104 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015105 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015106 return;
15107
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015108 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015109 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015111 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015114 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015116 --todo;
15117 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015118
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015119 li = listitem_alloc();
15120 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015121 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015122 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015123
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015124 if (what == 0)
15125 {
15126 /* keys() */
15127 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015128 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015129 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15130 }
15131 else if (what == 1)
15132 {
15133 /* values() */
15134 copy_tv(&di->di_tv, &li->li_tv);
15135 }
15136 else
15137 {
15138 /* items() */
15139 l2 = list_alloc();
15140 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015141 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015142 li->li_tv.vval.v_list = l2;
15143 if (l2 == NULL)
15144 break;
15145 ++l2->lv_refcount;
15146
15147 li2 = listitem_alloc();
15148 if (li2 == NULL)
15149 break;
15150 list_append(l2, li2);
15151 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015152 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015153 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15154
15155 li2 = listitem_alloc();
15156 if (li2 == NULL)
15157 break;
15158 list_append(l2, li2);
15159 copy_tv(&di->di_tv, &li2->li_tv);
15160 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015161 }
15162 }
15163}
15164
15165/*
15166 * "items(dict)" function
15167 */
15168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015169f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015170{
15171 dict_list(argvars, rettv, 2);
15172}
15173
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015174#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015175/*
15176 * Get the job from the argument.
15177 * Returns NULL if the job is invalid.
15178 */
15179 static job_T *
15180get_job_arg(typval_T *tv)
15181{
15182 job_T *job;
15183
15184 if (tv->v_type != VAR_JOB)
15185 {
15186 EMSG2(_(e_invarg2), get_tv_string(tv));
15187 return NULL;
15188 }
15189 job = tv->vval.v_job;
15190
15191 if (job == NULL)
15192 EMSG(_("E916: not a valid job"));
15193 return job;
15194}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015195
Bram Moolenaar835dc632016-02-07 14:27:38 +010015196/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015197 * "job_getchannel()" function
15198 */
15199 static void
15200f_job_getchannel(typval_T *argvars, typval_T *rettv)
15201{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015202 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015203
Bram Moolenaar65edff82016-02-21 16:40:11 +010015204 if (job != NULL)
15205 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015206 rettv->v_type = VAR_CHANNEL;
15207 rettv->vval.v_channel = job->jv_channel;
15208 if (job->jv_channel != NULL)
15209 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015210 }
15211}
15212
15213/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015214 * "job_info()" function
15215 */
15216 static void
15217f_job_info(typval_T *argvars, typval_T *rettv)
15218{
15219 job_T *job = get_job_arg(&argvars[0]);
15220
15221 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15222 job_info(job, rettv->vval.v_dict);
15223}
15224
15225/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015226 * "job_setoptions()" function
15227 */
15228 static void
15229f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15230{
15231 job_T *job = get_job_arg(&argvars[0]);
15232 jobopt_T opt;
15233
15234 if (job == NULL)
15235 return;
15236 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015237 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15238 job_set_options(job, &opt);
15239 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015240}
15241
15242/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015243 * "job_start()" function
15244 */
15245 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015246f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015247{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015248 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015249 if (check_restricted() || check_secure())
15250 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015251 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015252}
15253
15254/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015255 * "job_status()" function
15256 */
15257 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015258f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015259{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015260 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015261
Bram Moolenaar65edff82016-02-21 16:40:11 +010015262 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015263 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015264 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015265 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015266 }
15267}
15268
15269/*
15270 * "job_stop()" function
15271 */
15272 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015273f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015274{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015275 job_T *job = get_job_arg(&argvars[0]);
15276
15277 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015278 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015279}
15280#endif
15281
Bram Moolenaar071d4272004-06-13 20:20:40 +000015282/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015283 * "join()" function
15284 */
15285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015286f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015287{
15288 garray_T ga;
15289 char_u *sep;
15290
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015291 if (argvars[0].v_type != VAR_LIST)
15292 {
15293 EMSG(_(e_listreq));
15294 return;
15295 }
15296 if (argvars[0].vval.v_list == NULL)
15297 return;
15298 if (argvars[1].v_type == VAR_UNKNOWN)
15299 sep = (char_u *)" ";
15300 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015301 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015302
15303 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304
15305 if (sep != NULL)
15306 {
15307 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015308 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015309 ga_append(&ga, NUL);
15310 rettv->vval.v_string = (char_u *)ga.ga_data;
15311 }
15312 else
15313 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015314}
15315
15316/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015317 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015318 */
15319 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015320f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015321{
15322 js_read_T reader;
15323
15324 reader.js_buf = get_tv_string(&argvars[0]);
15325 reader.js_fill = NULL;
15326 reader.js_used = 0;
15327 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15328 EMSG(_(e_invarg));
15329}
15330
15331/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015332 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015333 */
15334 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015335f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015336{
15337 rettv->v_type = VAR_STRING;
15338 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15339}
15340
15341/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015342 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015343 */
15344 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015345f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015346{
15347 js_read_T reader;
15348
15349 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015350 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015351 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015352 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015353 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015354}
15355
15356/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015357 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015358 */
15359 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015360f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015361{
15362 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015363 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015364}
15365
15366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015367 * "keys()" function
15368 */
15369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015370f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015371{
15372 dict_list(argvars, rettv, 0);
15373}
15374
15375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 * "last_buffer_nr()" function.
15377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015379f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380{
15381 int n = 0;
15382 buf_T *buf;
15383
15384 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15385 if (n < buf->b_fnum)
15386 n = buf->b_fnum;
15387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015389}
15390
15391/*
15392 * "len()" function
15393 */
15394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015395f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015396{
15397 switch (argvars[0].v_type)
15398 {
15399 case VAR_STRING:
15400 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->vval.v_number = (varnumber_T)STRLEN(
15402 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015403 break;
15404 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015406 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015407 case VAR_DICT:
15408 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15409 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015410 case VAR_UNKNOWN:
15411 case VAR_SPECIAL:
15412 case VAR_FLOAT:
15413 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015414 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015415 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015416 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015417 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418 break;
15419 }
15420}
15421
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015422static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015423
15424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015425libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015426{
15427#ifdef FEAT_LIBCALL
15428 char_u *string_in;
15429 char_u **string_result;
15430 int nr_result;
15431#endif
15432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015434 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015435 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015436
15437 if (check_restricted() || check_secure())
15438 return;
15439
15440#ifdef FEAT_LIBCALL
15441 /* The first two args must be strings, otherwise its meaningless */
15442 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15443 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015444 string_in = NULL;
15445 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015446 string_in = argvars[2].vval.v_string;
15447 if (type == VAR_NUMBER)
15448 string_result = NULL;
15449 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015451 if (mch_libcall(argvars[0].vval.v_string,
15452 argvars[1].vval.v_string,
15453 string_in,
15454 argvars[2].vval.v_number,
15455 string_result,
15456 &nr_result) == OK
15457 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015459 }
15460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461}
15462
15463/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015464 * "libcall()" function
15465 */
15466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015467f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015468{
15469 libcall_common(argvars, rettv, VAR_STRING);
15470}
15471
15472/*
15473 * "libcallnr()" function
15474 */
15475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015476f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015477{
15478 libcall_common(argvars, rettv, VAR_NUMBER);
15479}
15480
15481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 * "line(string)" function
15483 */
15484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015485f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486{
15487 linenr_T lnum = 0;
15488 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015489 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015491 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 if (fp != NULL)
15493 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495}
15496
15497/*
15498 * "line2byte(lnum)" function
15499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015501f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502{
15503#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505#else
15506 linenr_T lnum;
15507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015512 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15513 if (rettv->vval.v_number >= 0)
15514 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515#endif
15516}
15517
15518/*
15519 * "lispindent(lnum)" function
15520 */
15521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015522f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523{
15524#ifdef FEAT_LISP
15525 pos_T pos;
15526 linenr_T lnum;
15527
15528 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15531 {
15532 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 curwin->w_cursor = pos;
15535 }
15536 else
15537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
15541/*
15542 * "localtime()" function
15543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015545f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548}
15549
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015550static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551
15552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015553get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554{
15555 char_u *keys;
15556 char_u *which;
15557 char_u buf[NUMBUFLEN];
15558 char_u *keys_buf = NULL;
15559 char_u *rhs;
15560 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015561 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015562 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015563 mapblock_T *mp;
15564 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565
15566 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->v_type = VAR_STRING;
15568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 if (*keys == NUL)
15572 return;
15573
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015574 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015576 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015578 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015579 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015580 if (argvars[3].v_type != VAR_UNKNOWN)
15581 get_dict = get_tv_number(&argvars[3]);
15582 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 else
15585 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 if (which == NULL)
15587 return;
15588
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589 mode = get_map_mode(&which, 0);
15590
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015591 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015592 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015594
15595 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015597 /* Return a string. */
15598 if (rhs != NULL)
15599 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600
Bram Moolenaarbd743252010-10-20 21:23:33 +020015601 }
15602 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15603 {
15604 /* Return a dictionary. */
15605 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15606 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15607 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608
Bram Moolenaarbd743252010-10-20 21:23:33 +020015609 dict_add_nr_str(dict, "lhs", 0L, lhs);
15610 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15611 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15612 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15613 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15614 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15615 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015616 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015617 dict_add_nr_str(dict, "mode", 0L, mapmode);
15618
15619 vim_free(lhs);
15620 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 }
15622}
15623
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015624#ifdef FEAT_FLOAT
15625/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015626 * "log()" function
15627 */
15628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015629f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015630{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015631 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015632
15633 rettv->v_type = VAR_FLOAT;
15634 if (get_float_arg(argvars, &f) == OK)
15635 rettv->vval.v_float = log(f);
15636 else
15637 rettv->vval.v_float = 0.0;
15638}
15639
15640/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015641 * "log10()" function
15642 */
15643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015644f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015645{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015646 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015647
15648 rettv->v_type = VAR_FLOAT;
15649 if (get_float_arg(argvars, &f) == OK)
15650 rettv->vval.v_float = log10(f);
15651 else
15652 rettv->vval.v_float = 0.0;
15653}
15654#endif
15655
Bram Moolenaar1dced572012-04-05 16:54:08 +020015656#ifdef FEAT_LUA
15657/*
15658 * "luaeval()" function
15659 */
15660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015661f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015662{
15663 char_u *str;
15664 char_u buf[NUMBUFLEN];
15665
15666 str = get_tv_string_buf(&argvars[0], buf);
15667 do_luaeval(str, argvars + 1, rettv);
15668}
15669#endif
15670
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015672 * "map()" function
15673 */
15674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015675f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015676{
15677 filter_map(argvars, rettv, TRUE);
15678}
15679
15680/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015681 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 */
15683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015684f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015686 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687}
15688
15689/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 */
15692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015693f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696}
15697
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015698static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699
15700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015701find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015703 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015704 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015705 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 char_u *pat;
15707 regmatch_T regmatch;
15708 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015709 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 char_u *save_cpo;
15711 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015712 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015713 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015714 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015715 list_T *l = NULL;
15716 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015717 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015718 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719
15720 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15721 save_cpo = p_cpo;
15722 p_cpo = (char_u *)"";
15723
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015724 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015725 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015727 /* type 3: return empty list when there are no matches.
15728 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015729 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015730 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015731 if (type == 4
15732 && (list_append_string(rettv->vval.v_list,
15733 (char_u *)"", 0) == FAIL
15734 || list_append_number(rettv->vval.v_list,
15735 (varnumber_T)-1) == FAIL
15736 || list_append_number(rettv->vval.v_list,
15737 (varnumber_T)-1) == FAIL
15738 || list_append_number(rettv->vval.v_list,
15739 (varnumber_T)-1) == FAIL))
15740 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015741 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015742 rettv->vval.v_list = NULL;
15743 goto theend;
15744 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015745 }
15746 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015748 rettv->v_type = VAR_STRING;
15749 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015752 if (argvars[0].v_type == VAR_LIST)
15753 {
15754 if ((l = argvars[0].vval.v_list) == NULL)
15755 goto theend;
15756 li = l->lv_first;
15757 }
15758 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015759 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015760 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015761 len = (long)STRLEN(str);
15762 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15765 if (pat == NULL)
15766 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015767
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015770 int error = FALSE;
15771
15772 start = get_tv_number_chk(&argvars[2], &error);
15773 if (error)
15774 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015775 if (l != NULL)
15776 {
15777 li = list_find(l, start);
15778 if (li == NULL)
15779 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015780 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015781 }
15782 else
15783 {
15784 if (start < 0)
15785 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015786 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015787 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015788 /* When "count" argument is there ignore matches before "start",
15789 * otherwise skip part of the string. Differs when pattern is "^"
15790 * or "\<". */
15791 if (argvars[3].v_type != VAR_UNKNOWN)
15792 startcol = start;
15793 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015794 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015795 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015796 len -= start;
15797 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015798 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015799
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015800 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015801 nth = get_tv_number_chk(&argvars[3], &error);
15802 if (error)
15803 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804 }
15805
15806 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15807 if (regmatch.regprog != NULL)
15808 {
15809 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015810
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015811 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015812 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015813 if (l != NULL)
15814 {
15815 if (li == NULL)
15816 {
15817 match = FALSE;
15818 break;
15819 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015820 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015821 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015822 if (str == NULL)
15823 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015824 }
15825
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015826 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015828 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015829 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015830 if (l == NULL && !match)
15831 break;
15832
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015833 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015834 if (l != NULL)
15835 {
15836 li = li->li_next;
15837 ++idx;
15838 }
15839 else
15840 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015841#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015842 startcol = (colnr_T)(regmatch.startp[0]
15843 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015844#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015845 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015846#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015847 if (startcol > (colnr_T)len
15848 || str + startcol <= regmatch.startp[0])
15849 {
15850 match = FALSE;
15851 break;
15852 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015853 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015854 }
15855
15856 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015858 if (type == 4)
15859 {
15860 listitem_T *li1 = rettv->vval.v_list->lv_first;
15861 listitem_T *li2 = li1->li_next;
15862 listitem_T *li3 = li2->li_next;
15863 listitem_T *li4 = li3->li_next;
15864
Bram Moolenaar3c809342016-06-01 22:34:48 +020015865 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015866 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15867 (int)(regmatch.endp[0] - regmatch.startp[0]));
15868 li3->li_tv.vval.v_number =
15869 (varnumber_T)(regmatch.startp[0] - expr);
15870 li4->li_tv.vval.v_number =
15871 (varnumber_T)(regmatch.endp[0] - expr);
15872 if (l != NULL)
15873 li2->li_tv.vval.v_number = (varnumber_T)idx;
15874 }
15875 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015876 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015877 int i;
15878
15879 /* return list with matched string and submatches */
15880 for (i = 0; i < NSUBEXP; ++i)
15881 {
15882 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015883 {
15884 if (list_append_string(rettv->vval.v_list,
15885 (char_u *)"", 0) == FAIL)
15886 break;
15887 }
15888 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015889 regmatch.startp[i],
15890 (int)(regmatch.endp[i] - regmatch.startp[i]))
15891 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015892 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015893 }
15894 }
15895 else if (type == 2)
15896 {
15897 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015898 if (l != NULL)
15899 copy_tv(&li->li_tv, rettv);
15900 else
15901 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015903 }
15904 else if (l != NULL)
15905 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 else
15907 {
15908 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015909 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015910 (varnumber_T)(regmatch.startp[0] - str);
15911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015912 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015914 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015915 }
15916 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015917 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 }
15919
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015920 if (type == 4 && l == NULL)
15921 /* matchstrpos() without a list: drop the second item. */
15922 listitem_remove(rettv->vval.v_list,
15923 rettv->vval.v_list->lv_first->li_next);
15924
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015926 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 p_cpo = save_cpo;
15928}
15929
15930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015931 * "match()" function
15932 */
15933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015934f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015935{
15936 find_some_match(argvars, rettv, 1);
15937}
15938
15939/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015940 * "matchadd()" function
15941 */
15942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015943f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015944{
15945#ifdef FEAT_SEARCH_EXTRA
15946 char_u buf[NUMBUFLEN];
15947 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15948 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15949 int prio = 10; /* default priority */
15950 int id = -1;
15951 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015952 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015953
15954 rettv->vval.v_number = -1;
15955
15956 if (grp == NULL || pat == NULL)
15957 return;
15958 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015959 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015960 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015961 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015962 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015963 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015964 if (argvars[4].v_type != VAR_UNKNOWN)
15965 {
15966 if (argvars[4].v_type != VAR_DICT)
15967 {
15968 EMSG(_(e_dictreq));
15969 return;
15970 }
15971 if (dict_find(argvars[4].vval.v_dict,
15972 (char_u *)"conceal", -1) != NULL)
15973 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15974 (char_u *)"conceal", FALSE);
15975 }
15976 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015977 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015978 if (error == TRUE)
15979 return;
15980 if (id >= 1 && id <= 3)
15981 {
15982 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15983 return;
15984 }
15985
Bram Moolenaar6561d522015-07-21 15:48:27 +020015986 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15987 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015988#endif
15989}
15990
15991/*
15992 * "matchaddpos()" function
15993 */
15994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015995f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015996{
15997#ifdef FEAT_SEARCH_EXTRA
15998 char_u buf[NUMBUFLEN];
15999 char_u *group;
16000 int prio = 10;
16001 int id = -1;
16002 int error = FALSE;
16003 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016004 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016005
16006 rettv->vval.v_number = -1;
16007
16008 group = get_tv_string_buf_chk(&argvars[0], buf);
16009 if (group == NULL)
16010 return;
16011
16012 if (argvars[1].v_type != VAR_LIST)
16013 {
16014 EMSG2(_(e_listarg), "matchaddpos()");
16015 return;
16016 }
16017 l = argvars[1].vval.v_list;
16018 if (l == NULL)
16019 return;
16020
16021 if (argvars[2].v_type != VAR_UNKNOWN)
16022 {
16023 prio = get_tv_number_chk(&argvars[2], &error);
16024 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016025 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016026 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016027 if (argvars[4].v_type != VAR_UNKNOWN)
16028 {
16029 if (argvars[4].v_type != VAR_DICT)
16030 {
16031 EMSG(_(e_dictreq));
16032 return;
16033 }
16034 if (dict_find(argvars[4].vval.v_dict,
16035 (char_u *)"conceal", -1) != NULL)
16036 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16037 (char_u *)"conceal", FALSE);
16038 }
16039 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016040 }
16041 if (error == TRUE)
16042 return;
16043
16044 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16045 if (id == 1 || id == 2)
16046 {
16047 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16048 return;
16049 }
16050
Bram Moolenaar6561d522015-07-21 15:48:27 +020016051 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16052 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016053#endif
16054}
16055
16056/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016057 * "matcharg()" function
16058 */
16059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016060f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016061{
16062 if (rettv_list_alloc(rettv) == OK)
16063 {
16064#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016065 int id = get_tv_number(&argvars[0]);
16066 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016067
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016068 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016069 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016070 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16071 {
16072 list_append_string(rettv->vval.v_list,
16073 syn_id2name(m->hlg_id), -1);
16074 list_append_string(rettv->vval.v_list, m->pattern, -1);
16075 }
16076 else
16077 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016078 list_append_string(rettv->vval.v_list, NULL, -1);
16079 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016080 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016081 }
16082#endif
16083 }
16084}
16085
16086/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016087 * "matchdelete()" function
16088 */
16089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016090f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016091{
16092#ifdef FEAT_SEARCH_EXTRA
16093 rettv->vval.v_number = match_delete(curwin,
16094 (int)get_tv_number(&argvars[0]), TRUE);
16095#endif
16096}
16097
16098/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099 * "matchend()" function
16100 */
16101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016102f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016103{
16104 find_some_match(argvars, rettv, 0);
16105}
16106
16107/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016108 * "matchlist()" function
16109 */
16110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016111f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016112{
16113 find_some_match(argvars, rettv, 3);
16114}
16115
16116/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016117 * "matchstr()" function
16118 */
16119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016120f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016121{
16122 find_some_match(argvars, rettv, 2);
16123}
16124
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016125/*
16126 * "matchstrpos()" function
16127 */
16128 static void
16129f_matchstrpos(typval_T *argvars, typval_T *rettv)
16130{
16131 find_some_match(argvars, rettv, 4);
16132}
16133
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016134static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016135
16136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016137max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016138{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016139 long n = 0;
16140 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016142
16143 if (argvars[0].v_type == VAR_LIST)
16144 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016145 list_T *l;
16146 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016147
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016148 l = argvars[0].vval.v_list;
16149 if (l != NULL)
16150 {
16151 li = l->lv_first;
16152 if (li != NULL)
16153 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016154 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016155 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016156 {
16157 li = li->li_next;
16158 if (li == NULL)
16159 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016160 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016161 if (domax ? i > n : i < n)
16162 n = i;
16163 }
16164 }
16165 }
16166 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016167 else if (argvars[0].v_type == VAR_DICT)
16168 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016169 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016170 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016171 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016172 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016173
16174 d = argvars[0].vval.v_dict;
16175 if (d != NULL)
16176 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016177 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016180 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016182 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016183 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016184 if (first)
16185 {
16186 n = i;
16187 first = FALSE;
16188 }
16189 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016190 n = i;
16191 }
16192 }
16193 }
16194 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016195 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016196 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016197 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016198}
16199
16200/*
16201 * "max()" function
16202 */
16203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016204f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016205{
16206 max_min(argvars, rettv, TRUE);
16207}
16208
16209/*
16210 * "min()" function
16211 */
16212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016213f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016214{
16215 max_min(argvars, rettv, FALSE);
16216}
16217
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016218static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016219
16220/*
16221 * Create the directory in which "dir" is located, and higher levels when
16222 * needed.
16223 */
16224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016226{
16227 char_u *p;
16228 char_u *updir;
16229 int r = FAIL;
16230
16231 /* Get end of directory name in "dir".
16232 * We're done when it's "/" or "c:/". */
16233 p = gettail_sep(dir);
16234 if (p <= get_past_head(dir))
16235 return OK;
16236
16237 /* If the directory exists we're done. Otherwise: create it.*/
16238 updir = vim_strnsave(dir, (int)(p - dir));
16239 if (updir == NULL)
16240 return FAIL;
16241 if (mch_isdir(updir))
16242 r = OK;
16243 else if (mkdir_recurse(updir, prot) == OK)
16244 r = vim_mkdir_emsg(updir, prot);
16245 vim_free(updir);
16246 return r;
16247}
16248
16249#ifdef vim_mkdir
16250/*
16251 * "mkdir()" function
16252 */
16253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016254f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016255{
16256 char_u *dir;
16257 char_u buf[NUMBUFLEN];
16258 int prot = 0755;
16259
16260 rettv->vval.v_number = FAIL;
16261 if (check_restricted() || check_secure())
16262 return;
16263
16264 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016265 if (*dir == NUL)
16266 rettv->vval.v_number = FAIL;
16267 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016268 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016269 if (*gettail(dir) == NUL)
16270 /* remove trailing slashes */
16271 *gettail_sep(dir) = NUL;
16272
16273 if (argvars[1].v_type != VAR_UNKNOWN)
16274 {
16275 if (argvars[2].v_type != VAR_UNKNOWN)
16276 prot = get_tv_number_chk(&argvars[2], NULL);
16277 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16278 mkdir_recurse(dir, prot);
16279 }
16280 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016281 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016282}
16283#endif
16284
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286 * "mode()" function
16287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016289f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016290{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016291 char_u buf[3];
16292
16293 buf[1] = NUL;
16294 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 if (VIsual_active)
16297 {
16298 if (VIsual_select)
16299 buf[0] = VIsual_mode + 's' - 'v';
16300 else
16301 buf[0] = VIsual_mode;
16302 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016303 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016304 || State == CONFIRM)
16305 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016307 if (State == ASKMORE)
16308 buf[1] = 'm';
16309 else if (State == CONFIRM)
16310 buf[1] = '?';
16311 }
16312 else if (State == EXTERNCMD)
16313 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 else if (State & INSERT)
16315 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016316#ifdef FEAT_VREPLACE
16317 if (State & VREPLACE_FLAG)
16318 {
16319 buf[0] = 'R';
16320 buf[1] = 'v';
16321 }
16322 else
16323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 if (State & REPLACE_FLAG)
16325 buf[0] = 'R';
16326 else
16327 buf[0] = 'i';
16328 }
16329 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016332 if (exmode_active)
16333 buf[1] = 'v';
16334 }
16335 else if (exmode_active)
16336 {
16337 buf[0] = 'c';
16338 buf[1] = 'e';
16339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016343 if (finish_op)
16344 buf[1] = 'o';
16345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016347 /* Clear out the minor mode when the argument is not a non-zero number or
16348 * non-empty string. */
16349 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016350 buf[1] = NUL;
16351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 rettv->vval.v_string = vim_strsave(buf);
16353 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354}
16355
Bram Moolenaar429fa852013-04-15 12:27:36 +020016356#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016357/*
16358 * "mzeval()" function
16359 */
16360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016361f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016362{
16363 char_u *str;
16364 char_u buf[NUMBUFLEN];
16365
16366 str = get_tv_string_buf(&argvars[0], buf);
16367 do_mzeval(str, rettv);
16368}
Bram Moolenaar75676462013-01-30 14:55:42 +010016369
16370 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016371mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016372{
16373 typval_T argvars[3];
16374
16375 argvars[0].v_type = VAR_STRING;
16376 argvars[0].vval.v_string = name;
16377 copy_tv(args, &argvars[1]);
16378 argvars[2].v_type = VAR_UNKNOWN;
16379 f_call(argvars, rettv);
16380 clear_tv(&argvars[1]);
16381}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016382#endif
16383
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016385 * "nextnonblank()" function
16386 */
16387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016388f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389{
16390 linenr_T lnum;
16391
16392 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 {
16396 lnum = 0;
16397 break;
16398 }
16399 if (*skipwhite(ml_get(lnum)) != NUL)
16400 break;
16401 }
16402 rettv->vval.v_number = lnum;
16403}
16404
16405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 * "nr2char()" function
16407 */
16408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016409f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410{
16411 char_u buf[NUMBUFLEN];
16412
16413#ifdef FEAT_MBYTE
16414 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016415 {
16416 int utf8 = 0;
16417
16418 if (argvars[1].v_type != VAR_UNKNOWN)
16419 utf8 = get_tv_number_chk(&argvars[1], NULL);
16420 if (utf8)
16421 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16422 else
16423 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016425 else
16426#endif
16427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016428 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 buf[1] = NUL;
16430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431 rettv->v_type = VAR_STRING;
16432 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016433}
16434
16435/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016436 * "or(expr, expr)" function
16437 */
16438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016439f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016440{
16441 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16442 | get_tv_number_chk(&argvars[1], NULL);
16443}
16444
16445/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016446 * "pathshorten()" function
16447 */
16448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016449f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016450{
16451 char_u *p;
16452
16453 rettv->v_type = VAR_STRING;
16454 p = get_tv_string_chk(&argvars[0]);
16455 if (p == NULL)
16456 rettv->vval.v_string = NULL;
16457 else
16458 {
16459 p = vim_strsave(p);
16460 rettv->vval.v_string = p;
16461 if (p != NULL)
16462 shorten_dir(p);
16463 }
16464}
16465
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016466#ifdef FEAT_PERL
16467/*
16468 * "perleval()" function
16469 */
16470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016471f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016472{
16473 char_u *str;
16474 char_u buf[NUMBUFLEN];
16475
16476 str = get_tv_string_buf(&argvars[0], buf);
16477 do_perleval(str, rettv);
16478}
16479#endif
16480
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016481#ifdef FEAT_FLOAT
16482/*
16483 * "pow()" function
16484 */
16485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016486f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016487{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016488 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016489
16490 rettv->v_type = VAR_FLOAT;
16491 if (get_float_arg(argvars, &fx) == OK
16492 && get_float_arg(&argvars[1], &fy) == OK)
16493 rettv->vval.v_float = pow(fx, fy);
16494 else
16495 rettv->vval.v_float = 0.0;
16496}
16497#endif
16498
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500 * "prevnonblank()" function
16501 */
16502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016503f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504{
16505 linenr_T lnum;
16506
16507 lnum = get_tv_lnum(argvars);
16508 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16509 lnum = 0;
16510 else
16511 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16512 --lnum;
16513 rettv->vval.v_number = lnum;
16514}
16515
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016516/* This dummy va_list is here because:
16517 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16518 * - locally in the function results in a "used before set" warning
16519 * - using va_start() to initialize it gives "function with fixed args" error */
16520static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016521
Bram Moolenaar8c711452005-01-14 21:53:12 +000016522/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016523 * "printf()" function
16524 */
16525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016526f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016527{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016528 char_u buf[NUMBUFLEN];
16529 int len;
16530 char_u *s;
16531 int saved_did_emsg = did_emsg;
16532 char *fmt;
16533
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016534 rettv->v_type = VAR_STRING;
16535 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016536
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016537 /* Get the required length, allocate the buffer and do it for real. */
16538 did_emsg = FALSE;
16539 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16540 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16541 if (!did_emsg)
16542 {
16543 s = alloc(len + 1);
16544 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016545 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016546 rettv->vval.v_string = s;
16547 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016548 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016549 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016550 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016551}
16552
16553/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016554 * "pumvisible()" function
16555 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016557f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016558{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016559#ifdef FEAT_INS_EXPAND
16560 if (pum_visible())
16561 rettv->vval.v_number = 1;
16562#endif
16563}
16564
Bram Moolenaardb913952012-06-29 12:54:53 +020016565#ifdef FEAT_PYTHON3
16566/*
16567 * "py3eval()" function
16568 */
16569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016570f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016571{
16572 char_u *str;
16573 char_u buf[NUMBUFLEN];
16574
16575 str = get_tv_string_buf(&argvars[0], buf);
16576 do_py3eval(str, rettv);
16577}
16578#endif
16579
16580#ifdef FEAT_PYTHON
16581/*
16582 * "pyeval()" function
16583 */
16584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016585f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016586{
16587 char_u *str;
16588 char_u buf[NUMBUFLEN];
16589
16590 str = get_tv_string_buf(&argvars[0], buf);
16591 do_pyeval(str, rettv);
16592}
16593#endif
16594
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016596 * "range()" function
16597 */
16598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016599f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016600{
16601 long start;
16602 long end;
16603 long stride = 1;
16604 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016605 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016607 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016608 if (argvars[1].v_type == VAR_UNKNOWN)
16609 {
16610 end = start - 1;
16611 start = 0;
16612 }
16613 else
16614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016615 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016616 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016618 }
16619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 if (error)
16621 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016622 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016623 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016624 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016625 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016626 else
16627 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016628 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016629 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016630 if (list_append_number(rettv->vval.v_list,
16631 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016632 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016633 }
16634}
16635
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016636/*
16637 * "readfile()" function
16638 */
16639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016640f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641{
16642 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644 char_u *fname;
16645 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16647 int io_size = sizeof(buf);
16648 int readlen; /* size of last fread() */
16649 char_u *prev = NULL; /* previously read bytes, if any */
16650 long prevlen = 0; /* length of data in prev */
16651 long prevsize = 0; /* size of prev buffer */
16652 long maxline = MAXLNUM;
16653 long cnt = 0;
16654 char_u *p; /* position in buf */
16655 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016656
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016657 if (argvars[1].v_type != VAR_UNKNOWN)
16658 {
16659 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16660 binary = TRUE;
16661 if (argvars[2].v_type != VAR_UNKNOWN)
16662 maxline = get_tv_number(&argvars[2]);
16663 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016664
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016665 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016666 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667
16668 /* Always open the file in binary mode, library functions have a mind of
16669 * their own about CR-LF conversion. */
16670 fname = get_tv_string(&argvars[0]);
16671 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16672 {
16673 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16674 return;
16675 }
16676
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016677 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016678 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016679 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016680
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016681 /* This for loop processes what was read, but is also entered at end
16682 * of file so that either:
16683 * - an incomplete line gets written
16684 * - a "binary" file gets an empty line at the end if it ends in a
16685 * newline. */
16686 for (p = buf, start = buf;
16687 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16688 ++p)
16689 {
16690 if (*p == '\n' || readlen <= 0)
16691 {
16692 listitem_T *li;
16693 char_u *s = NULL;
16694 long_u len = p - start;
16695
16696 /* Finished a line. Remove CRs before NL. */
16697 if (readlen > 0 && !binary)
16698 {
16699 while (len > 0 && start[len - 1] == '\r')
16700 --len;
16701 /* removal may cross back to the "prev" string */
16702 if (len == 0)
16703 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16704 --prevlen;
16705 }
16706 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016707 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016708 else
16709 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016710 /* Change "prev" buffer to be the right size. This way
16711 * the bytes are only copied once, and very long lines are
16712 * allocated only once. */
16713 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016714 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016715 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016716 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016717 prev = NULL; /* the list will own the string */
16718 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016719 }
16720 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016721 if (s == NULL)
16722 {
16723 do_outofmem_msg((long_u) prevlen + len + 1);
16724 failed = TRUE;
16725 break;
16726 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016727
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016728 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016729 {
16730 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016731 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016732 break;
16733 }
16734 li->li_tv.v_type = VAR_STRING;
16735 li->li_tv.v_lock = 0;
16736 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016737 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016738
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016739 start = p + 1; /* step over newline */
16740 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016741 break;
16742 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016743 else if (*p == NUL)
16744 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016745#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016746 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16747 * when finding the BF and check the previous two bytes. */
16748 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016749 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016750 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16751 * + 1, these may be in the "prev" string. */
16752 char_u back1 = p >= buf + 1 ? p[-1]
16753 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16754 char_u back2 = p >= buf + 2 ? p[-2]
16755 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16756 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016758 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016760 char_u *dest = p - 2;
16761
16762 /* Usually a BOM is at the beginning of a file, and so at
16763 * the beginning of a line; then we can just step over it.
16764 */
16765 if (start == dest)
16766 start = p + 1;
16767 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016768 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016769 /* have to shuffle buf to close gap */
16770 int adjust_prevlen = 0;
16771
16772 if (dest < buf)
16773 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016774 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016775 dest = buf;
16776 }
16777 if (readlen > p - buf + 1)
16778 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16779 readlen -= 3 - adjust_prevlen;
16780 prevlen -= adjust_prevlen;
16781 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016782 }
16783 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016784 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016785#endif
16786 } /* for */
16787
16788 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16789 break;
16790 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016791 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016792 /* There's part of a line in buf, store it in "prev". */
16793 if (p - start + prevlen >= prevsize)
16794 {
16795 /* need bigger "prev" buffer */
16796 char_u *newprev;
16797
16798 /* A common use case is ordinary text files and "prev" gets a
16799 * fragment of a line, so the first allocation is made
16800 * small, to avoid repeatedly 'allocing' large and
16801 * 'reallocing' small. */
16802 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016803 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016804 else
16805 {
16806 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016807 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016808 prevsize = grow50pc > growmin ? grow50pc : growmin;
16809 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016810 newprev = prev == NULL ? alloc(prevsize)
16811 : vim_realloc(prev, prevsize);
16812 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016813 {
16814 do_outofmem_msg((long_u)prevsize);
16815 failed = TRUE;
16816 break;
16817 }
16818 prev = newprev;
16819 }
16820 /* Add the line part to end of "prev". */
16821 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016822 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016823 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016824 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016825
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016826 /*
16827 * For a negative line count use only the lines at the end of the file,
16828 * free the rest.
16829 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016830 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016831 while (cnt > -maxline)
16832 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016833 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016834 --cnt;
16835 }
16836
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016837 if (failed)
16838 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016839 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016840 /* readfile doc says an empty list is returned on error */
16841 rettv->vval.v_list = list_alloc();
16842 }
16843
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016844 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016845 fclose(fd);
16846}
16847
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016848#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016849static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016850
16851/*
16852 * Convert a List to proftime_T.
16853 * Return FAIL when there is something wrong.
16854 */
16855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016856list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016857{
16858 long n1, n2;
16859 int error = FALSE;
16860
16861 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16862 || arg->vval.v_list->lv_len != 2)
16863 return FAIL;
16864 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16865 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16866# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016867 tm->HighPart = n1;
16868 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016869# else
16870 tm->tv_sec = n1;
16871 tm->tv_usec = n2;
16872# endif
16873 return error ? FAIL : OK;
16874}
16875#endif /* FEAT_RELTIME */
16876
16877/*
16878 * "reltime()" function
16879 */
16880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016881f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016882{
16883#ifdef FEAT_RELTIME
16884 proftime_T res;
16885 proftime_T start;
16886
16887 if (argvars[0].v_type == VAR_UNKNOWN)
16888 {
16889 /* No arguments: get current time. */
16890 profile_start(&res);
16891 }
16892 else if (argvars[1].v_type == VAR_UNKNOWN)
16893 {
16894 if (list2proftime(&argvars[0], &res) == FAIL)
16895 return;
16896 profile_end(&res);
16897 }
16898 else
16899 {
16900 /* Two arguments: compute the difference. */
16901 if (list2proftime(&argvars[0], &start) == FAIL
16902 || list2proftime(&argvars[1], &res) == FAIL)
16903 return;
16904 profile_sub(&res, &start);
16905 }
16906
16907 if (rettv_list_alloc(rettv) == OK)
16908 {
16909 long n1, n2;
16910
16911# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016912 n1 = res.HighPart;
16913 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016914# else
16915 n1 = res.tv_sec;
16916 n2 = res.tv_usec;
16917# endif
16918 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16919 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16920 }
16921#endif
16922}
16923
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016924#ifdef FEAT_FLOAT
16925/*
16926 * "reltimefloat()" function
16927 */
16928 static void
16929f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16930{
16931# ifdef FEAT_RELTIME
16932 proftime_T tm;
16933# endif
16934
16935 rettv->v_type = VAR_FLOAT;
16936 rettv->vval.v_float = 0;
16937# ifdef FEAT_RELTIME
16938 if (list2proftime(&argvars[0], &tm) == OK)
16939 rettv->vval.v_float = profile_float(&tm);
16940# endif
16941}
16942#endif
16943
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016944/*
16945 * "reltimestr()" function
16946 */
16947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016948f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016949{
16950#ifdef FEAT_RELTIME
16951 proftime_T tm;
16952#endif
16953
16954 rettv->v_type = VAR_STRING;
16955 rettv->vval.v_string = NULL;
16956#ifdef FEAT_RELTIME
16957 if (list2proftime(&argvars[0], &tm) == OK)
16958 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16959#endif
16960}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016961
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016963static void make_connection(void);
16964static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965
16966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016967make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968{
16969 if (X_DISPLAY == NULL
16970# ifdef FEAT_GUI
16971 && !gui.in_use
16972# endif
16973 )
16974 {
16975 x_force_connect = TRUE;
16976 setup_term_clip();
16977 x_force_connect = FALSE;
16978 }
16979}
16980
16981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016982check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983{
16984 make_connection();
16985 if (X_DISPLAY == NULL)
16986 {
16987 EMSG(_("E240: No connection to Vim server"));
16988 return FAIL;
16989 }
16990 return OK;
16991}
16992#endif
16993
16994#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016996remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997{
16998 char_u *server_name;
16999 char_u *keys;
17000 char_u *r = NULL;
17001 char_u buf[NUMBUFLEN];
17002# ifdef WIN32
17003 HWND w;
17004# else
17005 Window w;
17006# endif
17007
17008 if (check_restricted() || check_secure())
17009 return;
17010
17011# ifdef FEAT_X11
17012 if (check_connection() == FAIL)
17013 return;
17014# endif
17015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017016 server_name = get_tv_string_chk(&argvars[0]);
17017 if (server_name == NULL)
17018 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 keys = get_tv_string_buf(&argvars[1], buf);
17020# ifdef WIN32
17021 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17022# else
17023 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17024 < 0)
17025# endif
17026 {
17027 if (r != NULL)
17028 EMSG(r); /* sending worked but evaluation failed */
17029 else
17030 EMSG2(_("E241: Unable to send to %s"), server_name);
17031 return;
17032 }
17033
17034 rettv->vval.v_string = r;
17035
17036 if (argvars[2].v_type != VAR_UNKNOWN)
17037 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017038 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017039 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017040 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017042 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 v.di_tv.v_type = VAR_STRING;
17044 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 idvar = get_tv_string_chk(&argvars[2]);
17046 if (idvar != NULL)
17047 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049 }
17050}
17051#endif
17052
17053/*
17054 * "remote_expr()" function
17055 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017057f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058{
17059 rettv->v_type = VAR_STRING;
17060 rettv->vval.v_string = NULL;
17061#ifdef FEAT_CLIENTSERVER
17062 remote_common(argvars, rettv, TRUE);
17063#endif
17064}
17065
17066/*
17067 * "remote_foreground()" function
17068 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017070f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072#ifdef FEAT_CLIENTSERVER
17073# ifdef WIN32
17074 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 {
17076 char_u *server_name = get_tv_string_chk(&argvars[0]);
17077
17078 if (server_name != NULL)
17079 serverForeground(server_name);
17080 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081# else
17082 /* Send a foreground() expression to the server. */
17083 argvars[1].v_type = VAR_STRING;
17084 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17085 argvars[2].v_type = VAR_UNKNOWN;
17086 remote_common(argvars, rettv, TRUE);
17087 vim_free(argvars[1].vval.v_string);
17088# endif
17089#endif
17090}
17091
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017093f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094{
17095#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017096 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097 char_u *s = NULL;
17098# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017099 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017101 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102
17103 if (check_restricted() || check_secure())
17104 {
17105 rettv->vval.v_number = -1;
17106 return;
17107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017108 serverid = get_tv_string_chk(&argvars[0]);
17109 if (serverid == NULL)
17110 {
17111 rettv->vval.v_number = -1;
17112 return; /* type error; errmsg already given */
17113 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017115 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 if (n == 0)
17117 rettv->vval.v_number = -1;
17118 else
17119 {
17120 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17121 rettv->vval.v_number = (s != NULL);
17122 }
17123# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124 if (check_connection() == FAIL)
17125 return;
17126
17127 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017128 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129# endif
17130
17131 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 char_u *retvar;
17134
Bram Moolenaar33570922005-01-25 22:26:29 +000017135 v.di_tv.v_type = VAR_STRING;
17136 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 retvar = get_tv_string_chk(&argvars[1]);
17138 if (retvar != NULL)
17139 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017140 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 }
17142#else
17143 rettv->vval.v_number = -1;
17144#endif
17145}
17146
Bram Moolenaar0d660222005-01-07 21:51:51 +000017147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017148f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149{
17150 char_u *r = NULL;
17151
17152#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017153 char_u *serverid = get_tv_string_chk(&argvars[0]);
17154
17155 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 {
17157# ifdef WIN32
17158 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017159 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017161 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162 if (n != 0)
17163 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17164 if (r == NULL)
17165# else
17166 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017167 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017168# endif
17169 EMSG(_("E277: Unable to read a server reply"));
17170 }
17171#endif
17172 rettv->v_type = VAR_STRING;
17173 rettv->vval.v_string = r;
17174}
17175
17176/*
17177 * "remote_send()" function
17178 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017180f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017181{
17182 rettv->v_type = VAR_STRING;
17183 rettv->vval.v_string = NULL;
17184#ifdef FEAT_CLIENTSERVER
17185 remote_common(argvars, rettv, FALSE);
17186#endif
17187}
17188
17189/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017190 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017191 */
17192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017193f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194{
Bram Moolenaar33570922005-01-25 22:26:29 +000017195 list_T *l;
17196 listitem_T *item, *item2;
17197 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017198 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017199 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017200 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017201 dict_T *d;
17202 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017203 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017204
Bram Moolenaar8c711452005-01-14 21:53:12 +000017205 if (argvars[0].v_type == VAR_DICT)
17206 {
17207 if (argvars[2].v_type != VAR_UNKNOWN)
17208 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017209 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017210 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017211 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 key = get_tv_string_chk(&argvars[1]);
17213 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 di = dict_find(d, key, -1);
17216 if (di == NULL)
17217 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017218 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17219 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017220 {
17221 *rettv = di->di_tv;
17222 init_tv(&di->di_tv);
17223 dictitem_remove(d, di);
17224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017225 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017226 }
17227 }
17228 else if (argvars[0].v_type != VAR_LIST)
17229 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017230 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017231 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017232 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 int error = FALSE;
17234
17235 idx = get_tv_number_chk(&argvars[1], &error);
17236 if (error)
17237 ; /* type error: do nothing, errmsg already given */
17238 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017239 EMSGN(_(e_listidx), idx);
17240 else
17241 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017242 if (argvars[2].v_type == VAR_UNKNOWN)
17243 {
17244 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017245 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017246 *rettv = item->li_tv;
17247 vim_free(item);
17248 }
17249 else
17250 {
17251 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017252 end = get_tv_number_chk(&argvars[2], &error);
17253 if (error)
17254 ; /* type error: do nothing */
17255 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017256 EMSGN(_(e_listidx), end);
17257 else
17258 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017259 int cnt = 0;
17260
17261 for (li = item; li != NULL; li = li->li_next)
17262 {
17263 ++cnt;
17264 if (li == item2)
17265 break;
17266 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017267 if (li == NULL) /* didn't find "item2" after "item" */
17268 EMSG(_(e_invrange));
17269 else
17270 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017271 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017272 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017273 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017274 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017275 l->lv_first = item;
17276 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017277 item->li_prev = NULL;
17278 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017279 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017280 }
17281 }
17282 }
17283 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017284 }
17285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286}
17287
17288/*
17289 * "rename({from}, {to})" function
17290 */
17291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017292f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293{
17294 char_u buf[NUMBUFLEN];
17295
17296 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017299 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17300 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301}
17302
17303/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017304 * "repeat()" function
17305 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017307f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017308{
17309 char_u *p;
17310 int n;
17311 int slen;
17312 int len;
17313 char_u *r;
17314 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017315
17316 n = get_tv_number(&argvars[1]);
17317 if (argvars[0].v_type == VAR_LIST)
17318 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017319 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017320 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017321 if (list_extend(rettv->vval.v_list,
17322 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017323 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017324 }
17325 else
17326 {
17327 p = get_tv_string(&argvars[0]);
17328 rettv->v_type = VAR_STRING;
17329 rettv->vval.v_string = NULL;
17330
17331 slen = (int)STRLEN(p);
17332 len = slen * n;
17333 if (len <= 0)
17334 return;
17335
17336 r = alloc(len + 1);
17337 if (r != NULL)
17338 {
17339 for (i = 0; i < n; i++)
17340 mch_memmove(r + i * slen, p, (size_t)slen);
17341 r[len] = NUL;
17342 }
17343
17344 rettv->vval.v_string = r;
17345 }
17346}
17347
17348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 * "resolve()" function
17350 */
17351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017352f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353{
17354 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017355#ifdef HAVE_READLINK
17356 char_u *buf = NULL;
17357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017359 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360#ifdef FEAT_SHORTCUT
17361 {
17362 char_u *v = NULL;
17363
17364 v = mch_resolve_shortcut(p);
17365 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 }
17370#else
17371# ifdef HAVE_READLINK
17372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 char_u *cpy;
17374 int len;
17375 char_u *remain = NULL;
17376 char_u *q;
17377 int is_relative_to_current = FALSE;
17378 int has_trailing_pathsep = FALSE;
17379 int limit = 100;
17380
17381 p = vim_strsave(p);
17382
17383 if (p[0] == '.' && (vim_ispathsep(p[1])
17384 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17385 is_relative_to_current = TRUE;
17386
17387 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017388 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017391 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393
17394 q = getnextcomp(p);
17395 if (*q != NUL)
17396 {
17397 /* Separate the first path component in "p", and keep the
17398 * remainder (beginning with the path separator). */
17399 remain = vim_strsave(q - 1);
17400 q[-1] = NUL;
17401 }
17402
Bram Moolenaard9462e32011-04-11 21:35:11 +020017403 buf = alloc(MAXPATHL + 1);
17404 if (buf == NULL)
17405 goto fail;
17406
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 for (;;)
17408 {
17409 for (;;)
17410 {
17411 len = readlink((char *)p, (char *)buf, MAXPATHL);
17412 if (len <= 0)
17413 break;
17414 buf[len] = NUL;
17415
17416 if (limit-- == 0)
17417 {
17418 vim_free(p);
17419 vim_free(remain);
17420 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 goto fail;
17423 }
17424
17425 /* Ensure that the result will have a trailing path separator
17426 * if the argument has one. */
17427 if (remain == NULL && has_trailing_pathsep)
17428 add_pathsep(buf);
17429
17430 /* Separate the first path component in the link value and
17431 * concatenate the remainders. */
17432 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17433 if (*q != NUL)
17434 {
17435 if (remain == NULL)
17436 remain = vim_strsave(q - 1);
17437 else
17438 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017439 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 if (cpy != NULL)
17441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 vim_free(remain);
17443 remain = cpy;
17444 }
17445 }
17446 q[-1] = NUL;
17447 }
17448
17449 q = gettail(p);
17450 if (q > p && *q == NUL)
17451 {
17452 /* Ignore trailing path separator. */
17453 q[-1] = NUL;
17454 q = gettail(p);
17455 }
17456 if (q > p && !mch_isFullName(buf))
17457 {
17458 /* symlink is relative to directory of argument */
17459 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17460 if (cpy != NULL)
17461 {
17462 STRCPY(cpy, p);
17463 STRCPY(gettail(cpy), buf);
17464 vim_free(p);
17465 p = cpy;
17466 }
17467 }
17468 else
17469 {
17470 vim_free(p);
17471 p = vim_strsave(buf);
17472 }
17473 }
17474
17475 if (remain == NULL)
17476 break;
17477
17478 /* Append the first path component of "remain" to "p". */
17479 q = getnextcomp(remain + 1);
17480 len = q - remain - (*q != NUL);
17481 cpy = vim_strnsave(p, STRLEN(p) + len);
17482 if (cpy != NULL)
17483 {
17484 STRNCAT(cpy, remain, len);
17485 vim_free(p);
17486 p = cpy;
17487 }
17488 /* Shorten "remain". */
17489 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017490 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491 else
17492 {
17493 vim_free(remain);
17494 remain = NULL;
17495 }
17496 }
17497
17498 /* If the result is a relative path name, make it explicitly relative to
17499 * the current directory if and only if the argument had this form. */
17500 if (!vim_ispathsep(*p))
17501 {
17502 if (is_relative_to_current
17503 && *p != NUL
17504 && !(p[0] == '.'
17505 && (p[1] == NUL
17506 || vim_ispathsep(p[1])
17507 || (p[1] == '.'
17508 && (p[2] == NUL
17509 || vim_ispathsep(p[2]))))))
17510 {
17511 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017512 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513 if (cpy != NULL)
17514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 vim_free(p);
17516 p = cpy;
17517 }
17518 }
17519 else if (!is_relative_to_current)
17520 {
17521 /* Strip leading "./". */
17522 q = p;
17523 while (q[0] == '.' && vim_ispathsep(q[1]))
17524 q += 2;
17525 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017526 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 }
17528 }
17529
17530 /* Ensure that the result will have no trailing path separator
17531 * if the argument had none. But keep "/" or "//". */
17532 if (!has_trailing_pathsep)
17533 {
17534 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017535 if (after_pathsep(p, q))
17536 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 }
17538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017539 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 }
17541# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543# endif
17544#endif
17545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
17548#ifdef HAVE_READLINK
17549fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017550 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553}
17554
17555/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017556 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 */
17558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017559f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560{
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 list_T *l;
17562 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 if (argvars[0].v_type != VAR_LIST)
17565 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017566 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017567 && !tv_check_lock(l->lv_lock,
17568 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017569 {
17570 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017571 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017572 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017573 while (li != NULL)
17574 {
17575 ni = li->li_prev;
17576 list_append(l, li);
17577 li = ni;
17578 }
17579 rettv->vval.v_list = l;
17580 rettv->v_type = VAR_LIST;
17581 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017582 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584}
17585
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017586#define SP_NOMOVE 0x01 /* don't move cursor */
17587#define SP_REPEAT 0x02 /* repeat to find outer pair */
17588#define SP_RETCOUNT 0x04 /* return matchcount */
17589#define SP_SETPCMARK 0x08 /* set previous context mark */
17590#define SP_START 0x10 /* accept match at start position */
17591#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17592#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017593#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017594
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017595static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596
17597/*
17598 * Get flags for a search function.
17599 * Possibly sets "p_ws".
17600 * Returns BACKWARD, FORWARD or zero (for an error).
17601 */
17602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017603get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017604{
17605 int dir = FORWARD;
17606 char_u *flags;
17607 char_u nbuf[NUMBUFLEN];
17608 int mask;
17609
17610 if (varp->v_type != VAR_UNKNOWN)
17611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017612 flags = get_tv_string_buf_chk(varp, nbuf);
17613 if (flags == NULL)
17614 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615 while (*flags != NUL)
17616 {
17617 switch (*flags)
17618 {
17619 case 'b': dir = BACKWARD; break;
17620 case 'w': p_ws = TRUE; break;
17621 case 'W': p_ws = FALSE; break;
17622 default: mask = 0;
17623 if (flagsp != NULL)
17624 switch (*flags)
17625 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017626 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017627 case 'e': mask = SP_END; break;
17628 case 'm': mask = SP_RETCOUNT; break;
17629 case 'n': mask = SP_NOMOVE; break;
17630 case 'p': mask = SP_SUBPAT; break;
17631 case 'r': mask = SP_REPEAT; break;
17632 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017633 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 }
17635 if (mask == 0)
17636 {
17637 EMSG2(_(e_invarg2), flags);
17638 dir = 0;
17639 }
17640 else
17641 *flagsp |= mask;
17642 }
17643 if (dir == 0)
17644 break;
17645 ++flags;
17646 }
17647 }
17648 return dir;
17649}
17650
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017652 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017655search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017657 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 char_u *pat;
17659 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017660 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 int save_p_ws = p_ws;
17662 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017663 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017664 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017665 proftime_T tm;
17666#ifdef FEAT_RELTIME
17667 long time_limit = 0;
17668#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017669 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017670 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017672 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017673 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017674 if (dir == 0)
17675 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017676 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017677 if (flags & SP_START)
17678 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017679 if (flags & SP_END)
17680 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017681 if (flags & SP_COLUMN)
17682 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017683
Bram Moolenaar76929292008-01-06 19:07:36 +000017684 /* Optional arguments: line number to stop searching and timeout. */
17685 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017686 {
17687 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17688 if (lnum_stop < 0)
17689 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017690#ifdef FEAT_RELTIME
17691 if (argvars[3].v_type != VAR_UNKNOWN)
17692 {
17693 time_limit = get_tv_number_chk(&argvars[3], NULL);
17694 if (time_limit < 0)
17695 goto theend;
17696 }
17697#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017698 }
17699
Bram Moolenaar76929292008-01-06 19:07:36 +000017700#ifdef FEAT_RELTIME
17701 /* Set the time limit, if there is one. */
17702 profile_setlimit(time_limit, &tm);
17703#endif
17704
Bram Moolenaar231334e2005-07-25 20:46:57 +000017705 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017706 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017707 * Check to make sure only those flags are set.
17708 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17709 * flags cannot be set. Check for that condition also.
17710 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017711 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017712 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017715 goto theend;
17716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017718 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017719 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017720 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017721 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017723 if (flags & SP_SUBPAT)
17724 retval = subpatnum;
17725 else
17726 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017727 if (flags & SP_SETPCMARK)
17728 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017730 if (match_pos != NULL)
17731 {
17732 /* Store the match cursor position */
17733 match_pos->lnum = pos.lnum;
17734 match_pos->col = pos.col + 1;
17735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017736 /* "/$" will put the cursor after the end of the line, may need to
17737 * correct that here */
17738 check_cursor();
17739 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017740
17741 /* If 'n' flag is used: restore cursor position. */
17742 if (flags & SP_NOMOVE)
17743 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017744 else
17745 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017746theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017748
17749 return retval;
17750}
17751
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017752#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017753
17754/*
17755 * round() is not in C90, use ceil() or floor() instead.
17756 */
17757 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017758vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017759{
17760 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17761}
17762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017763/*
17764 * "round({float})" function
17765 */
17766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017767f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017768{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017769 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017770
17771 rettv->v_type = VAR_FLOAT;
17772 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017773 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017774 else
17775 rettv->vval.v_float = 0.0;
17776}
17777#endif
17778
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017779/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017780 * "screenattr()" function
17781 */
17782 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017783f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017784{
17785 int row;
17786 int col;
17787 int c;
17788
17789 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17790 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17791 if (row < 0 || row >= screen_Rows
17792 || col < 0 || col >= screen_Columns)
17793 c = -1;
17794 else
17795 c = ScreenAttrs[LineOffset[row] + col];
17796 rettv->vval.v_number = c;
17797}
17798
17799/*
17800 * "screenchar()" function
17801 */
17802 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017803f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017804{
17805 int row;
17806 int col;
17807 int off;
17808 int c;
17809
17810 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17811 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17812 if (row < 0 || row >= screen_Rows
17813 || col < 0 || col >= screen_Columns)
17814 c = -1;
17815 else
17816 {
17817 off = LineOffset[row] + col;
17818#ifdef FEAT_MBYTE
17819 if (enc_utf8 && ScreenLinesUC[off] != 0)
17820 c = ScreenLinesUC[off];
17821 else
17822#endif
17823 c = ScreenLines[off];
17824 }
17825 rettv->vval.v_number = c;
17826}
17827
17828/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017829 * "screencol()" function
17830 *
17831 * First column is 1 to be consistent with virtcol().
17832 */
17833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017834f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017835{
17836 rettv->vval.v_number = screen_screencol() + 1;
17837}
17838
17839/*
17840 * "screenrow()" function
17841 */
17842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017843f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017844{
17845 rettv->vval.v_number = screen_screenrow() + 1;
17846}
17847
17848/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017849 * "search()" function
17850 */
17851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017852f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017853{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017854 int flags = 0;
17855
17856 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857}
17858
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017860 * "searchdecl()" function
17861 */
17862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017863f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017864{
17865 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017866 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017867 int error = FALSE;
17868 char_u *name;
17869
17870 rettv->vval.v_number = 1; /* default: FAIL */
17871
17872 name = get_tv_string_chk(&argvars[0]);
17873 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017874 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017875 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017876 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17877 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17878 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017879 if (!error && name != NULL)
17880 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017881 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017882}
17883
17884/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017885 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017888searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889{
17890 char_u *spat, *mpat, *epat;
17891 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 int dir;
17894 int flags = 0;
17895 char_u nbuf1[NUMBUFLEN];
17896 char_u nbuf2[NUMBUFLEN];
17897 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017898 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017899 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017900 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017903 spat = get_tv_string_chk(&argvars[0]);
17904 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17905 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17906 if (spat == NULL || mpat == NULL || epat == NULL)
17907 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 /* Handle the optional fourth argument: flags */
17910 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017911 if (dir == 0)
17912 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017913
17914 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017915 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17916 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017917 if ((flags & (SP_END | SP_SUBPAT)) != 0
17918 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017919 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017920 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017921 goto theend;
17922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017924 /* Using 'r' implies 'W', otherwise it doesn't work. */
17925 if (flags & SP_REPEAT)
17926 p_ws = FALSE;
17927
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017928 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017929 if (argvars[3].v_type == VAR_UNKNOWN
17930 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 skip = (char_u *)"";
17932 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017934 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017935 if (argvars[5].v_type != VAR_UNKNOWN)
17936 {
17937 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17938 if (lnum_stop < 0)
17939 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017940#ifdef FEAT_RELTIME
17941 if (argvars[6].v_type != VAR_UNKNOWN)
17942 {
17943 time_limit = get_tv_number_chk(&argvars[6], NULL);
17944 if (time_limit < 0)
17945 goto theend;
17946 }
17947#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017948 }
17949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017950 if (skip == NULL)
17951 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017953 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017954 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017955
17956theend:
17957 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017958
17959 return retval;
17960}
17961
17962/*
17963 * "searchpair()" function
17964 */
17965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017966f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017967{
17968 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17969}
17970
17971/*
17972 * "searchpairpos()" function
17973 */
17974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017975f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017976{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977 pos_T match_pos;
17978 int lnum = 0;
17979 int col = 0;
17980
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017981 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983
17984 if (searchpair_cmn(argvars, &match_pos) > 0)
17985 {
17986 lnum = match_pos.lnum;
17987 col = match_pos.col;
17988 }
17989
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017990 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17991 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017992}
17993
17994/*
17995 * Search for a start/middle/end thing.
17996 * Used by searchpair(), see its documentation for the details.
17997 * Returns 0 or -1 for no match,
17998 */
17999 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018000do_searchpair(
18001 char_u *spat, /* start pattern */
18002 char_u *mpat, /* middle pattern */
18003 char_u *epat, /* end pattern */
18004 int dir, /* BACKWARD or FORWARD */
18005 char_u *skip, /* skip expression */
18006 int flags, /* SP_SETPCMARK and other SP_ values */
18007 pos_T *match_pos,
18008 linenr_T lnum_stop, /* stop at this line if not zero */
18009 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018010{
18011 char_u *save_cpo;
18012 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18013 long retval = 0;
18014 pos_T pos;
18015 pos_T firstpos;
18016 pos_T foundpos;
18017 pos_T save_cursor;
18018 pos_T save_pos;
18019 int n;
18020 int r;
18021 int nest = 1;
18022 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018023 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018024 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018025
18026 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18027 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018028 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018029
Bram Moolenaar76929292008-01-06 19:07:36 +000018030#ifdef FEAT_RELTIME
18031 /* Set the time limit, if there is one. */
18032 profile_setlimit(time_limit, &tm);
18033#endif
18034
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018035 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18036 * start/middle/end (pat3, for the top pair). */
18037 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18038 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18039 if (pat2 == NULL || pat3 == NULL)
18040 goto theend;
18041 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18042 if (*mpat == NUL)
18043 STRCPY(pat3, pat2);
18044 else
18045 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18046 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018047 if (flags & SP_START)
18048 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018049
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 save_cursor = curwin->w_cursor;
18051 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018052 clearpos(&firstpos);
18053 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 pat = pat3;
18055 for (;;)
18056 {
18057 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018058 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18060 /* didn't find it or found the first match again: FAIL */
18061 break;
18062
18063 if (firstpos.lnum == 0)
18064 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018065 if (equalpos(pos, foundpos))
18066 {
18067 /* Found the same position again. Can happen with a pattern that
18068 * has "\zs" at the end and searching backwards. Advance one
18069 * character and try again. */
18070 if (dir == BACKWARD)
18071 decl(&pos);
18072 else
18073 incl(&pos);
18074 }
18075 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018077 /* clear the start flag to avoid getting stuck here */
18078 options &= ~SEARCH_START;
18079
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 /* If the skip pattern matches, ignore this match. */
18081 if (*skip != NUL)
18082 {
18083 save_pos = curwin->w_cursor;
18084 curwin->w_cursor = pos;
18085 r = eval_to_bool(skip, &err, NULL, FALSE);
18086 curwin->w_cursor = save_pos;
18087 if (err)
18088 {
18089 /* Evaluating {skip} caused an error, break here. */
18090 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018091 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 break;
18093 }
18094 if (r)
18095 continue;
18096 }
18097
18098 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18099 {
18100 /* Found end when searching backwards or start when searching
18101 * forward: nested pair. */
18102 ++nest;
18103 pat = pat2; /* nested, don't search for middle */
18104 }
18105 else
18106 {
18107 /* Found end when searching forward or start when searching
18108 * backward: end of (nested) pair; or found middle in outer pair. */
18109 if (--nest == 1)
18110 pat = pat3; /* outer level, search for middle */
18111 }
18112
18113 if (nest == 0)
18114 {
18115 /* Found the match: return matchcount or line number. */
18116 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018117 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018119 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018120 if (flags & SP_SETPCMARK)
18121 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 curwin->w_cursor = pos;
18123 if (!(flags & SP_REPEAT))
18124 break;
18125 nest = 1; /* search for next unmatched */
18126 }
18127 }
18128
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018129 if (match_pos != NULL)
18130 {
18131 /* Store the match cursor position */
18132 match_pos->lnum = curwin->w_cursor.lnum;
18133 match_pos->col = curwin->w_cursor.col + 1;
18134 }
18135
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018137 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 curwin->w_cursor = save_cursor;
18139
18140theend:
18141 vim_free(pat2);
18142 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018143 if (p_cpo == empty_option)
18144 p_cpo = save_cpo;
18145 else
18146 /* Darn, evaluating the {skip} expression changed the value. */
18147 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018148
18149 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150}
18151
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018152/*
18153 * "searchpos()" function
18154 */
18155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018156f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018157{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018158 pos_T match_pos;
18159 int lnum = 0;
18160 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018161 int n;
18162 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018163
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018164 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018165 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018166
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018167 n = search_cmn(argvars, &match_pos, &flags);
18168 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018169 {
18170 lnum = match_pos.lnum;
18171 col = match_pos.col;
18172 }
18173
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018174 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18175 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018176 if (flags & SP_SUBPAT)
18177 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018178}
18179
Bram Moolenaar0d660222005-01-07 21:51:51 +000018180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018181f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183#ifdef FEAT_CLIENTSERVER
18184 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018185 char_u *server = get_tv_string_chk(&argvars[0]);
18186 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187
Bram Moolenaar0d660222005-01-07 21:51:51 +000018188 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018189 if (server == NULL || reply == NULL)
18190 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191 if (check_restricted() || check_secure())
18192 return;
18193# ifdef FEAT_X11
18194 if (check_connection() == FAIL)
18195 return;
18196# endif
18197
18198 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018200 EMSG(_("E258: Unable to send to client"));
18201 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 rettv->vval.v_number = 0;
18204#else
18205 rettv->vval.v_number = -1;
18206#endif
18207}
18208
Bram Moolenaar0d660222005-01-07 21:51:51 +000018209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018210f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018211{
18212 char_u *r = NULL;
18213
18214#ifdef FEAT_CLIENTSERVER
18215# ifdef WIN32
18216 r = serverGetVimNames();
18217# else
18218 make_connection();
18219 if (X_DISPLAY != NULL)
18220 r = serverGetVimNames(X_DISPLAY);
18221# endif
18222#endif
18223 rettv->v_type = VAR_STRING;
18224 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225}
18226
18227/*
18228 * "setbufvar()" function
18229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018231f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232{
18233 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018236 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 char_u nbuf[NUMBUFLEN];
18238
18239 if (check_restricted() || check_secure())
18240 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18242 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018243 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 varp = &argvars[2];
18245
18246 if (buf != NULL && varname != NULL && varp != NULL)
18247 {
18248 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250
18251 if (*varname == '&')
18252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018253 long numval;
18254 char_u *strval;
18255 int error = FALSE;
18256
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018258 numval = get_tv_number_chk(varp, &error);
18259 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018260 if (!error && strval != NULL)
18261 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 }
18263 else
18264 {
18265 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18266 if (bufvarname != NULL)
18267 {
18268 STRCPY(bufvarname, "b:");
18269 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018270 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 vim_free(bufvarname);
18272 }
18273 }
18274
18275 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278}
18279
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018281f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018282{
18283 dict_T *d;
18284 dictitem_T *di;
18285 char_u *csearch;
18286
18287 if (argvars[0].v_type != VAR_DICT)
18288 {
18289 EMSG(_(e_dictreq));
18290 return;
18291 }
18292
18293 if ((d = argvars[0].vval.v_dict) != NULL)
18294 {
18295 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18296 if (csearch != NULL)
18297 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018298#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018299 if (enc_utf8)
18300 {
18301 int pcc[MAX_MCO];
18302 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018303
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018304 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18305 }
18306 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018307#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018308 set_last_csearch(PTR2CHAR(csearch),
18309 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018310 }
18311
18312 di = dict_find(d, (char_u *)"forward", -1);
18313 if (di != NULL)
18314 set_csearch_direction(get_tv_number(&di->di_tv)
18315 ? FORWARD : BACKWARD);
18316
18317 di = dict_find(d, (char_u *)"until", -1);
18318 if (di != NULL)
18319 set_csearch_until(!!get_tv_number(&di->di_tv));
18320 }
18321}
18322
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323/*
18324 * "setcmdpos()" function
18325 */
18326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018327f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018329 int pos = (int)get_tv_number(&argvars[0]) - 1;
18330
18331 if (pos >= 0)
18332 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333}
18334
18335/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018336 * "setfperm({fname}, {mode})" function
18337 */
18338 static void
18339f_setfperm(typval_T *argvars, typval_T *rettv)
18340{
18341 char_u *fname;
18342 char_u modebuf[NUMBUFLEN];
18343 char_u *mode_str;
18344 int i;
18345 int mask;
18346 int mode = 0;
18347
18348 rettv->vval.v_number = 0;
18349 fname = get_tv_string_chk(&argvars[0]);
18350 if (fname == NULL)
18351 return;
18352 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18353 if (mode_str == NULL)
18354 return;
18355 if (STRLEN(mode_str) != 9)
18356 {
18357 EMSG2(_(e_invarg2), mode_str);
18358 return;
18359 }
18360
18361 mask = 1;
18362 for (i = 8; i >= 0; --i)
18363 {
18364 if (mode_str[i] != '-')
18365 mode |= mask;
18366 mask = mask << 1;
18367 }
18368 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18369}
18370
18371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 * "setline()" function
18373 */
18374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018375f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376{
18377 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018378 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018379 list_T *l = NULL;
18380 listitem_T *li = NULL;
18381 long added = 0;
18382 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018384 lnum = get_tv_lnum(&argvars[0]);
18385 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018387 l = argvars[1].vval.v_list;
18388 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018390 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018391 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018392
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018393 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018394 for (;;)
18395 {
18396 if (l != NULL)
18397 {
18398 /* list argument, get next string */
18399 if (li == NULL)
18400 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018401 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018402 li = li->li_next;
18403 }
18404
18405 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018406 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018407 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018408
18409 /* When coming here from Insert mode, sync undo, so that this can be
18410 * undone separately from what was previously inserted. */
18411 if (u_sync_once == 2)
18412 {
18413 u_sync_once = 1; /* notify that u_sync() was called */
18414 u_sync(TRUE);
18415 }
18416
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018417 if (lnum <= curbuf->b_ml.ml_line_count)
18418 {
18419 /* existing line, replace it */
18420 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18421 {
18422 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018423 if (lnum == curwin->w_cursor.lnum)
18424 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018425 rettv->vval.v_number = 0; /* OK */
18426 }
18427 }
18428 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18429 {
18430 /* lnum is one past the last line, append the line */
18431 ++added;
18432 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18433 rettv->vval.v_number = 0; /* OK */
18434 }
18435
18436 if (l == NULL) /* only one string argument */
18437 break;
18438 ++lnum;
18439 }
18440
18441 if (added > 0)
18442 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443}
18444
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018445static 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 +000018446
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018448 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018449 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018451set_qf_ll_list(
18452 win_T *wp UNUSED,
18453 typval_T *list_arg UNUSED,
18454 typval_T *action_arg UNUSED,
18455 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018456{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018457#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018458 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018459 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018460 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018461#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018462
Bram Moolenaar2641f772005-03-25 21:58:17 +000018463 rettv->vval.v_number = -1;
18464
18465#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018466 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018467 EMSG(_(e_listreq));
18468 else
18469 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018470 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018471
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018472 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018473 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018474 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018475 if (act == NULL)
18476 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018477 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018478 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018479 else
18480 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018481 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018482 else if (action_arg->v_type == VAR_UNKNOWN)
18483 action = ' ';
18484 else
18485 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018486
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018487 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018488 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018489 rettv->vval.v_number = 0;
18490 }
18491#endif
18492}
18493
18494/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018495 * "setloclist()" function
18496 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018498f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018499{
18500 win_T *win;
18501
18502 rettv->vval.v_number = -1;
18503
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018504 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018505 if (win != NULL)
18506 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18507}
18508
18509/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018510 * "setmatches()" function
18511 */
18512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018513f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018514{
18515#ifdef FEAT_SEARCH_EXTRA
18516 list_T *l;
18517 listitem_T *li;
18518 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018519 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018520
18521 rettv->vval.v_number = -1;
18522 if (argvars[0].v_type != VAR_LIST)
18523 {
18524 EMSG(_(e_listreq));
18525 return;
18526 }
18527 if ((l = argvars[0].vval.v_list) != NULL)
18528 {
18529
18530 /* To some extent make sure that we are dealing with a list from
18531 * "getmatches()". */
18532 li = l->lv_first;
18533 while (li != NULL)
18534 {
18535 if (li->li_tv.v_type != VAR_DICT
18536 || (d = li->li_tv.vval.v_dict) == NULL)
18537 {
18538 EMSG(_(e_invarg));
18539 return;
18540 }
18541 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018542 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18543 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018544 && dict_find(d, (char_u *)"priority", -1) != NULL
18545 && dict_find(d, (char_u *)"id", -1) != NULL))
18546 {
18547 EMSG(_(e_invarg));
18548 return;
18549 }
18550 li = li->li_next;
18551 }
18552
18553 clear_matches(curwin);
18554 li = l->lv_first;
18555 while (li != NULL)
18556 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018557 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018558 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018559 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018560 char_u *group;
18561 int priority;
18562 int id;
18563 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018564
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018565 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018566 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18567 {
18568 if (s == NULL)
18569 {
18570 s = list_alloc();
18571 if (s == NULL)
18572 return;
18573 }
18574
18575 /* match from matchaddpos() */
18576 for (i = 1; i < 9; i++)
18577 {
18578 sprintf((char *)buf, (char *)"pos%d", i);
18579 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18580 {
18581 if (di->di_tv.v_type != VAR_LIST)
18582 return;
18583
18584 list_append_tv(s, &di->di_tv);
18585 s->lv_refcount++;
18586 }
18587 else
18588 break;
18589 }
18590 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018591
18592 group = get_dict_string(d, (char_u *)"group", FALSE);
18593 priority = (int)get_dict_number(d, (char_u *)"priority");
18594 id = (int)get_dict_number(d, (char_u *)"id");
18595 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18596 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18597 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018598 if (i == 0)
18599 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018600 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018601 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018602 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018603 }
18604 else
18605 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018606 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018607 list_unref(s);
18608 s = NULL;
18609 }
18610
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018611 li = li->li_next;
18612 }
18613 rettv->vval.v_number = 0;
18614 }
18615#endif
18616}
18617
18618/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018619 * "setpos()" function
18620 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018622f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018623{
18624 pos_T pos;
18625 int fnum;
18626 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018627 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018628
Bram Moolenaar08250432008-02-13 11:42:46 +000018629 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018630 name = get_tv_string_chk(argvars);
18631 if (name != NULL)
18632 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018633 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018634 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018635 if (--pos.col < 0)
18636 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018637 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018638 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018639 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018640 if (fnum == curbuf->b_fnum)
18641 {
18642 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018643 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018644 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018645 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018646 curwin->w_set_curswant = FALSE;
18647 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018648 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018649 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018650 }
18651 else
18652 EMSG(_(e_invarg));
18653 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018654 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18655 {
18656 /* set mark */
18657 if (setmark_pos(name[1], &pos, fnum) == OK)
18658 rettv->vval.v_number = 0;
18659 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018660 else
18661 EMSG(_(e_invarg));
18662 }
18663 }
18664}
18665
18666/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018667 * "setqflist()" function
18668 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018670f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018671{
18672 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18673}
18674
18675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 * "setreg()" function
18677 */
18678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018679f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680{
18681 int regname;
18682 char_u *strregname;
18683 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018684 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 int append;
18686 char_u yank_type;
18687 long block_len;
18688
18689 block_len = -1;
18690 yank_type = MAUTO;
18691 append = FALSE;
18692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018693 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018696 if (strregname == NULL)
18697 return; /* type error; errmsg already given */
18698 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699 if (regname == 0 || regname == '@')
18700 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018704 stropt = get_tv_string_chk(&argvars[2]);
18705 if (stropt == NULL)
18706 return; /* type error */
18707 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708 switch (*stropt)
18709 {
18710 case 'a': case 'A': /* append */
18711 append = TRUE;
18712 break;
18713 case 'v': case 'c': /* character-wise selection */
18714 yank_type = MCHAR;
18715 break;
18716 case 'V': case 'l': /* line-wise selection */
18717 yank_type = MLINE;
18718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 case 'b': case Ctrl_V: /* block-wise selection */
18720 yank_type = MBLOCK;
18721 if (VIM_ISDIGIT(stropt[1]))
18722 {
18723 ++stropt;
18724 block_len = getdigits(&stropt) - 1;
18725 --stropt;
18726 }
18727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 }
18729 }
18730
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018731 if (argvars[1].v_type == VAR_LIST)
18732 {
18733 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018734 char_u **allocval;
18735 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018736 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018737 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018738 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018739 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018740 int len;
18741
18742 /* If the list is NULL handle like an empty list. */
18743 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018744
Bram Moolenaar7d647822014-04-05 21:28:56 +020018745 /* First half: use for pointers to result lines; second half: use for
18746 * pointers to allocated copies. */
18747 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018748 if (lstval == NULL)
18749 return;
18750 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018751 allocval = lstval + len + 2;
18752 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018753
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018754 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018755 li = li->li_next)
18756 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018757 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018758 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018759 goto free_lstval;
18760 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018761 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018762 /* Need to make a copy, next get_tv_string_buf_chk() will
18763 * overwrite the string. */
18764 strval = vim_strsave(buf);
18765 if (strval == NULL)
18766 goto free_lstval;
18767 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018768 }
18769 *curval++ = strval;
18770 }
18771 *curval++ = NULL;
18772
18773 write_reg_contents_lst(regname, lstval, -1,
18774 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018775free_lstval:
18776 while (curallocval > allocval)
18777 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018778 vim_free(lstval);
18779 }
18780 else
18781 {
18782 strval = get_tv_string_chk(&argvars[1]);
18783 if (strval == NULL)
18784 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018785 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018788 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789}
18790
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018791/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018792 * "settabvar()" function
18793 */
18794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018795f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018796{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018797#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018798 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018799 tabpage_T *tp;
18800#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018801 char_u *varname, *tabvarname;
18802 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018803
18804 rettv->vval.v_number = 0;
18805
18806 if (check_restricted() || check_secure())
18807 return;
18808
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018809#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018810 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018811#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018812 varname = get_tv_string_chk(&argvars[1]);
18813 varp = &argvars[2];
18814
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018815 if (varname != NULL && varp != NULL
18816#ifdef FEAT_WINDOWS
18817 && tp != NULL
18818#endif
18819 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018820 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018821#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018822 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018823 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018824#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018825
18826 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18827 if (tabvarname != NULL)
18828 {
18829 STRCPY(tabvarname, "t:");
18830 STRCPY(tabvarname + 2, varname);
18831 set_var(tabvarname, varp, TRUE);
18832 vim_free(tabvarname);
18833 }
18834
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018835#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018836 /* Restore current tabpage */
18837 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018838 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018839#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018840 }
18841}
18842
18843/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018844 * "settabwinvar()" function
18845 */
18846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018847f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018848{
18849 setwinvar(argvars, rettv, 1);
18850}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851
18852/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018853 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018856f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018858 setwinvar(argvars, rettv, 0);
18859}
18860
18861/*
18862 * "setwinvar()" and "settabwinvar()" functions
18863 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018864
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018866setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018867{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868 win_T *win;
18869#ifdef FEAT_WINDOWS
18870 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018871 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018872 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873#endif
18874 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018875 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018877 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878
18879 if (check_restricted() || check_secure())
18880 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018881
18882#ifdef FEAT_WINDOWS
18883 if (off == 1)
18884 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18885 else
18886 tp = curtab;
18887#endif
18888 win = find_win_by_nr(&argvars[off], tp);
18889 varname = get_tv_string_chk(&argvars[off + 1]);
18890 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891
18892 if (win != NULL && varname != NULL && varp != NULL)
18893 {
18894#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018895 need_switch_win = !(tp == curtab && win == curwin);
18896 if (!need_switch_win
18897 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018900 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018902 long numval;
18903 char_u *strval;
18904 int error = FALSE;
18905
18906 ++varname;
18907 numval = get_tv_number_chk(varp, &error);
18908 strval = get_tv_string_buf_chk(varp, nbuf);
18909 if (!error && strval != NULL)
18910 set_option_value(varname, numval, strval, OPT_LOCAL);
18911 }
18912 else
18913 {
18914 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18915 if (winvarname != NULL)
18916 {
18917 STRCPY(winvarname, "w:");
18918 STRCPY(winvarname + 2, varname);
18919 set_var(winvarname, varp, TRUE);
18920 vim_free(winvarname);
18921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 }
18923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018925 if (need_switch_win)
18926 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927#endif
18928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929}
18930
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018931#ifdef FEAT_CRYPT
18932/*
18933 * "sha256({string})" function
18934 */
18935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018936f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018937{
18938 char_u *p;
18939
18940 p = get_tv_string(&argvars[0]);
18941 rettv->vval.v_string = vim_strsave(
18942 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18943 rettv->v_type = VAR_STRING;
18944}
18945#endif /* FEAT_CRYPT */
18946
Bram Moolenaar071d4272004-06-13 20:20:40 +000018947/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018948 * "shellescape({string})" function
18949 */
18950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018951f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018952{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018953 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018954 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018955 rettv->v_type = VAR_STRING;
18956}
18957
18958/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018959 * shiftwidth() function
18960 */
18961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018962f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018963{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018964 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018965}
18966
18967/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018968 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 */
18970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018971f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 p = get_tv_string(&argvars[0]);
18976 rettv->vval.v_string = vim_strsave(p);
18977 simplify_filename(rettv->vval.v_string); /* simplify in place */
18978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979}
18980
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018981#ifdef FEAT_FLOAT
18982/*
18983 * "sin()" function
18984 */
18985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018986f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018987{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018988 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018989
18990 rettv->v_type = VAR_FLOAT;
18991 if (get_float_arg(argvars, &f) == OK)
18992 rettv->vval.v_float = sin(f);
18993 else
18994 rettv->vval.v_float = 0.0;
18995}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018996
18997/*
18998 * "sinh()" function
18999 */
19000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019001f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019002{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019003 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019004
19005 rettv->v_type = VAR_FLOAT;
19006 if (get_float_arg(argvars, &f) == OK)
19007 rettv->vval.v_float = sinh(f);
19008 else
19009 rettv->vval.v_float = 0.0;
19010}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019011#endif
19012
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013static int
19014#ifdef __BORLANDC__
19015 _RTLENTRYF
19016#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019017 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019018static int
19019#ifdef __BORLANDC__
19020 _RTLENTRYF
19021#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019022 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019024/* struct used in the array that's given to qsort() */
19025typedef struct
19026{
19027 listitem_T *item;
19028 int idx;
19029} sortItem_T;
19030
Bram Moolenaar0b962472016-02-22 22:51:33 +010019031/* struct storing information about current sort */
19032typedef struct
19033{
19034 int item_compare_ic;
19035 int item_compare_numeric;
19036 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019037#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019038 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019039#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019040 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019041 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019042 dict_T *item_compare_selfdict;
19043 int item_compare_func_err;
19044 int item_compare_keep_zero;
19045} sortinfo_T;
19046static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019047static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019048#define ITEM_COMPARE_FAIL 999
19049
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019051 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 static int
19054#ifdef __BORLANDC__
19055_RTLENTRYF
19056#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019057item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019059 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019060 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019061 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019062 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019063 int res;
19064 char_u numbuf1[NUMBUFLEN];
19065 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019067 si1 = (sortItem_T *)s1;
19068 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019069 tv1 = &si1->item->li_tv;
19070 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019071
Bram Moolenaar0b962472016-02-22 22:51:33 +010019072 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019073 {
19074 long v1 = get_tv_number(tv1);
19075 long v2 = get_tv_number(tv2);
19076
19077 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19078 }
19079
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019080#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019081 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019082 {
19083 float_T v1 = get_tv_float(tv1);
19084 float_T v2 = get_tv_float(tv2);
19085
19086 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19087 }
19088#endif
19089
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019090 /* tv2string() puts quotes around a string and allocates memory. Don't do
19091 * that for string variables. Use a single quote when comparing with a
19092 * non-string to do what the docs promise. */
19093 if (tv1->v_type == VAR_STRING)
19094 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019095 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019096 p1 = (char_u *)"'";
19097 else
19098 p1 = tv1->vval.v_string;
19099 }
19100 else
19101 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19102 if (tv2->v_type == VAR_STRING)
19103 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019104 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019105 p2 = (char_u *)"'";
19106 else
19107 p2 = tv2->vval.v_string;
19108 }
19109 else
19110 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019111 if (p1 == NULL)
19112 p1 = (char_u *)"";
19113 if (p2 == NULL)
19114 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019116 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019117 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019118 res = STRICMP(p1, p2);
19119 else
19120 res = STRCMP(p1, p2);
19121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019123 {
19124 double n1, n2;
19125 n1 = strtod((char *)p1, (char **)&p1);
19126 n2 = strtod((char *)p2, (char **)&p2);
19127 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19128 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019129
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019130 /* When the result would be zero, compare the item indexes. Makes the
19131 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019133 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019134
Bram Moolenaar0d660222005-01-07 21:51:51 +000019135 vim_free(tofree1);
19136 vim_free(tofree2);
19137 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138}
19139
19140 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019141#ifdef __BORLANDC__
19142_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019144item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019145{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019146 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019147 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019149 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019151 char_u *func_name;
19152 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019154 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019155 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019156 return 0;
19157
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019158 si1 = (sortItem_T *)s1;
19159 si2 = (sortItem_T *)s2;
19160
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019161 if (partial == NULL)
19162 func_name = sortinfo->item_compare_func;
19163 else
19164 func_name = partial->pt_name;
19165
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019166 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019167 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019168 copy_tv(&si1->item->li_tv, &argv[0]);
19169 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019170
19171 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019172 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019173 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019174 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019175 clear_tv(&argv[0]);
19176 clear_tv(&argv[1]);
19177
19178 if (res == FAIL)
19179 res = ITEM_COMPARE_FAIL;
19180 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19182 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019183 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019184 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019185
19186 /* When the result would be zero, compare the pointers themselves. Makes
19187 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019188 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019189 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019190
Bram Moolenaar0d660222005-01-07 21:51:51 +000019191 return res;
19192}
19193
19194/*
19195 * "sort({list})" function
19196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019198do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199{
Bram Moolenaar33570922005-01-25 22:26:29 +000019200 list_T *l;
19201 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019202 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019203 sortinfo_T *old_sortinfo;
19204 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019205 long len;
19206 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 /* Pointer to current info struct used in compare function. Save and
19209 * restore the current one for nested calls. */
19210 old_sortinfo = sortinfo;
19211 sortinfo = &info;
19212
Bram Moolenaar0d660222005-01-07 21:51:51 +000019213 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019214 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215 else
19216 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019217 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019218 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019219 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19220 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019221 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019222 rettv->vval.v_list = l;
19223 rettv->v_type = VAR_LIST;
19224 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225
Bram Moolenaar0d660222005-01-07 21:51:51 +000019226 len = list_len(l);
19227 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019228 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229
Bram Moolenaar0b962472016-02-22 22:51:33 +010019230 info.item_compare_ic = FALSE;
19231 info.item_compare_numeric = FALSE;
19232 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019233#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019234 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019235#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019236 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019237 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019238 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 if (argvars[1].v_type != VAR_UNKNOWN)
19240 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019241 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019242 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019243 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019244 else if (argvars[1].v_type == VAR_PARTIAL)
19245 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019246 else
19247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019248 int error = FALSE;
19249
19250 i = get_tv_number_chk(&argvars[1], &error);
19251 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019252 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019253 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019254 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019255 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019256 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019257 else if (i != 0)
19258 {
19259 EMSG(_(e_invarg));
19260 goto theend;
19261 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019262 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019263 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019264 if (*info.item_compare_func == NUL)
19265 {
19266 /* empty string means default sort */
19267 info.item_compare_func = NULL;
19268 }
19269 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019270 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019271 info.item_compare_func = NULL;
19272 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019273 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019274 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019275 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019276 info.item_compare_func = NULL;
19277 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019278 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019279#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019280 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019281 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019282 info.item_compare_func = NULL;
19283 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019284 }
19285#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019287 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019288 info.item_compare_func = NULL;
19289 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019290 }
19291 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019292 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019293
19294 if (argvars[2].v_type != VAR_UNKNOWN)
19295 {
19296 /* optional third argument: {dict} */
19297 if (argvars[2].v_type != VAR_DICT)
19298 {
19299 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019300 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019301 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019302 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019303 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305
Bram Moolenaar0d660222005-01-07 21:51:51 +000019306 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019307 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019308 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019309 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310
Bram Moolenaar327aa022014-03-25 18:24:23 +010019311 i = 0;
19312 if (sort)
19313 {
19314 /* sort(): ptrs will be the list to sort */
19315 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019316 {
19317 ptrs[i].item = li;
19318 ptrs[i].idx = i;
19319 ++i;
19320 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019321
Bram Moolenaar0b962472016-02-22 22:51:33 +010019322 info.item_compare_func_err = FALSE;
19323 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019324 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019325 if ((info.item_compare_func != NULL
19326 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019327 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019329 EMSG(_("E702: Sort compare function failed"));
19330 else
19331 {
19332 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019333 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019335 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019336 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019337
Bram Moolenaar0b962472016-02-22 22:51:33 +010019338 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019339 {
19340 /* Clear the List and append the items in sorted order. */
19341 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19342 l->lv_len = 0;
19343 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019344 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019345 }
19346 }
19347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019350 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019351
19352 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019353 info.item_compare_func_err = FALSE;
19354 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019355 item_compare_func_ptr = info.item_compare_func != NULL
19356 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019357 ? item_compare2 : item_compare;
19358
19359 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19360 li = li->li_next)
19361 {
19362 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19363 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019364 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019365 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019366 {
19367 EMSG(_("E882: Uniq compare function failed"));
19368 break;
19369 }
19370 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371
Bram Moolenaar0b962472016-02-22 22:51:33 +010019372 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019373 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019374 while (--i >= 0)
19375 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019376 li = ptrs[i].item->li_next;
19377 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019378 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019379 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019380 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019381 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019382 list_fix_watch(l, li);
19383 listitem_free(li);
19384 l->lv_len--;
19385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019386 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019387 }
19388
19389 vim_free(ptrs);
19390 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019391theend:
19392 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393}
19394
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019395/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019396 * "sort({list})" function
19397 */
19398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019399f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019400{
19401 do_sort_uniq(argvars, rettv, TRUE);
19402}
19403
19404/*
19405 * "uniq({list})" function
19406 */
19407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019408f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019409{
19410 do_sort_uniq(argvars, rettv, FALSE);
19411}
19412
19413/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019414 * "soundfold({word})" function
19415 */
19416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019417f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019418{
19419 char_u *s;
19420
19421 rettv->v_type = VAR_STRING;
19422 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019423#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019424 rettv->vval.v_string = eval_soundfold(s);
19425#else
19426 rettv->vval.v_string = vim_strsave(s);
19427#endif
19428}
19429
19430/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019431 * "spellbadword()" function
19432 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019434f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019435{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019436 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019437 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019438 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019439
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019440 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019441 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019442
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019443#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019444 if (argvars[0].v_type == VAR_UNKNOWN)
19445 {
19446 /* Find the start and length of the badly spelled word. */
19447 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19448 if (len != 0)
19449 word = ml_get_cursor();
19450 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019451 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019452 {
19453 char_u *str = get_tv_string_chk(&argvars[0]);
19454 int capcol = -1;
19455
19456 if (str != NULL)
19457 {
19458 /* Check the argument for spelling. */
19459 while (*str != NUL)
19460 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019461 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019462 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019463 {
19464 word = str;
19465 break;
19466 }
19467 str += len;
19468 }
19469 }
19470 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019471#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019472
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019473 list_append_string(rettv->vval.v_list, word, len);
19474 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019475 attr == HLF_SPB ? "bad" :
19476 attr == HLF_SPR ? "rare" :
19477 attr == HLF_SPL ? "local" :
19478 attr == HLF_SPC ? "caps" :
19479 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019480}
19481
19482/*
19483 * "spellsuggest()" function
19484 */
19485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019486f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019487{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019488#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019489 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019490 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019491 int maxcount;
19492 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019493 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019494 listitem_T *li;
19495 int need_capital = FALSE;
19496#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019497
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019498 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019499 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019500
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019501#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019502 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019503 {
19504 str = get_tv_string(&argvars[0]);
19505 if (argvars[1].v_type != VAR_UNKNOWN)
19506 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019507 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019508 if (maxcount <= 0)
19509 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019510 if (argvars[2].v_type != VAR_UNKNOWN)
19511 {
19512 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19513 if (typeerr)
19514 return;
19515 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019516 }
19517 else
19518 maxcount = 25;
19519
Bram Moolenaar4770d092006-01-12 23:22:24 +000019520 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019521
19522 for (i = 0; i < ga.ga_len; ++i)
19523 {
19524 str = ((char_u **)ga.ga_data)[i];
19525
19526 li = listitem_alloc();
19527 if (li == NULL)
19528 vim_free(str);
19529 else
19530 {
19531 li->li_tv.v_type = VAR_STRING;
19532 li->li_tv.v_lock = 0;
19533 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019534 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019535 }
19536 }
19537 ga_clear(&ga);
19538 }
19539#endif
19540}
19541
Bram Moolenaar0d660222005-01-07 21:51:51 +000019542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019543f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019544{
19545 char_u *str;
19546 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019547 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019548 regmatch_T regmatch;
19549 char_u patbuf[NUMBUFLEN];
19550 char_u *save_cpo;
19551 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019552 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019553 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019554 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019555
19556 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19557 save_cpo = p_cpo;
19558 p_cpo = (char_u *)"";
19559
19560 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019561 if (argvars[1].v_type != VAR_UNKNOWN)
19562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019563 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19564 if (pat == NULL)
19565 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019566 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019567 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019568 }
19569 if (pat == NULL || *pat == NUL)
19570 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019571
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019572 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019574 if (typeerr)
19575 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576
Bram Moolenaar0d660222005-01-07 21:51:51 +000019577 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19578 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019580 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019581 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019582 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019583 if (*str == NUL)
19584 match = FALSE; /* empty item at the end */
19585 else
19586 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019587 if (match)
19588 end = regmatch.startp[0];
19589 else
19590 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019591 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19592 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019593 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019594 if (list_append_string(rettv->vval.v_list, str,
19595 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019596 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019597 }
19598 if (!match)
19599 break;
19600 /* Advance to just after the match. */
19601 if (regmatch.endp[0] > str)
19602 col = 0;
19603 else
19604 {
19605 /* Don't get stuck at the same match. */
19606#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019607 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019608#else
19609 col = 1;
19610#endif
19611 }
19612 str = regmatch.endp[0];
19613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614
Bram Moolenaar473de612013-06-08 18:19:48 +020019615 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617
Bram Moolenaar0d660222005-01-07 21:51:51 +000019618 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619}
19620
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019621#ifdef FEAT_FLOAT
19622/*
19623 * "sqrt()" function
19624 */
19625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019626f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019627{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019628 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019629
19630 rettv->v_type = VAR_FLOAT;
19631 if (get_float_arg(argvars, &f) == OK)
19632 rettv->vval.v_float = sqrt(f);
19633 else
19634 rettv->vval.v_float = 0.0;
19635}
19636
19637/*
19638 * "str2float()" function
19639 */
19640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019641f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019642{
19643 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19644
19645 if (*p == '+')
19646 p = skipwhite(p + 1);
19647 (void)string2float(p, &rettv->vval.v_float);
19648 rettv->v_type = VAR_FLOAT;
19649}
19650#endif
19651
Bram Moolenaar2c932302006-03-18 21:42:09 +000019652/*
19653 * "str2nr()" function
19654 */
19655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019656f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019657{
19658 int base = 10;
19659 char_u *p;
19660 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019661 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019662
19663 if (argvars[1].v_type != VAR_UNKNOWN)
19664 {
19665 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019666 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019667 {
19668 EMSG(_(e_invarg));
19669 return;
19670 }
19671 }
19672
19673 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019674 if (*p == '+')
19675 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019676 switch (base)
19677 {
19678 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19679 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19680 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19681 default: what = 0;
19682 }
19683 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019684 rettv->vval.v_number = n;
19685}
19686
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687#ifdef HAVE_STRFTIME
19688/*
19689 * "strftime({format}[, {time}])" function
19690 */
19691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019692f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693{
19694 char_u result_buf[256];
19695 struct tm *curtime;
19696 time_t seconds;
19697 char_u *p;
19698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019701 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019702 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 seconds = time(NULL);
19704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019705 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 curtime = localtime(&seconds);
19707 /* MSVC returns NULL for an invalid value of seconds. */
19708 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 else
19711 {
19712# ifdef FEAT_MBYTE
19713 vimconv_T conv;
19714 char_u *enc;
19715
19716 conv.vc_type = CONV_NONE;
19717 enc = enc_locale();
19718 convert_setup(&conv, p_enc, enc);
19719 if (conv.vc_type != CONV_NONE)
19720 p = string_convert(&conv, p, NULL);
19721# endif
19722 if (p != NULL)
19723 (void)strftime((char *)result_buf, sizeof(result_buf),
19724 (char *)p, curtime);
19725 else
19726 result_buf[0] = NUL;
19727
19728# ifdef FEAT_MBYTE
19729 if (conv.vc_type != CONV_NONE)
19730 vim_free(p);
19731 convert_setup(&conv, enc, p_enc);
19732 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734 else
19735# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737
19738# ifdef FEAT_MBYTE
19739 /* Release conversion descriptors */
19740 convert_setup(&conv, NULL, NULL);
19741 vim_free(enc);
19742# endif
19743 }
19744}
19745#endif
19746
19747/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019748 * "strgetchar()" function
19749 */
19750 static void
19751f_strgetchar(typval_T *argvars, typval_T *rettv)
19752{
19753 char_u *str;
19754 int len;
19755 int error = FALSE;
19756 int charidx;
19757
19758 rettv->vval.v_number = -1;
19759 str = get_tv_string_chk(&argvars[0]);
19760 if (str == NULL)
19761 return;
19762 len = (int)STRLEN(str);
19763 charidx = get_tv_number_chk(&argvars[1], &error);
19764 if (error)
19765 return;
19766#ifdef FEAT_MBYTE
19767 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019768 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019769
19770 while (charidx >= 0 && byteidx < len)
19771 {
19772 if (charidx == 0)
19773 {
19774 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19775 break;
19776 }
19777 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019778 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019779 }
19780 }
19781#else
19782 if (charidx < len)
19783 rettv->vval.v_number = str[charidx];
19784#endif
19785}
19786
19787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 * "stridx()" function
19789 */
19790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019791f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792{
19793 char_u buf[NUMBUFLEN];
19794 char_u *needle;
19795 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019798 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019800 needle = get_tv_string_chk(&argvars[1]);
19801 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019802 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803 if (needle == NULL || haystack == NULL)
19804 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019805
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 if (argvars[2].v_type != VAR_UNKNOWN)
19807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019808 int error = FALSE;
19809
19810 start_idx = get_tv_number_chk(&argvars[2], &error);
19811 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019813 if (start_idx >= 0)
19814 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 }
19816
19817 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19818 if (pos != NULL)
19819 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820}
19821
19822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019823 * "string()" function
19824 */
19825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019826f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019827{
19828 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019829 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019831 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019832 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19833 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019834 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019835 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019836 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837}
19838
19839/*
19840 * "strlen()" function
19841 */
19842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019843f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019845 rettv->vval.v_number = (varnumber_T)(STRLEN(
19846 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847}
19848
19849/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019850 * "strchars()" function
19851 */
19852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019853f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019854{
19855 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019856 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019857#ifdef FEAT_MBYTE
19858 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019859 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019860#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019861
19862 if (argvars[1].v_type != VAR_UNKNOWN)
19863 skipcc = get_tv_number_chk(&argvars[1], NULL);
19864 if (skipcc < 0 || skipcc > 1)
19865 EMSG(_(e_invarg));
19866 else
19867 {
19868#ifdef FEAT_MBYTE
19869 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19870 while (*s != NUL)
19871 {
19872 func_mb_ptr2char_adv(&s);
19873 ++len;
19874 }
19875 rettv->vval.v_number = len;
19876#else
19877 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19878#endif
19879 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019880}
19881
19882/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019883 * "strdisplaywidth()" function
19884 */
19885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019886f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019887{
19888 char_u *s = get_tv_string(&argvars[0]);
19889 int col = 0;
19890
19891 if (argvars[1].v_type != VAR_UNKNOWN)
19892 col = get_tv_number(&argvars[1]);
19893
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019894 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019895}
19896
19897/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019898 * "strwidth()" function
19899 */
19900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019901f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019902{
19903 char_u *s = get_tv_string(&argvars[0]);
19904
19905 rettv->vval.v_number = (varnumber_T)(
19906#ifdef FEAT_MBYTE
19907 mb_string2cells(s, -1)
19908#else
19909 STRLEN(s)
19910#endif
19911 );
19912}
19913
19914/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019915 * "strcharpart()" function
19916 */
19917 static void
19918f_strcharpart(typval_T *argvars, typval_T *rettv)
19919{
19920#ifdef FEAT_MBYTE
19921 char_u *p;
19922 int nchar;
19923 int nbyte = 0;
19924 int charlen;
19925 int len = 0;
19926 int slen;
19927 int error = FALSE;
19928
19929 p = get_tv_string(&argvars[0]);
19930 slen = (int)STRLEN(p);
19931
19932 nchar = get_tv_number_chk(&argvars[1], &error);
19933 if (!error)
19934 {
19935 if (nchar > 0)
19936 while (nchar > 0 && nbyte < slen)
19937 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019938 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019939 --nchar;
19940 }
19941 else
19942 nbyte = nchar;
19943 if (argvars[2].v_type != VAR_UNKNOWN)
19944 {
19945 charlen = get_tv_number(&argvars[2]);
19946 while (charlen > 0 && nbyte + len < slen)
19947 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019948 int off = nbyte + len;
19949
19950 if (off < 0)
19951 len += 1;
19952 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019953 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019954 --charlen;
19955 }
19956 }
19957 else
19958 len = slen - nbyte; /* default: all bytes that are available. */
19959 }
19960
19961 /*
19962 * Only return the overlap between the specified part and the actual
19963 * string.
19964 */
19965 if (nbyte < 0)
19966 {
19967 len += nbyte;
19968 nbyte = 0;
19969 }
19970 else if (nbyte > slen)
19971 nbyte = slen;
19972 if (len < 0)
19973 len = 0;
19974 else if (nbyte + len > slen)
19975 len = slen - nbyte;
19976
19977 rettv->v_type = VAR_STRING;
19978 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19979#else
19980 f_strpart(argvars, rettv);
19981#endif
19982}
19983
19984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 * "strpart()" function
19986 */
19987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019988f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989{
19990 char_u *p;
19991 int n;
19992 int len;
19993 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019994 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 slen = (int)STRLEN(p);
19998
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 n = get_tv_number_chk(&argvars[1], &error);
20000 if (error)
20001 len = 0;
20002 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020003 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 else
20005 len = slen - n; /* default len: all bytes that are available. */
20006
20007 /*
20008 * Only return the overlap between the specified part and the actual
20009 * string.
20010 */
20011 if (n < 0)
20012 {
20013 len += n;
20014 n = 0;
20015 }
20016 else if (n > slen)
20017 n = slen;
20018 if (len < 0)
20019 len = 0;
20020 else if (n + len > slen)
20021 len = slen - n;
20022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 rettv->v_type = VAR_STRING;
20024 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025}
20026
20027/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020028 * "strridx()" function
20029 */
20030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020031f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020032{
20033 char_u buf[NUMBUFLEN];
20034 char_u *needle;
20035 char_u *haystack;
20036 char_u *rest;
20037 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020038 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020040 needle = get_tv_string_chk(&argvars[1]);
20041 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020042
20043 rettv->vval.v_number = -1;
20044 if (needle == NULL || haystack == NULL)
20045 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020046
20047 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020048 if (argvars[2].v_type != VAR_UNKNOWN)
20049 {
20050 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020051 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020052 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020053 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020054 }
20055 else
20056 end_idx = haystack_len;
20057
Bram Moolenaar0d660222005-01-07 21:51:51 +000020058 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020059 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020060 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020061 lastmatch = haystack + end_idx;
20062 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020063 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020064 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020065 for (rest = haystack; *rest != '\0'; ++rest)
20066 {
20067 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020068 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020069 break;
20070 lastmatch = rest;
20071 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020073
20074 if (lastmatch == NULL)
20075 rettv->vval.v_number = -1;
20076 else
20077 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20078}
20079
20080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 * "strtrans()" function
20082 */
20083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020084f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086 rettv->v_type = VAR_STRING;
20087 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088}
20089
20090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020091 * "submatch()" function
20092 */
20093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020094f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020095{
Bram Moolenaar41571762014-04-02 19:00:58 +020020096 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020097 int no;
20098 int retList = 0;
20099
20100 no = (int)get_tv_number_chk(&argvars[0], &error);
20101 if (error)
20102 return;
20103 error = FALSE;
20104 if (argvars[1].v_type != VAR_UNKNOWN)
20105 retList = get_tv_number_chk(&argvars[1], &error);
20106 if (error)
20107 return;
20108
20109 if (retList == 0)
20110 {
20111 rettv->v_type = VAR_STRING;
20112 rettv->vval.v_string = reg_submatch(no);
20113 }
20114 else
20115 {
20116 rettv->v_type = VAR_LIST;
20117 rettv->vval.v_list = reg_submatch_list(no);
20118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020119}
20120
20121/*
20122 * "substitute()" function
20123 */
20124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020126{
20127 char_u patbuf[NUMBUFLEN];
20128 char_u subbuf[NUMBUFLEN];
20129 char_u flagsbuf[NUMBUFLEN];
20130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020131 char_u *str = get_tv_string_chk(&argvars[0]);
20132 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20133 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20134 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20135
Bram Moolenaar0d660222005-01-07 21:51:51 +000020136 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020137 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20138 rettv->vval.v_string = NULL;
20139 else
20140 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020141}
20142
20143/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020144 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020147f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148{
20149 int id = 0;
20150#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020151 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 long col;
20153 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020154 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020156 lnum = get_tv_lnum(argvars); /* -1 on type error */
20157 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20158 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020160 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020161 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020162 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163#endif
20164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020165 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166}
20167
20168/*
20169 * "synIDattr(id, what [, mode])" function
20170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020172f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173{
20174 char_u *p = NULL;
20175#ifdef FEAT_SYN_HL
20176 int id;
20177 char_u *what;
20178 char_u *mode;
20179 char_u modebuf[NUMBUFLEN];
20180 int modec;
20181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020182 id = get_tv_number(&argvars[0]);
20183 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020186 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020187 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020188 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189 modec = 0; /* replace invalid with current */
20190 }
20191 else
20192 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020193#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020194 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 modec = 'g';
20196 else
20197#endif
20198 if (t_colors > 1)
20199 modec = 'c';
20200 else
20201 modec = 't';
20202 }
20203
20204
20205 switch (TOLOWER_ASC(what[0]))
20206 {
20207 case 'b':
20208 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20209 p = highlight_color(id, what, modec);
20210 else /* bold */
20211 p = highlight_has_attr(id, HL_BOLD, modec);
20212 break;
20213
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020214 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 p = highlight_color(id, what, modec);
20216 break;
20217
20218 case 'i':
20219 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20220 p = highlight_has_attr(id, HL_INVERSE, modec);
20221 else /* italic */
20222 p = highlight_has_attr(id, HL_ITALIC, modec);
20223 break;
20224
20225 case 'n': /* name */
20226 p = get_highlight_name(NULL, id - 1);
20227 break;
20228
20229 case 'r': /* reverse */
20230 p = highlight_has_attr(id, HL_INVERSE, modec);
20231 break;
20232
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020233 case 's':
20234 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20235 p = highlight_color(id, what, modec);
20236 else /* standout */
20237 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 break;
20239
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020240 case 'u':
20241 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20242 /* underline */
20243 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20244 else
20245 /* undercurl */
20246 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 break;
20248 }
20249
20250 if (p != NULL)
20251 p = vim_strsave(p);
20252#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 rettv->v_type = VAR_STRING;
20254 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255}
20256
20257/*
20258 * "synIDtrans(id)" function
20259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020261f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262{
20263 int id;
20264
20265#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020266 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267
20268 if (id > 0)
20269 id = syn_get_final_id(id);
20270 else
20271#endif
20272 id = 0;
20273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020274 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275}
20276
20277/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020278 * "synconcealed(lnum, col)" function
20279 */
20280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020281f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020282{
20283#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20284 long lnum;
20285 long col;
20286 int syntax_flags = 0;
20287 int cchar;
20288 int matchid = 0;
20289 char_u str[NUMBUFLEN];
20290#endif
20291
20292 rettv->v_type = VAR_LIST;
20293 rettv->vval.v_list = NULL;
20294
20295#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20296 lnum = get_tv_lnum(argvars); /* -1 on type error */
20297 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20298
20299 vim_memset(str, NUL, sizeof(str));
20300
20301 if (rettv_list_alloc(rettv) != FAIL)
20302 {
20303 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20304 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20305 && curwin->w_p_cole > 0)
20306 {
20307 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20308 syntax_flags = get_syntax_info(&matchid);
20309
20310 /* get the conceal character */
20311 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20312 {
20313 cchar = syn_get_sub_char();
20314 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20315 cchar = lcs_conceal;
20316 if (cchar != NUL)
20317 {
20318# ifdef FEAT_MBYTE
20319 if (has_mbyte)
20320 (*mb_char2bytes)(cchar, str);
20321 else
20322# endif
20323 str[0] = cchar;
20324 }
20325 }
20326 }
20327
20328 list_append_number(rettv->vval.v_list,
20329 (syntax_flags & HL_CONCEAL) != 0);
20330 /* -1 to auto-determine strlen */
20331 list_append_string(rettv->vval.v_list, str, -1);
20332 list_append_number(rettv->vval.v_list, matchid);
20333 }
20334#endif
20335}
20336
20337/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020338 * "synstack(lnum, col)" function
20339 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020341f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020342{
20343#ifdef FEAT_SYN_HL
20344 long lnum;
20345 long col;
20346 int i;
20347 int id;
20348#endif
20349
20350 rettv->v_type = VAR_LIST;
20351 rettv->vval.v_list = NULL;
20352
20353#ifdef FEAT_SYN_HL
20354 lnum = get_tv_lnum(argvars); /* -1 on type error */
20355 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20356
20357 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020358 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020359 && rettv_list_alloc(rettv) != FAIL)
20360 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020361 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020362 for (i = 0; ; ++i)
20363 {
20364 id = syn_get_stack_item(i);
20365 if (id < 0)
20366 break;
20367 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20368 break;
20369 }
20370 }
20371#endif
20372}
20373
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020375get_cmd_output_as_rettv(
20376 typval_T *argvars,
20377 typval_T *rettv,
20378 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020380 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020382 char_u *infile = NULL;
20383 char_u buf[NUMBUFLEN];
20384 int err = FALSE;
20385 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020387 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020389 rettv->v_type = VAR_STRING;
20390 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020391 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020392 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020393
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020394 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020395 {
20396 /*
20397 * Write the string to a temp file, to be used for input of the shell
20398 * command.
20399 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020400 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020401 {
20402 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020403 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020404 }
20405
20406 fd = mch_fopen((char *)infile, WRITEBIN);
20407 if (fd == NULL)
20408 {
20409 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020410 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020411 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020412 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020413 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020414 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20415 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020416 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020417 else
20418 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020419 size_t len;
20420
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020421 p = get_tv_string_buf_chk(&argvars[1], buf);
20422 if (p == NULL)
20423 {
20424 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020425 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020426 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020427 len = STRLEN(p);
20428 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020429 err = TRUE;
20430 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020431 if (fclose(fd) != 0)
20432 err = TRUE;
20433 if (err)
20434 {
20435 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020436 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020437 }
20438 }
20439
Bram Moolenaar52a72462014-08-29 15:53:52 +020020440 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20441 * echoes typeahead, that messes up the display. */
20442 if (!msg_silent)
20443 flags += SHELL_COOKED;
20444
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020445 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020447 int len;
20448 listitem_T *li;
20449 char_u *s = NULL;
20450 char_u *start;
20451 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020452 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453
Bram Moolenaar52a72462014-08-29 15:53:52 +020020454 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020455 if (res == NULL)
20456 goto errret;
20457
20458 list = list_alloc();
20459 if (list == NULL)
20460 goto errret;
20461
20462 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020464 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020465 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020466 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020467 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020468
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020469 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020470 if (s == NULL)
20471 goto errret;
20472
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020473 for (p = s; start < end; ++p, ++start)
20474 *p = *start == NUL ? NL : *start;
20475 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020476
20477 li = listitem_alloc();
20478 if (li == NULL)
20479 {
20480 vim_free(s);
20481 goto errret;
20482 }
20483 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020484 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020485 li->li_tv.vval.v_string = s;
20486 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020488
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020489 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020490 rettv->v_type = VAR_LIST;
20491 rettv->vval.v_list = list;
20492 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020494 else
20495 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020496 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020497#ifdef USE_CR
20498 /* translate <CR> into <NL> */
20499 if (res != NULL)
20500 {
20501 char_u *s;
20502
20503 for (s = res; *s; ++s)
20504 {
20505 if (*s == CAR)
20506 *s = NL;
20507 }
20508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509#else
20510# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020511 /* translate <CR><NL> into <NL> */
20512 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020514 char_u *s, *d;
20515
20516 d = res;
20517 for (s = res; *s; ++s)
20518 {
20519 if (s[0] == CAR && s[1] == NL)
20520 ++s;
20521 *d++ = *s;
20522 }
20523 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525# endif
20526#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020527 rettv->vval.v_string = res;
20528 res = NULL;
20529 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020530
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020531errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020532 if (infile != NULL)
20533 {
20534 mch_remove(infile);
20535 vim_free(infile);
20536 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020537 if (res != NULL)
20538 vim_free(res);
20539 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020540 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020541}
20542
20543/*
20544 * "system()" function
20545 */
20546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020548{
20549 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20550}
20551
20552/*
20553 * "systemlist()" function
20554 */
20555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020557{
20558 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559}
20560
20561/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020562 * "tabpagebuflist()" function
20563 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020565f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020566{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020567#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020568 tabpage_T *tp;
20569 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020570
20571 if (argvars[0].v_type == VAR_UNKNOWN)
20572 wp = firstwin;
20573 else
20574 {
20575 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20576 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020577 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020578 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020579 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020580 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020581 for (; wp != NULL; wp = wp->w_next)
20582 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020583 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020584 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020585 }
20586#endif
20587}
20588
20589
20590/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020591 * "tabpagenr()" function
20592 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020594f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020595{
20596 int nr = 1;
20597#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020598 char_u *arg;
20599
20600 if (argvars[0].v_type != VAR_UNKNOWN)
20601 {
20602 arg = get_tv_string_chk(&argvars[0]);
20603 nr = 0;
20604 if (arg != NULL)
20605 {
20606 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020607 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020608 else
20609 EMSG2(_(e_invexpr2), arg);
20610 }
20611 }
20612 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020613 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020614#endif
20615 rettv->vval.v_number = nr;
20616}
20617
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020618
20619#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020620static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020621
20622/*
20623 * Common code for tabpagewinnr() and winnr().
20624 */
20625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020626get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020627{
20628 win_T *twin;
20629 int nr = 1;
20630 win_T *wp;
20631 char_u *arg;
20632
20633 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20634 if (argvar->v_type != VAR_UNKNOWN)
20635 {
20636 arg = get_tv_string_chk(argvar);
20637 if (arg == NULL)
20638 nr = 0; /* type error; errmsg already given */
20639 else if (STRCMP(arg, "$") == 0)
20640 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20641 else if (STRCMP(arg, "#") == 0)
20642 {
20643 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20644 if (twin == NULL)
20645 nr = 0;
20646 }
20647 else
20648 {
20649 EMSG2(_(e_invexpr2), arg);
20650 nr = 0;
20651 }
20652 }
20653
20654 if (nr > 0)
20655 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20656 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020657 {
20658 if (wp == NULL)
20659 {
20660 /* didn't find it in this tabpage */
20661 nr = 0;
20662 break;
20663 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020664 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020665 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020666 return nr;
20667}
20668#endif
20669
20670/*
20671 * "tabpagewinnr()" function
20672 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020674f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020675{
20676 int nr = 1;
20677#ifdef FEAT_WINDOWS
20678 tabpage_T *tp;
20679
20680 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20681 if (tp == NULL)
20682 nr = 0;
20683 else
20684 nr = get_winnr(tp, &argvars[1]);
20685#endif
20686 rettv->vval.v_number = nr;
20687}
20688
20689
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020690/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020691 * "tagfiles()" function
20692 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020694f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020695{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020696 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020697 tagname_T tn;
20698 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020699
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020700 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020701 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020702 fname = alloc(MAXPATHL);
20703 if (fname == NULL)
20704 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020705
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020706 for (first = TRUE; ; first = FALSE)
20707 if (get_tagfname(&tn, first, fname) == FAIL
20708 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020709 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020710 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020711 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020712}
20713
20714/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020715 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020716 */
20717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020718f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020719{
20720 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020721
20722 tag_pattern = get_tv_string(&argvars[0]);
20723
20724 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020725 if (*tag_pattern == NUL)
20726 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020727
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020728 if (rettv_list_alloc(rettv) == OK)
20729 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020730}
20731
20732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 * "tempname()" function
20734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020736f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737{
20738 static int x = 'A';
20739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020741 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020742
20743 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20744 * names. Skip 'I' and 'O', they are used for shell redirection. */
20745 do
20746 {
20747 if (x == 'Z')
20748 x = '0';
20749 else if (x == '9')
20750 x = 'A';
20751 else
20752 {
20753#ifdef EBCDIC
20754 if (x == 'I')
20755 x = 'J';
20756 else if (x == 'R')
20757 x = 'S';
20758 else
20759#endif
20760 ++x;
20761 }
20762 } while (x == 'I' || x == 'O');
20763}
20764
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020765#ifdef FEAT_FLOAT
20766/*
20767 * "tan()" function
20768 */
20769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020770f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020771{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020772 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020773
20774 rettv->v_type = VAR_FLOAT;
20775 if (get_float_arg(argvars, &f) == OK)
20776 rettv->vval.v_float = tan(f);
20777 else
20778 rettv->vval.v_float = 0.0;
20779}
20780
20781/*
20782 * "tanh()" function
20783 */
20784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020785f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020786{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020787 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020788
20789 rettv->v_type = VAR_FLOAT;
20790 if (get_float_arg(argvars, &f) == OK)
20791 rettv->vval.v_float = tanh(f);
20792 else
20793 rettv->vval.v_float = 0.0;
20794}
20795#endif
20796
Bram Moolenaar574860b2016-05-24 17:33:34 +020020797/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020798 * "test_alloc_fail(id, countdown, repeat)" function
20799 */
20800 static void
20801f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20802{
20803 if (argvars[0].v_type != VAR_NUMBER
20804 || argvars[0].vval.v_number <= 0
20805 || argvars[1].v_type != VAR_NUMBER
20806 || argvars[1].vval.v_number < 0
20807 || argvars[2].v_type != VAR_NUMBER)
20808 EMSG(_(e_invarg));
20809 else
20810 {
20811 alloc_fail_id = argvars[0].vval.v_number;
20812 if (alloc_fail_id >= aid_last)
20813 EMSG(_(e_invarg));
20814 alloc_fail_countdown = argvars[1].vval.v_number;
20815 alloc_fail_repeat = argvars[2].vval.v_number;
20816 did_outofmem_msg = FALSE;
20817 }
20818}
20819
20820/*
20821 * "test_disable_char_avail({expr})" function
20822 */
20823 static void
20824f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20825{
20826 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20827}
20828
20829/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020830 * "test_garbagecollect_now()" function
20831 */
20832 static void
20833f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20834{
20835 /* This is dangerous, any Lists and Dicts used internally may be freed
20836 * while still in use. */
20837 garbage_collect(TRUE);
20838}
20839
20840#ifdef FEAT_JOB_CHANNEL
20841 static void
20842f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20843{
20844 rettv->v_type = VAR_CHANNEL;
20845 rettv->vval.v_channel = NULL;
20846}
20847#endif
20848
20849 static void
20850f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20851{
20852 rettv->v_type = VAR_DICT;
20853 rettv->vval.v_dict = NULL;
20854}
20855
20856#ifdef FEAT_JOB_CHANNEL
20857 static void
20858f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20859{
20860 rettv->v_type = VAR_JOB;
20861 rettv->vval.v_job = NULL;
20862}
20863#endif
20864
20865 static void
20866f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20867{
20868 rettv->v_type = VAR_LIST;
20869 rettv->vval.v_list = NULL;
20870}
20871
20872 static void
20873f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20874{
20875 rettv->v_type = VAR_PARTIAL;
20876 rettv->vval.v_partial = NULL;
20877}
20878
20879 static void
20880f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20881{
20882 rettv->v_type = VAR_STRING;
20883 rettv->vval.v_string = NULL;
20884}
20885
Bram Moolenaar975b5272016-03-15 23:10:59 +010020886#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20887/*
20888 * Get a callback from "arg". It can be a Funcref or a function name.
20889 * When "arg" is zero return an empty string.
20890 * Return NULL for an invalid argument.
20891 */
20892 char_u *
20893get_callback(typval_T *arg, partial_T **pp)
20894{
20895 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20896 {
20897 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020898 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020899 return (*pp)->pt_name;
20900 }
20901 *pp = NULL;
20902 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20903 return arg->vval.v_string;
20904 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20905 return (char_u *)"";
20906 EMSG(_("E921: Invalid callback argument"));
20907 return NULL;
20908}
20909#endif
20910
20911#ifdef FEAT_TIMERS
20912/*
20913 * "timer_start(time, callback [, options])" function
20914 */
20915 static void
20916f_timer_start(typval_T *argvars, typval_T *rettv)
20917{
20918 long msec = get_tv_number(&argvars[0]);
20919 timer_T *timer;
20920 int repeat = 0;
20921 char_u *callback;
20922 dict_T *dict;
20923
Bram Moolenaar38499922016-04-22 20:46:52 +020020924 if (check_secure())
20925 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020926 if (argvars[2].v_type != VAR_UNKNOWN)
20927 {
20928 if (argvars[2].v_type != VAR_DICT
20929 || (dict = argvars[2].vval.v_dict) == NULL)
20930 {
20931 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20932 return;
20933 }
20934 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20935 repeat = get_dict_number(dict, (char_u *)"repeat");
20936 }
20937
20938 timer = create_timer(msec, repeat);
20939 callback = get_callback(&argvars[1], &timer->tr_partial);
20940 if (callback == NULL)
20941 {
20942 stop_timer(timer);
20943 rettv->vval.v_number = -1;
20944 }
20945 else
20946 {
20947 timer->tr_callback = vim_strsave(callback);
20948 rettv->vval.v_number = timer->tr_id;
20949 }
20950}
20951
20952/*
20953 * "timer_stop(timer)" function
20954 */
20955 static void
20956f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20957{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020958 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020959
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020960 if (argvars[0].v_type != VAR_NUMBER)
20961 {
20962 EMSG(_(e_number_exp));
20963 return;
20964 }
20965 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020966 if (timer != NULL)
20967 stop_timer(timer);
20968}
20969#endif
20970
Bram Moolenaard52d9742005-08-21 22:20:28 +000020971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 * "tolower(string)" function
20973 */
20974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020975f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976{
20977 char_u *p;
20978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020979 p = vim_strsave(get_tv_string(&argvars[0]));
20980 rettv->v_type = VAR_STRING;
20981 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982
20983 if (p != NULL)
20984 while (*p != NUL)
20985 {
20986#ifdef FEAT_MBYTE
20987 int l;
20988
20989 if (enc_utf8)
20990 {
20991 int c, lc;
20992
20993 c = utf_ptr2char(p);
20994 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020995 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 /* TODO: reallocate string when byte count changes. */
20997 if (utf_char2len(lc) == l)
20998 utf_char2bytes(lc, p);
20999 p += l;
21000 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021001 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 p += l; /* skip multi-byte character */
21003 else
21004#endif
21005 {
21006 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21007 ++p;
21008 }
21009 }
21010}
21011
21012/*
21013 * "toupper(string)" function
21014 */
21015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021016f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021018 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021019 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020}
21021
21022/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021023 * "tr(string, fromstr, tostr)" function
21024 */
21025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021026f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021027{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021028 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021029 char_u *fromstr;
21030 char_u *tostr;
21031 char_u *p;
21032#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021033 int inlen;
21034 int fromlen;
21035 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021036 int idx;
21037 char_u *cpstr;
21038 int cplen;
21039 int first = TRUE;
21040#endif
21041 char_u buf[NUMBUFLEN];
21042 char_u buf2[NUMBUFLEN];
21043 garray_T ga;
21044
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021045 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021046 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21047 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021048
21049 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021050 rettv->v_type = VAR_STRING;
21051 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021052 if (fromstr == NULL || tostr == NULL)
21053 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021054 ga_init2(&ga, (int)sizeof(char), 80);
21055
21056#ifdef FEAT_MBYTE
21057 if (!has_mbyte)
21058#endif
21059 /* not multi-byte: fromstr and tostr must be the same length */
21060 if (STRLEN(fromstr) != STRLEN(tostr))
21061 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021062#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021063error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021064#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021065 EMSG2(_(e_invarg2), fromstr);
21066 ga_clear(&ga);
21067 return;
21068 }
21069
21070 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021071 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021072 {
21073#ifdef FEAT_MBYTE
21074 if (has_mbyte)
21075 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021076 inlen = (*mb_ptr2len)(in_str);
21077 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021078 cplen = inlen;
21079 idx = 0;
21080 for (p = fromstr; *p != NUL; p += fromlen)
21081 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021082 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021083 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021084 {
21085 for (p = tostr; *p != NUL; p += tolen)
21086 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021087 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021088 if (idx-- == 0)
21089 {
21090 cplen = tolen;
21091 cpstr = p;
21092 break;
21093 }
21094 }
21095 if (*p == NUL) /* tostr is shorter than fromstr */
21096 goto error;
21097 break;
21098 }
21099 ++idx;
21100 }
21101
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021102 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021103 {
21104 /* Check that fromstr and tostr have the same number of
21105 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021106 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021107 first = FALSE;
21108 for (p = tostr; *p != NUL; p += tolen)
21109 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021110 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021111 --idx;
21112 }
21113 if (idx != 0)
21114 goto error;
21115 }
21116
Bram Moolenaarcde88542015-08-11 19:14:00 +020021117 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021118 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021119 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021120
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021121 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021122 }
21123 else
21124#endif
21125 {
21126 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021127 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021128 if (p != NULL)
21129 ga_append(&ga, tostr[p - fromstr]);
21130 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021131 ga_append(&ga, *in_str);
21132 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021133 }
21134 }
21135
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021136 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021137 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021138 ga_append(&ga, NUL);
21139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021141}
21142
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021143#ifdef FEAT_FLOAT
21144/*
21145 * "trunc({float})" function
21146 */
21147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021148f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021149{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021150 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021151
21152 rettv->v_type = VAR_FLOAT;
21153 if (get_float_arg(argvars, &f) == OK)
21154 /* trunc() is not in C90, use floor() or ceil() instead. */
21155 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21156 else
21157 rettv->vval.v_float = 0.0;
21158}
21159#endif
21160
Bram Moolenaar8299df92004-07-10 09:47:34 +000021161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 * "type(expr)" function
21163 */
21164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021165f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021167 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021168
21169 switch (argvars[0].v_type)
21170 {
21171 case VAR_NUMBER: n = 0; break;
21172 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021173 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021174 case VAR_FUNC: n = 2; break;
21175 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021176 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021177 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021178 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021179 if (argvars[0].vval.v_number == VVAL_FALSE
21180 || argvars[0].vval.v_number == VVAL_TRUE)
21181 n = 6;
21182 else
21183 n = 7;
21184 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021185 case VAR_JOB: n = 8; break;
21186 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021187 case VAR_UNKNOWN:
21188 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21189 n = -1;
21190 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021191 }
21192 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193}
21194
21195/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021196 * "undofile(name)" function
21197 */
21198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021199f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021200{
21201 rettv->v_type = VAR_STRING;
21202#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021203 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021204 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021205
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021206 if (*fname == NUL)
21207 {
21208 /* If there is no file name there will be no undo file. */
21209 rettv->vval.v_string = NULL;
21210 }
21211 else
21212 {
21213 char_u *ffname = FullName_save(fname, FALSE);
21214
21215 if (ffname != NULL)
21216 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21217 vim_free(ffname);
21218 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021219 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021220#else
21221 rettv->vval.v_string = NULL;
21222#endif
21223}
21224
21225/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021226 * "undotree()" function
21227 */
21228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021229f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021230{
21231 if (rettv_dict_alloc(rettv) == OK)
21232 {
21233 dict_T *dict = rettv->vval.v_dict;
21234 list_T *list;
21235
Bram Moolenaar730cde92010-06-27 05:18:54 +020021236 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021237 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021238 dict_add_nr_str(dict, "save_last",
21239 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021240 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21241 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021242 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021243
21244 list = list_alloc();
21245 if (list != NULL)
21246 {
21247 u_eval_tree(curbuf->b_u_oldhead, list);
21248 dict_add_list(dict, "entries", list);
21249 }
21250 }
21251}
21252
21253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021254 * "values(dict)" function
21255 */
21256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021257f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021258{
21259 dict_list(argvars, rettv, 1);
21260}
21261
21262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 * "virtcol(string)" function
21264 */
21265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021266f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267{
21268 colnr_T vcol = 0;
21269 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021270 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021272 fp = var2fpos(&argvars[0], FALSE, &fnum);
21273 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21274 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 {
21276 getvvcol(curwin, fp, NULL, NULL, &vcol);
21277 ++vcol;
21278 }
21279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021280 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281}
21282
21283/*
21284 * "visualmode()" function
21285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021287f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 char_u str[2];
21290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021291 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 str[0] = curbuf->b_visual_mode_eval;
21293 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021294 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295
21296 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021297 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299}
21300
21301/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021302 * "wildmenumode()" function
21303 */
21304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021305f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021306{
21307#ifdef FEAT_WILDMENU
21308 if (wild_menu_showing)
21309 rettv->vval.v_number = 1;
21310#endif
21311}
21312
21313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 * "winbufnr(nr)" function
21315 */
21316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021317f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318{
21319 win_T *wp;
21320
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021321 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021323 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021325 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326}
21327
21328/*
21329 * "wincol()" function
21330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021332f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333{
21334 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021335 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336}
21337
21338/*
21339 * "winheight(nr)" function
21340 */
21341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021342f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343{
21344 win_T *wp;
21345
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021346 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021348 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021350 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351}
21352
21353/*
21354 * "winline()" function
21355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021357f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358{
21359 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361}
21362
21363/*
21364 * "winnr()" function
21365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368{
21369 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021370
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021372 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021374 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375}
21376
21377/*
21378 * "winrestcmd()" function
21379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021381f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382{
21383#ifdef FEAT_WINDOWS
21384 win_T *wp;
21385 int winnr = 1;
21386 garray_T ga;
21387 char_u buf[50];
21388
21389 ga_init2(&ga, (int)sizeof(char), 70);
21390 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21391 {
21392 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21393 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21395 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 ++winnr;
21397 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021398 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021400 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405}
21406
21407/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021408 * "winrestview()" function
21409 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021411f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021412{
21413 dict_T *dict;
21414
21415 if (argvars[0].v_type != VAR_DICT
21416 || (dict = argvars[0].vval.v_dict) == NULL)
21417 EMSG(_(e_invarg));
21418 else
21419 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021420 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21421 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21422 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21423 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021424#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021425 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21426 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021427#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021428 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21429 {
21430 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21431 curwin->w_set_curswant = FALSE;
21432 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021433
Bram Moolenaar82c25852014-05-28 16:47:16 +020021434 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21435 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021436#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021437 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21438 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021439#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021440 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21441 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21442 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21443 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021444
21445 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021446 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021447# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021448 win_new_width(curwin, W_WIDTH(curwin));
21449# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021450 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021451
Bram Moolenaarb851a962014-10-31 15:45:52 +010021452 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021453 curwin->w_topline = 1;
21454 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21455 curwin->w_topline = curbuf->b_ml.ml_line_count;
21456#ifdef FEAT_DIFF
21457 check_topfill(curwin, TRUE);
21458#endif
21459 }
21460}
21461
21462/*
21463 * "winsaveview()" function
21464 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021466f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021467{
21468 dict_T *dict;
21469
Bram Moolenaara800b422010-06-27 01:15:55 +020021470 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021471 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021472 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021473
21474 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21475 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21476#ifdef FEAT_VIRTUALEDIT
21477 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21478#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021479 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021480 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21481
21482 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21483#ifdef FEAT_DIFF
21484 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21485#endif
21486 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21487 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21488}
21489
21490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 * "winwidth(nr)" function
21492 */
21493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021494f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495{
21496 win_T *wp;
21497
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021498 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021502#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021503 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021505 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506#endif
21507}
21508
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021510 * "wordcount()" function
21511 */
21512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021513f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021514{
21515 if (rettv_dict_alloc(rettv) == FAIL)
21516 return;
21517 cursor_pos_info(rettv->vval.v_dict);
21518}
21519
21520/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021521 * Write list of strings to file
21522 */
21523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021524write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021525{
21526 listitem_T *li;
21527 int c;
21528 int ret = OK;
21529 char_u *s;
21530
21531 for (li = list->lv_first; li != NULL; li = li->li_next)
21532 {
21533 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21534 {
21535 if (*s == '\n')
21536 c = putc(NUL, fd);
21537 else
21538 c = putc(*s, fd);
21539 if (c == EOF)
21540 {
21541 ret = FAIL;
21542 break;
21543 }
21544 }
21545 if (!binary || li->li_next != NULL)
21546 if (putc('\n', fd) == EOF)
21547 {
21548 ret = FAIL;
21549 break;
21550 }
21551 if (ret == FAIL)
21552 {
21553 EMSG(_(e_write));
21554 break;
21555 }
21556 }
21557 return ret;
21558}
21559
21560/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021561 * "writefile()" function
21562 */
21563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021564f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021565{
21566 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021567 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021568 char_u *fname;
21569 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021570 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021571
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021572 if (check_restricted() || check_secure())
21573 return;
21574
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021575 if (argvars[0].v_type != VAR_LIST)
21576 {
21577 EMSG2(_(e_listarg), "writefile()");
21578 return;
21579 }
21580 if (argvars[0].vval.v_list == NULL)
21581 return;
21582
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021583 if (argvars[2].v_type != VAR_UNKNOWN)
21584 {
21585 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21586 binary = TRUE;
21587 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21588 append = TRUE;
21589 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021590
21591 /* Always open the file in binary mode, library functions have a mind of
21592 * their own about CR-LF conversion. */
21593 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021594 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21595 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021596 {
21597 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21598 ret = -1;
21599 }
21600 else
21601 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021602 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21603 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021604 fclose(fd);
21605 }
21606
21607 rettv->vval.v_number = ret;
21608}
21609
21610/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021611 * "xor(expr, expr)" function
21612 */
21613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021614f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021615{
21616 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21617 ^ get_tv_number_chk(&argvars[1], NULL);
21618}
21619
21620
21621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021623 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 */
21625 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021626var2fpos(
21627 typval_T *varp,
21628 int dollar_lnum, /* TRUE when $ is last line */
21629 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021631 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021633 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634
Bram Moolenaara5525202006-03-02 22:52:09 +000021635 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021636 if (varp->v_type == VAR_LIST)
21637 {
21638 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021639 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021640 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021641 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021642
21643 l = varp->vval.v_list;
21644 if (l == NULL)
21645 return NULL;
21646
21647 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021648 pos.lnum = list_find_nr(l, 0L, &error);
21649 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021650 return NULL; /* invalid line number */
21651
21652 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021653 pos.col = list_find_nr(l, 1L, &error);
21654 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021655 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021656 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021657
21658 /* We accept "$" for the column number: last column. */
21659 li = list_find(l, 1L);
21660 if (li != NULL && li->li_tv.v_type == VAR_STRING
21661 && li->li_tv.vval.v_string != NULL
21662 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21663 pos.col = len + 1;
21664
Bram Moolenaara5525202006-03-02 22:52:09 +000021665 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021666 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021667 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021668 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021669
Bram Moolenaara5525202006-03-02 22:52:09 +000021670#ifdef FEAT_VIRTUALEDIT
21671 /* Get the virtual offset. Defaults to zero. */
21672 pos.coladd = list_find_nr(l, 2L, &error);
21673 if (error)
21674 pos.coladd = 0;
21675#endif
21676
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021677 return &pos;
21678 }
21679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021680 name = get_tv_string_chk(varp);
21681 if (name == NULL)
21682 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021683 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021685 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21686 {
21687 if (VIsual_active)
21688 return &VIsual;
21689 return &curwin->w_cursor;
21690 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021691 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021693 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21695 return NULL;
21696 return pp;
21697 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021698
21699#ifdef FEAT_VIRTUALEDIT
21700 pos.coladd = 0;
21701#endif
21702
Bram Moolenaar477933c2007-07-17 14:32:23 +000021703 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021704 {
21705 pos.col = 0;
21706 if (name[1] == '0') /* "w0": first visible line */
21707 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021708 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021709 pos.lnum = curwin->w_topline;
21710 return &pos;
21711 }
21712 else if (name[1] == '$') /* "w$": last visible line */
21713 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021714 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021715 pos.lnum = curwin->w_botline - 1;
21716 return &pos;
21717 }
21718 }
21719 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021721 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021722 {
21723 pos.lnum = curbuf->b_ml.ml_line_count;
21724 pos.col = 0;
21725 }
21726 else
21727 {
21728 pos.lnum = curwin->w_cursor.lnum;
21729 pos.col = (colnr_T)STRLEN(ml_get_curline());
21730 }
21731 return &pos;
21732 }
21733 return NULL;
21734}
21735
21736/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021737 * Convert list in "arg" into a position and optional file number.
21738 * When "fnump" is NULL there is no file number, only 3 items.
21739 * Note that the column is passed on as-is, the caller may want to decrement
21740 * it to use 1 for the first column.
21741 * Return FAIL when conversion is not possible, doesn't check the position for
21742 * validity.
21743 */
21744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021745list2fpos(
21746 typval_T *arg,
21747 pos_T *posp,
21748 int *fnump,
21749 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021750{
21751 list_T *l = arg->vval.v_list;
21752 long i = 0;
21753 long n;
21754
Bram Moolenaar493c1782014-05-28 14:34:46 +020021755 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21756 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021757 if (arg->v_type != VAR_LIST
21758 || l == NULL
21759 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021760 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021761 return FAIL;
21762
21763 if (fnump != NULL)
21764 {
21765 n = list_find_nr(l, i++, NULL); /* fnum */
21766 if (n < 0)
21767 return FAIL;
21768 if (n == 0)
21769 n = curbuf->b_fnum; /* current buffer */
21770 *fnump = n;
21771 }
21772
21773 n = list_find_nr(l, i++, NULL); /* lnum */
21774 if (n < 0)
21775 return FAIL;
21776 posp->lnum = n;
21777
21778 n = list_find_nr(l, i++, NULL); /* col */
21779 if (n < 0)
21780 return FAIL;
21781 posp->col = n;
21782
21783#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021784 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021785 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021786 posp->coladd = 0;
21787 else
21788 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021789#endif
21790
Bram Moolenaar493c1782014-05-28 14:34:46 +020021791 if (curswantp != NULL)
21792 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21793
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021794 return OK;
21795}
21796
21797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 * Get the length of an environment variable name.
21799 * Advance "arg" to the first character after the name.
21800 * Return 0 for error.
21801 */
21802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021803get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804{
21805 char_u *p;
21806 int len;
21807
21808 for (p = *arg; vim_isIDc(*p); ++p)
21809 ;
21810 if (p == *arg) /* no name found */
21811 return 0;
21812
21813 len = (int)(p - *arg);
21814 *arg = p;
21815 return len;
21816}
21817
21818/*
21819 * Get the length of the name of a function or internal variable.
21820 * "arg" is advanced to the first non-white character after the name.
21821 * Return 0 if something is wrong.
21822 */
21823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021824get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825{
21826 char_u *p;
21827 int len;
21828
21829 /* Find the end of the name. */
21830 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021831 {
21832 if (*p == ':')
21833 {
21834 /* "s:" is start of "s:var", but "n:" is not and can be used in
21835 * slice "[n:]". Also "xx:" is not a namespace. */
21836 len = (int)(p - *arg);
21837 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21838 || len > 1)
21839 break;
21840 }
21841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 if (p == *arg) /* no name found */
21843 return 0;
21844
21845 len = (int)(p - *arg);
21846 *arg = skipwhite(p);
21847
21848 return len;
21849}
21850
21851/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021852 * Get the length of the name of a variable or function.
21853 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021855 * Return -1 if curly braces expansion failed.
21856 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 * If the name contains 'magic' {}'s, expand them and return the
21858 * expanded name in an allocated string via 'alias' - caller must free.
21859 */
21860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021861get_name_len(
21862 char_u **arg,
21863 char_u **alias,
21864 int evaluate,
21865 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866{
21867 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021868 char_u *p;
21869 char_u *expr_start;
21870 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871
21872 *alias = NULL; /* default to no alias */
21873
21874 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21875 && (*arg)[2] == (int)KE_SNR)
21876 {
21877 /* hard coded <SNR>, already translated */
21878 *arg += 3;
21879 return get_id_len(arg) + 3;
21880 }
21881 len = eval_fname_script(*arg);
21882 if (len > 0)
21883 {
21884 /* literal "<SID>", "s:" or "<SNR>" */
21885 *arg += len;
21886 }
21887
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021889 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021891 p = find_name_end(*arg, &expr_start, &expr_end,
21892 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 if (expr_start != NULL)
21894 {
21895 char_u *temp_string;
21896
21897 if (!evaluate)
21898 {
21899 len += (int)(p - *arg);
21900 *arg = skipwhite(p);
21901 return len;
21902 }
21903
21904 /*
21905 * Include any <SID> etc in the expanded string:
21906 * Thus the -len here.
21907 */
21908 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21909 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021910 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 *alias = temp_string;
21912 *arg = skipwhite(p);
21913 return (int)STRLEN(temp_string);
21914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915
21916 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021917 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918 EMSG2(_(e_invexpr2), *arg);
21919
21920 return len;
21921}
21922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021923/*
21924 * Find the end of a variable or function name, taking care of magic braces.
21925 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21926 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021927 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021928 * Return a pointer to just after the name. Equal to "arg" if there is no
21929 * valid name.
21930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932find_name_end(
21933 char_u *arg,
21934 char_u **expr_start,
21935 char_u **expr_end,
21936 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021938 int mb_nest = 0;
21939 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021941 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021943 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021945 *expr_start = NULL;
21946 *expr_end = NULL;
21947 }
21948
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021949 /* Quick check for valid starting character. */
21950 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21951 return arg;
21952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021953 for (p = arg; *p != NUL
21954 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021955 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021956 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021957 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021958 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021959 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021960 if (*p == '\'')
21961 {
21962 /* skip over 'string' to avoid counting [ and ] inside it. */
21963 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21964 ;
21965 if (*p == NUL)
21966 break;
21967 }
21968 else if (*p == '"')
21969 {
21970 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21971 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21972 if (*p == '\\' && p[1] != NUL)
21973 ++p;
21974 if (*p == NUL)
21975 break;
21976 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021977 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21978 {
21979 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021980 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021981 len = (int)(p - arg);
21982 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021983 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021984 break;
21985 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021987 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021989 if (*p == '[')
21990 ++br_nest;
21991 else if (*p == ']')
21992 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021995 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021997 if (*p == '{')
21998 {
21999 mb_nest++;
22000 if (expr_start != NULL && *expr_start == NULL)
22001 *expr_start = p;
22002 }
22003 else if (*p == '}')
22004 {
22005 mb_nest--;
22006 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22007 *expr_end = p;
22008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 }
22011
22012 return p;
22013}
22014
22015/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022016 * Expands out the 'magic' {}'s in a variable/function name.
22017 * Note that this can call itself recursively, to deal with
22018 * constructs like foo{bar}{baz}{bam}
22019 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22020 * "in_start" ^
22021 * "expr_start" ^
22022 * "expr_end" ^
22023 * "in_end" ^
22024 *
22025 * Returns a new allocated string, which the caller must free.
22026 * Returns NULL for failure.
22027 */
22028 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022029make_expanded_name(
22030 char_u *in_start,
22031 char_u *expr_start,
22032 char_u *expr_end,
22033 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022034{
22035 char_u c1;
22036 char_u *retval = NULL;
22037 char_u *temp_result;
22038 char_u *nextcmd = NULL;
22039
22040 if (expr_end == NULL || in_end == NULL)
22041 return NULL;
22042 *expr_start = NUL;
22043 *expr_end = NUL;
22044 c1 = *in_end;
22045 *in_end = NUL;
22046
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022047 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022048 if (temp_result != NULL && nextcmd == NULL)
22049 {
22050 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22051 + (in_end - expr_end) + 1));
22052 if (retval != NULL)
22053 {
22054 STRCPY(retval, in_start);
22055 STRCAT(retval, temp_result);
22056 STRCAT(retval, expr_end + 1);
22057 }
22058 }
22059 vim_free(temp_result);
22060
22061 *in_end = c1; /* put char back for error messages */
22062 *expr_start = '{';
22063 *expr_end = '}';
22064
22065 if (retval != NULL)
22066 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022067 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022068 if (expr_start != NULL)
22069 {
22070 /* Further expansion! */
22071 temp_result = make_expanded_name(retval, expr_start,
22072 expr_end, temp_result);
22073 vim_free(retval);
22074 retval = temp_result;
22075 }
22076 }
22077
22078 return retval;
22079}
22080
22081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022083 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 */
22085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022086eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022088 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22089}
22090
22091/*
22092 * Return TRUE if character "c" can be used as the first character in a
22093 * variable or function name (excluding '{' and '}').
22094 */
22095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022096eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022097{
22098 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099}
22100
22101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 * Set number v: variable to "val".
22103 */
22104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022105set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022107 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108}
22109
22110/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022111 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 */
22113 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022114get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022116 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117}
22118
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022119/*
22120 * Get string v: variable value. Uses a static buffer, can only be used once.
22121 */
22122 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022123get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022124{
22125 return get_tv_string(&vimvars[idx].vv_tv);
22126}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022127
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022129 * Get List v: variable value. Caller must take care of reference count when
22130 * needed.
22131 */
22132 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022133get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022134{
22135 return vimvars[idx].vv_list;
22136}
22137
22138/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022139 * Set v:char to character "c".
22140 */
22141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022142set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022143{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022144 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022145
22146#ifdef FEAT_MBYTE
22147 if (has_mbyte)
22148 buf[(*mb_char2bytes)(c, buf)] = NUL;
22149 else
22150#endif
22151 {
22152 buf[0] = c;
22153 buf[1] = NUL;
22154 }
22155 set_vim_var_string(VV_CHAR, buf, -1);
22156}
22157
22158/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022159 * Set v:count to "count" and v:count1 to "count1".
22160 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 */
22162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022163set_vcount(
22164 long count,
22165 long count1,
22166 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022168 if (set_prevcount)
22169 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022170 vimvars[VV_COUNT].vv_nr = count;
22171 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172}
22173
22174/*
22175 * Set string v: variable to a copy of "val".
22176 */
22177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022178set_vim_var_string(
22179 int idx,
22180 char_u *val,
22181 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182{
Bram Moolenaara542c682016-01-31 16:28:04 +010022183 clear_tv(&vimvars[idx].vv_di.di_tv);
22184 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022186 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022188 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022190 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191}
22192
22193/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022194 * Set List v: variable to "val".
22195 */
22196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022197set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022198{
Bram Moolenaara542c682016-01-31 16:28:04 +010022199 clear_tv(&vimvars[idx].vv_di.di_tv);
22200 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022201 vimvars[idx].vv_list = val;
22202 if (val != NULL)
22203 ++val->lv_refcount;
22204}
22205
22206/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022207 * Set Dictionary v: variable to "val".
22208 */
22209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022210set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022211{
22212 int todo;
22213 hashitem_T *hi;
22214
Bram Moolenaara542c682016-01-31 16:28:04 +010022215 clear_tv(&vimvars[idx].vv_di.di_tv);
22216 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022217 vimvars[idx].vv_dict = val;
22218 if (val != NULL)
22219 {
22220 ++val->dv_refcount;
22221
22222 /* Set readonly */
22223 todo = (int)val->dv_hashtab.ht_used;
22224 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22225 {
22226 if (HASHITEM_EMPTY(hi))
22227 continue;
22228 --todo;
22229 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22230 }
22231 }
22232}
22233
22234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 * Set v:register if needed.
22236 */
22237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022238set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239{
22240 char_u regname;
22241
22242 if (c == 0 || c == ' ')
22243 regname = '"';
22244 else
22245 regname = c;
22246 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022247 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 set_vim_var_string(VV_REG, &regname, 1);
22249}
22250
22251/*
22252 * Get or set v:exception. If "oldval" == NULL, return the current value.
22253 * Otherwise, restore the value to "oldval" and return NULL.
22254 * Must always be called in pairs to save and restore v:exception! Does not
22255 * take care of memory allocations.
22256 */
22257 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022258v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259{
22260 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262
Bram Moolenaare9a41262005-01-15 22:18:47 +000022263 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 return NULL;
22265}
22266
22267/*
22268 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22269 * Otherwise, restore the value to "oldval" and return NULL.
22270 * Must always be called in pairs to save and restore v:throwpoint! Does not
22271 * take care of memory allocations.
22272 */
22273 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022274v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275{
22276 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022277 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278
Bram Moolenaare9a41262005-01-15 22:18:47 +000022279 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 return NULL;
22281}
22282
22283#if defined(FEAT_AUTOCMD) || defined(PROTO)
22284/*
22285 * Set v:cmdarg.
22286 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22287 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22288 * Must always be called in pairs!
22289 */
22290 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022291set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292{
22293 char_u *oldval;
22294 char_u *newval;
22295 unsigned len;
22296
Bram Moolenaare9a41262005-01-15 22:18:47 +000022297 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022298 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022300 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022301 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022302 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303 }
22304
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022305 if (eap->force_bin == FORCE_BIN)
22306 len = 6;
22307 else if (eap->force_bin == FORCE_NOBIN)
22308 len = 8;
22309 else
22310 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022311
22312 if (eap->read_edit)
22313 len += 7;
22314
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022315 if (eap->force_ff != 0)
22316 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22317# ifdef FEAT_MBYTE
22318 if (eap->force_enc != 0)
22319 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022320 if (eap->bad_char != 0)
22321 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022322# endif
22323
22324 newval = alloc(len + 1);
22325 if (newval == NULL)
22326 return NULL;
22327
22328 if (eap->force_bin == FORCE_BIN)
22329 sprintf((char *)newval, " ++bin");
22330 else if (eap->force_bin == FORCE_NOBIN)
22331 sprintf((char *)newval, " ++nobin");
22332 else
22333 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022334
22335 if (eap->read_edit)
22336 STRCAT(newval, " ++edit");
22337
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022338 if (eap->force_ff != 0)
22339 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22340 eap->cmd + eap->force_ff);
22341# ifdef FEAT_MBYTE
22342 if (eap->force_enc != 0)
22343 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22344 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022345 if (eap->bad_char == BAD_KEEP)
22346 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22347 else if (eap->bad_char == BAD_DROP)
22348 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22349 else if (eap->bad_char != 0)
22350 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022351# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022352 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022353 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354}
22355#endif
22356
22357/*
22358 * Get the value of internal variable "name".
22359 * Return OK or FAIL.
22360 */
22361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022362get_var_tv(
22363 char_u *name,
22364 int len, /* length of "name" */
22365 typval_T *rettv, /* NULL when only checking existence */
22366 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22367 int verbose, /* may give error message */
22368 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369{
22370 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 typval_T *tv = NULL;
22372 typval_T atv;
22373 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375
22376 /* truncate the name, so that we can use strcmp() */
22377 cc = name[len];
22378 name[len] = NUL;
22379
22380 /*
22381 * Check for "b:changedtick".
22382 */
22383 if (STRCMP(name, "b:changedtick") == 0)
22384 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022385 atv.v_type = VAR_NUMBER;
22386 atv.vval.v_number = curbuf->b_changedtick;
22387 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 }
22389
22390 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 * Check for user-defined variables.
22392 */
22393 else
22394 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022395 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022397 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022398 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022399 if (dip != NULL)
22400 *dip = v;
22401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 }
22403
Bram Moolenaare9a41262005-01-15 22:18:47 +000022404 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022406 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022407 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 ret = FAIL;
22409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022410 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022411 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412
22413 name[len] = cc;
22414
22415 return ret;
22416}
22417
22418/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022419 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22420 * Also handle function call with Funcref variable: func(expr)
22421 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22422 */
22423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022424handle_subscript(
22425 char_u **arg,
22426 typval_T *rettv,
22427 int evaluate, /* do more than finding the end */
22428 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022429{
22430 int ret = OK;
22431 dict_T *selfdict = NULL;
22432 char_u *s;
22433 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022434 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022435
22436 while (ret == OK
22437 && (**arg == '['
22438 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022439 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22440 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022441 && !vim_iswhite(*(*arg - 1)))
22442 {
22443 if (**arg == '(')
22444 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022445 partial_T *pt = NULL;
22446
Bram Moolenaard9fba312005-06-26 22:34:35 +000022447 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022448 if (evaluate)
22449 {
22450 functv = *rettv;
22451 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022452
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022453 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022454 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022455 {
22456 pt = functv.vval.v_partial;
22457 s = pt->pt_name;
22458 }
22459 else
22460 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022461 }
22462 else
22463 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022464 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022465 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022466 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022467
22468 /* Clear the funcref afterwards, so that deleting it while
22469 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022470 if (evaluate)
22471 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022472
22473 /* Stop the expression evaluation when immediately aborting on
22474 * error, or when an interrupt occurred or an exception was thrown
22475 * but not caught. */
22476 if (aborting())
22477 {
22478 if (ret == OK)
22479 clear_tv(rettv);
22480 ret = FAIL;
22481 }
22482 dict_unref(selfdict);
22483 selfdict = NULL;
22484 }
22485 else /* **arg == '[' || **arg == '.' */
22486 {
22487 dict_unref(selfdict);
22488 if (rettv->v_type == VAR_DICT)
22489 {
22490 selfdict = rettv->vval.v_dict;
22491 if (selfdict != NULL)
22492 ++selfdict->dv_refcount;
22493 }
22494 else
22495 selfdict = NULL;
22496 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22497 {
22498 clear_tv(rettv);
22499 ret = FAIL;
22500 }
22501 }
22502 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022503
Bram Moolenaar1d429612016-05-24 15:44:17 +020022504 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22505 * Don't do this when "Func" is already a partial that was bound
22506 * explicitly (pt_auto is FALSE). */
22507 if (selfdict != NULL
22508 && (rettv->v_type == VAR_FUNC
22509 || (rettv->v_type == VAR_PARTIAL
22510 && (rettv->vval.v_partial->pt_auto
22511 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022512 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022513 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22514 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022515 char_u *tofree = NULL;
22516 ufunc_T *fp;
22517 char_u fname_buf[FLEN_FIXED + 1];
22518 int error;
22519
22520 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022521 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022522 fp = find_func(fname);
22523 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022524
Bram Moolenaar65639032016-03-16 21:40:30 +010022525 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022526 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022527 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22528
22529 if (pt != NULL)
22530 {
22531 pt->pt_refcount = 1;
22532 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022533 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022534 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022535 if (rettv->v_type == VAR_FUNC)
22536 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022537 /* Just a function: Take over the function name and use
22538 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022539 pt->pt_name = rettv->vval.v_string;
22540 }
22541 else
22542 {
22543 partial_T *ret_pt = rettv->vval.v_partial;
22544 int i;
22545
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022546 /* Partial: copy the function name, use selfdict and copy
22547 * args. Can't take over name or args, the partial might
22548 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022549 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022550 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022551 if (ret_pt->pt_argc > 0)
22552 {
22553 pt->pt_argv = (typval_T *)alloc(
22554 sizeof(typval_T) * ret_pt->pt_argc);
22555 if (pt->pt_argv == NULL)
22556 /* out of memory: drop the arguments */
22557 pt->pt_argc = 0;
22558 else
22559 {
22560 pt->pt_argc = ret_pt->pt_argc;
22561 for (i = 0; i < pt->pt_argc; i++)
22562 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22563 }
22564 }
22565 partial_unref(ret_pt);
22566 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022567 rettv->v_type = VAR_PARTIAL;
22568 rettv->vval.v_partial = pt;
22569 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022570 }
22571 }
22572
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022573 dict_unref(selfdict);
22574 return ret;
22575}
22576
22577/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022578 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022579 * value).
22580 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022581 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022582alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022583{
Bram Moolenaar33570922005-01-25 22:26:29 +000022584 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022585}
22586
22587/*
22588 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 * The string "s" must have been allocated, it is consumed.
22590 * Return NULL for out of memory, the variable otherwise.
22591 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022593alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594{
Bram Moolenaar33570922005-01-25 22:26:29 +000022595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022597 rettv = alloc_tv();
22598 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022600 rettv->v_type = VAR_STRING;
22601 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 }
22603 else
22604 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022605 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606}
22607
22608/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022609 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022612free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613{
22614 if (varp != NULL)
22615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 switch (varp->v_type)
22617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022619 func_unref(varp->vval.v_string);
22620 /*FALLTHROUGH*/
22621 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022622 vim_free(varp->vval.v_string);
22623 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022624 case VAR_PARTIAL:
22625 partial_unref(varp->vval.v_partial);
22626 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022627 case VAR_LIST:
22628 list_unref(varp->vval.v_list);
22629 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022630 case VAR_DICT:
22631 dict_unref(varp->vval.v_dict);
22632 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022633 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022634#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022635 job_unref(varp->vval.v_job);
22636 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022637#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022638 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022639#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022640 channel_unref(varp->vval.v_channel);
22641 break;
22642#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022643 case VAR_NUMBER:
22644 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022645 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022646 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649 vim_free(varp);
22650 }
22651}
22652
22653/*
22654 * Free the memory for a variable value and set the value to NULL or 0.
22655 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022657clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658{
22659 if (varp != NULL)
22660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022661 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022663 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022664 func_unref(varp->vval.v_string);
22665 /*FALLTHROUGH*/
22666 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022667 vim_free(varp->vval.v_string);
22668 varp->vval.v_string = NULL;
22669 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022670 case VAR_PARTIAL:
22671 partial_unref(varp->vval.v_partial);
22672 varp->vval.v_partial = NULL;
22673 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022674 case VAR_LIST:
22675 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022676 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022677 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022678 case VAR_DICT:
22679 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022680 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022681 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022682 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022683 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022684 varp->vval.v_number = 0;
22685 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022686 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022687#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022688 varp->vval.v_float = 0.0;
22689 break;
22690#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022691 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022692#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022693 job_unref(varp->vval.v_job);
22694 varp->vval.v_job = NULL;
22695#endif
22696 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022697 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022698#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022699 channel_unref(varp->vval.v_channel);
22700 varp->vval.v_channel = NULL;
22701#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022702 case VAR_UNKNOWN:
22703 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022705 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 }
22707}
22708
22709/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022710 * Set the value of a variable to NULL without freeing items.
22711 */
22712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022713init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022714{
22715 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022716 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022717}
22718
22719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 * Get the number value of a variable.
22721 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022722 * For incompatible types, return 0.
22723 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22724 * caller of incompatible types: it sets *denote to TRUE if "denote"
22725 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022727 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022728get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022730 int error = FALSE;
22731
22732 return get_tv_number_chk(varp, &error); /* return 0L on error */
22733}
22734
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022735 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022736get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022737{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022738 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022740 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022742 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022743 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022744 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022745#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022746 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022747 break;
22748#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022749 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022750 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022751 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022752 break;
22753 case VAR_STRING:
22754 if (varp->vval.v_string != NULL)
22755 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022756 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022757 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022758 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022759 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022760 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022761 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022762 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022763 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022764 case VAR_SPECIAL:
22765 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22766 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022767 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022768#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022769 EMSG(_("E910: Using a Job as a Number"));
22770 break;
22771#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022772 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022773#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022774 EMSG(_("E913: Using a Channel as a Number"));
22775 break;
22776#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022777 case VAR_UNKNOWN:
22778 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022779 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022781 if (denote == NULL) /* useful for values that must be unsigned */
22782 n = -1;
22783 else
22784 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022785 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786}
22787
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022788#ifdef FEAT_FLOAT
22789 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022790get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022791{
22792 switch (varp->v_type)
22793 {
22794 case VAR_NUMBER:
22795 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022796 case VAR_FLOAT:
22797 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022798 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022799 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022800 EMSG(_("E891: Using a Funcref as a Float"));
22801 break;
22802 case VAR_STRING:
22803 EMSG(_("E892: Using a String as a Float"));
22804 break;
22805 case VAR_LIST:
22806 EMSG(_("E893: Using a List as a Float"));
22807 break;
22808 case VAR_DICT:
22809 EMSG(_("E894: Using a Dictionary as a Float"));
22810 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022811 case VAR_SPECIAL:
22812 EMSG(_("E907: Using a special value as a Float"));
22813 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022814 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022815# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022816 EMSG(_("E911: Using a Job as a Float"));
22817 break;
22818# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022819 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022820# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022821 EMSG(_("E914: Using a Channel as a Float"));
22822 break;
22823# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022824 case VAR_UNKNOWN:
22825 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022826 break;
22827 }
22828 return 0;
22829}
22830#endif
22831
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022833 * Get the lnum from the first argument.
22834 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022835 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 */
22837 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022838get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839{
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 linenr_T lnum;
22842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022843 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 if (lnum == 0) /* no valid number, try using line() */
22845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022846 rettv.v_type = VAR_NUMBER;
22847 f_line(argvars, &rettv);
22848 lnum = rettv.vval.v_number;
22849 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850 }
22851 return lnum;
22852}
22853
22854/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022855 * Get the lnum from the first argument.
22856 * Also accepts "$", then "buf" is used.
22857 * Returns 0 on error.
22858 */
22859 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022860get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022861{
22862 if (argvars[0].v_type == VAR_STRING
22863 && argvars[0].vval.v_string != NULL
22864 && argvars[0].vval.v_string[0] == '$'
22865 && buf != NULL)
22866 return buf->b_ml.ml_line_count;
22867 return get_tv_number_chk(&argvars[0], NULL);
22868}
22869
22870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 * Get the string value of a variable.
22872 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022873 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22874 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 * If the String variable has never been set, return an empty string.
22876 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022877 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22878 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022881get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882{
22883 static char_u mybuf[NUMBUFLEN];
22884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022885 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886}
22887
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022888 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022889get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022891 char_u *res = get_tv_string_buf_chk(varp, buf);
22892
22893 return res != NULL ? res : (char_u *)"";
22894}
22895
Bram Moolenaar7d647822014-04-05 21:28:56 +020022896/*
22897 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22898 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022899 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022900get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022901{
22902 static char_u mybuf[NUMBUFLEN];
22903
22904 return get_tv_string_buf_chk(varp, mybuf);
22905}
22906
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022907 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022908get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022909{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022910 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022912 case VAR_NUMBER:
22913 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22914 return buf;
22915 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022916 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022917 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022918 break;
22919 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022920 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022921 break;
22922 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022923 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022924 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022925 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022926#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022927 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022928 break;
22929#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022930 case VAR_STRING:
22931 if (varp->vval.v_string != NULL)
22932 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022933 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022934 case VAR_SPECIAL:
22935 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22936 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022937 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022939 {
22940 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022941 char *status;
22942
22943 if (job == NULL)
22944 return (char_u *)"no process";
22945 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022946 : job->jv_status == JOB_ENDED ? "dead"
22947 : "run";
22948# ifdef UNIX
22949 vim_snprintf((char *)buf, NUMBUFLEN,
22950 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022951# elif defined(WIN32)
22952 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022953 "process %ld %s",
22954 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022955 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022956# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022957 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022958 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22959# endif
22960 return buf;
22961 }
22962#endif
22963 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022964 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022965#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022966 {
22967 channel_T *channel = varp->vval.v_channel;
22968 char *status = channel_status(channel);
22969
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022970 if (channel == NULL)
22971 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22972 else
22973 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022974 "channel %d %s", channel->ch_id, status);
22975 return buf;
22976 }
22977#endif
22978 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022979 case VAR_UNKNOWN:
22980 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022981 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022983 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984}
22985
22986/*
22987 * Find variable "name" in the list of variables.
22988 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022989 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022990 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022991 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022993 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022994find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998
Bram Moolenaara7043832005-01-21 11:56:39 +000022999 ht = find_var_ht(name, &varname);
23000 if (htp != NULL)
23001 *htp = ht;
23002 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023004 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005}
23006
23007/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023008 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023009 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023012find_var_in_ht(
23013 hashtab_T *ht,
23014 int htname,
23015 char_u *varname,
23016 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023017{
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 hashitem_T *hi;
23019
23020 if (*varname == NUL)
23021 {
23022 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023023 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023025 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023026 case 'g': return &globvars_var;
23027 case 'v': return &vimvars_var;
23028 case 'b': return &curbuf->b_bufvar;
23029 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023030#ifdef FEAT_WINDOWS
23031 case 't': return &curtab->tp_winvar;
23032#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023033 case 'l': return current_funccal == NULL
23034 ? NULL : &current_funccal->l_vars_var;
23035 case 'a': return current_funccal == NULL
23036 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023037 }
23038 return NULL;
23039 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023040
23041 hi = hash_find(ht, varname);
23042 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023043 {
23044 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023045 * worked find the variable again. Don't auto-load a script if it was
23046 * loaded already, otherwise it would be loaded every time when
23047 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023048 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023049 {
23050 /* Note: script_autoload() may make "hi" invalid. It must either
23051 * be obtained again or not used. */
23052 if (!script_autoload(varname, FALSE) || aborting())
23053 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023054 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023055 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023056 if (HASHITEM_EMPTY(hi))
23057 return NULL;
23058 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023059 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023060}
23061
23062/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023063 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023064 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023065 * Set "varname" to the start of name without ':'.
23066 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023067 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023068find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023070 hashitem_T *hi;
23071
Bram Moolenaar73627d02015-08-11 15:46:09 +020023072 if (name[0] == NUL)
23073 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074 if (name[1] != ':')
23075 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023076 /* The name must not start with a colon or #. */
23077 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 return NULL;
23079 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023080
23081 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023082 hi = hash_find(&compat_hashtab, name);
23083 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023084 return &compat_hashtab;
23085
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023088 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 }
23090 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023091 if (*name == 'g') /* global variable */
23092 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023093 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23094 */
23095 if (vim_strchr(name + 2, ':') != NULL
23096 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023097 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023099 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023101 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023102#ifdef FEAT_WINDOWS
23103 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023104 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023105#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023106 if (*name == 'v') /* v: variable */
23107 return &vimvarht;
23108 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023109 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023110 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023111 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 if (*name == 's' /* script variable */
23113 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23114 return &SCRIPT_VARS(current_SID);
23115 return NULL;
23116}
23117
23118/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023119 * Get function call environment based on bactrace debug level
23120 */
23121 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023122get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023123{
23124 int i;
23125 funccall_T *funccal;
23126 funccall_T *temp_funccal;
23127
23128 funccal = current_funccal;
23129 if (debug_backtrace_level > 0)
23130 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023131 for (i = 0; i < debug_backtrace_level; i++)
23132 {
23133 temp_funccal = funccal->caller;
23134 if (temp_funccal)
23135 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023136 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023137 /* backtrace level overflow. reset to max */
23138 debug_backtrace_level = i;
23139 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023140 }
23141 return funccal;
23142}
23143
23144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023146 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 * Returns NULL when it doesn't exist.
23148 */
23149 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023150get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151{
Bram Moolenaar33570922005-01-25 22:26:29 +000023152 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023154 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 if (v == NULL)
23156 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023157 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158}
23159
23160/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023161 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 * sourcing this script and when executing functions defined in the script.
23163 */
23164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023165new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166{
Bram Moolenaara7043832005-01-21 11:56:39 +000023167 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 hashtab_T *ht;
23169 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023170
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23172 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023173 /* Re-allocating ga_data means that an ht_array pointing to
23174 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023175 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023176 for (i = 1; i <= ga_scripts.ga_len; ++i)
23177 {
23178 ht = &SCRIPT_VARS(i);
23179 if (ht->ht_mask == HT_INIT_SIZE - 1)
23180 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023181 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023183 }
23184
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 while (ga_scripts.ga_len < id)
23186 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023187 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023188 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023189 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 }
23192 }
23193}
23194
23195/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023196 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23197 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198 */
23199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023200init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201{
Bram Moolenaar33570922005-01-25 22:26:29 +000023202 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023203 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023204 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023205 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023206 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 dict_var->di_tv.vval.v_dict = dict;
23208 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023209 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023210 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23211 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212}
23213
23214/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023215 * Unreference a dictionary initialized by init_var_dict().
23216 */
23217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023218unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023219{
23220 /* Now the dict needs to be freed if no one else is using it, go back to
23221 * normal reference counting. */
23222 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23223 dict_unref(dict);
23224}
23225
23226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023228 * Frees all allocated variables and the value they contain.
23229 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 */
23231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023233{
23234 vars_clear_ext(ht, TRUE);
23235}
23236
23237/*
23238 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23239 */
23240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023241vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242{
Bram Moolenaara7043832005-01-21 11:56:39 +000023243 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023244 hashitem_T *hi;
23245 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246
Bram Moolenaar33570922005-01-25 22:26:29 +000023247 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023248 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023249 for (hi = ht->ht_array; todo > 0; ++hi)
23250 {
23251 if (!HASHITEM_EMPTY(hi))
23252 {
23253 --todo;
23254
Bram Moolenaar33570922005-01-25 22:26:29 +000023255 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023256 * ht_array might change then. hash_clear() takes care of it
23257 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 v = HI2DI(hi);
23259 if (free_val)
23260 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023261 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023263 }
23264 }
23265 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023266 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267}
23268
Bram Moolenaara7043832005-01-21 11:56:39 +000023269/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023270 * Delete a variable from hashtab "ht" at item "hi".
23271 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023274delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275{
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023277
23278 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023279 clear_tv(&di->di_tv);
23280 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281}
23282
23283/*
23284 * List the value of one internal variable.
23285 */
23286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023287list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023289 char_u *tofree;
23290 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023291 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023292
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023293 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023294 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023295 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023296 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297}
23298
Bram Moolenaar071d4272004-06-13 20:20:40 +000023299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023300list_one_var_a(
23301 char_u *prefix,
23302 char_u *name,
23303 int type,
23304 char_u *string,
23305 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306{
Bram Moolenaar31859182007-08-14 20:41:13 +000023307 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23308 msg_start();
23309 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 if (name != NULL) /* "a:" vars don't have a name stored */
23311 msg_puts(name);
23312 msg_putchar(' ');
23313 msg_advance(22);
23314 if (type == VAR_NUMBER)
23315 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023316 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023317 msg_putchar('*');
23318 else if (type == VAR_LIST)
23319 {
23320 msg_putchar('[');
23321 if (*string == '[')
23322 ++string;
23323 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023324 else if (type == VAR_DICT)
23325 {
23326 msg_putchar('{');
23327 if (*string == '{')
23328 ++string;
23329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 else
23331 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023332
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023334
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023335 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023336 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023337 if (*first)
23338 {
23339 msg_clr_eos();
23340 *first = FALSE;
23341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342}
23343
23344/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023345 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 * If the variable already exists, the value is updated.
23347 * Otherwise the variable is created.
23348 */
23349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023350set_var(
23351 char_u *name,
23352 typval_T *tv,
23353 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354{
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023357 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023359 ht = find_var_ht(name, &varname);
23360 if (ht == NULL || *varname == NUL)
23361 {
23362 EMSG2(_(e_illvar), name);
23363 return;
23364 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023365 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023366
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023367 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23368 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023369 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023370
Bram Moolenaar33570922005-01-25 22:26:29 +000023371 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023373 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023374 if (var_check_ro(v->di_flags, name, FALSE)
23375 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023376 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023377
23378 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023379 * Handle setting internal v: variables separately where needed to
23380 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 */
23382 if (ht == &vimvarht)
23383 {
23384 if (v->di_tv.v_type == VAR_STRING)
23385 {
23386 vim_free(v->di_tv.vval.v_string);
23387 if (copy || tv->v_type != VAR_STRING)
23388 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23389 else
23390 {
23391 /* Take over the string to avoid an extra alloc/free. */
23392 v->di_tv.vval.v_string = tv->vval.v_string;
23393 tv->vval.v_string = NULL;
23394 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023395 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023396 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023397 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023398 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023399 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023400 if (STRCMP(varname, "searchforward") == 0)
23401 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023402#ifdef FEAT_SEARCH_EXTRA
23403 else if (STRCMP(varname, "hlsearch") == 0)
23404 {
23405 no_hlsearch = !v->di_tv.vval.v_number;
23406 redraw_all_later(SOME_VALID);
23407 }
23408#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023409 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023410 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023411 else if (v->di_tv.v_type != tv->v_type)
23412 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023413 }
23414
23415 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 }
23417 else /* add a new variable */
23418 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023419 /* Can't add "v:" variable. */
23420 if (ht == &vimvarht)
23421 {
23422 EMSG2(_(e_illvar), name);
23423 return;
23424 }
23425
Bram Moolenaar92124a32005-06-17 22:03:40 +000023426 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023427 if (!valid_varname(varname))
23428 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023429
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023430 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23431 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023432 if (v == NULL)
23433 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023435 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023437 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 return;
23439 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023440 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023443 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023444 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023445 else
23446 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023447 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023448 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023449 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451}
23452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023453/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023454 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023455 * Also give an error message.
23456 */
23457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023458var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023459{
23460 if (flags & DI_FLAGS_RO)
23461 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023462 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023463 return TRUE;
23464 }
23465 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23466 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023467 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 return TRUE;
23469 }
23470 return FALSE;
23471}
23472
23473/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023474 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23475 * Also give an error message.
23476 */
23477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023478var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023479{
23480 if (flags & DI_FLAGS_FIX)
23481 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023482 EMSG2(_("E795: Cannot delete variable %s"),
23483 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023484 return TRUE;
23485 }
23486 return FALSE;
23487}
23488
23489/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023490 * Check if a funcref is assigned to a valid variable name.
23491 * Return TRUE and give an error if not.
23492 */
23493 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023494var_check_func_name(
23495 char_u *name, /* points to start of variable name */
23496 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023497{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023498 /* Allow for w: b: s: and t:. */
23499 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023500 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23501 ? name[2] : name[0]))
23502 {
23503 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23504 name);
23505 return TRUE;
23506 }
23507 /* Don't allow hiding a function. When "v" is not NULL we might be
23508 * assigning another function to the same var, the type is checked
23509 * below. */
23510 if (new_var && function_exists(name))
23511 {
23512 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23513 name);
23514 return TRUE;
23515 }
23516 return FALSE;
23517}
23518
23519/*
23520 * Check if a variable name is valid.
23521 * Return FALSE and give an error if not.
23522 */
23523 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023524valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023525{
23526 char_u *p;
23527
23528 for (p = varname; *p != NUL; ++p)
23529 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23530 && *p != AUTOLOAD_CHAR)
23531 {
23532 EMSG2(_(e_illvar), varname);
23533 return FALSE;
23534 }
23535 return TRUE;
23536}
23537
23538/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023539 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023540 * Also give an error message, using "name" or _("name") when use_gettext is
23541 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023542 */
23543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023544tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023545{
23546 if (lock & VAR_LOCKED)
23547 {
23548 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023549 name == NULL ? (char_u *)_("Unknown")
23550 : use_gettext ? (char_u *)_(name)
23551 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023552 return TRUE;
23553 }
23554 if (lock & VAR_FIXED)
23555 {
23556 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023557 name == NULL ? (char_u *)_("Unknown")
23558 : use_gettext ? (char_u *)_(name)
23559 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023560 return TRUE;
23561 }
23562 return FALSE;
23563}
23564
23565/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023566 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023567 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023568 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023569 * It is OK for "from" and "to" to point to the same item. This is used to
23570 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023571 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023573copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023575 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023576 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023577 switch (from->v_type)
23578 {
23579 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023580 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023581 to->vval.v_number = from->vval.v_number;
23582 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023583 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023584#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023585 to->vval.v_float = from->vval.v_float;
23586 break;
23587#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023588 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023589#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023590 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023591 if (to->vval.v_job != NULL)
23592 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023593 break;
23594#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023595 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023596#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023597 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023598 if (to->vval.v_channel != NULL)
23599 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023600 break;
23601#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023602 case VAR_STRING:
23603 case VAR_FUNC:
23604 if (from->vval.v_string == NULL)
23605 to->vval.v_string = NULL;
23606 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023608 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023609 if (from->v_type == VAR_FUNC)
23610 func_ref(to->vval.v_string);
23611 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023612 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023613 case VAR_PARTIAL:
23614 if (from->vval.v_partial == NULL)
23615 to->vval.v_partial = NULL;
23616 else
23617 {
23618 to->vval.v_partial = from->vval.v_partial;
23619 ++to->vval.v_partial->pt_refcount;
23620 }
23621 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023622 case VAR_LIST:
23623 if (from->vval.v_list == NULL)
23624 to->vval.v_list = NULL;
23625 else
23626 {
23627 to->vval.v_list = from->vval.v_list;
23628 ++to->vval.v_list->lv_refcount;
23629 }
23630 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023631 case VAR_DICT:
23632 if (from->vval.v_dict == NULL)
23633 to->vval.v_dict = NULL;
23634 else
23635 {
23636 to->vval.v_dict = from->vval.v_dict;
23637 ++to->vval.v_dict->dv_refcount;
23638 }
23639 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023640 case VAR_UNKNOWN:
23641 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023642 break;
23643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644}
23645
23646/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023647 * Make a copy of an item.
23648 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023649 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23650 * reference to an already copied list/dict can be used.
23651 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023652 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023654item_copy(
23655 typval_T *from,
23656 typval_T *to,
23657 int deep,
23658 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023659{
23660 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023661 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023662
Bram Moolenaar33570922005-01-25 22:26:29 +000023663 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023664 {
23665 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023666 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023667 }
23668 ++recurse;
23669
23670 switch (from->v_type)
23671 {
23672 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023673 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023674 case VAR_STRING:
23675 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023676 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023677 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023678 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023679 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023680 copy_tv(from, to);
23681 break;
23682 case VAR_LIST:
23683 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023684 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023685 if (from->vval.v_list == NULL)
23686 to->vval.v_list = NULL;
23687 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23688 {
23689 /* use the copy made earlier */
23690 to->vval.v_list = from->vval.v_list->lv_copylist;
23691 ++to->vval.v_list->lv_refcount;
23692 }
23693 else
23694 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23695 if (to->vval.v_list == NULL)
23696 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023697 break;
23698 case VAR_DICT:
23699 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023700 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023701 if (from->vval.v_dict == NULL)
23702 to->vval.v_dict = NULL;
23703 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23704 {
23705 /* use the copy made earlier */
23706 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23707 ++to->vval.v_dict->dv_refcount;
23708 }
23709 else
23710 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23711 if (to->vval.v_dict == NULL)
23712 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023713 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023714 case VAR_UNKNOWN:
23715 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023716 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023717 }
23718 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023719 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023720}
23721
23722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 * ":echo expr1 ..." print each argument separated with a space, add a
23724 * newline at the end.
23725 * ":echon expr1 ..." print each argument plain.
23726 */
23727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023728ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729{
23730 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023731 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023732 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 char_u *p;
23734 int needclr = TRUE;
23735 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023736 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737
23738 if (eap->skip)
23739 ++emsg_skip;
23740 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23741 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023742 /* If eval1() causes an error message the text from the command may
23743 * still need to be cleared. E.g., "echo 22,44". */
23744 need_clr_eos = needclr;
23745
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023747 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 {
23749 /*
23750 * Report the invalid expression unless the expression evaluation
23751 * has been cancelled due to an aborting error, an interrupt, or an
23752 * exception.
23753 */
23754 if (!aborting())
23755 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023756 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 break;
23758 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023759 need_clr_eos = FALSE;
23760
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 if (!eap->skip)
23762 {
23763 if (atstart)
23764 {
23765 atstart = FALSE;
23766 /* Call msg_start() after eval1(), evaluating the expression
23767 * may cause a message to appear. */
23768 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023769 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023770 /* Mark the saved text as finishing the line, so that what
23771 * follows is displayed on a new line when scrolling back
23772 * at the more prompt. */
23773 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 }
23777 else if (eap->cmdidx == CMD_echo)
23778 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023779 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023780 if (p != NULL)
23781 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023783 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023785 if (*p != TAB && needclr)
23786 {
23787 /* remove any text still there from the command */
23788 msg_clr_eos();
23789 needclr = FALSE;
23790 }
23791 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 }
23793 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023794 {
23795#ifdef FEAT_MBYTE
23796 if (has_mbyte)
23797 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023798 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023799
23800 (void)msg_outtrans_len_attr(p, i, echo_attr);
23801 p += i - 1;
23802 }
23803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023805 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023808 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 arg = skipwhite(arg);
23812 }
23813 eap->nextcmd = check_nextcmd(arg);
23814
23815 if (eap->skip)
23816 --emsg_skip;
23817 else
23818 {
23819 /* remove text that may still be there from the command */
23820 if (needclr)
23821 msg_clr_eos();
23822 if (eap->cmdidx == CMD_echo)
23823 msg_end();
23824 }
23825}
23826
23827/*
23828 * ":echohl {name}".
23829 */
23830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023831ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832{
23833 int id;
23834
23835 id = syn_name2id(eap->arg);
23836 if (id == 0)
23837 echo_attr = 0;
23838 else
23839 echo_attr = syn_id2attr(id);
23840}
23841
23842/*
23843 * ":execute expr1 ..." execute the result of an expression.
23844 * ":echomsg expr1 ..." Print a message
23845 * ":echoerr expr1 ..." Print an error
23846 * Each gets spaces around each argument and a newline at the end for
23847 * echo commands
23848 */
23849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023850ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851{
23852 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023853 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 int ret = OK;
23855 char_u *p;
23856 garray_T ga;
23857 int len;
23858 int save_did_emsg;
23859
23860 ga_init2(&ga, 1, 80);
23861
23862 if (eap->skip)
23863 ++emsg_skip;
23864 while (*arg != NUL && *arg != '|' && *arg != '\n')
23865 {
23866 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023867 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 {
23869 /*
23870 * Report the invalid expression unless the expression evaluation
23871 * has been cancelled due to an aborting error, an interrupt, or an
23872 * exception.
23873 */
23874 if (!aborting())
23875 EMSG2(_(e_invexpr2), p);
23876 ret = FAIL;
23877 break;
23878 }
23879
23880 if (!eap->skip)
23881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023882 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 len = (int)STRLEN(p);
23884 if (ga_grow(&ga, len + 2) == FAIL)
23885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023886 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 ret = FAIL;
23888 break;
23889 }
23890 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 ga.ga_len += len;
23894 }
23895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 arg = skipwhite(arg);
23898 }
23899
23900 if (ret != FAIL && ga.ga_data != NULL)
23901 {
23902 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023905 out_flush();
23906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 else if (eap->cmdidx == CMD_echoerr)
23908 {
23909 /* We don't want to abort following commands, restore did_emsg. */
23910 save_did_emsg = did_emsg;
23911 EMSG((char_u *)ga.ga_data);
23912 if (!force_abort)
23913 did_emsg = save_did_emsg;
23914 }
23915 else if (eap->cmdidx == CMD_execute)
23916 do_cmdline((char_u *)ga.ga_data,
23917 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23918 }
23919
23920 ga_clear(&ga);
23921
23922 if (eap->skip)
23923 --emsg_skip;
23924
23925 eap->nextcmd = check_nextcmd(arg);
23926}
23927
23928/*
23929 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23930 * "arg" points to the "&" or '+' when called, to "option" when returning.
23931 * Returns NULL when no option name found. Otherwise pointer to the char
23932 * after the option name.
23933 */
23934 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023935find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936{
23937 char_u *p = *arg;
23938
23939 ++p;
23940 if (*p == 'g' && p[1] == ':')
23941 {
23942 *opt_flags = OPT_GLOBAL;
23943 p += 2;
23944 }
23945 else if (*p == 'l' && p[1] == ':')
23946 {
23947 *opt_flags = OPT_LOCAL;
23948 p += 2;
23949 }
23950 else
23951 *opt_flags = 0;
23952
23953 if (!ASCII_ISALPHA(*p))
23954 return NULL;
23955 *arg = p;
23956
23957 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23958 p += 4; /* termcap option */
23959 else
23960 while (ASCII_ISALPHA(*p))
23961 ++p;
23962 return p;
23963}
23964
23965/*
23966 * ":function"
23967 */
23968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023969ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023970{
23971 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023972 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 int j;
23974 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023976 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023977 char_u *name = NULL;
23978 char_u *p;
23979 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023980 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981 garray_T newargs;
23982 garray_T newlines;
23983 int varargs = FALSE;
23984 int mustend = FALSE;
23985 int flags = 0;
23986 ufunc_T *fp;
23987 int indent;
23988 int nesting;
23989 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023990 dictitem_T *v;
23991 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023992 static int func_nr = 0; /* number for nameless function */
23993 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023994 hashtab_T *ht;
23995 int todo;
23996 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023997 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998
23999 /*
24000 * ":function" without argument: list functions.
24001 */
24002 if (ends_excmd(*eap->arg))
24003 {
24004 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024005 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024006 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024007 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024008 {
24009 if (!HASHITEM_EMPTY(hi))
24010 {
24011 --todo;
24012 fp = HI2UF(hi);
24013 if (!isdigit(*fp->uf_name))
24014 list_func_head(fp, FALSE);
24015 }
24016 }
24017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 eap->nextcmd = check_nextcmd(eap->arg);
24019 return;
24020 }
24021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024022 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024023 * ":function /pat": list functions matching pattern.
24024 */
24025 if (*eap->arg == '/')
24026 {
24027 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24028 if (!eap->skip)
24029 {
24030 regmatch_T regmatch;
24031
24032 c = *p;
24033 *p = NUL;
24034 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24035 *p = c;
24036 if (regmatch.regprog != NULL)
24037 {
24038 regmatch.rm_ic = p_ic;
24039
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024040 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024041 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24042 {
24043 if (!HASHITEM_EMPTY(hi))
24044 {
24045 --todo;
24046 fp = HI2UF(hi);
24047 if (!isdigit(*fp->uf_name)
24048 && vim_regexec(&regmatch, fp->uf_name, 0))
24049 list_func_head(fp, FALSE);
24050 }
24051 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024052 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024053 }
24054 }
24055 if (*p == '/')
24056 ++p;
24057 eap->nextcmd = check_nextcmd(p);
24058 return;
24059 }
24060
24061 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062 * Get the function name. There are these situations:
24063 * func normal function name
24064 * "name" == func, "fudi.fd_dict" == NULL
24065 * dict.func new dictionary entry
24066 * "name" == NULL, "fudi.fd_dict" set,
24067 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24068 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024069 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024070 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24071 * dict.func existing dict entry that's not a Funcref
24072 * "name" == NULL, "fudi.fd_dict" set,
24073 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024074 * s:func script-local function name
24075 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024077 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024078 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024079 paren = (vim_strchr(p, '(') != NULL);
24080 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081 {
24082 /*
24083 * Return on an invalid expression in braces, unless the expression
24084 * evaluation has been cancelled due to an aborting error, an
24085 * interrupt, or an exception.
24086 */
24087 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024089 if (!eap->skip && fudi.fd_newkey != NULL)
24090 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024091 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 else
24095 eap->skip = TRUE;
24096 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024097
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 /* An error in a function call during evaluation of an expression in magic
24099 * braces should not cause the function not to be defined. */
24100 saved_did_emsg = did_emsg;
24101 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102
24103 /*
24104 * ":function func" with only function name: list function.
24105 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024106 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 {
24108 if (!ends_excmd(*skipwhite(p)))
24109 {
24110 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024111 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 }
24113 eap->nextcmd = check_nextcmd(p);
24114 if (eap->nextcmd != NULL)
24115 *p = NUL;
24116 if (!eap->skip && !got_int)
24117 {
24118 fp = find_func(name);
24119 if (fp != NULL)
24120 {
24121 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024122 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024124 if (FUNCLINE(fp, j) == NULL)
24125 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 msg_putchar('\n');
24127 msg_outnum((long)(j + 1));
24128 if (j < 9)
24129 msg_putchar(' ');
24130 if (j < 99)
24131 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 out_flush(); /* show a line at a time */
24134 ui_breakcheck();
24135 }
24136 if (!got_int)
24137 {
24138 msg_putchar('\n');
24139 msg_puts((char_u *)" endfunction");
24140 }
24141 }
24142 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024143 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024145 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 }
24147
24148 /*
24149 * ":function name(arg1, arg2)" Define function.
24150 */
24151 p = skipwhite(p);
24152 if (*p != '(')
24153 {
24154 if (!eap->skip)
24155 {
24156 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 }
24159 /* attempt to continue by skipping some text */
24160 if (vim_strchr(p, '(') != NULL)
24161 p = vim_strchr(p, '(');
24162 }
24163 p = skipwhite(p + 1);
24164
24165 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24166 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24167
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024168 if (!eap->skip)
24169 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024170 /* Check the name of the function. Unless it's a dictionary function
24171 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024172 if (name != NULL)
24173 arg = name;
24174 else
24175 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024176 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024177 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24178 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024179 {
24180 if (*arg == K_SPECIAL)
24181 j = 3;
24182 else
24183 j = 0;
24184 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24185 : eval_isnamec(arg[j])))
24186 ++j;
24187 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024188 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024189 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024190 /* Disallow using the g: dict. */
24191 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24192 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024193 }
24194
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 /*
24196 * Isolate the arguments: "arg1, arg2, ...)"
24197 */
24198 while (*p != ')')
24199 {
24200 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24201 {
24202 varargs = TRUE;
24203 p += 3;
24204 mustend = TRUE;
24205 }
24206 else
24207 {
24208 arg = p;
24209 while (ASCII_ISALNUM(*p) || *p == '_')
24210 ++p;
24211 if (arg == p || isdigit(*arg)
24212 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24213 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24214 {
24215 if (!eap->skip)
24216 EMSG2(_("E125: Illegal argument: %s"), arg);
24217 break;
24218 }
24219 if (ga_grow(&newargs, 1) == FAIL)
24220 goto erret;
24221 c = *p;
24222 *p = NUL;
24223 arg = vim_strsave(arg);
24224 if (arg == NULL)
24225 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024226
24227 /* Check for duplicate argument name. */
24228 for (i = 0; i < newargs.ga_len; ++i)
24229 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24230 {
24231 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024232 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024233 goto erret;
24234 }
24235
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24237 *p = c;
24238 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 if (*p == ',')
24240 ++p;
24241 else
24242 mustend = TRUE;
24243 }
24244 p = skipwhite(p);
24245 if (mustend && *p != ')')
24246 {
24247 if (!eap->skip)
24248 EMSG2(_(e_invarg2), eap->arg);
24249 break;
24250 }
24251 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024252 if (*p != ')')
24253 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 ++p; /* skip the ')' */
24255
Bram Moolenaare9a41262005-01-15 22:18:47 +000024256 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 for (;;)
24258 {
24259 p = skipwhite(p);
24260 if (STRNCMP(p, "range", 5) == 0)
24261 {
24262 flags |= FC_RANGE;
24263 p += 5;
24264 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024265 else if (STRNCMP(p, "dict", 4) == 0)
24266 {
24267 flags |= FC_DICT;
24268 p += 4;
24269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270 else if (STRNCMP(p, "abort", 5) == 0)
24271 {
24272 flags |= FC_ABORT;
24273 p += 5;
24274 }
24275 else
24276 break;
24277 }
24278
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024279 /* When there is a line break use what follows for the function body.
24280 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24281 if (*p == '\n')
24282 line_arg = p + 1;
24283 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 EMSG(_(e_trailing));
24285
24286 /*
24287 * Read the body of the function, until ":endfunction" is found.
24288 */
24289 if (KeyTyped)
24290 {
24291 /* Check if the function already exists, don't let the user type the
24292 * whole function before telling him it doesn't work! For a script we
24293 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024294 if (!eap->skip && !eap->forceit)
24295 {
24296 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24297 EMSG(_(e_funcdict));
24298 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024299 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024302 if (!eap->skip && did_emsg)
24303 goto erret;
24304
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305 msg_putchar('\n'); /* don't overwrite the function name */
24306 cmdline_row = msg_row;
24307 }
24308
24309 indent = 2;
24310 nesting = 0;
24311 for (;;)
24312 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024313 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024314 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024315 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024316 saved_wait_return = FALSE;
24317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024319 sourcing_lnum_off = sourcing_lnum;
24320
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024321 if (line_arg != NULL)
24322 {
24323 /* Use eap->arg, split up in parts by line breaks. */
24324 theline = line_arg;
24325 p = vim_strchr(theline, '\n');
24326 if (p == NULL)
24327 line_arg += STRLEN(line_arg);
24328 else
24329 {
24330 *p = NUL;
24331 line_arg = p + 1;
24332 }
24333 }
24334 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 theline = getcmdline(':', 0L, indent);
24336 else
24337 theline = eap->getline(':', eap->cookie, indent);
24338 if (KeyTyped)
24339 lines_left = Rows - 1;
24340 if (theline == NULL)
24341 {
24342 EMSG(_("E126: Missing :endfunction"));
24343 goto erret;
24344 }
24345
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024346 /* Detect line continuation: sourcing_lnum increased more than one. */
24347 if (sourcing_lnum > sourcing_lnum_off + 1)
24348 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24349 else
24350 sourcing_lnum_off = 0;
24351
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 if (skip_until != NULL)
24353 {
24354 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24355 * don't check for ":endfunc". */
24356 if (STRCMP(theline, skip_until) == 0)
24357 {
24358 vim_free(skip_until);
24359 skip_until = NULL;
24360 }
24361 }
24362 else
24363 {
24364 /* skip ':' and blanks*/
24365 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24366 ;
24367
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024368 /* Check for "endfunction". */
24369 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024371 if (line_arg == NULL)
24372 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 break;
24374 }
24375
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024376 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 * at "end". */
24378 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24379 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024380 else if (STRNCMP(p, "if", 2) == 0
24381 || STRNCMP(p, "wh", 2) == 0
24382 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 || STRNCMP(p, "try", 3) == 0)
24384 indent += 2;
24385
24386 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024387 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024389 if (*p == '!')
24390 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024392 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024393 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024395 ++nesting;
24396 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 }
24398 }
24399
24400 /* Check for ":append" or ":insert". */
24401 p = skip_range(p, NULL);
24402 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24403 || (p[0] == 'i'
24404 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24405 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24406 skip_until = vim_strsave((char_u *)".");
24407
24408 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24409 arg = skipwhite(skiptowhite(p));
24410 if (arg[0] == '<' && arg[1] =='<'
24411 && ((p[0] == 'p' && p[1] == 'y'
24412 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24413 || (p[0] == 'p' && p[1] == 'e'
24414 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24415 || (p[0] == 't' && p[1] == 'c'
24416 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024417 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24418 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24420 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024421 || (p[0] == 'm' && p[1] == 'z'
24422 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024423 ))
24424 {
24425 /* ":python <<" continues until a dot, like ":append" */
24426 p = skipwhite(arg + 2);
24427 if (*p == NUL)
24428 skip_until = vim_strsave((char_u *)".");
24429 else
24430 skip_until = vim_strsave(p);
24431 }
24432 }
24433
24434 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024435 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024436 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024437 if (line_arg == NULL)
24438 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024439 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024440 }
24441
24442 /* Copy the line to newly allocated memory. get_one_sourceline()
24443 * allocates 250 bytes per line, this saves 80% on average. The cost
24444 * is an extra alloc/free. */
24445 p = vim_strsave(theline);
24446 if (p != NULL)
24447 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024448 if (line_arg == NULL)
24449 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024450 theline = p;
24451 }
24452
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024453 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24454
24455 /* Add NULL lines for continuation lines, so that the line count is
24456 * equal to the index in the growarray. */
24457 while (sourcing_lnum_off-- > 0)
24458 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024459
24460 /* Check for end of eap->arg. */
24461 if (line_arg != NULL && *line_arg == NUL)
24462 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024463 }
24464
24465 /* Don't define the function when skipping commands or when an error was
24466 * detected. */
24467 if (eap->skip || did_emsg)
24468 goto erret;
24469
24470 /*
24471 * If there are no errors, add the function
24472 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024473 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024474 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024475 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024476 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024477 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024478 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024479 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024480 goto erret;
24481 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024483 fp = find_func(name);
24484 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024486 if (!eap->forceit)
24487 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024488 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024489 goto erret;
24490 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024491 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024492 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024493 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024494 name);
24495 goto erret;
24496 }
24497 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024498 ga_clear_strings(&(fp->uf_args));
24499 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024500 vim_free(name);
24501 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 }
24504 else
24505 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024506 char numbuf[20];
24507
24508 fp = NULL;
24509 if (fudi.fd_newkey == NULL && !eap->forceit)
24510 {
24511 EMSG(_(e_funcdict));
24512 goto erret;
24513 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024514 if (fudi.fd_di == NULL)
24515 {
24516 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024517 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024518 goto erret;
24519 }
24520 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024521 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024522 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024523
24524 /* Give the function a sequential number. Can only be used with a
24525 * Funcref! */
24526 vim_free(name);
24527 sprintf(numbuf, "%d", ++func_nr);
24528 name = vim_strsave((char_u *)numbuf);
24529 if (name == NULL)
24530 goto erret;
24531 }
24532
24533 if (fp == NULL)
24534 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024535 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024536 {
24537 int slen, plen;
24538 char_u *scriptname;
24539
24540 /* Check that the autoload name matches the script name. */
24541 j = FAIL;
24542 if (sourcing_name != NULL)
24543 {
24544 scriptname = autoload_name(name);
24545 if (scriptname != NULL)
24546 {
24547 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024548 plen = (int)STRLEN(p);
24549 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024550 if (slen > plen && fnamecmp(p,
24551 sourcing_name + slen - plen) == 0)
24552 j = OK;
24553 vim_free(scriptname);
24554 }
24555 }
24556 if (j == FAIL)
24557 {
24558 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24559 goto erret;
24560 }
24561 }
24562
24563 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024564 if (fp == NULL)
24565 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566
24567 if (fudi.fd_dict != NULL)
24568 {
24569 if (fudi.fd_di == NULL)
24570 {
24571 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024572 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024573 if (fudi.fd_di == NULL)
24574 {
24575 vim_free(fp);
24576 goto erret;
24577 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024578 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24579 {
24580 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024581 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024582 goto erret;
24583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024584 }
24585 else
24586 /* overwrite existing dict entry */
24587 clear_tv(&fudi.fd_di->di_tv);
24588 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024589 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024590 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024591 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024592
24593 /* behave like "dict" was used */
24594 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024595 }
24596
Bram Moolenaar071d4272004-06-13 20:20:40 +000024597 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024598 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024599 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24600 {
24601 vim_free(fp);
24602 goto erret;
24603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024605 fp->uf_args = newargs;
24606 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024607#ifdef FEAT_PROFILE
24608 fp->uf_tml_count = NULL;
24609 fp->uf_tml_total = NULL;
24610 fp->uf_tml_self = NULL;
24611 fp->uf_profiling = FALSE;
24612 if (prof_def_func())
24613 func_do_profile(fp);
24614#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024615 fp->uf_varargs = varargs;
24616 fp->uf_flags = flags;
24617 fp->uf_calls = 0;
24618 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024619 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620
24621erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 ga_clear_strings(&newargs);
24623 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024624ret_free:
24625 vim_free(skip_until);
24626 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024629 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630}
24631
24632/*
24633 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024634 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024636 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024637 * TFN_INT: internal function name OK
24638 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024639 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640 * Advances "pp" to just after the function name (if no error).
24641 */
24642 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024643trans_function_name(
24644 char_u **pp,
24645 int skip, /* only find the end, don't evaluate */
24646 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024647 funcdict_T *fdp, /* return: info about dictionary used */
24648 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024650 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 char_u *start;
24652 char_u *end;
24653 int lead;
24654 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024656 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024657
24658 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024659 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024660 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024661
24662 /* Check for hard coded <SNR>: already translated function ID (from a user
24663 * command). */
24664 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24665 && (*pp)[2] == (int)KE_SNR)
24666 {
24667 *pp += 3;
24668 len = get_id_len(pp) + 3;
24669 return vim_strnsave(start, len);
24670 }
24671
24672 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24673 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024675 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024677
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024678 /* Note that TFN_ flags use the same values as GLV_ flags. */
24679 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024680 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024681 if (end == start)
24682 {
24683 if (!skip)
24684 EMSG(_("E129: Function name required"));
24685 goto theend;
24686 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024687 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024688 {
24689 /*
24690 * Report an invalid expression in braces, unless the expression
24691 * evaluation has been cancelled due to an aborting error, an
24692 * interrupt, or an exception.
24693 */
24694 if (!aborting())
24695 {
24696 if (end != NULL)
24697 EMSG2(_(e_invarg2), start);
24698 }
24699 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024700 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024701 goto theend;
24702 }
24703
24704 if (lv.ll_tv != NULL)
24705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024706 if (fdp != NULL)
24707 {
24708 fdp->fd_dict = lv.ll_dict;
24709 fdp->fd_newkey = lv.ll_newkey;
24710 lv.ll_newkey = NULL;
24711 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024713 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24714 {
24715 name = vim_strsave(lv.ll_tv->vval.v_string);
24716 *pp = end;
24717 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024718 else if (lv.ll_tv->v_type == VAR_PARTIAL
24719 && lv.ll_tv->vval.v_partial != NULL)
24720 {
24721 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24722 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024723 if (partial != NULL)
24724 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024725 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024726 else
24727 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024728 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24729 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024730 EMSG(_(e_funcref));
24731 else
24732 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024733 name = NULL;
24734 }
24735 goto theend;
24736 }
24737
24738 if (lv.ll_name == NULL)
24739 {
24740 /* Error found, but continue after the function name. */
24741 *pp = end;
24742 goto theend;
24743 }
24744
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024745 /* Check if the name is a Funcref. If so, use the value. */
24746 if (lv.ll_exp_name != NULL)
24747 {
24748 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024749 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024750 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024751 if (name == lv.ll_exp_name)
24752 name = NULL;
24753 }
24754 else
24755 {
24756 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024757 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024758 if (name == *pp)
24759 name = NULL;
24760 }
24761 if (name != NULL)
24762 {
24763 name = vim_strsave(name);
24764 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024765 if (STRNCMP(name, "<SNR>", 5) == 0)
24766 {
24767 /* Change "<SNR>" to the byte sequence. */
24768 name[0] = K_SPECIAL;
24769 name[1] = KS_EXTRA;
24770 name[2] = (int)KE_SNR;
24771 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24772 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024773 goto theend;
24774 }
24775
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024776 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024777 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024778 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024779 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24780 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24781 {
24782 /* When there was "s:" already or the name expanded to get a
24783 * leading "s:" then remove it. */
24784 lv.ll_name += 2;
24785 len -= 2;
24786 lead = 2;
24787 }
24788 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024789 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024790 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024791 /* skip over "s:" and "g:" */
24792 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024793 lv.ll_name += 2;
24794 len = (int)(end - lv.ll_name);
24795 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024796
24797 /*
24798 * Copy the function name to allocated memory.
24799 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24800 * Accept <SNR>123_name() outside a script.
24801 */
24802 if (skip)
24803 lead = 0; /* do nothing */
24804 else if (lead > 0)
24805 {
24806 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024807 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24808 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024809 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024810 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024811 if (current_SID <= 0)
24812 {
24813 EMSG(_(e_usingsid));
24814 goto theend;
24815 }
24816 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24817 lead += (int)STRLEN(sid_buf);
24818 }
24819 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024820 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024821 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024822 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024823 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024824 goto theend;
24825 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024826 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024827 {
24828 char_u *cp = vim_strchr(lv.ll_name, ':');
24829
24830 if (cp != NULL && cp < end)
24831 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024832 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024833 goto theend;
24834 }
24835 }
24836
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024837 name = alloc((unsigned)(len + lead + 1));
24838 if (name != NULL)
24839 {
24840 if (lead > 0)
24841 {
24842 name[0] = K_SPECIAL;
24843 name[1] = KS_EXTRA;
24844 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024845 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024846 STRCPY(name + 3, sid_buf);
24847 }
24848 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024849 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024850 }
24851 *pp = end;
24852
24853theend:
24854 clear_lval(&lv);
24855 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856}
24857
24858/*
24859 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24860 * Return 2 if "p" starts with "s:".
24861 * Return 0 otherwise.
24862 */
24863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024864eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024866 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24867 * the standard library function. */
24868 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24869 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 return 5;
24871 if (p[0] == 's' && p[1] == ':')
24872 return 2;
24873 return 0;
24874}
24875
24876/*
24877 * Return TRUE if "p" starts with "<SID>" or "s:".
24878 * Only works if eval_fname_script() returned non-zero for "p"!
24879 */
24880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024881eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024882{
24883 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24884}
24885
24886/*
24887 * List the head of the function: "name(arg1, arg2)".
24888 */
24889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024890list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891{
24892 int j;
24893
24894 msg_start();
24895 if (indent)
24896 MSG_PUTS(" ");
24897 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024898 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 {
24900 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024901 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 }
24903 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024904 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024906 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 {
24908 if (j)
24909 MSG_PUTS(", ");
24910 msg_puts(FUNCARG(fp, j));
24911 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024912 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 {
24914 if (j)
24915 MSG_PUTS(", ");
24916 MSG_PUTS("...");
24917 }
24918 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024919 if (fp->uf_flags & FC_ABORT)
24920 MSG_PUTS(" abort");
24921 if (fp->uf_flags & FC_RANGE)
24922 MSG_PUTS(" range");
24923 if (fp->uf_flags & FC_DICT)
24924 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024925 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024926 if (p_verbose > 0)
24927 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928}
24929
24930/*
24931 * Find a function by name, return pointer to it in ufuncs.
24932 * Return NULL for unknown function.
24933 */
24934 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024935find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024937 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024939 hi = hash_find(&func_hashtab, name);
24940 if (!HASHITEM_EMPTY(hi))
24941 return HI2UF(hi);
24942 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943}
24944
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024945#if defined(EXITFREE) || defined(PROTO)
24946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024947free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024948{
24949 hashitem_T *hi;
24950
24951 /* Need to start all over every time, because func_free() may change the
24952 * hash table. */
24953 while (func_hashtab.ht_used > 0)
24954 for (hi = func_hashtab.ht_array; ; ++hi)
24955 if (!HASHITEM_EMPTY(hi))
24956 {
24957 func_free(HI2UF(hi));
24958 break;
24959 }
24960}
24961#endif
24962
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024963 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024964translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024965{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024966 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024967 return find_internal_func(name) >= 0;
24968 return find_func(name) != NULL;
24969}
24970
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024971/*
24972 * Return TRUE if a function "name" exists.
24973 */
24974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024975function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024976{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024977 char_u *nm = name;
24978 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024979 int n = FALSE;
24980
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024981 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024982 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024983 nm = skipwhite(nm);
24984
24985 /* Only accept "funcname", "funcname ", "funcname (..." and
24986 * "funcname(...", not "funcname!...". */
24987 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024988 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024989 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024990 return n;
24991}
24992
Bram Moolenaara1544c02013-05-30 12:35:52 +020024993 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024994get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024995{
24996 char_u *nm = name;
24997 char_u *p;
24998
Bram Moolenaar65639032016-03-16 21:40:30 +010024999 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025000
25001 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025002 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025003 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025004
Bram Moolenaara1544c02013-05-30 12:35:52 +020025005 vim_free(p);
25006 return NULL;
25007}
25008
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025009/*
25010 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025011 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25012 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025013 */
25014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025015builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025016{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025017 char_u *p;
25018
25019 if (!ASCII_ISLOWER(name[0]))
25020 return FALSE;
25021 p = vim_strchr(name, AUTOLOAD_CHAR);
25022 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025023}
25024
Bram Moolenaar05159a02005-02-26 23:04:13 +000025025#if defined(FEAT_PROFILE) || defined(PROTO)
25026/*
25027 * Start profiling function "fp".
25028 */
25029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025030func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025031{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025032 int len = fp->uf_lines.ga_len;
25033
25034 if (len == 0)
25035 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025036 fp->uf_tm_count = 0;
25037 profile_zero(&fp->uf_tm_self);
25038 profile_zero(&fp->uf_tm_total);
25039 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025040 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025041 if (fp->uf_tml_total == NULL)
25042 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025043 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025044 if (fp->uf_tml_self == NULL)
25045 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025046 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025047 fp->uf_tml_idx = -1;
25048 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25049 || fp->uf_tml_self == NULL)
25050 return; /* out of memory */
25051
25052 fp->uf_profiling = TRUE;
25053}
25054
25055/*
25056 * Dump the profiling results for all functions in file "fd".
25057 */
25058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025059func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025060{
25061 hashitem_T *hi;
25062 int todo;
25063 ufunc_T *fp;
25064 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025065 ufunc_T **sorttab;
25066 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025068 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025069 if (todo == 0)
25070 return; /* nothing to dump */
25071
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025072 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025073
Bram Moolenaar05159a02005-02-26 23:04:13 +000025074 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25075 {
25076 if (!HASHITEM_EMPTY(hi))
25077 {
25078 --todo;
25079 fp = HI2UF(hi);
25080 if (fp->uf_profiling)
25081 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025082 if (sorttab != NULL)
25083 sorttab[st_len++] = fp;
25084
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085 if (fp->uf_name[0] == K_SPECIAL)
25086 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25087 else
25088 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25089 if (fp->uf_tm_count == 1)
25090 fprintf(fd, "Called 1 time\n");
25091 else
25092 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25093 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25094 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25095 fprintf(fd, "\n");
25096 fprintf(fd, "count total (s) self (s)\n");
25097
25098 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25099 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025100 if (FUNCLINE(fp, i) == NULL)
25101 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025102 prof_func_line(fd, fp->uf_tml_count[i],
25103 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025104 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25105 }
25106 fprintf(fd, "\n");
25107 }
25108 }
25109 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025110
25111 if (sorttab != NULL && st_len > 0)
25112 {
25113 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25114 prof_total_cmp);
25115 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25116 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25117 prof_self_cmp);
25118 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25119 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025120
25121 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025122}
Bram Moolenaar73830342005-02-28 22:48:19 +000025123
25124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025125prof_sort_list(
25126 FILE *fd,
25127 ufunc_T **sorttab,
25128 int st_len,
25129 char *title,
25130 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025131{
25132 int i;
25133 ufunc_T *fp;
25134
25135 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25136 fprintf(fd, "count total (s) self (s) function\n");
25137 for (i = 0; i < 20 && i < st_len; ++i)
25138 {
25139 fp = sorttab[i];
25140 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25141 prefer_self);
25142 if (fp->uf_name[0] == K_SPECIAL)
25143 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25144 else
25145 fprintf(fd, " %s()\n", fp->uf_name);
25146 }
25147 fprintf(fd, "\n");
25148}
25149
25150/*
25151 * Print the count and times for one function or function line.
25152 */
25153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025154prof_func_line(
25155 FILE *fd,
25156 int count,
25157 proftime_T *total,
25158 proftime_T *self,
25159 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025160{
25161 if (count > 0)
25162 {
25163 fprintf(fd, "%5d ", count);
25164 if (prefer_self && profile_equal(total, self))
25165 fprintf(fd, " ");
25166 else
25167 fprintf(fd, "%s ", profile_msg(total));
25168 if (!prefer_self && profile_equal(total, self))
25169 fprintf(fd, " ");
25170 else
25171 fprintf(fd, "%s ", profile_msg(self));
25172 }
25173 else
25174 fprintf(fd, " ");
25175}
25176
25177/*
25178 * Compare function for total time sorting.
25179 */
25180 static int
25181#ifdef __BORLANDC__
25182_RTLENTRYF
25183#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025184prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025185{
25186 ufunc_T *p1, *p2;
25187
25188 p1 = *(ufunc_T **)s1;
25189 p2 = *(ufunc_T **)s2;
25190 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25191}
25192
25193/*
25194 * Compare function for self time sorting.
25195 */
25196 static int
25197#ifdef __BORLANDC__
25198_RTLENTRYF
25199#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025200prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025201{
25202 ufunc_T *p1, *p2;
25203
25204 p1 = *(ufunc_T **)s1;
25205 p2 = *(ufunc_T **)s2;
25206 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25207}
25208
Bram Moolenaar05159a02005-02-26 23:04:13 +000025209#endif
25210
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025211/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025212 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025213 * Return TRUE if a package was loaded.
25214 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025216script_autoload(
25217 char_u *name,
25218 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025219{
25220 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025221 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025222 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025223 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025224
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025225 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025226 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025227 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025228 return FALSE;
25229
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025230 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025231
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025232 /* Find the name in the list of previously loaded package names. Skip
25233 * "autoload/", it's always the same. */
25234 for (i = 0; i < ga_loaded.ga_len; ++i)
25235 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25236 break;
25237 if (!reload && i < ga_loaded.ga_len)
25238 ret = FALSE; /* was loaded already */
25239 else
25240 {
25241 /* Remember the name if it wasn't loaded already. */
25242 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25243 {
25244 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25245 tofree = NULL;
25246 }
25247
25248 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025249 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025250 ret = TRUE;
25251 }
25252
25253 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025254 return ret;
25255}
25256
25257/*
25258 * Return the autoload script name for a function or variable name.
25259 * Returns NULL when out of memory.
25260 */
25261 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025262autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025263{
25264 char_u *p;
25265 char_u *scriptname;
25266
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025267 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025268 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25269 if (scriptname == NULL)
25270 return FALSE;
25271 STRCPY(scriptname, "autoload/");
25272 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025273 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025274 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025275 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025276 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025277 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025278}
25279
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25281
25282/*
25283 * Function given to ExpandGeneric() to obtain the list of user defined
25284 * function names.
25285 */
25286 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025287get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025289 static long_u done;
25290 static hashitem_T *hi;
25291 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292
25293 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025295 done = 0;
25296 hi = func_hashtab.ht_array;
25297 }
25298 if (done < func_hashtab.ht_used)
25299 {
25300 if (done++ > 0)
25301 ++hi;
25302 while (HASHITEM_EMPTY(hi))
25303 ++hi;
25304 fp = HI2UF(hi);
25305
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025306 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025307 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025308
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025309 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25310 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025311
25312 cat_func_name(IObuff, fp);
25313 if (xp->xp_context != EXPAND_USER_FUNC)
25314 {
25315 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025316 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 STRCAT(IObuff, ")");
25318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 return IObuff;
25320 }
25321 return NULL;
25322}
25323
25324#endif /* FEAT_CMDL_COMPL */
25325
25326/*
25327 * Copy the function name of "fp" to buffer "buf".
25328 * "buf" must be able to hold the function name plus three bytes.
25329 * Takes care of script-local function names.
25330 */
25331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025332cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025334 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 {
25336 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025337 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 }
25339 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025340 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341}
25342
25343/*
25344 * ":delfunction {name}"
25345 */
25346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025347ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025349 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 char_u *p;
25351 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025352 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353
25354 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025355 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025356 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025358 {
25359 if (fudi.fd_dict != NULL && !eap->skip)
25360 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 if (!ends_excmd(*skipwhite(p)))
25364 {
25365 vim_free(name);
25366 EMSG(_(e_trailing));
25367 return;
25368 }
25369 eap->nextcmd = check_nextcmd(p);
25370 if (eap->nextcmd != NULL)
25371 *p = NUL;
25372
25373 if (!eap->skip)
25374 fp = find_func(name);
25375 vim_free(name);
25376
25377 if (!eap->skip)
25378 {
25379 if (fp == NULL)
25380 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025381 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 return;
25383 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025384 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 {
25386 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25387 return;
25388 }
25389
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025390 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025392 /* Delete the dict item that refers to the function, it will
25393 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025394 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025396 else
25397 func_free(fp);
25398 }
25399}
25400
25401/*
25402 * Free a function and remove it from the list of functions.
25403 */
25404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025405func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025406{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025407 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025408
25409 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025410 ga_clear_strings(&(fp->uf_args));
25411 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025412#ifdef FEAT_PROFILE
25413 vim_free(fp->uf_tml_count);
25414 vim_free(fp->uf_tml_total);
25415 vim_free(fp->uf_tml_self);
25416#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025417
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025418 /* remove the function from the function hashtable */
25419 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25420 if (HASHITEM_EMPTY(hi))
25421 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025422 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025423 hash_remove(&func_hashtab, hi);
25424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025425 vim_free(fp);
25426}
25427
25428/*
25429 * Unreference a Function: decrement the reference count and free it when it
25430 * becomes zero. Only for numbered functions.
25431 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025433func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025434{
25435 ufunc_T *fp;
25436
25437 if (name != NULL && isdigit(*name))
25438 {
25439 fp = find_func(name);
25440 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025441 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025442#ifdef EXITFREE
25443 if (!entered_free_all_mem)
25444#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025445 EMSG2(_(e_intern2), "func_unref()");
25446 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025447 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025448 {
25449 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025450 * when "uf_calls" becomes zero. */
25451 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025452 func_free(fp);
25453 }
25454 }
25455}
25456
25457/*
25458 * Count a reference to a Function.
25459 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025461func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025462{
25463 ufunc_T *fp;
25464
25465 if (name != NULL && isdigit(*name))
25466 {
25467 fp = find_func(name);
25468 if (fp == NULL)
25469 EMSG2(_(e_intern2), "func_ref()");
25470 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025471 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472 }
25473}
25474
25475/*
25476 * Call a user function.
25477 */
25478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025479call_user_func(
25480 ufunc_T *fp, /* pointer to function */
25481 int argcount, /* nr of args */
25482 typval_T *argvars, /* arguments */
25483 typval_T *rettv, /* return value */
25484 linenr_T firstline, /* first line of range */
25485 linenr_T lastline, /* last line of range */
25486 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487{
Bram Moolenaar33570922005-01-25 22:26:29 +000025488 char_u *save_sourcing_name;
25489 linenr_T save_sourcing_lnum;
25490 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025491 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025492 int save_did_emsg;
25493 static int depth = 0;
25494 dictitem_T *v;
25495 int fixvar_idx = 0; /* index in fixvar[] */
25496 int i;
25497 int ai;
25498 char_u numbuf[NUMBUFLEN];
25499 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025500 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025501#ifdef FEAT_PROFILE
25502 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025503 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505
25506 /* If depth of calling is getting too high, don't execute the function */
25507 if (depth >= p_mfd)
25508 {
25509 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025510 rettv->v_type = VAR_NUMBER;
25511 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 return;
25513 }
25514 ++depth;
25515
25516 line_breakcheck(); /* check for CTRL-C hit */
25517
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025518 fc = (funccall_T *)alloc(sizeof(funccall_T));
25519 fc->caller = current_funccal;
25520 current_funccal = fc;
25521 fc->func = fp;
25522 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025523 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025524 fc->linenr = 0;
25525 fc->returned = FALSE;
25526 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025528 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25529 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530
Bram Moolenaar33570922005-01-25 22:26:29 +000025531 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025532 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025533 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25534 * each argument variable and saves a lot of time.
25535 */
25536 /*
25537 * Init l: variables.
25538 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025539 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025540 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025541 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025542 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25543 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025544 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025545 name = v->di_key;
25546 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025547 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025548 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025549 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025550 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025551 v->di_tv.vval.v_dict = selfdict;
25552 ++selfdict->dv_refcount;
25553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025554
Bram Moolenaar33570922005-01-25 22:26:29 +000025555 /*
25556 * Init a: variables.
25557 * Set a:0 to "argcount".
25558 * Set a:000 to a list with room for the "..." arguments.
25559 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025560 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025561 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025562 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025563 /* Use "name" to avoid a warning from some compiler that checks the
25564 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025565 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025566 name = v->di_key;
25567 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025568 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025569 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025570 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025571 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025572 v->di_tv.vval.v_list = &fc->l_varlist;
25573 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25574 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25575 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025576
25577 /*
25578 * Set a:firstline to "firstline" and a:lastline to "lastline".
25579 * Set a:name to named arguments.
25580 * Set a:N to the "..." arguments.
25581 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025582 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025583 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025584 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025585 (varnumber_T)lastline);
25586 for (i = 0; i < argcount; ++i)
25587 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025588 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025589 if (ai < 0)
25590 /* named argument a:name */
25591 name = FUNCARG(fp, i);
25592 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025593 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025594 /* "..." argument a:1, a:2, etc. */
25595 sprintf((char *)numbuf, "%d", ai + 1);
25596 name = numbuf;
25597 }
25598 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25599 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025600 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025601 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25602 }
25603 else
25604 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025605 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25606 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025607 if (v == NULL)
25608 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025609 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025610 }
25611 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025612 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025613
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025614 /* Note: the values are copied directly to avoid alloc/free.
25615 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025616 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025617 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025618
25619 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25620 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025621 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25622 fc->l_listitems[ai].li_tv = argvars[i];
25623 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025624 }
25625 }
25626
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627 /* Don't redraw while executing the function. */
25628 ++RedrawingDisabled;
25629 save_sourcing_name = sourcing_name;
25630 save_sourcing_lnum = sourcing_lnum;
25631 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025632 /* need space for function name + ("function " + 3) or "[number]" */
25633 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25634 + STRLEN(fp->uf_name) + 20;
25635 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 if (sourcing_name != NULL)
25637 {
25638 if (save_sourcing_name != NULL
25639 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025640 sprintf((char *)sourcing_name, "%s[%d]..",
25641 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642 else
25643 STRCPY(sourcing_name, "function ");
25644 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25645
25646 if (p_verbose >= 12)
25647 {
25648 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025649 verbose_enter_scroll();
25650
Bram Moolenaar555b2802005-05-19 21:08:39 +000025651 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 if (p_verbose >= 14)
25653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025655 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025656 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025657 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658
25659 msg_puts((char_u *)"(");
25660 for (i = 0; i < argcount; ++i)
25661 {
25662 if (i > 0)
25663 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025664 if (argvars[i].v_type == VAR_NUMBER)
25665 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 else
25667 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025668 /* Do not want errors such as E724 here. */
25669 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025670 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025671 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025672 if (s != NULL)
25673 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025674 if (vim_strsize(s) > MSG_BUF_CLEN)
25675 {
25676 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25677 s = buf;
25678 }
25679 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025680 vim_free(tofree);
25681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 }
25683 }
25684 msg_puts((char_u *)")");
25685 }
25686 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025687
25688 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 --no_wait_return;
25690 }
25691 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025692#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025693 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025694 {
25695 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25696 func_do_profile(fp);
25697 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025698 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699 {
25700 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025701 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025702 profile_zero(&fp->uf_tm_children);
25703 }
25704 script_prof_save(&wait_start);
25705 }
25706#endif
25707
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025709 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710 save_did_emsg = did_emsg;
25711 did_emsg = FALSE;
25712
25713 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025714 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25716
25717 --RedrawingDisabled;
25718
25719 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025720 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025722 clear_tv(rettv);
25723 rettv->v_type = VAR_NUMBER;
25724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 }
25726
Bram Moolenaar05159a02005-02-26 23:04:13 +000025727#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025728 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025729 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025730 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025731 profile_end(&call_start);
25732 profile_sub_wait(&wait_start, &call_start);
25733 profile_add(&fp->uf_tm_total, &call_start);
25734 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025735 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025736 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025737 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25738 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025739 }
25740 }
25741#endif
25742
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743 /* when being verbose, mention the return value */
25744 if (p_verbose >= 12)
25745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025747 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025750 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025751 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025752 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025753 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025756 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025757 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025758 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025759 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025760
Bram Moolenaar555b2802005-05-19 21:08:39 +000025761 /* The value may be very long. Skip the middle part, so that we
25762 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025763 * truncate it at the end. Don't want errors such as E724 here. */
25764 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025765 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025766 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025767 if (s != NULL)
25768 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025769 if (vim_strsize(s) > MSG_BUF_CLEN)
25770 {
25771 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25772 s = buf;
25773 }
25774 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025775 vim_free(tofree);
25776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 }
25778 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025779
25780 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781 --no_wait_return;
25782 }
25783
25784 vim_free(sourcing_name);
25785 sourcing_name = save_sourcing_name;
25786 sourcing_lnum = save_sourcing_lnum;
25787 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025788#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025789 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025790 script_prof_restore(&wait_start);
25791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792
25793 if (p_verbose >= 12 && sourcing_name != NULL)
25794 {
25795 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025796 verbose_enter_scroll();
25797
Bram Moolenaar555b2802005-05-19 21:08:39 +000025798 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025799 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025800
25801 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 --no_wait_return;
25803 }
25804
25805 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025806 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025808
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025809 /* If the a:000 list and the l: and a: dicts are not referenced we can
25810 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025811 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25812 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25813 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25814 {
25815 free_funccal(fc, FALSE);
25816 }
25817 else
25818 {
25819 hashitem_T *hi;
25820 listitem_T *li;
25821 int todo;
25822
25823 /* "fc" is still in use. This can happen when returning "a:000" or
25824 * assigning "l:" to a global variable.
25825 * Link "fc" in the list for garbage collection later. */
25826 fc->caller = previous_funccal;
25827 previous_funccal = fc;
25828
25829 /* Make a copy of the a: variables, since we didn't do that above. */
25830 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25831 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25832 {
25833 if (!HASHITEM_EMPTY(hi))
25834 {
25835 --todo;
25836 v = HI2DI(hi);
25837 copy_tv(&v->di_tv, &v->di_tv);
25838 }
25839 }
25840
25841 /* Make a copy of the a:000 items, since we didn't do that above. */
25842 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25843 copy_tv(&li->li_tv, &li->li_tv);
25844 }
25845}
25846
25847/*
25848 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025849 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025850 */
25851 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025852can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025853{
25854 return (fc->l_varlist.lv_copyID != copyID
25855 && fc->l_vars.dv_copyID != copyID
25856 && fc->l_avars.dv_copyID != copyID);
25857}
25858
25859/*
25860 * Free "fc" and what it contains.
25861 */
25862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025863free_funccal(
25864 funccall_T *fc,
25865 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025866{
25867 listitem_T *li;
25868
25869 /* The a: variables typevals may not have been allocated, only free the
25870 * allocated variables. */
25871 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25872
25873 /* free all l: variables */
25874 vars_clear(&fc->l_vars.dv_hashtab);
25875
25876 /* Free the a:000 variables if they were allocated. */
25877 if (free_val)
25878 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25879 clear_tv(&li->li_tv);
25880
25881 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882}
25883
25884/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025885 * Add a number variable "name" to dict "dp" with value "nr".
25886 */
25887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025888add_nr_var(
25889 dict_T *dp,
25890 dictitem_T *v,
25891 char *name,
25892 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025893{
25894 STRCPY(v->di_key, name);
25895 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25896 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25897 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025898 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025899 v->di_tv.vval.v_number = nr;
25900}
25901
25902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903 * ":return [expr]"
25904 */
25905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025906ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907{
25908 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025909 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 int returning = FALSE;
25911
25912 if (current_funccal == NULL)
25913 {
25914 EMSG(_("E133: :return not inside a function"));
25915 return;
25916 }
25917
25918 if (eap->skip)
25919 ++emsg_skip;
25920
25921 eap->nextcmd = NULL;
25922 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025923 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924 {
25925 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025926 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025928 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929 }
25930 /* It's safer to return also on error. */
25931 else if (!eap->skip)
25932 {
25933 /*
25934 * Return unless the expression evaluation has been cancelled due to an
25935 * aborting error, an interrupt, or an exception.
25936 */
25937 if (!aborting())
25938 returning = do_return(eap, FALSE, TRUE, NULL);
25939 }
25940
25941 /* When skipping or the return gets pending, advance to the next command
25942 * in this line (!returning). Otherwise, ignore the rest of the line.
25943 * Following lines will be ignored by get_func_line(). */
25944 if (returning)
25945 eap->nextcmd = NULL;
25946 else if (eap->nextcmd == NULL) /* no argument */
25947 eap->nextcmd = check_nextcmd(arg);
25948
25949 if (eap->skip)
25950 --emsg_skip;
25951}
25952
25953/*
25954 * Return from a function. Possibly makes the return pending. Also called
25955 * for a pending return at the ":endtry" or after returning from an extra
25956 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025957 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025958 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 * FALSE when the return gets pending.
25960 */
25961 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025962do_return(
25963 exarg_T *eap,
25964 int reanimate,
25965 int is_cmd,
25966 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967{
25968 int idx;
25969 struct condstack *cstack = eap->cstack;
25970
25971 if (reanimate)
25972 /* Undo the return. */
25973 current_funccal->returned = FALSE;
25974
25975 /*
25976 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25977 * not in its finally clause (which then is to be executed next) is found.
25978 * In this case, make the ":return" pending for execution at the ":endtry".
25979 * Otherwise, return normally.
25980 */
25981 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25982 if (idx >= 0)
25983 {
25984 cstack->cs_pending[idx] = CSTP_RETURN;
25985
25986 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025987 /* A pending return again gets pending. "rettv" points to an
25988 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025990 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991 else
25992 {
25993 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025994 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025995 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025996 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025998 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 {
26000 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026001 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026002 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003 else
26004 EMSG(_(e_outofmem));
26005 }
26006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026007 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008
26009 if (reanimate)
26010 {
26011 /* The pending return value could be overwritten by a ":return"
26012 * without argument in a finally clause; reset the default
26013 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026014 current_funccal->rettv->v_type = VAR_NUMBER;
26015 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016 }
26017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026018 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019 }
26020 else
26021 {
26022 current_funccal->returned = TRUE;
26023
26024 /* If the return is carried out now, store the return value. For
26025 * a return immediately after reanimation, the value is already
26026 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026027 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026029 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026030 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026032 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033 }
26034 }
26035
26036 return idx < 0;
26037}
26038
26039/*
26040 * Free the variable with a pending return value.
26041 */
26042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026043discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026044{
Bram Moolenaar33570922005-01-25 22:26:29 +000026045 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046}
26047
26048/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026049 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026050 * is an allocated string. Used by report_pending() for verbose messages.
26051 */
26052 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026053get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026055 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026056 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026057 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026059 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026060 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026061 if (s == NULL)
26062 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026063
26064 STRCPY(IObuff, ":return ");
26065 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26066 if (STRLEN(s) + 8 >= IOSIZE)
26067 STRCPY(IObuff + IOSIZE - 4, "...");
26068 vim_free(tofree);
26069 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026070}
26071
26072/*
26073 * Get next function line.
26074 * Called by do_cmdline() to get the next line.
26075 * Returns allocated string, or NULL for end of function.
26076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026078get_func_line(
26079 int c UNUSED,
26080 void *cookie,
26081 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026082{
Bram Moolenaar33570922005-01-25 22:26:29 +000026083 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026084 ufunc_T *fp = fcp->func;
26085 char_u *retval;
26086 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087
26088 /* If breakpoints have been added/deleted need to check for it. */
26089 if (fcp->dbg_tick != debug_tick)
26090 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026091 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092 sourcing_lnum);
26093 fcp->dbg_tick = debug_tick;
26094 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026095#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026096 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026097 func_line_end(cookie);
26098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099
Bram Moolenaar05159a02005-02-26 23:04:13 +000026100 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026101 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26102 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 retval = NULL;
26104 else
26105 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026106 /* Skip NULL lines (continuation lines). */
26107 while (fcp->linenr < gap->ga_len
26108 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26109 ++fcp->linenr;
26110 if (fcp->linenr >= gap->ga_len)
26111 retval = NULL;
26112 else
26113 {
26114 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26115 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026116#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026117 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026118 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026119#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 }
26122
26123 /* Did we encounter a breakpoint? */
26124 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26125 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026126 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026128 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026129 sourcing_lnum);
26130 fcp->dbg_tick = debug_tick;
26131 }
26132
26133 return retval;
26134}
26135
Bram Moolenaar05159a02005-02-26 23:04:13 +000026136#if defined(FEAT_PROFILE) || defined(PROTO)
26137/*
26138 * Called when starting to read a function line.
26139 * "sourcing_lnum" must be correct!
26140 * When skipping lines it may not actually be executed, but we won't find out
26141 * until later and we need to store the time now.
26142 */
26143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026144func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026145{
26146 funccall_T *fcp = (funccall_T *)cookie;
26147 ufunc_T *fp = fcp->func;
26148
26149 if (fp->uf_profiling && sourcing_lnum >= 1
26150 && sourcing_lnum <= fp->uf_lines.ga_len)
26151 {
26152 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026153 /* Skip continuation lines. */
26154 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26155 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026156 fp->uf_tml_execed = FALSE;
26157 profile_start(&fp->uf_tml_start);
26158 profile_zero(&fp->uf_tml_children);
26159 profile_get_wait(&fp->uf_tml_wait);
26160 }
26161}
26162
26163/*
26164 * Called when actually executing a function line.
26165 */
26166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026167func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026168{
26169 funccall_T *fcp = (funccall_T *)cookie;
26170 ufunc_T *fp = fcp->func;
26171
26172 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26173 fp->uf_tml_execed = TRUE;
26174}
26175
26176/*
26177 * Called when done with a function line.
26178 */
26179 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026180func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026181{
26182 funccall_T *fcp = (funccall_T *)cookie;
26183 ufunc_T *fp = fcp->func;
26184
26185 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26186 {
26187 if (fp->uf_tml_execed)
26188 {
26189 ++fp->uf_tml_count[fp->uf_tml_idx];
26190 profile_end(&fp->uf_tml_start);
26191 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026192 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026193 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26194 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026195 }
26196 fp->uf_tml_idx = -1;
26197 }
26198}
26199#endif
26200
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201/*
26202 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026203 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204 */
26205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026206func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207{
Bram Moolenaar33570922005-01-25 22:26:29 +000026208 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026209
26210 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26211 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026212 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026213 || fcp->returned);
26214}
26215
26216/*
26217 * return TRUE if cookie indicates a function which "abort"s on errors.
26218 */
26219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026220func_has_abort(
26221 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026223 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026224}
26225
26226#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26227typedef enum
26228{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026229 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26230 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26231 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026232} var_flavour_T;
26233
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026234static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235
26236 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026237var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238{
26239 char_u *p = varname;
26240
26241 if (ASCII_ISUPPER(*p))
26242 {
26243 while (*(++p))
26244 if (ASCII_ISLOWER(*p))
26245 return VAR_FLAVOUR_SESSION;
26246 return VAR_FLAVOUR_VIMINFO;
26247 }
26248 else
26249 return VAR_FLAVOUR_DEFAULT;
26250}
26251#endif
26252
26253#if defined(FEAT_VIMINFO) || defined(PROTO)
26254/*
26255 * Restore global vars that start with a capital from the viminfo file
26256 */
26257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026258read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026259{
26260 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026261 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026262 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026263 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026264
26265 if (!writing && (find_viminfo_parameter('!') != NULL))
26266 {
26267 tab = vim_strchr(virp->vir_line + 1, '\t');
26268 if (tab != NULL)
26269 {
26270 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026271 switch (*tab)
26272 {
26273 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026274#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026275 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026276#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026277 case 'D': type = VAR_DICT; break;
26278 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026279 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281
26282 tab = vim_strchr(tab, '\t');
26283 if (tab != NULL)
26284 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026285 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026286 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026287 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026289#ifdef FEAT_FLOAT
26290 else if (type == VAR_FLOAT)
26291 (void)string2float(tab + 1, &tv.vval.v_float);
26292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026294 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026295 if (type == VAR_DICT || type == VAR_LIST)
26296 {
26297 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26298
26299 if (etv == NULL)
26300 /* Failed to parse back the dict or list, use it as a
26301 * string. */
26302 tv.v_type = VAR_STRING;
26303 else
26304 {
26305 vim_free(tv.vval.v_string);
26306 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026307 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026308 }
26309 }
26310
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026311 /* when in a function use global variables */
26312 save_funccal = current_funccal;
26313 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026314 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026315 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026316
26317 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026318 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026319 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026321 }
26322 }
26323 }
26324
26325 return viminfo_readline(virp);
26326}
26327
26328/*
26329 * Write global vars that start with a capital to the viminfo file
26330 */
26331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026332write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333{
Bram Moolenaar33570922005-01-25 22:26:29 +000026334 hashitem_T *hi;
26335 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026336 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026337 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026338 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026339 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026340 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026341
26342 if (find_viminfo_parameter('!') == NULL)
26343 return;
26344
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026345 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026346
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026347 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026348 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026350 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026351 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026352 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026353 this_var = HI2DI(hi);
26354 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026355 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026356 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026357 {
26358 case VAR_STRING: s = "STR"; break;
26359 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026360 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026361 case VAR_DICT: s = "DIC"; break;
26362 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026363 case VAR_SPECIAL: s = "XPL"; break;
26364
26365 case VAR_UNKNOWN:
26366 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026367 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026368 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026369 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026370 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026371 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026372 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026373 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026374 if (p != NULL)
26375 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026376 vim_free(tofree);
26377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378 }
26379 }
26380}
26381#endif
26382
26383#if defined(FEAT_SESSION) || defined(PROTO)
26384 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026385store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026386{
Bram Moolenaar33570922005-01-25 22:26:29 +000026387 hashitem_T *hi;
26388 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026389 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390 char_u *p, *t;
26391
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026392 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026393 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026395 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026397 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026398 this_var = HI2DI(hi);
26399 if ((this_var->di_tv.v_type == VAR_NUMBER
26400 || this_var->di_tv.v_type == VAR_STRING)
26401 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026402 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026403 /* Escape special characters with a backslash. Turn a LF and
26404 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026405 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026406 (char_u *)"\\\"\n\r");
26407 if (p == NULL) /* out of memory */
26408 break;
26409 for (t = p; *t != NUL; ++t)
26410 if (*t == '\n')
26411 *t = 'n';
26412 else if (*t == '\r')
26413 *t = 'r';
26414 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026415 this_var->di_key,
26416 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26417 : ' ',
26418 p,
26419 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26420 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026421 || put_eol(fd) == FAIL)
26422 {
26423 vim_free(p);
26424 return FAIL;
26425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 vim_free(p);
26427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026428#ifdef FEAT_FLOAT
26429 else if (this_var->di_tv.v_type == VAR_FLOAT
26430 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26431 {
26432 float_T f = this_var->di_tv.vval.v_float;
26433 int sign = ' ';
26434
26435 if (f < 0)
26436 {
26437 f = -f;
26438 sign = '-';
26439 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026440 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026441 this_var->di_key, sign, f) < 0)
26442 || put_eol(fd) == FAIL)
26443 return FAIL;
26444 }
26445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446 }
26447 }
26448 return OK;
26449}
26450#endif
26451
Bram Moolenaar661b1822005-07-28 22:36:45 +000026452/*
26453 * Display script name where an item was last set.
26454 * Should only be invoked when 'verbose' is non-zero.
26455 */
26456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026457last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026458{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026459 char_u *p;
26460
Bram Moolenaar661b1822005-07-28 22:36:45 +000026461 if (scriptID != 0)
26462 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026463 p = home_replace_save(NULL, get_scriptname(scriptID));
26464 if (p != NULL)
26465 {
26466 verbose_enter();
26467 MSG_PUTS(_("\n\tLast set from "));
26468 MSG_PUTS(p);
26469 vim_free(p);
26470 verbose_leave();
26471 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026472 }
26473}
26474
Bram Moolenaard812df62008-11-09 12:46:09 +000026475/*
26476 * List v:oldfiles in a nice way.
26477 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026479ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026480{
26481 list_T *l = vimvars[VV_OLDFILES].vv_list;
26482 listitem_T *li;
26483 int nr = 0;
26484
26485 if (l == NULL)
26486 msg((char_u *)_("No old files"));
26487 else
26488 {
26489 msg_start();
26490 msg_scroll = TRUE;
26491 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26492 {
26493 msg_outnum((long)++nr);
26494 MSG_PUTS(": ");
26495 msg_outtrans(get_tv_string(&li->li_tv));
26496 msg_putchar('\n');
26497 out_flush(); /* output one line at a time */
26498 ui_breakcheck();
26499 }
26500 /* Assume "got_int" was set to truncate the listing. */
26501 got_int = FALSE;
26502
26503#ifdef FEAT_BROWSE_CMD
26504 if (cmdmod.browse)
26505 {
26506 quit_more = FALSE;
26507 nr = prompt_for_number(FALSE);
26508 msg_starthere();
26509 if (nr > 0)
26510 {
26511 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26512 (long)nr);
26513
26514 if (p != NULL)
26515 {
26516 p = expand_env_save(p);
26517 eap->arg = p;
26518 eap->cmdidx = CMD_edit;
26519 cmdmod.browse = FALSE;
26520 do_exedit(eap, NULL);
26521 vim_free(p);
26522 }
26523 }
26524 }
26525#endif
26526 }
26527}
26528
Bram Moolenaar53744302015-07-17 17:38:22 +020026529/* reset v:option_new, v:option_old and v:option_type */
26530 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026531reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026532{
26533 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26534 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26535 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26536}
26537
26538
Bram Moolenaar071d4272004-06-13 20:20:40 +000026539#endif /* FEAT_EVAL */
26540
Bram Moolenaar071d4272004-06-13 20:20:40 +000026541
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026542#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026543
26544#ifdef WIN3264
26545/*
26546 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26547 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026548static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26549static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26550static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026551
26552/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026553 * Get the short path (8.3) for the filename in "fnamep".
26554 * Only works for a valid file name.
26555 * When the path gets longer "fnamep" is changed and the allocated buffer
26556 * is put in "bufp".
26557 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26558 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026559 */
26560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026561get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026563 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026564 char_u *newbuf;
26565
26566 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026567 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026568 if (l > len - 1)
26569 {
26570 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026571 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026572 newbuf = vim_strnsave(*fnamep, l);
26573 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575
26576 vim_free(*bufp);
26577 *fnamep = *bufp = newbuf;
26578
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026579 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026580 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026581 }
26582
26583 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026584 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026585}
26586
26587/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026588 * Get the short path (8.3) for the filename in "fname". The converted
26589 * path is returned in "bufp".
26590 *
26591 * Some of the directories specified in "fname" may not exist. This function
26592 * will shorten the existing directories at the beginning of the path and then
26593 * append the remaining non-existing path.
26594 *
26595 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026596 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026597 * bufp - Pointer to an allocated buffer for the filename.
26598 * fnamelen - Length of the filename pointed to by fname
26599 *
26600 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601 */
26602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026603shortpath_for_invalid_fname(
26604 char_u **fname,
26605 char_u **bufp,
26606 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026608 char_u *short_fname, *save_fname, *pbuf_unused;
26609 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026610 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026611 int old_len, len;
26612 int new_len, sfx_len;
26613 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026614
26615 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026616 old_len = *fnamelen;
26617 save_fname = vim_strnsave(*fname, old_len);
26618 pbuf_unused = NULL;
26619 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026620
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026621 endp = save_fname + old_len - 1; /* Find the end of the copy */
26622 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026623
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026624 /*
26625 * Try shortening the supplied path till it succeeds by removing one
26626 * directory at a time from the tail of the path.
26627 */
26628 len = 0;
26629 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026630 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026631 /* go back one path-separator */
26632 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26633 --endp;
26634 if (endp <= save_fname)
26635 break; /* processed the complete path */
26636
26637 /*
26638 * Replace the path separator with a NUL and try to shorten the
26639 * resulting path.
26640 */
26641 ch = *endp;
26642 *endp = 0;
26643 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026644 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026645 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26646 {
26647 retval = FAIL;
26648 goto theend;
26649 }
26650 *endp = ch; /* preserve the string */
26651
26652 if (len > 0)
26653 break; /* successfully shortened the path */
26654
26655 /* failed to shorten the path. Skip the path separator */
26656 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657 }
26658
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026659 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026660 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026661 /*
26662 * Succeeded in shortening the path. Now concatenate the shortened
26663 * path with the remaining path at the tail.
26664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026665
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026666 /* Compute the length of the new path. */
26667 sfx_len = (int)(save_endp - endp) + 1;
26668 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026669
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026670 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026671 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026672 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026673 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026674 /* There is not enough space in the currently allocated string,
26675 * copy it to a buffer big enough. */
26676 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026677 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026678 {
26679 retval = FAIL;
26680 goto theend;
26681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026682 }
26683 else
26684 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026685 /* Transfer short_fname to the main buffer (it's big enough),
26686 * unless get_short_pathname() did its work in-place. */
26687 *fname = *bufp = save_fname;
26688 if (short_fname != save_fname)
26689 vim_strncpy(save_fname, short_fname, len);
26690 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026691 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026692
26693 /* concat the not-shortened part of the path */
26694 vim_strncpy(*fname + len, endp, sfx_len);
26695 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026696 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026697
26698theend:
26699 vim_free(pbuf_unused);
26700 vim_free(save_fname);
26701
26702 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026703}
26704
26705/*
26706 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026707 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026708 */
26709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026710shortpath_for_partial(
26711 char_u **fnamep,
26712 char_u **bufp,
26713 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026714{
26715 int sepcount, len, tflen;
26716 char_u *p;
26717 char_u *pbuf, *tfname;
26718 int hasTilde;
26719
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026720 /* Count up the path separators from the RHS.. so we know which part
26721 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026722 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026723 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026724 if (vim_ispathsep(*p))
26725 ++sepcount;
26726
26727 /* Need full path first (use expand_env() to remove a "~/") */
26728 hasTilde = (**fnamep == '~');
26729 if (hasTilde)
26730 pbuf = tfname = expand_env_save(*fnamep);
26731 else
26732 pbuf = tfname = FullName_save(*fnamep, FALSE);
26733
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026734 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026735
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026736 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26737 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026738
26739 if (len == 0)
26740 {
26741 /* Don't have a valid filename, so shorten the rest of the
26742 * path if we can. This CAN give us invalid 8.3 filenames, but
26743 * there's not a lot of point in guessing what it might be.
26744 */
26745 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026746 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026748 }
26749
26750 /* Count the paths backward to find the beginning of the desired string. */
26751 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026752 {
26753#ifdef FEAT_MBYTE
26754 if (has_mbyte)
26755 p -= mb_head_off(tfname, p);
26756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026757 if (vim_ispathsep(*p))
26758 {
26759 if (sepcount == 0 || (hasTilde && sepcount == 1))
26760 break;
26761 else
26762 sepcount --;
26763 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026765 if (hasTilde)
26766 {
26767 --p;
26768 if (p >= tfname)
26769 *p = '~';
26770 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026771 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026772 }
26773 else
26774 ++p;
26775
26776 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26777 vim_free(*bufp);
26778 *fnamelen = (int)STRLEN(p);
26779 *bufp = pbuf;
26780 *fnamep = p;
26781
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026782 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026783}
26784#endif /* WIN3264 */
26785
26786/*
26787 * Adjust a filename, according to a string of modifiers.
26788 * *fnamep must be NUL terminated when called. When returning, the length is
26789 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026790 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026791 * When there is an error, *fnamep is set to NULL.
26792 */
26793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026794modify_fname(
26795 char_u *src, /* string with modifiers */
26796 int *usedlen, /* characters after src that are used */
26797 char_u **fnamep, /* file name so far */
26798 char_u **bufp, /* buffer for allocated file name or NULL */
26799 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026800{
26801 int valid = 0;
26802 char_u *tail;
26803 char_u *s, *p, *pbuf;
26804 char_u dirname[MAXPATHL];
26805 int c;
26806 int has_fullname = 0;
26807#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026808 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809 int has_shortname = 0;
26810#endif
26811
26812repeat:
26813 /* ":p" - full path/file_name */
26814 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26815 {
26816 has_fullname = 1;
26817
26818 valid |= VALID_PATH;
26819 *usedlen += 2;
26820
26821 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26822 if ((*fnamep)[0] == '~'
26823#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26824 && ((*fnamep)[1] == '/'
26825# ifdef BACKSLASH_IN_FILENAME
26826 || (*fnamep)[1] == '\\'
26827# endif
26828 || (*fnamep)[1] == NUL)
26829
26830#endif
26831 )
26832 {
26833 *fnamep = expand_env_save(*fnamep);
26834 vim_free(*bufp); /* free any allocated file name */
26835 *bufp = *fnamep;
26836 if (*fnamep == NULL)
26837 return -1;
26838 }
26839
26840 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026841 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026842 {
26843 if (vim_ispathsep(*p)
26844 && p[1] == '.'
26845 && (p[2] == NUL
26846 || vim_ispathsep(p[2])
26847 || (p[2] == '.'
26848 && (p[3] == NUL || vim_ispathsep(p[3])))))
26849 break;
26850 }
26851
26852 /* FullName_save() is slow, don't use it when not needed. */
26853 if (*p != NUL || !vim_isAbsName(*fnamep))
26854 {
26855 *fnamep = FullName_save(*fnamep, *p != NUL);
26856 vim_free(*bufp); /* free any allocated file name */
26857 *bufp = *fnamep;
26858 if (*fnamep == NULL)
26859 return -1;
26860 }
26861
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026862#ifdef WIN3264
26863# if _WIN32_WINNT >= 0x0500
26864 if (vim_strchr(*fnamep, '~') != NULL)
26865 {
26866 /* Expand 8.3 filename to full path. Needed to make sure the same
26867 * file does not have two different names.
26868 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26869 p = alloc(_MAX_PATH + 1);
26870 if (p != NULL)
26871 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026872 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026873 {
26874 vim_free(*bufp);
26875 *bufp = *fnamep = p;
26876 }
26877 else
26878 vim_free(p);
26879 }
26880 }
26881# endif
26882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026883 /* Append a path separator to a directory. */
26884 if (mch_isdir(*fnamep))
26885 {
26886 /* Make room for one or two extra characters. */
26887 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26888 vim_free(*bufp); /* free any allocated file name */
26889 *bufp = *fnamep;
26890 if (*fnamep == NULL)
26891 return -1;
26892 add_pathsep(*fnamep);
26893 }
26894 }
26895
26896 /* ":." - path relative to the current directory */
26897 /* ":~" - path relative to the home directory */
26898 /* ":8" - shortname path - postponed till after */
26899 while (src[*usedlen] == ':'
26900 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26901 {
26902 *usedlen += 2;
26903 if (c == '8')
26904 {
26905#ifdef WIN3264
26906 has_shortname = 1; /* Postpone this. */
26907#endif
26908 continue;
26909 }
26910 pbuf = NULL;
26911 /* Need full path first (use expand_env() to remove a "~/") */
26912 if (!has_fullname)
26913 {
26914 if (c == '.' && **fnamep == '~')
26915 p = pbuf = expand_env_save(*fnamep);
26916 else
26917 p = pbuf = FullName_save(*fnamep, FALSE);
26918 }
26919 else
26920 p = *fnamep;
26921
26922 has_fullname = 0;
26923
26924 if (p != NULL)
26925 {
26926 if (c == '.')
26927 {
26928 mch_dirname(dirname, MAXPATHL);
26929 s = shorten_fname(p, dirname);
26930 if (s != NULL)
26931 {
26932 *fnamep = s;
26933 if (pbuf != NULL)
26934 {
26935 vim_free(*bufp); /* free any allocated file name */
26936 *bufp = pbuf;
26937 pbuf = NULL;
26938 }
26939 }
26940 }
26941 else
26942 {
26943 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26944 /* Only replace it when it starts with '~' */
26945 if (*dirname == '~')
26946 {
26947 s = vim_strsave(dirname);
26948 if (s != NULL)
26949 {
26950 *fnamep = s;
26951 vim_free(*bufp);
26952 *bufp = s;
26953 }
26954 }
26955 }
26956 vim_free(pbuf);
26957 }
26958 }
26959
26960 tail = gettail(*fnamep);
26961 *fnamelen = (int)STRLEN(*fnamep);
26962
26963 /* ":h" - head, remove "/file_name", can be repeated */
26964 /* Don't remove the first "/" or "c:\" */
26965 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26966 {
26967 valid |= VALID_HEAD;
26968 *usedlen += 2;
26969 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026970 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026971 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026972 *fnamelen = (int)(tail - *fnamep);
26973#ifdef VMS
26974 if (*fnamelen > 0)
26975 *fnamelen += 1; /* the path separator is part of the path */
26976#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026977 if (*fnamelen == 0)
26978 {
26979 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26980 p = vim_strsave((char_u *)".");
26981 if (p == NULL)
26982 return -1;
26983 vim_free(*bufp);
26984 *bufp = *fnamep = tail = p;
26985 *fnamelen = 1;
26986 }
26987 else
26988 {
26989 while (tail > s && !after_pathsep(s, tail))
26990 mb_ptr_back(*fnamep, tail);
26991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026992 }
26993
26994 /* ":8" - shortname */
26995 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26996 {
26997 *usedlen += 2;
26998#ifdef WIN3264
26999 has_shortname = 1;
27000#endif
27001 }
27002
27003#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027004 /*
27005 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027006 */
27007 if (has_shortname)
27008 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027009 /* Copy the string if it is shortened by :h and when it wasn't copied
27010 * yet, because we are going to change it in place. Avoids changing
27011 * the buffer name for "%:8". */
27012 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027013 {
27014 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027015 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027016 return -1;
27017 vim_free(*bufp);
27018 *bufp = *fnamep = p;
27019 }
27020
27021 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027022 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023 if (!has_fullname && !vim_isAbsName(*fnamep))
27024 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027025 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027026 return -1;
27027 }
27028 else
27029 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027030 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027031
Bram Moolenaardc935552011-08-17 15:23:23 +020027032 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027033 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027034 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027035 return -1;
27036
27037 if (l == 0)
27038 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027039 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027040 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027041 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027042 return -1;
27043 }
27044 *fnamelen = l;
27045 }
27046 }
27047#endif /* WIN3264 */
27048
27049 /* ":t" - tail, just the basename */
27050 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27051 {
27052 *usedlen += 2;
27053 *fnamelen -= (int)(tail - *fnamep);
27054 *fnamep = tail;
27055 }
27056
27057 /* ":e" - extension, can be repeated */
27058 /* ":r" - root, without extension, can be repeated */
27059 while (src[*usedlen] == ':'
27060 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27061 {
27062 /* find a '.' in the tail:
27063 * - for second :e: before the current fname
27064 * - otherwise: The last '.'
27065 */
27066 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27067 s = *fnamep - 2;
27068 else
27069 s = *fnamep + *fnamelen - 1;
27070 for ( ; s > tail; --s)
27071 if (s[0] == '.')
27072 break;
27073 if (src[*usedlen + 1] == 'e') /* :e */
27074 {
27075 if (s > tail)
27076 {
27077 *fnamelen += (int)(*fnamep - (s + 1));
27078 *fnamep = s + 1;
27079#ifdef VMS
27080 /* cut version from the extension */
27081 s = *fnamep + *fnamelen - 1;
27082 for ( ; s > *fnamep; --s)
27083 if (s[0] == ';')
27084 break;
27085 if (s > *fnamep)
27086 *fnamelen = s - *fnamep;
27087#endif
27088 }
27089 else if (*fnamep <= tail)
27090 *fnamelen = 0;
27091 }
27092 else /* :r */
27093 {
27094 if (s > tail) /* remove one extension */
27095 *fnamelen = (int)(s - *fnamep);
27096 }
27097 *usedlen += 2;
27098 }
27099
27100 /* ":s?pat?foo?" - substitute */
27101 /* ":gs?pat?foo?" - global substitute */
27102 if (src[*usedlen] == ':'
27103 && (src[*usedlen + 1] == 's'
27104 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27105 {
27106 char_u *str;
27107 char_u *pat;
27108 char_u *sub;
27109 int sep;
27110 char_u *flags;
27111 int didit = FALSE;
27112
27113 flags = (char_u *)"";
27114 s = src + *usedlen + 2;
27115 if (src[*usedlen + 1] == 'g')
27116 {
27117 flags = (char_u *)"g";
27118 ++s;
27119 }
27120
27121 sep = *s++;
27122 if (sep)
27123 {
27124 /* find end of pattern */
27125 p = vim_strchr(s, sep);
27126 if (p != NULL)
27127 {
27128 pat = vim_strnsave(s, (int)(p - s));
27129 if (pat != NULL)
27130 {
27131 s = p + 1;
27132 /* find end of substitution */
27133 p = vim_strchr(s, sep);
27134 if (p != NULL)
27135 {
27136 sub = vim_strnsave(s, (int)(p - s));
27137 str = vim_strnsave(*fnamep, *fnamelen);
27138 if (sub != NULL && str != NULL)
27139 {
27140 *usedlen = (int)(p + 1 - src);
27141 s = do_string_sub(str, pat, sub, flags);
27142 if (s != NULL)
27143 {
27144 *fnamep = s;
27145 *fnamelen = (int)STRLEN(s);
27146 vim_free(*bufp);
27147 *bufp = s;
27148 didit = TRUE;
27149 }
27150 }
27151 vim_free(sub);
27152 vim_free(str);
27153 }
27154 vim_free(pat);
27155 }
27156 }
27157 /* after using ":s", repeat all the modifiers */
27158 if (didit)
27159 goto repeat;
27160 }
27161 }
27162
Bram Moolenaar26df0922014-02-23 23:39:13 +010027163 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27164 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027165 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027166 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027167 if (c != NUL)
27168 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027169 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027170 if (c != NUL)
27171 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027172 if (p == NULL)
27173 return -1;
27174 vim_free(*bufp);
27175 *bufp = *fnamep = p;
27176 *fnamelen = (int)STRLEN(p);
27177 *usedlen += 2;
27178 }
27179
Bram Moolenaar071d4272004-06-13 20:20:40 +000027180 return valid;
27181}
27182
27183/*
27184 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27185 * "flags" can be "g" to do a global substitute.
27186 * Returns an allocated string, NULL for error.
27187 */
27188 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027189do_string_sub(
27190 char_u *str,
27191 char_u *pat,
27192 char_u *sub,
27193 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027194{
27195 int sublen;
27196 regmatch_T regmatch;
27197 int i;
27198 int do_all;
27199 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027200 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027201 garray_T ga;
27202 char_u *ret;
27203 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027204 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027205
27206 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27207 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027208 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027209
27210 ga_init2(&ga, 1, 200);
27211
27212 do_all = (flags[0] == 'g');
27213
27214 regmatch.rm_ic = p_ic;
27215 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27216 if (regmatch.regprog != NULL)
27217 {
27218 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027219 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027220 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27221 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027222 /* Skip empty match except for first match. */
27223 if (regmatch.startp[0] == regmatch.endp[0])
27224 {
27225 if (zero_width == regmatch.startp[0])
27226 {
27227 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027228 i = MB_PTR2LEN(tail);
27229 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27230 (size_t)i);
27231 ga.ga_len += i;
27232 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027233 continue;
27234 }
27235 zero_width = regmatch.startp[0];
27236 }
27237
Bram Moolenaar071d4272004-06-13 20:20:40 +000027238 /*
27239 * Get some space for a temporary buffer to do the substitution
27240 * into. It will contain:
27241 * - The text up to where the match is.
27242 * - The substituted text.
27243 * - The text after the match.
27244 */
27245 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027246 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027247 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27248 {
27249 ga_clear(&ga);
27250 break;
27251 }
27252
27253 /* copy the text up to where the match is */
27254 i = (int)(regmatch.startp[0] - tail);
27255 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27256 /* add the substituted text */
27257 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27258 + ga.ga_len + i, TRUE, TRUE, FALSE);
27259 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027260 tail = regmatch.endp[0];
27261 if (*tail == NUL)
27262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027263 if (!do_all)
27264 break;
27265 }
27266
27267 if (ga.ga_data != NULL)
27268 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27269
Bram Moolenaar473de612013-06-08 18:19:48 +020027270 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027271 }
27272
27273 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27274 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027275 if (p_cpo == empty_option)
27276 p_cpo = save_cpo;
27277 else
27278 /* Darn, evaluating {sub} expression changed the value. */
27279 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027280
27281 return ret;
27282}
27283
27284#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */