blob: 2ca6611843a2185681690e48fd195722fc99a700 [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);
503static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
504static void f_byte2line(typval_T *argvars, typval_T *rettv);
505static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
506static void f_byteidx(typval_T *argvars, typval_T *rettv);
507static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
508static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000509#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100510static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100514static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
515static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100516static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100517static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100518static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100519static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100520static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
521static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100522static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100523static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100524static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
525static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100526static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100527static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100528#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_changenr(typval_T *argvars, typval_T *rettv);
530static void f_char2nr(typval_T *argvars, typval_T *rettv);
531static void f_cindent(typval_T *argvars, typval_T *rettv);
532static void f_clearmatches(typval_T *argvars, typval_T *rettv);
533static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000534#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100535static void f_complete(typval_T *argvars, typval_T *rettv);
536static void f_complete_add(typval_T *argvars, typval_T *rettv);
537static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000538#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_confirm(typval_T *argvars, typval_T *rettv);
540static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000541#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_cos(typval_T *argvars, typval_T *rettv);
543static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100545static void f_count(typval_T *argvars, typval_T *rettv);
546static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
547static void f_cursor(typval_T *argsvars, typval_T *rettv);
548static void f_deepcopy(typval_T *argvars, typval_T *rettv);
549static void f_delete(typval_T *argvars, typval_T *rettv);
550static void f_did_filetype(typval_T *argvars, typval_T *rettv);
551static void f_diff_filler(typval_T *argvars, typval_T *rettv);
552static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
553static void f_empty(typval_T *argvars, typval_T *rettv);
554static void f_escape(typval_T *argvars, typval_T *rettv);
555static void f_eval(typval_T *argvars, typval_T *rettv);
556static void f_eventhandler(typval_T *argvars, typval_T *rettv);
557static void f_executable(typval_T *argvars, typval_T *rettv);
558static void f_exepath(typval_T *argvars, typval_T *rettv);
559static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_expand(typval_T *argvars, typval_T *rettv);
564static void f_extend(typval_T *argvars, typval_T *rettv);
565static void f_feedkeys(typval_T *argvars, typval_T *rettv);
566static void f_filereadable(typval_T *argvars, typval_T *rettv);
567static void f_filewritable(typval_T *argvars, typval_T *rettv);
568static void f_filter(typval_T *argvars, typval_T *rettv);
569static void f_finddir(typval_T *argvars, typval_T *rettv);
570static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000571#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100572static void f_float2nr(typval_T *argvars, typval_T *rettv);
573static void f_floor(typval_T *argvars, typval_T *rettv);
574static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000575#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100576static void f_fnameescape(typval_T *argvars, typval_T *rettv);
577static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
578static void f_foldclosed(typval_T *argvars, typval_T *rettv);
579static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
580static void f_foldlevel(typval_T *argvars, typval_T *rettv);
581static void f_foldtext(typval_T *argvars, typval_T *rettv);
582static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
583static void f_foreground(typval_T *argvars, typval_T *rettv);
584static void f_function(typval_T *argvars, typval_T *rettv);
585static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
586static void f_get(typval_T *argvars, typval_T *rettv);
587static void f_getbufline(typval_T *argvars, typval_T *rettv);
588static void f_getbufvar(typval_T *argvars, typval_T *rettv);
589static void f_getchar(typval_T *argvars, typval_T *rettv);
590static void f_getcharmod(typval_T *argvars, typval_T *rettv);
591static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
592static void f_getcmdline(typval_T *argvars, typval_T *rettv);
593static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
594static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
595static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
596static void f_getcwd(typval_T *argvars, typval_T *rettv);
597static void f_getfontname(typval_T *argvars, typval_T *rettv);
598static void f_getfperm(typval_T *argvars, typval_T *rettv);
599static void f_getfsize(typval_T *argvars, typval_T *rettv);
600static void f_getftime(typval_T *argvars, typval_T *rettv);
601static void f_getftype(typval_T *argvars, typval_T *rettv);
602static void f_getline(typval_T *argvars, typval_T *rettv);
603static void f_getmatches(typval_T *argvars, typval_T *rettv);
604static void f_getpid(typval_T *argvars, typval_T *rettv);
605static void f_getcurpos(typval_T *argvars, typval_T *rettv);
606static void f_getpos(typval_T *argvars, typval_T *rettv);
607static void f_getqflist(typval_T *argvars, typval_T *rettv);
608static void f_getreg(typval_T *argvars, typval_T *rettv);
609static void f_getregtype(typval_T *argvars, typval_T *rettv);
610static void f_gettabvar(typval_T *argvars, typval_T *rettv);
611static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
612static void f_getwinposx(typval_T *argvars, typval_T *rettv);
613static void f_getwinposy(typval_T *argvars, typval_T *rettv);
614static void f_getwinvar(typval_T *argvars, typval_T *rettv);
615static void f_glob(typval_T *argvars, typval_T *rettv);
616static void f_globpath(typval_T *argvars, typval_T *rettv);
617static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
618static void f_has(typval_T *argvars, typval_T *rettv);
619static void f_has_key(typval_T *argvars, typval_T *rettv);
620static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
621static void f_hasmapto(typval_T *argvars, typval_T *rettv);
622static void f_histadd(typval_T *argvars, typval_T *rettv);
623static void f_histdel(typval_T *argvars, typval_T *rettv);
624static void f_histget(typval_T *argvars, typval_T *rettv);
625static void f_histnr(typval_T *argvars, typval_T *rettv);
626static void f_hlID(typval_T *argvars, typval_T *rettv);
627static void f_hlexists(typval_T *argvars, typval_T *rettv);
628static void f_hostname(typval_T *argvars, typval_T *rettv);
629static void f_iconv(typval_T *argvars, typval_T *rettv);
630static void f_indent(typval_T *argvars, typval_T *rettv);
631static void f_index(typval_T *argvars, typval_T *rettv);
632static void f_input(typval_T *argvars, typval_T *rettv);
633static void f_inputdialog(typval_T *argvars, typval_T *rettv);
634static void f_inputlist(typval_T *argvars, typval_T *rettv);
635static void f_inputrestore(typval_T *argvars, typval_T *rettv);
636static void f_inputsave(typval_T *argvars, typval_T *rettv);
637static void f_inputsecret(typval_T *argvars, typval_T *rettv);
638static void f_insert(typval_T *argvars, typval_T *rettv);
639static void f_invert(typval_T *argvars, typval_T *rettv);
640static void f_isdirectory(typval_T *argvars, typval_T *rettv);
641static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100642#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
643static void f_isnan(typval_T *argvars, typval_T *rettv);
644#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100647static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100648static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100649static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100650static void f_job_start(typval_T *argvars, typval_T *rettv);
651static void f_job_stop(typval_T *argvars, typval_T *rettv);
652static void f_job_status(typval_T *argvars, typval_T *rettv);
653#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100655static void f_js_decode(typval_T *argvars, typval_T *rettv);
656static void f_js_encode(typval_T *argvars, typval_T *rettv);
657static void f_json_decode(typval_T *argvars, typval_T *rettv);
658static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_keys(typval_T *argvars, typval_T *rettv);
660static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
661static void f_len(typval_T *argvars, typval_T *rettv);
662static void f_libcall(typval_T *argvars, typval_T *rettv);
663static void f_libcallnr(typval_T *argvars, typval_T *rettv);
664static void f_line(typval_T *argvars, typval_T *rettv);
665static void f_line2byte(typval_T *argvars, typval_T *rettv);
666static void f_lispindent(typval_T *argvars, typval_T *rettv);
667static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_log(typval_T *argvars, typval_T *rettv);
670static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200674#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_map(typval_T *argvars, typval_T *rettv);
676static void f_maparg(typval_T *argvars, typval_T *rettv);
677static void f_mapcheck(typval_T *argvars, typval_T *rettv);
678static void f_match(typval_T *argvars, typval_T *rettv);
679static void f_matchadd(typval_T *argvars, typval_T *rettv);
680static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
681static void f_matcharg(typval_T *argvars, typval_T *rettv);
682static void f_matchdelete(typval_T *argvars, typval_T *rettv);
683static void f_matchend(typval_T *argvars, typval_T *rettv);
684static void f_matchlist(typval_T *argvars, typval_T *rettv);
685static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200686static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_max(typval_T *argvars, typval_T *rettv);
688static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100693#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
697static void f_nr2char(typval_T *argvars, typval_T *rettv);
698static void f_or(typval_T *argvars, typval_T *rettv);
699static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100700#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100702#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
707static void f_printf(typval_T *argvars, typval_T *rettv);
708static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#endif
712#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200714#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_range(typval_T *argvars, typval_T *rettv);
716static void f_readfile(typval_T *argvars, typval_T *rettv);
717static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100718#ifdef FEAT_FLOAT
719static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_reltimestr(typval_T *argvars, typval_T *rettv);
722static void f_remote_expr(typval_T *argvars, typval_T *rettv);
723static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
724static void f_remote_peek(typval_T *argvars, typval_T *rettv);
725static void f_remote_read(typval_T *argvars, typval_T *rettv);
726static void f_remote_send(typval_T *argvars, typval_T *rettv);
727static void f_remove(typval_T *argvars, typval_T *rettv);
728static void f_rename(typval_T *argvars, typval_T *rettv);
729static void f_repeat(typval_T *argvars, typval_T *rettv);
730static void f_resolve(typval_T *argvars, typval_T *rettv);
731static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_screenattr(typval_T *argvars, typval_T *rettv);
736static void f_screenchar(typval_T *argvars, typval_T *rettv);
737static void f_screencol(typval_T *argvars, typval_T *rettv);
738static void f_screenrow(typval_T *argvars, typval_T *rettv);
739static void f_search(typval_T *argvars, typval_T *rettv);
740static void f_searchdecl(typval_T *argvars, typval_T *rettv);
741static void f_searchpair(typval_T *argvars, typval_T *rettv);
742static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
743static void f_searchpos(typval_T *argvars, typval_T *rettv);
744static void f_server2client(typval_T *argvars, typval_T *rettv);
745static void f_serverlist(typval_T *argvars, typval_T *rettv);
746static void f_setbufvar(typval_T *argvars, typval_T *rettv);
747static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
748static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100749static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_setline(typval_T *argvars, typval_T *rettv);
751static void f_setloclist(typval_T *argvars, typval_T *rettv);
752static void f_setmatches(typval_T *argvars, typval_T *rettv);
753static void f_setpos(typval_T *argvars, typval_T *rettv);
754static void f_setqflist(typval_T *argvars, typval_T *rettv);
755static void f_setreg(typval_T *argvars, typval_T *rettv);
756static void f_settabvar(typval_T *argvars, typval_T *rettv);
757static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
758static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100759#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100761#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_shellescape(typval_T *argvars, typval_T *rettv);
763static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
764static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sin(typval_T *argvars, typval_T *rettv);
767static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sort(typval_T *argvars, typval_T *rettv);
770static void f_soundfold(typval_T *argvars, typval_T *rettv);
771static void f_spellbadword(typval_T *argvars, typval_T *rettv);
772static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
773static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sqrt(typval_T *argvars, typval_T *rettv);
776static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_str2nr(typval_T *argvars, typval_T *rettv);
779static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000782#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200783static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_stridx(typval_T *argvars, typval_T *rettv);
785static void f_string(typval_T *argvars, typval_T *rettv);
786static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200787static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100788static void f_strpart(typval_T *argvars, typval_T *rettv);
789static void f_strridx(typval_T *argvars, typval_T *rettv);
790static void f_strtrans(typval_T *argvars, typval_T *rettv);
791static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
792static void f_strwidth(typval_T *argvars, typval_T *rettv);
793static void f_submatch(typval_T *argvars, typval_T *rettv);
794static void f_substitute(typval_T *argvars, typval_T *rettv);
795static void f_synID(typval_T *argvars, typval_T *rettv);
796static void f_synIDattr(typval_T *argvars, typval_T *rettv);
797static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
798static void f_synstack(typval_T *argvars, typval_T *rettv);
799static void f_synconcealed(typval_T *argvars, typval_T *rettv);
800static void f_system(typval_T *argvars, typval_T *rettv);
801static void f_systemlist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
804static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
805static void f_taglist(typval_T *argvars, typval_T *rettv);
806static void f_tagfiles(typval_T *argvars, typval_T *rettv);
807static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200808static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
809static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200810static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
811#ifdef FEAT_JOB_CHANNEL
812static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
813#endif
814static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
815#ifdef FEAT_JOB_CHANNEL
816static void f_test_null_job(typval_T *argvars, typval_T *rettv);
817#endif
818static void f_test_null_list(typval_T *argvars, typval_T *rettv);
819static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
820static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200821#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_tan(typval_T *argvars, typval_T *rettv);
823static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200824#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100825#ifdef FEAT_TIMERS
826static void f_timer_start(typval_T *argvars, typval_T *rettv);
827static void f_timer_stop(typval_T *argvars, typval_T *rettv);
828#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100829static void f_tolower(typval_T *argvars, typval_T *rettv);
830static void f_toupper(typval_T *argvars, typval_T *rettv);
831static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000834#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static void f_type(typval_T *argvars, typval_T *rettv);
836static void f_undofile(typval_T *argvars, typval_T *rettv);
837static void f_undotree(typval_T *argvars, typval_T *rettv);
838static void f_uniq(typval_T *argvars, typval_T *rettv);
839static void f_values(typval_T *argvars, typval_T *rettv);
840static void f_virtcol(typval_T *argvars, typval_T *rettv);
841static void f_visualmode(typval_T *argvars, typval_T *rettv);
842static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100843static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100844static void f_win_getid(typval_T *argvars, typval_T *rettv);
845static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
846static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
847static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static void f_winbufnr(typval_T *argvars, typval_T *rettv);
849static void f_wincol(typval_T *argvars, typval_T *rettv);
850static void f_winheight(typval_T *argvars, typval_T *rettv);
851static void f_winline(typval_T *argvars, typval_T *rettv);
852static void f_winnr(typval_T *argvars, typval_T *rettv);
853static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
854static void f_winrestview(typval_T *argvars, typval_T *rettv);
855static void f_winsaveview(typval_T *argvars, typval_T *rettv);
856static void f_winwidth(typval_T *argvars, typval_T *rettv);
857static void f_writefile(typval_T *argvars, typval_T *rettv);
858static void f_wordcount(typval_T *argvars, typval_T *rettv);
859static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000860
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100861static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
862static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
863static int get_env_len(char_u **arg);
864static int get_id_len(char_u **arg);
865static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
866static 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 +0000867#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
868#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
869 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100870static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
871static int eval_isnamec(int c);
872static int eval_isnamec1(int c);
873static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
874static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100875static typval_T *alloc_string_tv(char_u *string);
876static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100877#ifdef FEAT_FLOAT
878static float_T get_tv_float(typval_T *varp);
879#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static linenr_T get_tv_lnum(typval_T *argvars);
881static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
883static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
884static hashtab_T *find_var_ht(char_u *name, char_u **varname);
885static funccall_T *get_funccal(void);
886static void vars_clear_ext(hashtab_T *ht, int free_val);
887static void delete_var(hashtab_T *ht, hashitem_T *hi);
888static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
889static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
890static void set_var(char_u *name, typval_T *varp, int copy);
891static int var_check_ro(int flags, char_u *name, int use_gettext);
892static int var_check_fixed(int flags, char_u *name, int use_gettext);
893static int var_check_func_name(char_u *name, int new_var);
894static int valid_varname(char_u *varname);
895static int tv_check_lock(int lock, char_u *name, int use_gettext);
896static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
897static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100898static 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 +0100899static int eval_fname_script(char_u *p);
900static int eval_fname_sid(char_u *p);
901static void list_func_head(ufunc_T *fp, int indent);
902static ufunc_T *find_func(char_u *name);
903static int function_exists(char_u *name);
904static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100906static void func_do_profile(ufunc_T *fp);
907static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
908static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000909static int
910# ifdef __BORLANDC__
911 _RTLENTRYF
912# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100913 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000914static int
915# ifdef __BORLANDC__
916 _RTLENTRYF
917# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100918 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000919#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100920static int script_autoload(char_u *name, int reload);
921static char_u *autoload_name(char_u *name);
922static void cat_func_name(char_u *buf, ufunc_T *fp);
923static void func_free(ufunc_T *fp);
924static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
925static int can_free_funccal(funccall_T *fc, int copyID) ;
926static void free_funccal(funccall_T *fc, int free_val);
927static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
928static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
929static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
930static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
931static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
932static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
933static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
934static int write_list(FILE *fd, list_T *list, int binary);
935static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000936
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200937
938#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100939static int compare_func_name(const void *s1, const void *s2);
940static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200941#endif
942
Bram Moolenaar33570922005-01-25 22:26:29 +0000943/*
944 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000945 */
946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100947eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000948{
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 int i;
950 struct vimvar *p;
951
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200952 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
953 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200954 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000955 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000956 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000957
958 for (i = 0; i < VV_LEN; ++i)
959 {
960 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200961 if (STRLEN(p->vv_name) > 16)
962 {
963 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
964 getout(1);
965 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 STRCPY(p->vv_di.di_key, p->vv_name);
967 if (p->vv_flags & VV_RO)
968 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
969 else if (p->vv_flags & VV_RO_SBX)
970 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
971 else
972 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000973
974 /* add to v: scope dict, unless the value is not always available */
975 if (p->vv_type != VAR_UNKNOWN)
976 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000977 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000978 /* add to compat scope dict */
979 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000980 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100981 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
982
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000983 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100984 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200985 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100986 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100987
988 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
989 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
990 set_vim_var_nr(VV_NONE, VVAL_NONE);
991 set_vim_var_nr(VV_NULL, VVAL_NULL);
992
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200993 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200994
995#ifdef EBCDIC
996 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100997 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200998 */
999 sortFunctions();
1000#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001001}
1002
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001003#if defined(EXITFREE) || defined(PROTO)
1004 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001005eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001006{
1007 int i;
1008 struct vimvar *p;
1009
1010 for (i = 0; i < VV_LEN; ++i)
1011 {
1012 p = &vimvars[i];
1013 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001014 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001015 vim_free(p->vv_str);
1016 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001017 }
1018 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1019 {
1020 list_unref(p->vv_list);
1021 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001022 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001023 }
1024 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001025 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001026 hash_clear(&compat_hashtab);
1027
Bram Moolenaard9fba312005-06-26 22:34:35 +00001028 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001030 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001031# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001032
1033 /* global variables */
1034 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001035
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001036 /* autoloaded script names */
1037 ga_clear_strings(&ga_loaded);
1038
Bram Moolenaarcca74132013-09-25 21:00:28 +02001039 /* Script-local variables. First clear all the variables and in a second
1040 * loop free the scriptvar_T, because a variable in one script might hold
1041 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001042 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001044 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 ga_clear(&ga_scripts);
1047
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001048 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001049 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001050
1051 /* functions */
1052 free_all_functions();
1053 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001054}
1055#endif
1056
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 * Return the name of the executed function.
1059 */
1060 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001061func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
1065
1066/*
1067 * Return the address holding the next breakpoint line for a funccall cookie.
1068 */
1069 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001070func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaar33570922005-01-25 22:26:29 +00001072 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073}
1074
1075/*
1076 * Return the address holding the debug tick for a funccall cookie.
1077 */
1078 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082}
1083
1084/*
1085 * Return the nesting level for a funccall cookie.
1086 */
1087 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001088func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
Bram Moolenaar33570922005-01-25 22:26:29 +00001090 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091}
1092
1093/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001094funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001096/* pointer to list of previously used funccal, still around because some
1097 * item in it is still being used. */
1098funccall_T *previous_funccal = NULL;
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100/*
1101 * Return TRUE when a function was ended by a ":return" command.
1102 */
1103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001104current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
1106 return current_funccal->returned;
1107}
1108
1109
1110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 * Set an internal variable to a string value. Creates the variable if it does
1112 * not already exist.
1113 */
1114 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001115set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001117 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001118 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
1120 val = vim_strsave(value);
1121 if (val != NULL)
1122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001126 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 }
1130}
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001133static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134static char_u *redir_endp = NULL;
1135static char_u *redir_varname = NULL;
1136
1137/*
1138 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001139 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 * Returns OK if successfully completed the setup. FAIL otherwise.
1141 */
1142 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001143var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144{
1145 int save_emsg;
1146 int err;
1147 typval_T tv;
1148
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001150 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 {
1152 EMSG(_(e_invarg));
1153 return FAIL;
1154 }
1155
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 redir_varname = vim_strsave(name);
1158 if (redir_varname == NULL)
1159 return FAIL;
1160
1161 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1162 if (redir_lval == NULL)
1163 {
1164 var_redir_stop();
1165 return FAIL;
1166 }
1167
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001168 /* The output is stored in growarray "redir_ga" until redirection ends. */
1169 ga_init2(&redir_ga, (int)sizeof(char), 500);
1170
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001172 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001173 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1175 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001176 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177 if (redir_endp != NULL && *redir_endp != NUL)
1178 /* Trailing characters are present after the variable name */
1179 EMSG(_(e_trailing));
1180 else
1181 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001182 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 var_redir_stop();
1184 return FAIL;
1185 }
1186
1187 /* check if we can write to the variable: set it to or append an empty
1188 * string */
1189 save_emsg = did_emsg;
1190 did_emsg = FALSE;
1191 tv.v_type = VAR_STRING;
1192 tv.vval.v_string = (char_u *)"";
1193 if (append)
1194 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1195 else
1196 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001197 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001199 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 if (err)
1201 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001202 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 var_redir_stop();
1204 return FAIL;
1205 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206
1207 return OK;
1208}
1209
1210/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 * Append "value[value_len]" to the variable set by var_redir_start().
1212 * The actual appending is postponed until redirection ends, because the value
1213 * appended may in fact be the string we write to, changing it may cause freed
1214 * memory to be used:
1215 * :redir => foo
1216 * :let foo
1217 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218 */
1219 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001220var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001222 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223
1224 if (redir_lval == NULL)
1225 return;
1226
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001227 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001228 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001232 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233 {
1234 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001235 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236 }
1237 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239}
1240
1241/*
1242 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001243 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001244 */
1245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001246var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001248 typval_T tv;
1249
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001250 if (redir_lval != NULL)
1251 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001252 /* If there was no error: assign the text to the variable. */
1253 if (redir_endp != NULL)
1254 {
1255 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1256 tv.v_type = VAR_STRING;
1257 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001258 /* Call get_lval() again, if it's inside a Dict or List it may
1259 * have changed. */
1260 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001261 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001262 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1263 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1264 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001265 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001266
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001267 /* free the collected output */
1268 vim_free(redir_ga.ga_data);
1269 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001270
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001271 vim_free(redir_lval);
1272 redir_lval = NULL;
1273 }
1274 vim_free(redir_varname);
1275 redir_varname = NULL;
1276}
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278# if defined(FEAT_MBYTE) || defined(PROTO)
1279 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001280eval_charconvert(
1281 char_u *enc_from,
1282 char_u *enc_to,
1283 char_u *fname_from,
1284 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 int err = FALSE;
1287
1288 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1289 set_vim_var_string(VV_CC_TO, enc_to, -1);
1290 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1291 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1292 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1293 err = TRUE;
1294 set_vim_var_string(VV_CC_FROM, NULL, -1);
1295 set_vim_var_string(VV_CC_TO, NULL, -1);
1296 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1297 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1298
1299 if (err)
1300 return FAIL;
1301 return OK;
1302}
1303# endif
1304
1305# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1306 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001307eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 int err = FALSE;
1310
1311 set_vim_var_string(VV_FNAME_IN, fname, -1);
1312 set_vim_var_string(VV_CMDARG, args, -1);
1313 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1314 err = TRUE;
1315 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1316 set_vim_var_string(VV_CMDARG, NULL, -1);
1317
1318 if (err)
1319 {
1320 mch_remove(fname);
1321 return FAIL;
1322 }
1323 return OK;
1324}
1325# endif
1326
1327# if defined(FEAT_DIFF) || defined(PROTO)
1328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329eval_diff(
1330 char_u *origfile,
1331 char_u *newfile,
1332 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 int err = FALSE;
1335
1336 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1337 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1338 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1339 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1340 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1341 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1342 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1343}
1344
1345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001346eval_patch(
1347 char_u *origfile,
1348 char_u *difffile,
1349 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 int err;
1352
1353 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1354 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1355 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1356 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1357 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1358 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1359 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1360}
1361# endif
1362
1363/*
1364 * Top level evaluation function, returning a boolean.
1365 * Sets "error" to TRUE if there was an error.
1366 * Return TRUE or FALSE.
1367 */
1368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369eval_to_bool(
1370 char_u *arg,
1371 int *error,
1372 char_u **nextcmd,
1373 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 int retval = FALSE;
1377
1378 if (skip)
1379 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 else
1383 {
1384 *error = FALSE;
1385 if (!skip)
1386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001387 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 }
1391 if (skip)
1392 --emsg_skip;
1393
1394 return retval;
1395}
1396
1397/*
1398 * Top level evaluation function, returning a string. If "skip" is TRUE,
1399 * only parsing to "nextcmd" is done, without reporting errors. Return
1400 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1401 */
1402 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001403eval_to_string_skip(
1404 char_u *arg,
1405 char_u **nextcmd,
1406 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaar33570922005-01-25 22:26:29 +00001408 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 char_u *retval;
1410
1411 if (skip)
1412 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 retval = NULL;
1415 else
1416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 retval = vim_strsave(get_tv_string(&tv));
1418 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 if (skip)
1421 --emsg_skip;
1422
1423 return retval;
1424}
1425
1426/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001427 * Skip over an expression at "*pp".
1428 * Return FAIL for an error, OK otherwise.
1429 */
1430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001431skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001434
1435 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001437}
1438
1439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001441 * When "convert" is TRUE convert a List into a sequence of lines and convert
1442 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 * Return pointer to allocated memory, or NULL for failure.
1444 */
1445 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446eval_to_string(
1447 char_u *arg,
1448 char_u **nextcmd,
1449 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar33570922005-01-25 22:26:29 +00001451 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001453 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001454#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001455 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001458 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 retval = NULL;
1460 else
1461 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001462 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001463 {
1464 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001466 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001467 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001468 if (tv.vval.v_list->lv_len > 0)
1469 ga_append(&ga, NL);
1470 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001471 ga_append(&ga, NUL);
1472 retval = (char_u *)ga.ga_data;
1473 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001474#ifdef FEAT_FLOAT
1475 else if (convert && tv.v_type == VAR_FLOAT)
1476 {
1477 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1478 retval = vim_strsave(numbuf);
1479 }
1480#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001481 else
1482 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001483 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485
1486 return retval;
1487}
1488
1489/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001490 * Call eval_to_string() without using current local variables and using
1491 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 */
1493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001494eval_to_string_safe(
1495 char_u *arg,
1496 char_u **nextcmd,
1497 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 char_u *retval;
1500 void *save_funccalp;
1501
1502 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001503 if (use_sandbox)
1504 ++sandbox;
1505 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001506 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001507 if (use_sandbox)
1508 --sandbox;
1509 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 restore_funccal(save_funccalp);
1511 return retval;
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
1515 * Top level evaluation function, returning a number.
1516 * Evaluates "expr" silently.
1517 * Returns -1 for an error.
1518 */
1519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar33570922005-01-25 22:26:29 +00001522 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001524 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526 ++emsg_off;
1527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001528 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 retval = -1;
1530 else
1531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001532 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001533 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
1535 --emsg_off;
1536
1537 return retval;
1538}
1539
Bram Moolenaara40058a2005-07-11 22:42:07 +00001540/*
1541 * Prepare v: variable "idx" to be used.
1542 * Save the current typeval in "save_tv".
1543 * When not used yet add the variable to the v: hashtable.
1544 */
1545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001546prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001547{
1548 *save_tv = vimvars[idx].vv_tv;
1549 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1550 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1551}
1552
1553/*
1554 * Restore v: variable "idx" to typeval "save_tv".
1555 * When no longer defined, remove the variable from the v: hashtable.
1556 */
1557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001558restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001559{
1560 hashitem_T *hi;
1561
Bram Moolenaara40058a2005-07-11 22:42:07 +00001562 vimvars[idx].vv_tv = *save_tv;
1563 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1564 {
1565 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1566 if (HASHITEM_EMPTY(hi))
1567 EMSG2(_(e_intern2), "restore_vimvar()");
1568 else
1569 hash_remove(&vimvarht, hi);
1570 }
1571}
1572
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001573#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001574/*
1575 * Evaluate an expression to a list with suggestions.
1576 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001577 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001578 */
1579 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001580eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001581{
1582 typval_T save_val;
1583 typval_T rettv;
1584 list_T *list = NULL;
1585 char_u *p = skipwhite(expr);
1586
1587 /* Set "v:val" to the bad word. */
1588 prepare_vimvar(VV_VAL, &save_val);
1589 vimvars[VV_VAL].vv_type = VAR_STRING;
1590 vimvars[VV_VAL].vv_str = badword;
1591 if (p_verbose == 0)
1592 ++emsg_off;
1593
1594 if (eval1(&p, &rettv, TRUE) == OK)
1595 {
1596 if (rettv.v_type != VAR_LIST)
1597 clear_tv(&rettv);
1598 else
1599 list = rettv.vval.v_list;
1600 }
1601
1602 if (p_verbose == 0)
1603 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001604 restore_vimvar(VV_VAL, &save_val);
1605
1606 return list;
1607}
1608
1609/*
1610 * "list" is supposed to contain two items: a word and a number. Return the
1611 * word in "pp" and the number as the return value.
1612 * Return -1 if anything isn't right.
1613 * Used to get the good word and score from the eval_spell_expr() result.
1614 */
1615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001617{
1618 listitem_T *li;
1619
1620 li = list->lv_first;
1621 if (li == NULL)
1622 return -1;
1623 *pp = get_tv_string(&li->li_tv);
1624
1625 li = li->li_next;
1626 if (li == NULL)
1627 return -1;
1628 return get_tv_number(&li->li_tv);
1629}
1630#endif
1631
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001632/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001633 * Top level evaluation function.
1634 * Returns an allocated typval_T with the result.
1635 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001636 */
1637 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001638eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001639{
1640 typval_T *tv;
1641
1642 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001643 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001644 {
1645 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001646 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 }
1648
1649 return tv;
1650}
1651
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001655 * Uses argv[argc] for the function arguments. Only Number and String
1656 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001659 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660call_vim_function(
1661 char_u *func,
1662 int argc,
1663 char_u **argv,
1664 int safe, /* use the sandbox */
1665 int str_arg_only, /* all arguments are strings */
1666 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
Bram Moolenaar33570922005-01-25 22:26:29 +00001668 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 long n;
1670 int len;
1671 int i;
1672 int doesrange;
1673 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001676 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001678 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 for (i = 0; i < argc; i++)
1681 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001682 /* Pass a NULL or empty argument as an empty string */
1683 if (argv[i] == NULL || *argv[i] == NUL)
1684 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001685 argvars[i].v_type = VAR_STRING;
1686 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001687 continue;
1688 }
1689
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001690 if (str_arg_only)
1691 len = 0;
1692 else
1693 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001694 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (len != 0 && len == (int)STRLEN(argv[i]))
1696 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001697 argvars[i].v_type = VAR_NUMBER;
1698 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 }
1700 else
1701 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001702 argvars[i].v_type = VAR_STRING;
1703 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 }
1705 }
1706
1707 if (safe)
1708 {
1709 save_funccalp = save_funccal();
1710 ++sandbox;
1711 }
1712
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001713 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1714 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001716 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (safe)
1718 {
1719 --sandbox;
1720 restore_funccal(save_funccalp);
1721 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 vim_free(argvars);
1723
1724 if (ret == FAIL)
1725 clear_tv(rettv);
1726
1727 return ret;
1728}
1729
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001730/*
1731 * Call vimL function "func" and return the result as a number.
1732 * Returns -1 when calling the function fails.
1733 * Uses argv[argc] for the function arguments.
1734 */
1735 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001736call_func_retnr(
1737 char_u *func,
1738 int argc,
1739 char_u **argv,
1740 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001741{
1742 typval_T rettv;
1743 long retval;
1744
1745 /* All arguments are passed as strings, no conversion to number. */
1746 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1747 return -1;
1748
1749 retval = get_tv_number_chk(&rettv, NULL);
1750 clear_tv(&rettv);
1751 return retval;
1752}
1753
1754#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1755 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1756
Bram Moolenaar4f688582007-07-24 12:34:30 +00001757# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001759 * Call vimL function "func" and return the result as a string.
1760 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761 * Uses argv[argc] for the function arguments.
1762 */
1763 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764call_func_retstr(
1765 char_u *func,
1766 int argc,
1767 char_u **argv,
1768 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001769{
1770 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001771 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001773 /* All arguments are passed as strings, no conversion to number. */
1774 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001775 return NULL;
1776
1777 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001778 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 return retval;
1780}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001781# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001782
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001783/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001784 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001786 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001787 */
1788 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001789call_func_retlist(
1790 char_u *func,
1791 int argc,
1792 char_u **argv,
1793 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001794{
1795 typval_T rettv;
1796
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001797 /* All arguments are passed as strings, no conversion to number. */
1798 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001799 return NULL;
1800
1801 if (rettv.v_type != VAR_LIST)
1802 {
1803 clear_tv(&rettv);
1804 return NULL;
1805 }
1806
1807 return rettv.vval.v_list;
1808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
1810
1811/*
1812 * Save the current function call pointer, and set it to NULL.
1813 * Used when executing autocommands and for ":source".
1814 */
1815 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001818 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 current_funccal = NULL;
1821 return (void *)fc;
1822}
1823
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001827 funccall_T *fc = (funccall_T *)vfc;
1828
1829 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830}
1831
Bram Moolenaar05159a02005-02-26 23:04:13 +00001832#if defined(FEAT_PROFILE) || defined(PROTO)
1833/*
1834 * Prepare profiling for entering a child or something else that is not
1835 * counted for the script/function itself.
1836 * Should always be called in pair with prof_child_exit().
1837 */
1838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001839prof_child_enter(
1840 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001841{
1842 funccall_T *fc = current_funccal;
1843
1844 if (fc != NULL && fc->func->uf_profiling)
1845 profile_start(&fc->prof_child);
1846 script_prof_save(tm);
1847}
1848
1849/*
1850 * Take care of time spent in a child.
1851 * Should always be called after prof_child_enter().
1852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854prof_child_exit(
1855 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001856{
1857 funccall_T *fc = current_funccal;
1858
1859 if (fc != NULL && fc->func->uf_profiling)
1860 {
1861 profile_end(&fc->prof_child);
1862 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1863 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1864 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1865 }
1866 script_prof_restore(tm);
1867}
1868#endif
1869
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#ifdef FEAT_FOLDING
1872/*
1873 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1874 * it in "*cp". Doesn't give error messages.
1875 */
1876 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001877eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
Bram Moolenaar33570922005-01-25 22:26:29 +00001879 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 int retval;
1881 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001882 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1883 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001886 if (use_sandbox)
1887 ++sandbox;
1888 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 retval = 0;
1892 else
1893 {
1894 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 if (tv.v_type == VAR_NUMBER)
1896 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001897 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 retval = 0;
1899 else
1900 {
1901 /* If the result is a string, check if there is a non-digit before
1902 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (!VIM_ISDIGIT(*s) && *s != '-')
1905 *cp = *s++;
1906 retval = atol((char *)s);
1907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001911 if (use_sandbox)
1912 --sandbox;
1913 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915 return retval;
1916}
1917#endif
1918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 * ":let" list all variable values
1921 * ":let var1 var2" list variable values
1922 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 * ":let var += expr" assignment command.
1924 * ":let var -= expr" assignment command.
1925 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 */
1928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001929ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
1931 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 int var_count = 0;
1936 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001938 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001939 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaardb552d602006-03-23 22:59:57 +00001941 argend = skip_var_list(arg, &var_count, &semicolon);
1942 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001944 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1945 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 expr = skipwhite(argend);
1947 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1948 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001950 /*
1951 * ":let" without "=": list variables
1952 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 if (*arg == '[')
1954 EMSG(_(e_invarg));
1955 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001957 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001961 list_glob_vars(&first);
1962 list_buf_vars(&first);
1963 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001966#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001967 list_script_vars(&first);
1968 list_func_vars(&first);
1969 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 eap->nextcmd = check_nextcmd(arg);
1972 }
1973 else
1974 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001975 op[0] = '=';
1976 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001977 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001979 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1980 op[0] = *expr; /* +=, -= or .= */
1981 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001983 else
1984 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (eap->skip)
1987 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (eap->skip)
1990 {
1991 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 --emsg_skip;
1994 }
1995 else if (i != FAIL)
1996 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001 }
2002}
2003
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004/*
2005 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2006 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002007 * When "nextchars" is not NULL it points to a string with characters that
2008 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2009 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 * Returns OK or FAIL;
2011 */
2012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002013ex_let_vars(
2014 char_u *arg_start,
2015 typval_T *tv,
2016 int copy, /* copy values from "tv", don't move */
2017 int semicolon, /* from skip_var_list() */
2018 int var_count, /* from skip_var_list() */
2019 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020{
2021 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002024 listitem_T *item;
2025 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026
2027 if (*arg != '[')
2028 {
2029 /*
2030 * ":let var = expr" or ":for var in list"
2031 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 return FAIL;
2034 return OK;
2035 }
2036
2037 /*
2038 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2039 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002040 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 {
2042 EMSG(_(e_listreq));
2043 return FAIL;
2044 }
2045
2046 i = list_len(l);
2047 if (semicolon == 0 && var_count < i)
2048 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002049 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 return FAIL;
2051 }
2052 if (var_count - semicolon > i)
2053 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002054 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055 return FAIL;
2056 }
2057
2058 item = l->lv_first;
2059 while (*arg != ']')
2060 {
2061 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 item = item->li_next;
2064 if (arg == NULL)
2065 return FAIL;
2066
2067 arg = skipwhite(arg);
2068 if (*arg == ';')
2069 {
2070 /* Put the rest of the list (may be empty) in the var after ';'.
2071 * Create a new list for this. */
2072 l = list_alloc();
2073 if (l == NULL)
2074 return FAIL;
2075 while (item != NULL)
2076 {
2077 list_append_tv(l, &item->li_tv);
2078 item = item->li_next;
2079 }
2080
2081 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002082 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083 ltv.vval.v_list = l;
2084 l->lv_refcount = 1;
2085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002086 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2087 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002088 clear_tv(&ltv);
2089 if (arg == NULL)
2090 return FAIL;
2091 break;
2092 }
2093 else if (*arg != ',' && *arg != ']')
2094 {
2095 EMSG2(_(e_intern2), "ex_let_vars()");
2096 return FAIL;
2097 }
2098 }
2099
2100 return OK;
2101}
2102
2103/*
2104 * Skip over assignable variable "var" or list of variables "[var, var]".
2105 * Used for ":let varvar = expr" and ":for varvar in expr".
2106 * For "[var, var]" increment "*var_count" for each variable.
2107 * for "[var, var; var]" set "semicolon".
2108 * Return NULL for an error.
2109 */
2110 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002111skip_var_list(
2112 char_u *arg,
2113 int *var_count,
2114 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002115{
2116 char_u *p, *s;
2117
2118 if (*arg == '[')
2119 {
2120 /* "[var, var]": find the matching ']'. */
2121 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002122 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123 {
2124 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2125 s = skip_var_one(p);
2126 if (s == p)
2127 {
2128 EMSG2(_(e_invarg2), p);
2129 return NULL;
2130 }
2131 ++*var_count;
2132
2133 p = skipwhite(s);
2134 if (*p == ']')
2135 break;
2136 else if (*p == ';')
2137 {
2138 if (*semicolon == 1)
2139 {
2140 EMSG(_("Double ; in list of variables"));
2141 return NULL;
2142 }
2143 *semicolon = 1;
2144 }
2145 else if (*p != ',')
2146 {
2147 EMSG2(_(e_invarg2), p);
2148 return NULL;
2149 }
2150 }
2151 return p + 1;
2152 }
2153 else
2154 return skip_var_one(arg);
2155}
2156
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002157/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002158 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002159 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002160 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002162skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002164 if (*arg == '@' && arg[1] != NUL)
2165 return arg + 2;
2166 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2167 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002168}
2169
Bram Moolenaara7043832005-01-21 11:56:39 +00002170/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002171 * List variables for hashtab "ht" with prefix "prefix".
2172 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_hashtable_vars(
2176 hashtab_T *ht,
2177 char_u *prefix,
2178 int empty,
2179 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002180{
Bram Moolenaar33570922005-01-25 22:26:29 +00002181 hashitem_T *hi;
2182 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002183 int todo;
2184
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002185 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002186 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2187 {
2188 if (!HASHITEM_EMPTY(hi))
2189 {
2190 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002191 di = HI2DI(hi);
2192 if (empty || di->di_tv.v_type != VAR_STRING
2193 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195 }
2196 }
2197}
2198
2199/*
2200 * List global variables.
2201 */
2202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002204{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002206}
2207
2208/*
2209 * List buffer variables.
2210 */
2211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002213{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002214 char_u numbuf[NUMBUFLEN];
2215
Bram Moolenaar429fa852013-04-15 12:27:36 +02002216 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218
2219 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2221 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002222}
2223
2224/*
2225 * List window variables.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002229{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002230 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002232}
2233
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234#ifdef FEAT_WINDOWS
2235/*
2236 * List tab page variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002240{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002241 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243}
2244#endif
2245
Bram Moolenaara7043832005-01-21 11:56:39 +00002246/*
2247 * List Vim variables.
2248 */
2249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253}
2254
2255/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002256 * List script-local variables, if there is a script.
2257 */
2258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002260{
2261 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2263 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002264}
2265
2266/*
2267 * List function variables, if there is a function.
2268 */
2269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271{
2272 if (current_funccal != NULL)
2273 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275}
2276
2277/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 * List variables in "arg".
2279 */
2280 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282{
2283 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 char_u *name_start;
2287 char_u *arg_subsc;
2288 char_u *tofree;
2289 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290
2291 while (!ends_excmd(*arg) && !got_int)
2292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002295 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2297 {
2298 emsg_severe = TRUE;
2299 EMSG(_(e_trailing));
2300 break;
2301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 /* get_name_len() takes care of expanding curly braces */
2306 name_start = name = arg;
2307 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2308 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 /* This is mainly to keep test 49 working: when expanding
2311 * curly braces fails overrule the exception error message. */
2312 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 emsg_severe = TRUE;
2315 EMSG2(_(e_invarg2), arg);
2316 break;
2317 }
2318 error = TRUE;
2319 }
2320 else
2321 {
2322 if (tofree != NULL)
2323 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002324 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 else
2327 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 /* handle d.key, l[idx], f(expr) */
2329 arg_subsc = arg;
2330 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002337 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002338 case 'g': list_glob_vars(first); break;
2339 case 'b': list_buf_vars(first); break;
2340 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002341#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002343#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002344 case 'v': list_vim_vars(first); break;
2345 case 's': list_script_vars(first); break;
2346 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002347 default:
2348 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002349 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002350 }
2351 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002352 {
2353 char_u numbuf[NUMBUFLEN];
2354 char_u *tf;
2355 int c;
2356 char_u *s;
2357
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002358 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002359 c = *arg;
2360 *arg = NUL;
2361 list_one_var_a((char_u *)"",
2362 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002363 tv.v_type,
2364 s == NULL ? (char_u *)"" : s,
2365 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002366 *arg = c;
2367 vim_free(tf);
2368 }
2369 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 }
2372 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002373
2374 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002376
2377 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 }
2379
2380 return arg;
2381}
2382
2383/*
2384 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2385 * Returns a pointer to the char just after the var name.
2386 * Returns NULL if there is an error.
2387 */
2388 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002389ex_let_one(
2390 char_u *arg, /* points to variable name */
2391 typval_T *tv, /* value to assign to variable */
2392 int copy, /* copy value from "tv" */
2393 char_u *endchars, /* valid chars after variable name or NULL */
2394 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002395{
2396 int c1;
2397 char_u *name;
2398 char_u *p;
2399 char_u *arg_end = NULL;
2400 int len;
2401 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002403
2404 /*
2405 * ":let $VAR = expr": Set environment variable.
2406 */
2407 if (*arg == '$')
2408 {
2409 /* Find the end of the name. */
2410 ++arg;
2411 name = arg;
2412 len = get_env_len(&arg);
2413 if (len == 0)
2414 EMSG2(_(e_invarg2), name - 1);
2415 else
2416 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 if (op != NULL && (*op == '+' || *op == '-'))
2418 EMSG2(_(e_letwrong), op);
2419 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002420 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002422 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 {
2424 c1 = name[len];
2425 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 p = get_tv_string_chk(tv);
2427 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 {
2429 int mustfree = FALSE;
2430 char_u *s = vim_getenv(name, &mustfree);
2431
2432 if (s != NULL)
2433 {
2434 p = tofree = concat_str(s, p);
2435 if (mustfree)
2436 vim_free(s);
2437 }
2438 }
2439 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (STRICMP(name, "HOME") == 0)
2443 init_homedir();
2444 else if (didset_vim && STRICMP(name, "VIM") == 0)
2445 didset_vim = FALSE;
2446 else if (didset_vimruntime
2447 && STRICMP(name, "VIMRUNTIME") == 0)
2448 didset_vimruntime = FALSE;
2449 arg_end = arg;
2450 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 }
2454 }
2455 }
2456
2457 /*
2458 * ":let &option = expr": Set option value.
2459 * ":let &l:option = expr": Set local option value.
2460 * ":let &g:option = expr": Set global option value.
2461 */
2462 else if (*arg == '&')
2463 {
2464 /* Find the end of the name. */
2465 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002466 if (p == NULL || (endchars != NULL
2467 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 EMSG(_(e_letunexp));
2469 else
2470 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 long n;
2472 int opt_type;
2473 long numval;
2474 char_u *stringval = NULL;
2475 char_u *s;
2476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 c1 = *p;
2478 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479
2480 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 s = get_tv_string_chk(tv); /* != NULL if number or string */
2482 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 {
2484 opt_type = get_option_value(arg, &numval,
2485 &stringval, opt_flags);
2486 if ((opt_type == 1 && *op == '.')
2487 || (opt_type == 0 && *op != '.'))
2488 EMSG2(_(e_letwrong), op);
2489 else
2490 {
2491 if (opt_type == 1) /* number */
2492 {
2493 if (*op == '+')
2494 n = numval + n;
2495 else
2496 n = numval - n;
2497 }
2498 else if (opt_type == 0 && stringval != NULL) /* string */
2499 {
2500 s = concat_str(stringval, s);
2501 vim_free(stringval);
2502 stringval = s;
2503 }
2504 }
2505 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 if (s != NULL)
2507 {
2508 set_option_value(arg, n, s, opt_flags);
2509 arg_end = p;
2510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 }
2514 }
2515
2516 /*
2517 * ":let @r = expr": Set register contents.
2518 */
2519 else if (*arg == '@')
2520 {
2521 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 if (op != NULL && (*op == '+' || *op == '-'))
2523 EMSG2(_(e_letwrong), op);
2524 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002525 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 EMSG(_(e_letunexp));
2527 else
2528 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002529 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 char_u *s;
2531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 p = get_tv_string_chk(tv);
2533 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002535 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 if (s != NULL)
2537 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002538 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002539 vim_free(s);
2540 }
2541 }
2542 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 arg_end = arg + 1;
2546 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002547 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002548 }
2549 }
2550
2551 /*
2552 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002557 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002559 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2563 EMSG(_(e_letunexp));
2564 else
2565 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 arg_end = p;
2568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 }
2572
2573 else
2574 EMSG2(_(e_invarg2), arg);
2575
2576 return arg_end;
2577}
2578
2579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002580 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2581 */
2582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002584{
2585 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2586 {
2587 EMSG2(_(e_readonlyvar), arg);
2588 return TRUE;
2589 }
2590 return FALSE;
2591}
2592
2593/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 * Get an lval: variable, Dict item or List item that can be assigned a value
2595 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2596 * "name.key", "name.key[expr]" etc.
2597 * Indexing only works if "name" is an existing List or Dictionary.
2598 * "name" points to the start of the name.
2599 * If "rettv" is not NULL it points to the value to be assigned.
2600 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2601 * wrong; must end in space or cmd separator.
2602 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002603 * flags:
2604 * GLV_QUIET: do not give error messages
2605 * GLV_NO_AUTOLOAD: do not use script autoloading
2606 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002608 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 */
2611 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002612get_lval(
2613 char_u *name,
2614 typval_T *rettv,
2615 lval_T *lp,
2616 int unlet,
2617 int skip,
2618 int flags, /* GLV_ values */
2619 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 char_u *expr_start, *expr_end;
2623 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 dictitem_T *v;
2625 typval_T var1;
2626 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002627 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002628 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002630 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002631 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002632 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636
2637 if (skip)
2638 {
2639 /* When skipping just find the end of the name. */
2640 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002641 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 }
2643
2644 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002645 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (expr_start != NULL)
2647 {
2648 /* Don't expand the name when we already know there is an error. */
2649 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2650 && *p != '[' && *p != '.')
2651 {
2652 EMSG(_(e_trailing));
2653 return NULL;
2654 }
2655
2656 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2657 if (lp->ll_exp_name == NULL)
2658 {
2659 /* Report an invalid expression in braces, unless the
2660 * expression evaluation has been cancelled due to an
2661 * aborting error, an interrupt, or an exception. */
2662 if (!aborting() && !quiet)
2663 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002664 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 EMSG2(_(e_invarg2), name);
2666 return NULL;
2667 }
2668 }
2669 lp->ll_name = lp->ll_exp_name;
2670 }
2671 else
2672 lp->ll_name = name;
2673
2674 /* Without [idx] or .key we are done. */
2675 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2676 return p;
2677
2678 cc = *p;
2679 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002680 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (v == NULL && !quiet)
2682 EMSG2(_(e_undefvar), lp->ll_name);
2683 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684 if (v == NULL)
2685 return NULL;
2686
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 /*
2688 * Loop until no more [idx] or .key is following.
2689 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002690 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002692 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2694 && !(lp->ll_tv->v_type == VAR_DICT
2695 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 if (!quiet)
2698 EMSG(_("E689: Can only index a List or Dictionary"));
2699 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (!quiet)
2704 EMSG(_("E708: [:] must come last"));
2705 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002706 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 len = -1;
2709 if (*p == '.')
2710 {
2711 key = p + 1;
2712 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2713 ;
2714 if (len == 0)
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_(e_emptykey));
2718 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 }
2720 p = key + len;
2721 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722 else
2723 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (*p == ':')
2727 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002728 else
2729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 empty1 = FALSE;
2731 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 if (get_tv_string_chk(&var1) == NULL)
2734 {
2735 /* not a number or string */
2736 clear_tv(&var1);
2737 return NULL;
2738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
2740
2741 /* Optionally get the second index [ :expr]. */
2742 if (*p == ':')
2743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002747 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 if (!empty1)
2749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (rettv != NULL && (rettv->v_type != VAR_LIST
2753 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (!empty1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761 p = skipwhite(p + 1);
2762 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 else
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (!empty1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 if (get_tv_string_chk(&var2) == NULL)
2774 {
2775 /* not a number or string */
2776 if (!empty1)
2777 clear_tv(&var1);
2778 clear_tv(&var2);
2779 return NULL;
2780 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 if (!quiet)
2790 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 if (!empty1)
2792 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 }
2797
2798 /* Skip to past ']'. */
2799 ++p;
2800 }
2801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 {
2804 if (len == -1)
2805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002807 key = get_tv_string_chk(&var1); /* is number or string */
2808 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 }
2813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 lp->ll_list = NULL;
2815 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002816 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002817
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002818 /* When assigning to a scope dictionary check that a function and
2819 * variable name is valid (only variable name unless it is l: or
2820 * g: dictionary). Disallow overwriting a builtin function. */
2821 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002822 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002823 int prevval;
2824 int wrong;
2825
2826 if (len != -1)
2827 {
2828 prevval = key[len];
2829 key[len] = NUL;
2830 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002831 else
2832 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002833 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2834 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002835 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002836 || !valid_varname(key);
2837 if (len != -1)
2838 key[len] = prevval;
2839 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002840 return NULL;
2841 }
2842
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002845 /* Can't add "v:" variable. */
2846 if (lp->ll_dict == &vimvardict)
2847 {
2848 EMSG2(_(e_illvar), name);
2849 return NULL;
2850 }
2851
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002852 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 if (len == -1)
2858 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 }
2861 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 if (len == -1)
2866 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 p = NULL;
2869 break;
2870 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002871 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002872 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002873 return NULL;
2874
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 if (len == -1)
2876 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879 else
2880 {
2881 /*
2882 * Get the number and item for the only or first index of the List.
2883 */
2884 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 else
2887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002888 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 clear_tv(&var1);
2890 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_list = lp->ll_tv->vval.v_list;
2893 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2894 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002896 if (lp->ll_n1 < 0)
2897 {
2898 lp->ll_n1 = 0;
2899 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2900 }
2901 }
2902 if (lp->ll_li == NULL)
2903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 if (!quiet)
2907 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 }
2910
2911 /*
2912 * May need to find the item or absolute index for the second
2913 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 * When no index given: "lp->ll_empty2" is TRUE.
2915 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002919 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002924 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002925 {
2926 if (!quiet)
2927 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002929 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931 }
2932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2934 if (lp->ll_n1 < 0)
2935 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2936 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002937 {
2938 if (!quiet)
2939 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002941 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 }
2943
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 }
2947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 return p;
2949}
2950
2951/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 */
2954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002955clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956{
2957 vim_free(lp->ll_exp_name);
2958 vim_free(lp->ll_newkey);
2959}
2960
2961/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002962 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 */
2966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002967set_var_lval(
2968 lval_T *lp,
2969 char_u *endp,
2970 typval_T *rettv,
2971 int copy,
2972 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973{
2974 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002975 listitem_T *ri;
2976 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977
2978 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 cc = *endp;
2983 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002984 if (op != NULL && *op != '=')
2985 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002986 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002989 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002990 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002992 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002993 if ((di == NULL
2994 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2995 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2996 FALSE)))
2997 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 set_var(lp->ll_name, &tv, FALSE);
2999 clear_tv(&tv);
3000 }
3001 }
3002 else
3003 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003005 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003007 else if (tv_check_lock(lp->ll_newkey == NULL
3008 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003009 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003010 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 else if (lp->ll_range)
3012 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003013 listitem_T *ll_li = lp->ll_li;
3014 int ll_n1 = lp->ll_n1;
3015
3016 /*
3017 * Check whether any of the list items is locked
3018 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003019 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003021 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003022 return;
3023 ri = ri->li_next;
3024 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3025 break;
3026 ll_li = ll_li->li_next;
3027 ++ll_n1;
3028 }
3029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 /*
3031 * Assign the List values to the list items.
3032 */
3033 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003034 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 if (op != NULL && *op != '=')
3036 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3037 else
3038 {
3039 clear_tv(&lp->ll_li->li_tv);
3040 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3041 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 ri = ri->li_next;
3043 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3044 break;
3045 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003046 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003048 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003049 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 ri = NULL;
3051 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 lp->ll_li = lp->ll_li->li_next;
3055 ++lp->ll_n1;
3056 }
3057 if (ri != NULL)
3058 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 else if (lp->ll_empty2
3060 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 : lp->ll_n1 != lp->ll_n2)
3062 EMSG(_("E711: List value has not enough items"));
3063 }
3064 else
3065 {
3066 /*
3067 * Assign to a List or Dictionary item.
3068 */
3069 if (lp->ll_newkey != NULL)
3070 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 if (op != NULL && *op != '=')
3072 {
3073 EMSG2(_(e_letwrong), op);
3074 return;
3075 }
3076
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003078 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003079 if (di == NULL)
3080 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003081 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3082 {
3083 vim_free(di);
3084 return;
3085 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003086 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003087 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 else if (op != NULL && *op != '=')
3089 {
3090 tv_op(lp->ll_tv, rettv, op);
3091 return;
3092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003093 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003095
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 /*
3097 * Assign the value to the variable or list item.
3098 */
3099 if (copy)
3100 copy_tv(rettv, lp->ll_tv);
3101 else
3102 {
3103 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003104 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003105 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 }
3107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003108}
3109
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003111 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3112 * Returns OK or FAIL.
3113 */
3114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003115tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003116{
3117 long n;
3118 char_u numbuf[NUMBUFLEN];
3119 char_u *s;
3120
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003121 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3122 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3123 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 {
3125 switch (tv1->v_type)
3126 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003127 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 case VAR_DICT:
3129 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003130 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003131 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003132 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003133 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 break;
3135
3136 case VAR_LIST:
3137 if (*op != '+' || tv2->v_type != VAR_LIST)
3138 break;
3139 /* List += List */
3140 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3141 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3142 return OK;
3143
3144 case VAR_NUMBER:
3145 case VAR_STRING:
3146 if (tv2->v_type == VAR_LIST)
3147 break;
3148 if (*op == '+' || *op == '-')
3149 {
3150 /* nr += nr or nr -= nr*/
3151 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152#ifdef FEAT_FLOAT
3153 if (tv2->v_type == VAR_FLOAT)
3154 {
3155 float_T f = n;
3156
3157 if (*op == '+')
3158 f += tv2->vval.v_float;
3159 else
3160 f -= tv2->vval.v_float;
3161 clear_tv(tv1);
3162 tv1->v_type = VAR_FLOAT;
3163 tv1->vval.v_float = f;
3164 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003165 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003166#endif
3167 {
3168 if (*op == '+')
3169 n += get_tv_number(tv2);
3170 else
3171 n -= get_tv_number(tv2);
3172 clear_tv(tv1);
3173 tv1->v_type = VAR_NUMBER;
3174 tv1->vval.v_number = n;
3175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003176 }
3177 else
3178 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179 if (tv2->v_type == VAR_FLOAT)
3180 break;
3181
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 /* str .= str */
3183 s = get_tv_string(tv1);
3184 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3185 clear_tv(tv1);
3186 tv1->v_type = VAR_STRING;
3187 tv1->vval.v_string = s;
3188 }
3189 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003192#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193 {
3194 float_T f;
3195
3196 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3197 && tv2->v_type != VAR_NUMBER
3198 && tv2->v_type != VAR_STRING))
3199 break;
3200 if (tv2->v_type == VAR_FLOAT)
3201 f = tv2->vval.v_float;
3202 else
3203 f = get_tv_number(tv2);
3204 if (*op == '+')
3205 tv1->vval.v_float += f;
3206 else
3207 tv1->vval.v_float -= f;
3208 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003209#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003210 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003211 }
3212 }
3213
3214 EMSG2(_(e_letwrong), op);
3215 return FAIL;
3216}
3217
3218/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 * Add a watcher to a list.
3220 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
3224 lw->lw_next = l->lv_watch;
3225 l->lv_watch = lw;
3226}
3227
3228/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003229 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 * No warning when it isn't found...
3231 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003233list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236
3237 lwp = &l->lv_watch;
3238 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3239 {
3240 if (lw == lwrem)
3241 {
3242 *lwp = lw->lw_next;
3243 break;
3244 }
3245 lwp = &lw->lw_next;
3246 }
3247}
3248
3249/*
3250 * Just before removing an item from a list: advance watchers to the next
3251 * item.
3252 */
3253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003254list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255{
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257
3258 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3259 if (lw->lw_item == item)
3260 lw->lw_item = item->li_next;
3261}
3262
3263/*
3264 * Evaluate the expression used in a ":for var in expr" command.
3265 * "arg" points to "var".
3266 * Set "*errp" to TRUE for an error, FALSE otherwise;
3267 * Return a pointer that holds the info. Null when there is an error.
3268 */
3269 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270eval_for_line(
3271 char_u *arg,
3272 int *errp,
3273 char_u **nextcmdp,
3274 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275{
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 typval_T tv;
3279 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280
3281 *errp = TRUE; /* default: there is an error */
3282
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 if (fi == NULL)
3285 return NULL;
3286
3287 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3288 if (expr == NULL)
3289 return fi;
3290
3291 expr = skipwhite(expr);
3292 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3293 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003294 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 return fi;
3296 }
3297
3298 if (skip)
3299 ++emsg_skip;
3300 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3301 {
3302 *errp = FALSE;
3303 if (!skip)
3304 {
3305 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003306 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003309 clear_tv(&tv);
3310 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003311 else if (l == NULL)
3312 {
3313 /* a null list is like an empty list: do nothing */
3314 clear_tv(&tv);
3315 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316 else
3317 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003318 /* No need to increment the refcount, it's already set for the
3319 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 fi->fi_list = l;
3321 list_add_watch(l, &fi->fi_lw);
3322 fi->fi_lw.lw_item = l->lv_first;
3323 }
3324 }
3325 }
3326 if (skip)
3327 --emsg_skip;
3328
3329 return fi;
3330}
3331
3332/*
3333 * Use the first item in a ":for" list. Advance to the next.
3334 * Assign the values to the variable (list). "arg" points to the first one.
3335 * Return TRUE when a valid item was found, FALSE when at end of list or
3336 * something wrong.
3337 */
3338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003341 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003343 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344
3345 item = fi->fi_lw.lw_item;
3346 if (item == NULL)
3347 result = FALSE;
3348 else
3349 {
3350 fi->fi_lw.lw_item = item->li_next;
3351 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3352 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3353 }
3354 return result;
3355}
3356
3357/*
3358 * Free the structure used to store info used by ":for".
3359 */
3360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003361free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362{
Bram Moolenaar33570922005-01-25 22:26:29 +00003363 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003365 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003366 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003368 list_unref(fi->fi_list);
3369 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 vim_free(fi);
3371}
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3374
3375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003376set_context_for_expression(
3377 expand_T *xp,
3378 char_u *arg,
3379 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
3381 int got_eq = FALSE;
3382 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003385 if (cmdidx == CMD_let)
3386 {
3387 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003388 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003389 {
3390 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003391 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003392 {
3393 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003394 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003395 if (vim_iswhite(*p))
3396 break;
3397 }
3398 return;
3399 }
3400 }
3401 else
3402 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3403 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 while ((xp->xp_pattern = vim_strpbrk(arg,
3405 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3406 {
3407 c = *xp->xp_pattern;
3408 if (c == '&')
3409 {
3410 c = xp->xp_pattern[1];
3411 if (c == '&')
3412 {
3413 ++xp->xp_pattern;
3414 xp->xp_context = cmdidx != CMD_let || got_eq
3415 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3416 }
3417 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003418 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003420 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3421 xp->xp_pattern += 2;
3422
3423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
3425 else if (c == '$')
3426 {
3427 /* environment variable */
3428 xp->xp_context = EXPAND_ENV_VARS;
3429 }
3430 else if (c == '=')
3431 {
3432 got_eq = TRUE;
3433 xp->xp_context = EXPAND_EXPRESSION;
3434 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003435 else if (c == '#'
3436 && xp->xp_context == EXPAND_EXPRESSION)
3437 {
3438 /* Autoload function/variable contains '#'. */
3439 break;
3440 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003441 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 && xp->xp_context == EXPAND_FUNCTIONS
3443 && vim_strchr(xp->xp_pattern, '(') == NULL)
3444 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003445 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 break;
3447 }
3448 else if (cmdidx != CMD_let || got_eq)
3449 {
3450 if (c == '"') /* string */
3451 {
3452 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3453 if (c == '\\' && xp->xp_pattern[1] != NUL)
3454 ++xp->xp_pattern;
3455 xp->xp_context = EXPAND_NOTHING;
3456 }
3457 else if (c == '\'') /* literal string */
3458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003459 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3461 /* skip */ ;
3462 xp->xp_context = EXPAND_NOTHING;
3463 }
3464 else if (c == '|')
3465 {
3466 if (xp->xp_pattern[1] == '|')
3467 {
3468 ++xp->xp_pattern;
3469 xp->xp_context = EXPAND_EXPRESSION;
3470 }
3471 else
3472 xp->xp_context = EXPAND_COMMANDS;
3473 }
3474 else
3475 xp->xp_context = EXPAND_EXPRESSION;
3476 }
3477 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003478 /* Doesn't look like something valid, expand as an expression
3479 * anyway. */
3480 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 arg = xp->xp_pattern;
3482 if (*arg != NUL)
3483 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3484 /* skip */ ;
3485 }
3486 xp->xp_pattern = arg;
3487}
3488
3489#endif /* FEAT_CMDL_COMPL */
3490
3491/*
3492 * ":1,25call func(arg1, arg2)" function call.
3493 */
3494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003495ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 char_u *arg = eap->arg;
3498 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003500 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003502 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 linenr_T lnum;
3504 int doesrange;
3505 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003506 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003507 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003509 if (eap->skip)
3510 {
3511 /* trans_function_name() doesn't work well when skipping, use eval0()
3512 * instead to skip to any following command, e.g. for:
3513 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003514 ++emsg_skip;
3515 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3516 clear_tv(&rettv);
3517 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003518 return;
3519 }
3520
Bram Moolenaar65639032016-03-16 21:40:30 +01003521 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003522 if (fudi.fd_newkey != NULL)
3523 {
3524 /* Still need to give an error message for missing key. */
3525 EMSG2(_(e_dictkey), fudi.fd_newkey);
3526 vim_free(fudi.fd_newkey);
3527 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003528 if (tofree == NULL)
3529 return;
3530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003531 /* Increase refcount on dictionary, it could get deleted when evaluating
3532 * the arguments. */
3533 if (fudi.fd_dict != NULL)
3534 ++fudi.fd_dict->dv_refcount;
3535
Bram Moolenaar65639032016-03-16 21:40:30 +01003536 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3537 * contents. For VAR_PARTIAL get its partial, unless we already have one
3538 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003539 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003540 name = deref_func_name(tofree, &len,
3541 partial != NULL ? NULL : &partial, FALSE);
3542
Bram Moolenaar532c7802005-01-27 14:44:31 +00003543 /* Skip white space to allow ":call func ()". Not good, but required for
3544 * backward compatibility. */
3545 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
3548 if (*startarg != '(')
3549 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003550 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 goto end;
3552 }
3553
3554 /*
3555 * When skipping, evaluate the function once, to find the end of the
3556 * arguments.
3557 * When the function takes a range, this is discovered after the first
3558 * call, and the loop is broken.
3559 */
3560 if (eap->skip)
3561 {
3562 ++emsg_skip;
3563 lnum = eap->line2; /* do it once, also with an invalid range */
3564 }
3565 else
3566 lnum = eap->line1;
3567 for ( ; lnum <= eap->line2; ++lnum)
3568 {
3569 if (!eap->skip && eap->addr_count > 0)
3570 {
3571 curwin->w_cursor.lnum = lnum;
3572 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003573#ifdef FEAT_VIRTUALEDIT
3574 curwin->w_cursor.coladd = 0;
3575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
3577 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003578 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003579 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003580 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 failed = TRUE;
3583 break;
3584 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003585
3586 /* Handle a function returning a Funcref, Dictionary or List. */
3587 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3588 {
3589 failed = TRUE;
3590 break;
3591 }
3592
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003593 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (doesrange || eap->skip)
3595 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003600 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (aborting())
3602 break;
3603 }
3604 if (eap->skip)
3605 --emsg_skip;
3606
3607 if (!failed)
3608 {
3609 /* Check for trailing illegal characters and a following command. */
3610 if (!ends_excmd(*arg))
3611 {
3612 emsg_severe = TRUE;
3613 EMSG(_(e_trailing));
3614 }
3615 else
3616 eap->nextcmd = check_nextcmd(arg);
3617 }
3618
3619end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003620 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003621 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622}
3623
3624/*
3625 * ":unlet[!] var1 ... " command.
3626 */
3627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003628ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003630 ex_unletlock(eap, eap->arg, 0);
3631}
3632
3633/*
3634 * ":lockvar" and ":unlockvar" commands
3635 */
3636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003637ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003640 int deep = 2;
3641
3642 if (eap->forceit)
3643 deep = -1;
3644 else if (vim_isdigit(*arg))
3645 {
3646 deep = getdigits(&arg);
3647 arg = skipwhite(arg);
3648 }
3649
3650 ex_unletlock(eap, arg, deep);
3651}
3652
3653/*
3654 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3655 */
3656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657ex_unletlock(
3658 exarg_T *eap,
3659 char_u *argstart,
3660 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661{
3662 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003665 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 do
3668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003670 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003671 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 if (lv.ll_name == NULL)
3673 error = TRUE; /* error but continue parsing */
3674 if (name_end == NULL || (!vim_iswhite(*name_end)
3675 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 if (name_end != NULL)
3678 {
3679 emsg_severe = TRUE;
3680 EMSG(_(e_trailing));
3681 }
3682 if (!(eap->skip || error))
3683 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 break;
3685 }
3686
3687 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 {
3689 if (eap->cmdidx == CMD_unlet)
3690 {
3691 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3692 error = TRUE;
3693 }
3694 else
3695 {
3696 if (do_lock_var(&lv, name_end, deep,
3697 eap->cmdidx == CMD_lockvar) == FAIL)
3698 error = TRUE;
3699 }
3700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 if (!eap->skip)
3703 clear_lval(&lv);
3704
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 arg = skipwhite(name_end);
3706 } while (!ends_excmd(*arg));
3707
3708 eap->nextcmd = check_nextcmd(arg);
3709}
3710
Bram Moolenaar8c711452005-01-14 21:53:12 +00003711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003712do_unlet_var(
3713 lval_T *lp,
3714 char_u *name_end,
3715 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 int ret = OK;
3718 int cc;
3719
3720 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 cc = *name_end;
3723 *name_end = NUL;
3724
3725 /* Normal name or expanded name. */
3726 if (check_changedtick(lp->ll_name))
3727 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003728 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003731 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003735 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003737 else if (lp->ll_range)
3738 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003740 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003741 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003742
3743 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3744 {
3745 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003747 return FAIL;
3748 ll_li = li;
3749 ++ll_n1;
3750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003751
3752 /* Delete a range of List items. */
3753 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3754 {
3755 li = lp->ll_li->li_next;
3756 listitem_remove(lp->ll_list, lp->ll_li);
3757 lp->ll_li = li;
3758 ++lp->ll_n1;
3759 }
3760 }
3761 else
3762 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003763 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 /* unlet a List item. */
3765 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003766 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003768 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003769 }
3770
3771 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003772}
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774/*
3775 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 */
3778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780{
Bram Moolenaar33570922005-01-25 22:26:29 +00003781 hashtab_T *ht;
3782 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003783 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003784 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003785 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
Bram Moolenaar33570922005-01-25 22:26:29 +00003787 ht = find_var_ht(name, &varname);
3788 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003790 if (ht == &globvarht)
3791 d = &globvardict;
3792 else if (current_funccal != NULL
3793 && ht == &current_funccal->l_vars.dv_hashtab)
3794 d = &current_funccal->l_vars;
3795 else if (ht == &compat_hashtab)
3796 d = &vimvardict;
3797 else
3798 {
3799 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3800 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3801 }
3802 if (d == NULL)
3803 {
3804 EMSG2(_(e_intern2), "do_unlet()");
3805 return FAIL;
3806 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003807 hi = hash_find(ht, varname);
3808 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003809 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003810 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003811 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003812 || var_check_ro(di->di_flags, name, FALSE)
3813 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003814 return FAIL;
3815
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003816 delete_var(ht, hi);
3817 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003820 if (forceit)
3821 return OK;
3822 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 return FAIL;
3824}
3825
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826/*
3827 * Lock or unlock variable indicated by "lp".
3828 * "deep" is the levels to go (-1 for unlimited);
3829 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3830 */
3831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003832do_lock_var(
3833 lval_T *lp,
3834 char_u *name_end,
3835 int deep,
3836 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837{
3838 int ret = OK;
3839 int cc;
3840 dictitem_T *di;
3841
3842 if (deep == 0) /* nothing to do */
3843 return OK;
3844
3845 if (lp->ll_tv == NULL)
3846 {
3847 cc = *name_end;
3848 *name_end = NUL;
3849
3850 /* Normal name or expanded name. */
3851 if (check_changedtick(lp->ll_name))
3852 ret = FAIL;
3853 else
3854 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003855 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003856 if (di == NULL)
3857 ret = FAIL;
3858 else
3859 {
3860 if (lock)
3861 di->di_flags |= DI_FLAGS_LOCK;
3862 else
3863 di->di_flags &= ~DI_FLAGS_LOCK;
3864 item_lock(&di->di_tv, deep, lock);
3865 }
3866 }
3867 *name_end = cc;
3868 }
3869 else if (lp->ll_range)
3870 {
3871 listitem_T *li = lp->ll_li;
3872
3873 /* (un)lock a range of List items. */
3874 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3875 {
3876 item_lock(&li->li_tv, deep, lock);
3877 li = li->li_next;
3878 ++lp->ll_n1;
3879 }
3880 }
3881 else if (lp->ll_list != NULL)
3882 /* (un)lock a List item. */
3883 item_lock(&lp->ll_li->li_tv, deep, lock);
3884 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003885 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003886 item_lock(&lp->ll_di->di_tv, deep, lock);
3887
3888 return ret;
3889}
3890
3891/*
3892 * Lock or unlock an item. "deep" is nr of levels to go.
3893 */
3894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003895item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003896{
3897 static int recurse = 0;
3898 list_T *l;
3899 listitem_T *li;
3900 dict_T *d;
3901 hashitem_T *hi;
3902 int todo;
3903
3904 if (recurse >= DICT_MAXNEST)
3905 {
3906 EMSG(_("E743: variable nested too deep for (un)lock"));
3907 return;
3908 }
3909 if (deep == 0)
3910 return;
3911 ++recurse;
3912
3913 /* lock/unlock the item itself */
3914 if (lock)
3915 tv->v_lock |= VAR_LOCKED;
3916 else
3917 tv->v_lock &= ~VAR_LOCKED;
3918
3919 switch (tv->v_type)
3920 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003921 case VAR_UNKNOWN:
3922 case VAR_NUMBER:
3923 case VAR_STRING:
3924 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003925 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003926 case VAR_FLOAT:
3927 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003928 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003929 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003930 break;
3931
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003932 case VAR_LIST:
3933 if ((l = tv->vval.v_list) != NULL)
3934 {
3935 if (lock)
3936 l->lv_lock |= VAR_LOCKED;
3937 else
3938 l->lv_lock &= ~VAR_LOCKED;
3939 if (deep < 0 || deep > 1)
3940 /* recursive: lock/unlock the items the List contains */
3941 for (li = l->lv_first; li != NULL; li = li->li_next)
3942 item_lock(&li->li_tv, deep - 1, lock);
3943 }
3944 break;
3945 case VAR_DICT:
3946 if ((d = tv->vval.v_dict) != NULL)
3947 {
3948 if (lock)
3949 d->dv_lock |= VAR_LOCKED;
3950 else
3951 d->dv_lock &= ~VAR_LOCKED;
3952 if (deep < 0 || deep > 1)
3953 {
3954 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003955 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003956 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3957 {
3958 if (!HASHITEM_EMPTY(hi))
3959 {
3960 --todo;
3961 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3962 }
3963 }
3964 }
3965 }
3966 }
3967 --recurse;
3968}
3969
Bram Moolenaara40058a2005-07-11 22:42:07 +00003970/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003971 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3972 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003973 */
3974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003975tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003976{
3977 return (tv->v_lock & VAR_LOCKED)
3978 || (tv->v_type == VAR_LIST
3979 && tv->vval.v_list != NULL
3980 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3981 || (tv->v_type == VAR_DICT
3982 && tv->vval.v_dict != NULL
3983 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3984}
3985
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3987/*
3988 * Delete all "menutrans_" variables.
3989 */
3990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003991del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
Bram Moolenaar33570922005-01-25 22:26:29 +00003993 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003994 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003997 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003998 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 {
4000 if (!HASHITEM_EMPTY(hi))
4001 {
4002 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4004 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004005 }
4006 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008}
4009#endif
4010
4011#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4012
4013/*
4014 * Local string buffer for the next two functions to store a variable name
4015 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4016 * get_user_var_name().
4017 */
4018
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004019static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
4021static char_u *varnamebuf = NULL;
4022static int varnamebuflen = 0;
4023
4024/*
4025 * Function to concatenate a prefix and a variable name.
4026 */
4027 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004028cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
4030 int len;
4031
4032 len = (int)STRLEN(name) + 3;
4033 if (len > varnamebuflen)
4034 {
4035 vim_free(varnamebuf);
4036 len += 10; /* some additional space */
4037 varnamebuf = alloc(len);
4038 if (varnamebuf == NULL)
4039 {
4040 varnamebuflen = 0;
4041 return NULL;
4042 }
4043 varnamebuflen = len;
4044 }
4045 *varnamebuf = prefix;
4046 varnamebuf[1] = ':';
4047 STRCPY(varnamebuf + 2, name);
4048 return varnamebuf;
4049}
4050
4051/*
4052 * Function given to ExpandGeneric() to obtain the list of user defined
4053 * (global/buffer/window/built-in) variable names.
4054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004056get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004058 static long_u gdone;
4059 static long_u bdone;
4060 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004061#ifdef FEAT_WINDOWS
4062 static long_u tdone;
4063#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004064 static int vidx;
4065 static hashitem_T *hi;
4066 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004070 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004071#ifdef FEAT_WINDOWS
4072 tdone = 0;
4073#endif
4074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004075
4076 /* Global variables */
4077 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004079 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004080 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004081 else
4082 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 while (HASHITEM_EMPTY(hi))
4084 ++hi;
4085 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4086 return cat_prefix_varname('g', hi->hi_key);
4087 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004089
4090 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004091 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004094 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004095 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004096 else
4097 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004098 while (HASHITEM_EMPTY(hi))
4099 ++hi;
4100 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004102 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004104 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 return (char_u *)"b:changedtick";
4106 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004107
4108 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004109 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004110 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004112 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004114 else
4115 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004116 while (HASHITEM_EMPTY(hi))
4117 ++hi;
4118 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004120
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004121#ifdef FEAT_WINDOWS
4122 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004123 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004124 if (tdone < ht->ht_used)
4125 {
4126 if (tdone++ == 0)
4127 hi = ht->ht_array;
4128 else
4129 ++hi;
4130 while (HASHITEM_EMPTY(hi))
4131 ++hi;
4132 return cat_prefix_varname('t', hi->hi_key);
4133 }
4134#endif
4135
Bram Moolenaar33570922005-01-25 22:26:29 +00004136 /* v: variables */
4137 if (vidx < VV_LEN)
4138 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
4140 vim_free(varnamebuf);
4141 varnamebuf = NULL;
4142 varnamebuflen = 0;
4143 return NULL;
4144}
4145
4146#endif /* FEAT_CMDL_COMPL */
4147
4148/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004149 * Return TRUE if "pat" matches "text".
4150 * Does not use 'cpo' and always uses 'magic'.
4151 */
4152 static int
4153pattern_match(char_u *pat, char_u *text, int ic)
4154{
4155 int matches = FALSE;
4156 char_u *save_cpo;
4157 regmatch_T regmatch;
4158
4159 /* avoid 'l' flag in 'cpoptions' */
4160 save_cpo = p_cpo;
4161 p_cpo = (char_u *)"";
4162 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4163 if (regmatch.regprog != NULL)
4164 {
4165 regmatch.rm_ic = ic;
4166 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4167 vim_regfree(regmatch.regprog);
4168 }
4169 p_cpo = save_cpo;
4170 return matches;
4171}
4172
4173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 * types for expressions.
4175 */
4176typedef enum
4177{
4178 TYPE_UNKNOWN = 0
4179 , TYPE_EQUAL /* == */
4180 , TYPE_NEQUAL /* != */
4181 , TYPE_GREATER /* > */
4182 , TYPE_GEQUAL /* >= */
4183 , TYPE_SMALLER /* < */
4184 , TYPE_SEQUAL /* <= */
4185 , TYPE_MATCH /* =~ */
4186 , TYPE_NOMATCH /* !~ */
4187} exptype_T;
4188
4189/*
4190 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4193 */
4194
4195/*
4196 * Handle zero level expression.
4197 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004199 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 * Return OK or FAIL.
4201 */
4202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004203eval0(
4204 char_u *arg,
4205 typval_T *rettv,
4206 char_u **nextcmd,
4207 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
4209 int ret;
4210 char_u *p;
4211
4212 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 if (ret == FAIL || !ends_excmd(*p))
4215 {
4216 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 /*
4219 * Report the invalid expression unless the expression evaluation has
4220 * been cancelled due to an aborting error, an interrupt, or an
4221 * exception.
4222 */
4223 if (!aborting())
4224 EMSG2(_(e_invexpr2), arg);
4225 ret = FAIL;
4226 }
4227 if (nextcmd != NULL)
4228 *nextcmd = check_nextcmd(p);
4229
4230 return ret;
4231}
4232
4233/*
4234 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004235 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 *
4237 * "arg" must point to the first non-white of the expression.
4238 * "arg" is advanced to the next non-white after the recognized expression.
4239 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004240 * Note: "rettv.v_lock" is not set.
4241 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 * Return OK or FAIL.
4243 */
4244 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004245eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004248 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250 /*
4251 * Get the first variable.
4252 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 return FAIL;
4255
4256 if ((*arg)[0] == '?')
4257 {
4258 result = FALSE;
4259 if (evaluate)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 int error = FALSE;
4262
4263 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004265 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (error)
4267 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269
4270 /*
4271 * Get the second variable.
4272 */
4273 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 return FAIL;
4276
4277 /*
4278 * Check for the ":".
4279 */
4280 if ((*arg)[0] != ':')
4281 {
4282 EMSG(_("E109: Missing ':' after '?'"));
4283 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286 }
4287
4288 /*
4289 * Get the third variable.
4290 */
4291 *arg = skipwhite(*arg + 1);
4292 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4293 {
4294 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004295 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 return FAIL;
4297 }
4298 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004299 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301
4302 return OK;
4303}
4304
4305/*
4306 * Handle first level expression:
4307 * expr2 || expr2 || expr2 logical OR
4308 *
4309 * "arg" must point to the first non-white of the expression.
4310 * "arg" is advanced to the next non-white after the recognized expression.
4311 *
4312 * Return OK or FAIL.
4313 */
4314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004315eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
Bram Moolenaar33570922005-01-25 22:26:29 +00004317 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 long result;
4319 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Get the first variable.
4324 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 return FAIL;
4327
4328 /*
4329 * Repeat until there is no following "||".
4330 */
4331 first = TRUE;
4332 result = FALSE;
4333 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4334 {
4335 if (evaluate && first)
4336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004339 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (error)
4341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 first = FALSE;
4343 }
4344
4345 /*
4346 * Get the second variable.
4347 */
4348 *arg = skipwhite(*arg + 2);
4349 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4350 return FAIL;
4351
4352 /*
4353 * Compute the result.
4354 */
4355 if (evaluate && !result)
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (error)
4361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 if (evaluate)
4364 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 rettv->v_type = VAR_NUMBER;
4366 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 }
4369
4370 return OK;
4371}
4372
4373/*
4374 * Handle second level expression:
4375 * expr3 && expr3 && expr3 logical AND
4376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 long result;
4387 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 /*
4391 * Get the first variable.
4392 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 return FAIL;
4395
4396 /*
4397 * Repeat until there is no following "&&".
4398 */
4399 first = TRUE;
4400 result = TRUE;
4401 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4402 {
4403 if (evaluate && first)
4404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004405 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004408 if (error)
4409 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 first = FALSE;
4411 }
4412
4413 /*
4414 * Get the second variable.
4415 */
4416 *arg = skipwhite(*arg + 2);
4417 if (eval4(arg, &var2, evaluate && result) == FAIL)
4418 return FAIL;
4419
4420 /*
4421 * Compute the result.
4422 */
4423 if (evaluate && result)
4424 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004427 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004428 if (error)
4429 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 if (evaluate)
4432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 rettv->v_type = VAR_NUMBER;
4434 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436 }
4437
4438 return OK;
4439}
4440
4441/*
4442 * Handle third level expression:
4443 * var1 == var2
4444 * var1 =~ var2
4445 * var1 != var2
4446 * var1 !~ var2
4447 * var1 > var2
4448 * var1 >= var2
4449 * var1 < var2
4450 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 * var1 is var2
4452 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 *
4454 * "arg" must point to the first non-white of the expression.
4455 * "arg" is advanced to the next non-white after the recognized expression.
4456 *
4457 * Return OK or FAIL.
4458 */
4459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004460eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461{
Bram Moolenaar33570922005-01-25 22:26:29 +00004462 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 char_u *p;
4464 int i;
4465 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 int len = 2;
4468 long n1, n2;
4469 char_u *s1, *s2;
4470 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 /*
4474 * Get the first variable.
4475 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 return FAIL;
4478
4479 p = *arg;
4480 switch (p[0])
4481 {
4482 case '=': if (p[1] == '=')
4483 type = TYPE_EQUAL;
4484 else if (p[1] == '~')
4485 type = TYPE_MATCH;
4486 break;
4487 case '!': if (p[1] == '=')
4488 type = TYPE_NEQUAL;
4489 else if (p[1] == '~')
4490 type = TYPE_NOMATCH;
4491 break;
4492 case '>': if (p[1] != '=')
4493 {
4494 type = TYPE_GREATER;
4495 len = 1;
4496 }
4497 else
4498 type = TYPE_GEQUAL;
4499 break;
4500 case '<': if (p[1] != '=')
4501 {
4502 type = TYPE_SMALLER;
4503 len = 1;
4504 }
4505 else
4506 type = TYPE_SEQUAL;
4507 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 case 'i': if (p[1] == 's')
4509 {
4510 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4511 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004512 i = p[len];
4513 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 {
4515 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4516 type_is = TRUE;
4517 }
4518 }
4519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521
4522 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004523 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 */
4525 if (type != TYPE_UNKNOWN)
4526 {
4527 /* extra question mark appended: ignore case */
4528 if (p[len] == '?')
4529 {
4530 ic = TRUE;
4531 ++len;
4532 }
4533 /* extra '#' appended: match case */
4534 else if (p[len] == '#')
4535 {
4536 ic = FALSE;
4537 ++len;
4538 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004539 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
4541 ic = p_ic;
4542
4543 /*
4544 * Get the second variable.
4545 */
4546 *arg = skipwhite(p + len);
4547 if (eval5(arg, &var2, evaluate) == FAIL)
4548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004549 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return FAIL;
4551 }
4552
4553 if (evaluate)
4554 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 if (type_is && rettv->v_type != var2.v_type)
4556 {
4557 /* For "is" a different type always means FALSE, for "notis"
4558 * it means TRUE. */
4559 n1 = (type == TYPE_NEQUAL);
4560 }
4561 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4562 {
4563 if (type_is)
4564 {
4565 n1 = (rettv->v_type == var2.v_type
4566 && rettv->vval.v_list == var2.vval.v_list);
4567 if (type == TYPE_NEQUAL)
4568 n1 = !n1;
4569 }
4570 else if (rettv->v_type != var2.v_type
4571 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4572 {
4573 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004574 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004576 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004577 clear_tv(rettv);
4578 clear_tv(&var2);
4579 return FAIL;
4580 }
4581 else
4582 {
4583 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004584 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4585 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004586 if (type == TYPE_NEQUAL)
4587 n1 = !n1;
4588 }
4589 }
4590
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004591 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4592 {
4593 if (type_is)
4594 {
4595 n1 = (rettv->v_type == var2.v_type
4596 && rettv->vval.v_dict == var2.vval.v_dict);
4597 if (type == TYPE_NEQUAL)
4598 n1 = !n1;
4599 }
4600 else if (rettv->v_type != var2.v_type
4601 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4602 {
4603 if (rettv->v_type != var2.v_type)
4604 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4605 else
4606 EMSG(_("E736: Invalid operation for Dictionary"));
4607 clear_tv(rettv);
4608 clear_tv(&var2);
4609 return FAIL;
4610 }
4611 else
4612 {
4613 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004614 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4615 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004616 if (type == TYPE_NEQUAL)
4617 n1 = !n1;
4618 }
4619 }
4620
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004621 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4622 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004624 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004626 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004627 clear_tv(rettv);
4628 clear_tv(&var2);
4629 return FAIL;
4630 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004631 if ((rettv->v_type == VAR_PARTIAL
4632 && rettv->vval.v_partial == NULL)
4633 || (var2.v_type == VAR_PARTIAL
4634 && var2.vval.v_partial == NULL))
4635 /* when a partial is NULL assume not equal */
4636 n1 = FALSE;
4637 else if (type_is)
4638 {
4639 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4640 /* strings are considered the same if their value is
4641 * the same */
4642 n1 = tv_equal(rettv, &var2, ic, FALSE);
4643 else if (rettv->v_type == VAR_PARTIAL
4644 && var2.v_type == VAR_PARTIAL)
4645 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4646 else
4647 n1 = FALSE;
4648 }
4649 else
4650 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004651 if (type == TYPE_NEQUAL)
4652 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004653 }
4654
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004655#ifdef FEAT_FLOAT
4656 /*
4657 * If one of the two variables is a float, compare as a float.
4658 * When using "=~" or "!~", always compare as string.
4659 */
4660 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4661 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4662 {
4663 float_T f1, f2;
4664
4665 if (rettv->v_type == VAR_FLOAT)
4666 f1 = rettv->vval.v_float;
4667 else
4668 f1 = get_tv_number(rettv);
4669 if (var2.v_type == VAR_FLOAT)
4670 f2 = var2.vval.v_float;
4671 else
4672 f2 = get_tv_number(&var2);
4673 n1 = FALSE;
4674 switch (type)
4675 {
4676 case TYPE_EQUAL: n1 = (f1 == f2); break;
4677 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4678 case TYPE_GREATER: n1 = (f1 > f2); break;
4679 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4680 case TYPE_SMALLER: n1 = (f1 < f2); break;
4681 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4682 case TYPE_UNKNOWN:
4683 case TYPE_MATCH:
4684 case TYPE_NOMATCH: break; /* avoid gcc warning */
4685 }
4686 }
4687#endif
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 /*
4690 * If one of the two variables is a number, compare as a number.
4691 * When using "=~" or "!~", always compare as string.
4692 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004693 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004696 n1 = get_tv_number(rettv);
4697 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 switch (type)
4699 {
4700 case TYPE_EQUAL: n1 = (n1 == n2); break;
4701 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4702 case TYPE_GREATER: n1 = (n1 > n2); break;
4703 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4704 case TYPE_SMALLER: n1 = (n1 < n2); break;
4705 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4706 case TYPE_UNKNOWN:
4707 case TYPE_MATCH:
4708 case TYPE_NOMATCH: break; /* avoid gcc warning */
4709 }
4710 }
4711 else
4712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 s1 = get_tv_string_buf(rettv, buf1);
4714 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4716 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4717 else
4718 i = 0;
4719 n1 = FALSE;
4720 switch (type)
4721 {
4722 case TYPE_EQUAL: n1 = (i == 0); break;
4723 case TYPE_NEQUAL: n1 = (i != 0); break;
4724 case TYPE_GREATER: n1 = (i > 0); break;
4725 case TYPE_GEQUAL: n1 = (i >= 0); break;
4726 case TYPE_SMALLER: n1 = (i < 0); break;
4727 case TYPE_SEQUAL: n1 = (i <= 0); break;
4728
4729 case TYPE_MATCH:
4730 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004731 n1 = pattern_match(s2, s1, ic);
4732 if (type == TYPE_NOMATCH)
4733 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 break;
4735
4736 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4737 }
4738 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 clear_tv(rettv);
4740 clear_tv(&var2);
4741 rettv->v_type = VAR_NUMBER;
4742 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744 }
4745
4746 return OK;
4747}
4748
4749/*
4750 * Handle fourth level expression:
4751 * + number addition
4752 * - number subtraction
4753 * . string concatenation
4754 *
4755 * "arg" must point to the first non-white of the expression.
4756 * "arg" is advanced to the next non-white after the recognized expression.
4757 *
4758 * Return OK or FAIL.
4759 */
4760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004761eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
Bram Moolenaar33570922005-01-25 22:26:29 +00004763 typval_T var2;
4764 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 int op;
4766 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004767#ifdef FEAT_FLOAT
4768 float_T f1 = 0, f2 = 0;
4769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 char_u *s1, *s2;
4771 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4772 char_u *p;
4773
4774 /*
4775 * Get the first variable.
4776 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004777 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 return FAIL;
4779
4780 /*
4781 * Repeat computing, until no '+', '-' or '.' is following.
4782 */
4783 for (;;)
4784 {
4785 op = **arg;
4786 if (op != '+' && op != '-' && op != '.')
4787 break;
4788
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 if ((op != '+' || rettv->v_type != VAR_LIST)
4790#ifdef FEAT_FLOAT
4791 && (op == '.' || rettv->v_type != VAR_FLOAT)
4792#endif
4793 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
4795 /* For "list + ...", an illegal use of the first operand as
4796 * a number cannot be determined before evaluating the 2nd
4797 * operand: if this is also a list, all is ok.
4798 * For "something . ...", "something - ..." or "non-list + ...",
4799 * we know that the first operand needs to be a string or number
4800 * without evaluating the 2nd operand. So check before to avoid
4801 * side effects after an error. */
4802 if (evaluate && get_tv_string_chk(rettv) == NULL)
4803 {
4804 clear_tv(rettv);
4805 return FAIL;
4806 }
4807 }
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 /*
4810 * Get the second variable.
4811 */
4812 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004813 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return FAIL;
4817 }
4818
4819 if (evaluate)
4820 {
4821 /*
4822 * Compute the result.
4823 */
4824 if (op == '.')
4825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4827 s2 = get_tv_string_buf_chk(&var2, buf2);
4828 if (s2 == NULL) /* type error ? */
4829 {
4830 clear_tv(rettv);
4831 clear_tv(&var2);
4832 return FAIL;
4833 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004834 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835 clear_tv(rettv);
4836 rettv->v_type = VAR_STRING;
4837 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004839 else if (op == '+' && rettv->v_type == VAR_LIST
4840 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004841 {
4842 /* concatenate Lists */
4843 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4844 &var3) == FAIL)
4845 {
4846 clear_tv(rettv);
4847 clear_tv(&var2);
4848 return FAIL;
4849 }
4850 clear_tv(rettv);
4851 *rettv = var3;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
4854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004855 int error = FALSE;
4856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857#ifdef FEAT_FLOAT
4858 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004859 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004860 f1 = rettv->vval.v_float;
4861 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004862 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863 else
4864#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866 n1 = get_tv_number_chk(rettv, &error);
4867 if (error)
4868 {
4869 /* This can only happen for "list + non-list". For
4870 * "non-list + ..." or "something - ...", we returned
4871 * before evaluating the 2nd operand. */
4872 clear_tv(rettv);
4873 return FAIL;
4874 }
4875#ifdef FEAT_FLOAT
4876 if (var2.v_type == VAR_FLOAT)
4877 f1 = n1;
4878#endif
4879 }
4880#ifdef FEAT_FLOAT
4881 if (var2.v_type == VAR_FLOAT)
4882 {
4883 f2 = var2.vval.v_float;
4884 n2 = 0;
4885 }
4886 else
4887#endif
4888 {
4889 n2 = get_tv_number_chk(&var2, &error);
4890 if (error)
4891 {
4892 clear_tv(rettv);
4893 clear_tv(&var2);
4894 return FAIL;
4895 }
4896#ifdef FEAT_FLOAT
4897 if (rettv->v_type == VAR_FLOAT)
4898 f2 = n2;
4899#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004901 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902
4903#ifdef FEAT_FLOAT
4904 /* If there is a float on either side the result is a float. */
4905 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4906 {
4907 if (op == '+')
4908 f1 = f1 + f2;
4909 else
4910 f1 = f1 - f2;
4911 rettv->v_type = VAR_FLOAT;
4912 rettv->vval.v_float = f1;
4913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915#endif
4916 {
4917 if (op == '+')
4918 n1 = n1 + n2;
4919 else
4920 n1 = n1 - n2;
4921 rettv->v_type = VAR_NUMBER;
4922 rettv->vval.v_number = n1;
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 }
4928 return OK;
4929}
4930
4931/*
4932 * Handle fifth level expression:
4933 * * number multiplication
4934 * / number division
4935 * % number modulo
4936 *
4937 * "arg" must point to the first non-white of the expression.
4938 * "arg" is advanced to the next non-white after the recognized expression.
4939 *
4940 * Return OK or FAIL.
4941 */
4942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004943eval6(
4944 char_u **arg,
4945 typval_T *rettv,
4946 int evaluate,
4947 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
Bram Moolenaar33570922005-01-25 22:26:29 +00004949 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 int op;
4951 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952#ifdef FEAT_FLOAT
4953 int use_float = FALSE;
4954 float_T f1 = 0, f2;
4955#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 /*
4959 * Get the first variable.
4960 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004961 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return FAIL;
4963
4964 /*
4965 * Repeat computing, until no '*', '/' or '%' is following.
4966 */
4967 for (;;)
4968 {
4969 op = **arg;
4970 if (op != '*' && op != '/' && op != '%')
4971 break;
4972
4973 if (evaluate)
4974 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975#ifdef FEAT_FLOAT
4976 if (rettv->v_type == VAR_FLOAT)
4977 {
4978 f1 = rettv->vval.v_float;
4979 use_float = TRUE;
4980 n1 = 0;
4981 }
4982 else
4983#endif
4984 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004985 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004986 if (error)
4987 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
4990 n1 = 0;
4991
4992 /*
4993 * Get the second variable.
4994 */
4995 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004996 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 return FAIL;
4998
4999 if (evaluate)
5000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001#ifdef FEAT_FLOAT
5002 if (var2.v_type == VAR_FLOAT)
5003 {
5004 if (!use_float)
5005 {
5006 f1 = n1;
5007 use_float = TRUE;
5008 }
5009 f2 = var2.vval.v_float;
5010 n2 = 0;
5011 }
5012 else
5013#endif
5014 {
5015 n2 = get_tv_number_chk(&var2, &error);
5016 clear_tv(&var2);
5017 if (error)
5018 return FAIL;
5019#ifdef FEAT_FLOAT
5020 if (use_float)
5021 f2 = n2;
5022#endif
5023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025 /*
5026 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005027 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029#ifdef FEAT_FLOAT
5030 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005032 if (op == '*')
5033 f1 = f1 * f2;
5034 else if (op == '/')
5035 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005036# ifdef VMS
5037 /* VMS crashes on divide by zero, work around it */
5038 if (f2 == 0.0)
5039 {
5040 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005041 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005042 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005043 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005044 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005045 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005046 }
5047 else
5048 f1 = f1 / f2;
5049# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005050 /* We rely on the floating point library to handle divide
5051 * by zero to result in "inf" and not a crash. */
5052 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005053# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005057 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058 return FAIL;
5059 }
5060 rettv->v_type = VAR_FLOAT;
5061 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066 if (op == '*')
5067 n1 = n1 * n2;
5068 else if (op == '/')
5069 {
5070 if (n2 == 0) /* give an error message? */
5071 {
5072 if (n1 == 0)
5073 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5074 else if (n1 < 0)
5075 n1 = -0x7fffffffL;
5076 else
5077 n1 = 0x7fffffffL;
5078 }
5079 else
5080 n1 = n1 / n2;
5081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005083 {
5084 if (n2 == 0) /* give an error message? */
5085 n1 = 0;
5086 else
5087 n1 = n1 % n2;
5088 }
5089 rettv->v_type = VAR_NUMBER;
5090 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 }
5094
5095 return OK;
5096}
5097
5098/*
5099 * Handle sixth level expression:
5100 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005101 * "string" string constant
5102 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 * &option-name option value
5104 * @r register contents
5105 * identifier variable value
5106 * function() function call
5107 * $VAR environment variable
5108 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005109 * [expr, expr] List
5110 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 *
5112 * Also handle:
5113 * ! in front logical NOT
5114 * - in front unary minus
5115 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005116 * trailing [] subscript in String or List
5117 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 *
5119 * "arg" must point to the first non-white of the expression.
5120 * "arg" is advanced to the next non-white after the recognized expression.
5121 *
5122 * Return OK or FAIL.
5123 */
5124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005125eval7(
5126 char_u **arg,
5127 typval_T *rettv,
5128 int evaluate,
5129 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 long n;
5132 int len;
5133 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 char_u *start_leader, *end_leader;
5135 int ret = OK;
5136 char_u *alias;
5137
5138 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005139 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005140 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005142 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 /*
5145 * Skip '!' and '-' characters. They are handled later.
5146 */
5147 start_leader = *arg;
5148 while (**arg == '!' || **arg == '-' || **arg == '+')
5149 *arg = skipwhite(*arg + 1);
5150 end_leader = *arg;
5151
5152 switch (**arg)
5153 {
5154 /*
5155 * Number constant.
5156 */
5157 case '0':
5158 case '1':
5159 case '2':
5160 case '3':
5161 case '4':
5162 case '5':
5163 case '6':
5164 case '7':
5165 case '8':
5166 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005167 {
5168#ifdef FEAT_FLOAT
5169 char_u *p = skipdigits(*arg + 1);
5170 int get_float = FALSE;
5171
5172 /* We accept a float when the format matches
5173 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005174 * strict to avoid backwards compatibility problems.
5175 * Don't look for a float after the "." operator, so that
5176 * ":let vers = 1.2.3" doesn't fail. */
5177 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005179 get_float = TRUE;
5180 p = skipdigits(p + 2);
5181 if (*p == 'e' || *p == 'E')
5182 {
5183 ++p;
5184 if (*p == '-' || *p == '+')
5185 ++p;
5186 if (!vim_isdigit(*p))
5187 get_float = FALSE;
5188 else
5189 p = skipdigits(p + 1);
5190 }
5191 if (ASCII_ISALPHA(*p) || *p == '.')
5192 get_float = FALSE;
5193 }
5194 if (get_float)
5195 {
5196 float_T f;
5197
5198 *arg += string2float(*arg, &f);
5199 if (evaluate)
5200 {
5201 rettv->v_type = VAR_FLOAT;
5202 rettv->vval.v_float = f;
5203 }
5204 }
5205 else
5206#endif
5207 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005208 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005209 *arg += len;
5210 if (evaluate)
5211 {
5212 rettv->v_type = VAR_NUMBER;
5213 rettv->vval.v_number = n;
5214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
5219 /*
5220 * String constant: "string".
5221 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224
5225 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 break;
5230
5231 /*
5232 * List: [expr, expr]
5233 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005234 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 break;
5236
5237 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 * Dictionary: {key: val, key: val}
5239 */
5240 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5241 break;
5242
5243 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005244 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005246 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 break;
5248
5249 /*
5250 * Environment variable: $VAR.
5251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 break;
5254
5255 /*
5256 * Register contents: @r.
5257 */
5258 case '@': ++*arg;
5259 if (evaluate)
5260 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005262 rettv->vval.v_string = get_reg_contents(**arg,
5263 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265 if (**arg != NUL)
5266 ++*arg;
5267 break;
5268
5269 /*
5270 * nested expression: (expression).
5271 */
5272 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005273 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 if (**arg == ')')
5275 ++*arg;
5276 else if (ret == OK)
5277 {
5278 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005279 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 ret = FAIL;
5281 }
5282 break;
5283
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 break;
5286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287
5288 if (ret == NOTDONE)
5289 {
5290 /*
5291 * Must be a variable or function name.
5292 * Can also be a curly-braces kind of name: {expr}.
5293 */
5294 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005295 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005296 if (alias != NULL)
5297 s = alias;
5298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 ret = FAIL;
5301 else
5302 {
5303 if (**arg == '(') /* recursive! */
5304 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005305 partial_T *partial;
5306
Bram Moolenaar8c711452005-01-14 21:53:12 +00005307 /* If "s" is the name of a variable of type VAR_FUNC
5308 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005309 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310
5311 /* Invoke the function. */
5312 ret = get_func_tv(s, len, rettv, arg,
5313 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005314 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005315
5316 /* If evaluate is FALSE rettv->v_type was not set in
5317 * get_func_tv, but it's needed in handle_subscript() to parse
5318 * what follows. So set it here. */
5319 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5320 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005321 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005322 rettv->v_type = VAR_FUNC;
5323 }
5324
Bram Moolenaar8c711452005-01-14 21:53:12 +00005325 /* Stop the expression evaluation when immediately
5326 * aborting on error, or when an interrupt occurred or
5327 * an exception was thrown but not caught. */
5328 if (aborting())
5329 {
5330 if (ret == OK)
5331 clear_tv(rettv);
5332 ret = FAIL;
5333 }
5334 }
5335 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005336 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005337 else
5338 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005339 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005340 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 }
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 *arg = skipwhite(*arg);
5344
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005345 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5346 * expr(expr). */
5347 if (ret == OK)
5348 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350 /*
5351 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5352 */
5353 if (ret == OK && evaluate && end_leader > start_leader)
5354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356 int val = 0;
5357#ifdef FEAT_FLOAT
5358 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005359
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005360 if (rettv->v_type == VAR_FLOAT)
5361 f = rettv->vval.v_float;
5362 else
5363#endif
5364 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005365 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005367 clear_tv(rettv);
5368 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005370 else
5371 {
5372 while (end_leader > start_leader)
5373 {
5374 --end_leader;
5375 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005376 {
5377#ifdef FEAT_FLOAT
5378 if (rettv->v_type == VAR_FLOAT)
5379 f = !f;
5380 else
5381#endif
5382 val = !val;
5383 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005384 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005385 {
5386#ifdef FEAT_FLOAT
5387 if (rettv->v_type == VAR_FLOAT)
5388 f = -f;
5389 else
5390#endif
5391 val = -val;
5392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005393 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005394#ifdef FEAT_FLOAT
5395 if (rettv->v_type == VAR_FLOAT)
5396 {
5397 clear_tv(rettv);
5398 rettv->vval.v_float = f;
5399 }
5400 else
5401#endif
5402 {
5403 clear_tv(rettv);
5404 rettv->v_type = VAR_NUMBER;
5405 rettv->vval.v_number = val;
5406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
5409
5410 return ret;
5411}
5412
5413/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005414 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5415 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5417 */
5418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419eval_index(
5420 char_u **arg,
5421 typval_T *rettv,
5422 int evaluate,
5423 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005424{
5425 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005426 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428 long len = -1;
5429 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432
Bram Moolenaara03f2332016-02-06 18:09:59 +01005433 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005435 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005436 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005437 if (verbose)
5438 EMSG(_("E695: Cannot index a Funcref"));
5439 return FAIL;
5440 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005441#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005442 if (verbose)
5443 EMSG(_(e_float_as_string));
5444 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005445#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005446 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005447 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005448 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005449 if (verbose)
5450 EMSG(_("E909: Cannot index a special variable"));
5451 return FAIL;
5452 case VAR_UNKNOWN:
5453 if (evaluate)
5454 return FAIL;
5455 /* FALLTHROUGH */
5456
5457 case VAR_STRING:
5458 case VAR_NUMBER:
5459 case VAR_LIST:
5460 case VAR_DICT:
5461 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005462 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005464 init_tv(&var1);
5465 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 /*
5469 * dict.name
5470 */
5471 key = *arg + 1;
5472 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5473 ;
5474 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005480 /*
5481 * something[idx]
5482 *
5483 * Get the (first) variable from inside the [].
5484 */
5485 *arg = skipwhite(*arg + 1);
5486 if (**arg == ':')
5487 empty1 = TRUE;
5488 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5489 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005490 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5491 {
5492 /* not a number or string */
5493 clear_tv(&var1);
5494 return FAIL;
5495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496
5497 /*
5498 * Get the second variable from inside the [:].
5499 */
5500 if (**arg == ':')
5501 {
5502 range = TRUE;
5503 *arg = skipwhite(*arg + 1);
5504 if (**arg == ']')
5505 empty2 = TRUE;
5506 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005508 if (!empty1)
5509 clear_tv(&var1);
5510 return FAIL;
5511 }
5512 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5513 {
5514 /* not a number or string */
5515 if (!empty1)
5516 clear_tv(&var1);
5517 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518 return FAIL;
5519 }
5520 }
5521
5522 /* Check for the ']'. */
5523 if (**arg != ']')
5524 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005525 if (verbose)
5526 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005527 clear_tv(&var1);
5528 if (range)
5529 clear_tv(&var2);
5530 return FAIL;
5531 }
5532 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 }
5534
5535 if (evaluate)
5536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005537 n1 = 0;
5538 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005540 n1 = get_tv_number(&var1);
5541 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 }
5543 if (range)
5544 {
5545 if (empty2)
5546 n2 = -1;
5547 else
5548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 n2 = get_tv_number(&var2);
5550 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 }
5552 }
5553
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005556 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005557 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005558 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005559 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005560 case VAR_SPECIAL:
5561 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005562 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005563 break; /* not evaluating, skipping over subscript */
5564
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 case VAR_NUMBER:
5566 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 len = (long)STRLEN(s);
5569 if (range)
5570 {
5571 /* The resulting variable is a substring. If the indexes
5572 * are out of range the result is empty. */
5573 if (n1 < 0)
5574 {
5575 n1 = len + n1;
5576 if (n1 < 0)
5577 n1 = 0;
5578 }
5579 if (n2 < 0)
5580 n2 = len + n2;
5581 else if (n2 >= len)
5582 n2 = len;
5583 if (n1 >= len || n2 < 0 || n1 > n2)
5584 s = NULL;
5585 else
5586 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5587 }
5588 else
5589 {
5590 /* The resulting variable is a string of a single
5591 * character. If the index is too big or negative the
5592 * result is empty. */
5593 if (n1 >= len || n1 < 0)
5594 s = NULL;
5595 else
5596 s = vim_strnsave(s + n1, 1);
5597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 clear_tv(rettv);
5599 rettv->v_type = VAR_STRING;
5600 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 break;
5602
5603 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005605 if (n1 < 0)
5606 n1 = len + n1;
5607 if (!empty1 && (n1 < 0 || n1 >= len))
5608 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005609 /* For a range we allow invalid values and return an empty
5610 * list. A list index out of range is an error. */
5611 if (!range)
5612 {
5613 if (verbose)
5614 EMSGN(_(e_listidx), n1);
5615 return FAIL;
5616 }
5617 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 }
5619 if (range)
5620 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005621 list_T *l;
5622 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623
5624 if (n2 < 0)
5625 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005626 else if (n2 >= len)
5627 n2 = len - 1;
5628 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005629 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 l = list_alloc();
5631 if (l == NULL)
5632 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 n1 <= n2; ++n1)
5635 {
5636 if (list_append_tv(l, &item->li_tv) == FAIL)
5637 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005638 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005639 return FAIL;
5640 }
5641 item = item->li_next;
5642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005643 clear_tv(rettv);
5644 rettv->v_type = VAR_LIST;
5645 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005646 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005647 }
5648 else
5649 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005650 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005651 clear_tv(rettv);
5652 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 }
5654 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005655
5656 case VAR_DICT:
5657 if (range)
5658 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 if (verbose)
5660 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (len == -1)
5662 clear_tv(&var1);
5663 return FAIL;
5664 }
5665 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005666 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667
5668 if (len == -1)
5669 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005670 key = get_tv_string_chk(&var1);
5671 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 clear_tv(&var1);
5674 return FAIL;
5675 }
5676 }
5677
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005678 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005680 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005681 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 if (len == -1)
5683 clear_tv(&var1);
5684 if (item == NULL)
5685 return FAIL;
5686
5687 copy_tv(&item->di_tv, &var1);
5688 clear_tv(rettv);
5689 *rettv = var1;
5690 }
5691 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005692 }
5693 }
5694
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 return OK;
5696}
5697
5698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 * Get an option value.
5700 * "arg" points to the '&' or '+' before the option name.
5701 * "arg" is advanced to character after the option name.
5702 * Return OK or FAIL.
5703 */
5704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005705get_option_tv(
5706 char_u **arg,
5707 typval_T *rettv, /* when NULL, only check if option exists */
5708 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 char_u *option_end;
5711 long numval;
5712 char_u *stringval;
5713 int opt_type;
5714 int c;
5715 int working = (**arg == '+'); /* has("+option") */
5716 int ret = OK;
5717 int opt_flags;
5718
5719 /*
5720 * Isolate the option name and find its value.
5721 */
5722 option_end = find_option_end(arg, &opt_flags);
5723 if (option_end == NULL)
5724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005725 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 EMSG2(_("E112: Option name missing: %s"), *arg);
5727 return FAIL;
5728 }
5729
5730 if (!evaluate)
5731 {
5732 *arg = option_end;
5733 return OK;
5734 }
5735
5736 c = *option_end;
5737 *option_end = NUL;
5738 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005739 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
5741 if (opt_type == -3) /* invalid name */
5742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005743 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 EMSG2(_("E113: Unknown option: %s"), *arg);
5745 ret = FAIL;
5746 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005747 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (opt_type == -2) /* hidden string option */
5750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005751 rettv->v_type = VAR_STRING;
5752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754 else if (opt_type == -1) /* hidden number option */
5755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 rettv->v_type = VAR_NUMBER;
5757 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 else if (opt_type == 1) /* number option */
5760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 rettv->v_type = VAR_NUMBER;
5762 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764 else /* string option */
5765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005766 rettv->v_type = VAR_STRING;
5767 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 }
5770 else if (working && (opt_type == -2 || opt_type == -1))
5771 ret = FAIL;
5772
5773 *option_end = c; /* put back for error messages */
5774 *arg = option_end;
5775
5776 return ret;
5777}
5778
5779/*
5780 * Allocate a variable for a string constant.
5781 * Return OK or FAIL.
5782 */
5783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005784get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 char_u *p;
5787 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 int extra = 0;
5789
5790 /*
5791 * Find the end of the string, skipping backslashed characters.
5792 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 if (*p == '\\' && p[1] != NUL)
5796 {
5797 ++p;
5798 /* A "\<x>" form occupies at least 4 characters, and produces up
5799 * to 6 characters: reserve space for 2 extra */
5800 if (*p == '<')
5801 extra += 2;
5802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
5804
5805 if (*p != '"')
5806 {
5807 EMSG2(_("E114: Missing quote: %s"), *arg);
5808 return FAIL;
5809 }
5810
5811 /* If only parsing, set *arg and return here */
5812 if (!evaluate)
5813 {
5814 *arg = p + 1;
5815 return OK;
5816 }
5817
5818 /*
5819 * Copy the string into allocated memory, handling backslashed
5820 * characters.
5821 */
5822 name = alloc((unsigned)(p - *arg + extra));
5823 if (name == NULL)
5824 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 rettv->v_type = VAR_STRING;
5826 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {
5830 if (*p == '\\')
5831 {
5832 switch (*++p)
5833 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 case 'b': *name++ = BS; ++p; break;
5835 case 'e': *name++ = ESC; ++p; break;
5836 case 'f': *name++ = FF; ++p; break;
5837 case 'n': *name++ = NL; ++p; break;
5838 case 'r': *name++ = CAR; ++p; break;
5839 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841 case 'X': /* hex: "\x1", "\x12" */
5842 case 'x':
5843 case 'u': /* Unicode: "\u0023" */
5844 case 'U':
5845 if (vim_isxdigit(p[1]))
5846 {
5847 int n, nr;
5848 int c = toupper(*p);
5849
5850 if (c == 'X')
5851 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005852 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005854 else
5855 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 nr = 0;
5857 while (--n >= 0 && vim_isxdigit(p[1]))
5858 {
5859 ++p;
5860 nr = (nr << 4) + hex2nr(*p);
5861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#ifdef FEAT_MBYTE
5864 /* For "\u" store the number according to
5865 * 'encoding'. */
5866 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 else
5869#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 break;
5873
5874 /* octal: "\1", "\12", "\123" */
5875 case '0':
5876 case '1':
5877 case '2':
5878 case '3':
5879 case '4':
5880 case '5':
5881 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 case '7': *name = *p++ - '0';
5883 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 *name = (*name << 3) + *p++ - '0';
5886 if (*p >= '0' && *p <= '7')
5887 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 break;
5891
5892 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 if (extra != 0)
5895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 break;
5898 }
5899 /* FALLTHROUGH */
5900
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 break;
5903 }
5904 }
5905 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 *arg = p + 1;
5911
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 return OK;
5913}
5914
5915/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 * Return OK or FAIL.
5918 */
5919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 int reduce = 0;
5925
5926 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 */
5929 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 break;
5935 ++reduce;
5936 ++p;
5937 }
5938 }
5939
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005943 return FAIL;
5944 }
5945
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 if (!evaluate)
5948 {
5949 *arg = p + 1;
5950 return OK;
5951 }
5952
5953 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955 */
5956 str = alloc((unsigned)((p - *arg) - reduce));
5957 if (str == NULL)
5958 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 rettv->v_type = VAR_STRING;
5960 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005961
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 break;
5968 ++p;
5969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005970 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 *arg = p + 1;
5974
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005975 return OK;
5976}
5977
Bram Moolenaarddecc252016-04-06 22:59:37 +02005978 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980{
5981 int i;
5982
5983 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005984 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005985 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005986 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005987 func_unref(pt->pt_name);
5988 vim_free(pt->pt_name);
5989 vim_free(pt);
5990}
5991
5992/*
5993 * Unreference a closure: decrement the reference count and free it when it
5994 * becomes zero.
5995 */
5996 void
5997partial_unref(partial_T *pt)
5998{
5999 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006000 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006001}
6002
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006003/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 * Allocate a variable for a List and fill it from "*arg".
6005 * Return OK or FAIL.
6006 */
6007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009{
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 list_T *l = NULL;
6011 typval_T tv;
6012 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013
6014 if (evaluate)
6015 {
6016 l = list_alloc();
6017 if (l == NULL)
6018 return FAIL;
6019 }
6020
6021 *arg = skipwhite(*arg + 1);
6022 while (**arg != ']' && **arg != NUL)
6023 {
6024 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6025 goto failret;
6026 if (evaluate)
6027 {
6028 item = listitem_alloc();
6029 if (item != NULL)
6030 {
6031 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006032 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 list_append(l, item);
6034 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006035 else
6036 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 }
6038
6039 if (**arg == ']')
6040 break;
6041 if (**arg != ',')
6042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006043 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 goto failret;
6045 }
6046 *arg = skipwhite(*arg + 1);
6047 }
6048
6049 if (**arg != ']')
6050 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006051 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052failret:
6053 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006054 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 return FAIL;
6056 }
6057
6058 *arg = skipwhite(*arg + 1);
6059 if (evaluate)
6060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006061 rettv->v_type = VAR_LIST;
6062 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 ++l->lv_refcount;
6064 }
6065
6066 return OK;
6067}
6068
6069/*
6070 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006071 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006073 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006076 list_T *l;
6077
6078 l = (list_T *)alloc_clear(sizeof(list_T));
6079 if (l != NULL)
6080 {
6081 /* Prepend the list to the list of lists for garbage collection. */
6082 if (first_list != NULL)
6083 first_list->lv_used_prev = l;
6084 l->lv_used_prev = NULL;
6085 l->lv_used_next = first_list;
6086 first_list = l;
6087 }
6088 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006092 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006093 * Returns OK or FAIL.
6094 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006097{
6098 list_T *l = list_alloc();
6099
6100 if (l == NULL)
6101 return FAIL;
6102
6103 rettv->vval.v_list = l;
6104 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006105 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006106 ++l->lv_refcount;
6107 return OK;
6108}
6109
6110/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111 * Unreference a list: decrement the reference count and free it when it
6112 * becomes zero.
6113 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006115list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006117 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006118 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119}
6120
6121/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006122 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123 * Ignores the reference count.
6124 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006125 static void
6126list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127{
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006130 for (item = l->lv_first; item != NULL; item = l->lv_first)
6131 {
6132 /* Remove the item before deleting it. */
6133 l->lv_first = item->li_next;
6134 clear_tv(&item->li_tv);
6135 vim_free(item);
6136 }
6137}
6138
6139 static void
6140list_free_list(list_T *l)
6141{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006142 /* Remove the list from the list of lists for garbage collection. */
6143 if (l->lv_used_prev == NULL)
6144 first_list = l->lv_used_next;
6145 else
6146 l->lv_used_prev->lv_used_next = l->lv_used_next;
6147 if (l->lv_used_next != NULL)
6148 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6149
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 vim_free(l);
6151}
6152
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006153 void
6154list_free(list_T *l)
6155{
6156 if (!in_free_unref_items)
6157 {
6158 list_free_contents(l);
6159 list_free_list(l);
6160 }
6161}
6162
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163/*
6164 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006165 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006167 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006168listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169{
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171}
6172
6173/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006174 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006175 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180 vim_free(item);
6181}
6182
6183/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006184 * Remove a list item from a List and free it. Also clears the value.
6185 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006188{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006189 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006190 listitem_free(item);
6191}
6192
6193/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006194 * Get the number of items in a list.
6195 */
6196 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006197list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199 if (l == NULL)
6200 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006201 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202}
6203
6204/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 * Return TRUE when two lists have exactly the same values.
6206 */
6207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006208list_equal(
6209 list_T *l1,
6210 list_T *l2,
6211 int ic, /* ignore case for strings */
6212 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006213{
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006216 if (l1 == NULL || l2 == NULL)
6217 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006218 if (l1 == l2)
6219 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 if (list_len(l1) != list_len(l2))
6221 return FALSE;
6222
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006223 for (item1 = l1->lv_first, item2 = l2->lv_first;
6224 item1 != NULL && item2 != NULL;
6225 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006226 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 return FALSE;
6228 return item1 == NULL && item2 == NULL;
6229}
6230
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006231/*
6232 * Return the dictitem that an entry in a hashtable points to.
6233 */
6234 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006235dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006236{
6237 return HI2DI(hi);
6238}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006239
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006241 * Return TRUE when two dictionaries have exactly the same key/values.
6242 */
6243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006244dict_equal(
6245 dict_T *d1,
6246 dict_T *d2,
6247 int ic, /* ignore case for strings */
6248 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006249{
Bram Moolenaar33570922005-01-25 22:26:29 +00006250 hashitem_T *hi;
6251 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006253
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006254 if (d1 == NULL && d2 == NULL)
6255 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006256 if (d1 == NULL || d2 == NULL)
6257 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006258 if (d1 == d2)
6259 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006260 if (dict_len(d1) != dict_len(d2))
6261 return FALSE;
6262
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006263 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006264 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006265 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006266 if (!HASHITEM_EMPTY(hi))
6267 {
6268 item2 = dict_find(d2, hi->hi_key, -1);
6269 if (item2 == NULL)
6270 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006271 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006272 return FALSE;
6273 --todo;
6274 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275 }
6276 return TRUE;
6277}
6278
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006279static int tv_equal_recurse_limit;
6280
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006281 static int
6282func_equal(
6283 typval_T *tv1,
6284 typval_T *tv2,
6285 int ic) /* ignore case */
6286{
6287 char_u *s1, *s2;
6288 dict_T *d1, *d2;
6289 int a1, a2;
6290 int i;
6291
6292 /* empty and NULL function name considered the same */
6293 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6294 : tv1->vval.v_partial->pt_name;
6295 if (s1 != NULL && *s1 == NUL)
6296 s1 = NULL;
6297 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6298 : tv2->vval.v_partial->pt_name;
6299 if (s2 != NULL && *s2 == NUL)
6300 s2 = NULL;
6301 if (s1 == NULL || s2 == NULL)
6302 {
6303 if (s1 != s2)
6304 return FALSE;
6305 }
6306 else if (STRCMP(s1, s2) != 0)
6307 return FALSE;
6308
6309 /* empty dict and NULL dict is different */
6310 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6311 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6312 if (d1 == NULL || d2 == NULL)
6313 {
6314 if (d1 != d2)
6315 return FALSE;
6316 }
6317 else if (!dict_equal(d1, d2, ic, TRUE))
6318 return FALSE;
6319
6320 /* empty list and no list considered the same */
6321 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6322 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6323 if (a1 != a2)
6324 return FALSE;
6325 for (i = 0; i < a1; ++i)
6326 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6327 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6328 return FALSE;
6329
6330 return TRUE;
6331}
6332
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006333/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006335 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006336 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 */
6338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006339tv_equal(
6340 typval_T *tv1,
6341 typval_T *tv2,
6342 int ic, /* ignore case */
6343 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006344{
6345 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006346 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006347 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006348 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006349
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006350 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006351 * recursiveness to a limit. We guess they are equal then.
6352 * A fixed limit has the problem of still taking an awful long time.
6353 * Reduce the limit every time running into it. That should work fine for
6354 * deeply linked structures that are not recursively linked and catch
6355 * recursiveness quickly. */
6356 if (!recursive)
6357 tv_equal_recurse_limit = 1000;
6358 if (recursive_cnt >= tv_equal_recurse_limit)
6359 {
6360 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006361 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006362 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006363
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006364 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6365 if ((tv1->v_type == VAR_FUNC
6366 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6367 && (tv2->v_type == VAR_FUNC
6368 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6369 {
6370 ++recursive_cnt;
6371 r = func_equal(tv1, tv2, ic);
6372 --recursive_cnt;
6373 return r;
6374 }
6375
6376 if (tv1->v_type != tv2->v_type)
6377 return FALSE;
6378
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006379 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006380 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006381 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006382 ++recursive_cnt;
6383 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6384 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006385 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006386
6387 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006388 ++recursive_cnt;
6389 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6390 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006391 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006392
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006393 case VAR_NUMBER:
6394 return tv1->vval.v_number == tv2->vval.v_number;
6395
6396 case VAR_STRING:
6397 s1 = get_tv_string_buf(tv1, buf1);
6398 s2 = get_tv_string_buf(tv2, buf2);
6399 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006400
6401 case VAR_SPECIAL:
6402 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006403
6404 case VAR_FLOAT:
6405#ifdef FEAT_FLOAT
6406 return tv1->vval.v_float == tv2->vval.v_float;
6407#endif
6408 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006409#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006410 return tv1->vval.v_job == tv2->vval.v_job;
6411#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006412 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006413#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006414 return tv1->vval.v_channel == tv2->vval.v_channel;
6415#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006416 case VAR_FUNC:
6417 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006418 case VAR_UNKNOWN:
6419 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006420 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006421
Bram Moolenaara03f2332016-02-06 18:09:59 +01006422 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6423 * does not equal anything, not even itself. */
6424 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425}
6426
6427/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428 * Locate item with index "n" in list "l" and return it.
6429 * A negative index is counted from the end; -1 is the last item.
6430 * Returns NULL when "n" is out of range.
6431 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006432 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434{
Bram Moolenaar33570922005-01-25 22:26:29 +00006435 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 long idx;
6437
6438 if (l == NULL)
6439 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006440
6441 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006443 n = l->lv_len + n;
6444
6445 /* Check for index out of range. */
6446 if (n < 0 || n >= l->lv_len)
6447 return NULL;
6448
6449 /* When there is a cached index may start search from there. */
6450 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006452 if (n < l->lv_idx / 2)
6453 {
6454 /* closest to the start of the list */
6455 item = l->lv_first;
6456 idx = 0;
6457 }
6458 else if (n > (l->lv_idx + l->lv_len) / 2)
6459 {
6460 /* closest to the end of the list */
6461 item = l->lv_last;
6462 idx = l->lv_len - 1;
6463 }
6464 else
6465 {
6466 /* closest to the cached index */
6467 item = l->lv_idx_item;
6468 idx = l->lv_idx;
6469 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006470 }
6471 else
6472 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006473 if (n < l->lv_len / 2)
6474 {
6475 /* closest to the start of the list */
6476 item = l->lv_first;
6477 idx = 0;
6478 }
6479 else
6480 {
6481 /* closest to the end of the list */
6482 item = l->lv_last;
6483 idx = l->lv_len - 1;
6484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006486
6487 while (n > idx)
6488 {
6489 /* search forward */
6490 item = item->li_next;
6491 ++idx;
6492 }
6493 while (n < idx)
6494 {
6495 /* search backward */
6496 item = item->li_prev;
6497 --idx;
6498 }
6499
6500 /* cache the used index */
6501 l->lv_idx = idx;
6502 l->lv_idx_item = item;
6503
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 return item;
6505}
6506
6507/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006508 * Get list item "l[idx]" as a number.
6509 */
6510 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511list_find_nr(
6512 list_T *l,
6513 long idx,
6514 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006515{
6516 listitem_T *li;
6517
6518 li = list_find(l, idx);
6519 if (li == NULL)
6520 {
6521 if (errorp != NULL)
6522 *errorp = TRUE;
6523 return -1L;
6524 }
6525 return get_tv_number_chk(&li->li_tv, errorp);
6526}
6527
6528/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006529 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6530 */
6531 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006533{
6534 listitem_T *li;
6535
6536 li = list_find(l, idx - 1);
6537 if (li == NULL)
6538 {
6539 EMSGN(_(e_listidx), idx);
6540 return NULL;
6541 }
6542 return get_tv_string(&li->li_tv);
6543}
6544
6545/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006546 * Locate "item" list "l" and return its index.
6547 * Returns -1 when "item" is not in the list.
6548 */
6549 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006551{
6552 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006554
6555 if (l == NULL)
6556 return -1;
6557 idx = 0;
6558 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6559 ++idx;
6560 if (li == NULL)
6561 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006562 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006563}
6564
6565/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 * Append item "item" to the end of list "l".
6567 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006569list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570{
6571 if (l->lv_last == NULL)
6572 {
6573 /* empty list */
6574 l->lv_first = item;
6575 l->lv_last = item;
6576 item->li_prev = NULL;
6577 }
6578 else
6579 {
6580 l->lv_last->li_next = item;
6581 item->li_prev = l->lv_last;
6582 l->lv_last = item;
6583 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006584 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 item->li_next = NULL;
6586}
6587
6588/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006592 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006593list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006594{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006595 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596
Bram Moolenaar05159a02005-02-26 23:04:13 +00006597 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006599 copy_tv(tv, &li->li_tv);
6600 list_append(l, li);
6601 return OK;
6602}
6603
6604/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006605 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006606 * Return FAIL when out of memory.
6607 */
6608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006610{
6611 listitem_T *li = listitem_alloc();
6612
6613 if (li == NULL)
6614 return FAIL;
6615 li->li_tv.v_type = VAR_DICT;
6616 li->li_tv.v_lock = 0;
6617 li->li_tv.vval.v_dict = dict;
6618 list_append(list, li);
6619 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 return OK;
6621}
6622
6623/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006624 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006625 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006626 * Returns FAIL when out of memory.
6627 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006629list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006630{
6631 listitem_T *li = listitem_alloc();
6632
6633 if (li == NULL)
6634 return FAIL;
6635 list_append(l, li);
6636 li->li_tv.v_type = VAR_STRING;
6637 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006638 if (str == NULL)
6639 li->li_tv.vval.v_string = NULL;
6640 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006641 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006642 return FAIL;
6643 return OK;
6644}
6645
6646/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006647 * Append "n" to list "l".
6648 * Returns FAIL when out of memory.
6649 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006650 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006651list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006652{
6653 listitem_T *li;
6654
6655 li = listitem_alloc();
6656 if (li == NULL)
6657 return FAIL;
6658 li->li_tv.v_type = VAR_NUMBER;
6659 li->li_tv.v_lock = 0;
6660 li->li_tv.vval.v_number = n;
6661 list_append(l, li);
6662 return OK;
6663}
6664
6665/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006666 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 * If "item" is NULL append at the end.
6668 * Return FAIL when out of memory.
6669 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006671list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006672{
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674
6675 if (ni == NULL)
6676 return FAIL;
6677 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006678 list_insert(l, ni, item);
6679 return OK;
6680}
6681
6682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006684{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006685 if (item == NULL)
6686 /* Append new item at end of list. */
6687 list_append(l, ni);
6688 else
6689 {
6690 /* Insert new item before existing item. */
6691 ni->li_prev = item->li_prev;
6692 ni->li_next = item;
6693 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006694 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006695 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006696 ++l->lv_idx;
6697 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006698 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006699 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006700 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006701 l->lv_idx_item = NULL;
6702 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006703 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006704 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006705 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006706}
6707
6708/*
6709 * Extend "l1" with "l2".
6710 * If "bef" is NULL append at the end, otherwise insert before this item.
6711 * Returns FAIL when out of memory.
6712 */
6713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006715{
Bram Moolenaar33570922005-01-25 22:26:29 +00006716 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006717 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006719 /* We also quit the loop when we have inserted the original item count of
6720 * the list, avoid a hang when we extend a list with itself. */
6721 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006722 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6723 return FAIL;
6724 return OK;
6725}
6726
6727/*
6728 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6729 * Return FAIL when out of memory.
6730 */
6731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006732list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733{
Bram Moolenaar33570922005-01-25 22:26:29 +00006734 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006735
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006736 if (l1 == NULL || l2 == NULL)
6737 return FAIL;
6738
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006739 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006740 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741 if (l == NULL)
6742 return FAIL;
6743 tv->v_type = VAR_LIST;
6744 tv->vval.v_list = l;
6745
6746 /* append all items from the second list */
6747 return list_extend(l, l2, NULL);
6748}
6749
6750/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006751 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006752 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006753 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754 * Returns NULL when out of memory.
6755 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006756 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006757list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006758{
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 list_T *copy;
6760 listitem_T *item;
6761 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762
6763 if (orig == NULL)
6764 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765
6766 copy = list_alloc();
6767 if (copy != NULL)
6768 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 if (copyID != 0)
6770 {
6771 /* Do this before adding the items, because one of the items may
6772 * refer back to this list. */
6773 orig->lv_copyID = copyID;
6774 orig->lv_copylist = copy;
6775 }
6776 for (item = orig->lv_first; item != NULL && !got_int;
6777 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778 {
6779 ni = listitem_alloc();
6780 if (ni == NULL)
6781 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006782 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 {
6784 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6785 {
6786 vim_free(ni);
6787 break;
6788 }
6789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792 list_append(copy, ni);
6793 }
6794 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006795 if (item != NULL)
6796 {
6797 list_unref(copy);
6798 copy = NULL;
6799 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006800 }
6801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802 return copy;
6803}
6804
6805/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006806 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006807 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006808 * This used to be called list_remove, but that conflicts with a Sun header
6809 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006810 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006812vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006813{
Bram Moolenaar33570922005-01-25 22:26:29 +00006814 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006816 /* notify watchers */
6817 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006818 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006819 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006820 list_fix_watch(l, ip);
6821 if (ip == item2)
6822 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006824
6825 if (item2->li_next == NULL)
6826 l->lv_last = item->li_prev;
6827 else
6828 item2->li_next->li_prev = item->li_prev;
6829 if (item->li_prev == NULL)
6830 l->lv_first = item2->li_next;
6831 else
6832 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006833 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006834}
6835
6836/*
6837 * Return an allocated string with the string representation of a list.
6838 * May return NULL.
6839 */
6840 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006841list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842{
6843 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844
6845 if (tv->vval.v_list == NULL)
6846 return NULL;
6847 ga_init2(&ga, (int)sizeof(char), 80);
6848 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006849 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6850 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006851 {
6852 vim_free(ga.ga_data);
6853 return NULL;
6854 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006855 ga_append(&ga, ']');
6856 ga_append(&ga, NUL);
6857 return (char_u *)ga.ga_data;
6858}
6859
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006860typedef struct join_S {
6861 char_u *s;
6862 char_u *tofree;
6863} join_T;
6864
6865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006866list_join_inner(
6867 garray_T *gap, /* to store the result in */
6868 list_T *l,
6869 char_u *sep,
6870 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006871 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872 int copyID,
6873 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006874{
6875 int i;
6876 join_T *p;
6877 int len;
6878 int sumlen = 0;
6879 int first = TRUE;
6880 char_u *tofree;
6881 char_u numbuf[NUMBUFLEN];
6882 listitem_T *item;
6883 char_u *s;
6884
6885 /* Stringify each item in the list. */
6886 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6887 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006888 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6889 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006890 if (s == NULL)
6891 return FAIL;
6892
6893 len = (int)STRLEN(s);
6894 sumlen += len;
6895
Bram Moolenaarcde88542015-08-11 19:14:00 +02006896 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6898 if (tofree != NULL || s != numbuf)
6899 {
6900 p->s = s;
6901 p->tofree = tofree;
6902 }
6903 else
6904 {
6905 p->s = vim_strnsave(s, len);
6906 p->tofree = p->s;
6907 }
6908
6909 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006910 if (did_echo_string_emsg) /* recursion error, bail out */
6911 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006912 }
6913
6914 /* Allocate result buffer with its total size, avoid re-allocation and
6915 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6916 if (join_gap->ga_len >= 2)
6917 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6918 if (ga_grow(gap, sumlen + 2) == FAIL)
6919 return FAIL;
6920
6921 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6922 {
6923 if (first)
6924 first = FALSE;
6925 else
6926 ga_concat(gap, sep);
6927 p = ((join_T *)join_gap->ga_data) + i;
6928
6929 if (p->s != NULL)
6930 ga_concat(gap, p->s);
6931 line_breakcheck();
6932 }
6933
6934 return OK;
6935}
6936
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006937/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006938 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006939 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006940 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006941 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006942 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006943list_join(
6944 garray_T *gap,
6945 list_T *l,
6946 char_u *sep,
6947 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006948 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006949 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006950{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006951 garray_T join_ga;
6952 int retval;
6953 join_T *p;
6954 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006955
Bram Moolenaard39a7512015-04-16 22:51:22 +02006956 if (l->lv_len < 1)
6957 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006958 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006959 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6960 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006961
6962 /* Dispose each item in join_ga. */
6963 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006965 p = (join_T *)join_ga.ga_data;
6966 for (i = 0; i < join_ga.ga_len; ++i)
6967 {
6968 vim_free(p->tofree);
6969 ++p;
6970 }
6971 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006972 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006973
6974 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006975}
6976
6977/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006978 * Return the next (unique) copy ID.
6979 * Used for serializing nested structures.
6980 */
6981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006982get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006983{
6984 current_copyID += COPYID_INC;
6985 return current_copyID;
6986}
6987
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006988/* Used by get_func_tv() */
6989static garray_T funcargs = GA_EMPTY;
6990
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006991/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 * Garbage collection for lists and dictionaries.
6993 *
6994 * We use reference counts to be able to free most items right away when they
6995 * are no longer used. But for composite items it's possible that it becomes
6996 * unused while the reference count is > 0: When there is a recursive
6997 * reference. Example:
6998 * :let l = [1, 2, 3]
6999 * :let d = {9: l}
7000 * :let l[1] = d
7001 *
7002 * Since this is quite unusual we handle this with garbage collection: every
7003 * once in a while find out which lists and dicts are not referenced from any
7004 * variable.
7005 *
7006 * Here is a good reference text about garbage collection (refers to Python
7007 * but it applies to all reference-counting mechanisms):
7008 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007009 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007010
7011/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007013 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007015 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007017garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007018{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007019 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 buf_T *buf;
7022 win_T *wp;
7023 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007024 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007025 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007026 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007027#ifdef FEAT_WINDOWS
7028 tabpage_T *tp;
7029#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007031 if (!testing)
7032 {
7033 /* Only do this once. */
7034 want_garbage_collect = FALSE;
7035 may_garbage_collect = FALSE;
7036 garbage_collect_at_exit = FALSE;
7037 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007038
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007039 /* We advance by two because we add one for items referenced through
7040 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007041 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007042
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 /*
7044 * 1. Go through all accessible variables and mark all lists and dicts
7045 * with copyID.
7046 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007047
7048 /* Don't free variables in the previous_funccal list unless they are only
7049 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007050 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007051 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7052 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7054 NULL);
7055 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7056 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007057 }
7058
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059 /* script-local variables */
7060 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062
7063 /* buffer-local variables */
7064 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7066 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067
7068 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007069 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7071 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007072#ifdef FEAT_AUTOCMD
7073 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7075 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007076#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007078#ifdef FEAT_WINDOWS
7079 /* tabpage-local variables */
7080 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7082 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007083#endif
7084
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087
7088 /* function-local variables */
7089 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7090 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7092 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093 }
7094
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007095 /* function call arguments, if v:testing is set. */
7096 for (i = 0; i < funcargs.ga_len; ++i)
7097 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7098 copyID, NULL, NULL);
7099
Bram Moolenaard812df62008-11-09 12:46:09 +00007100 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007102
Bram Moolenaar1dced572012-04-05 16:54:08 +02007103#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007105#endif
7106
Bram Moolenaardb913952012-06-29 12:54:53 +02007107#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007109#endif
7110
7111#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007113#endif
7114
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007115#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007116 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007117 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007118#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007119#ifdef FEAT_NETBEANS_INTG
7120 abort = abort || set_ref_in_nb_channel(copyID);
7121#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007122
Bram Moolenaare3188e22016-05-31 21:13:04 +02007123#ifdef FEAT_TIMERS
7124 abort = abort || set_ref_in_timer(copyID);
7125#endif
7126
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007128 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007129 /*
7130 * 2. Free lists and dictionaries that are not referenced.
7131 */
7132 did_free = free_unref_items(copyID);
7133
7134 /*
7135 * 3. Check if any funccal can be freed now.
7136 */
7137 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007138 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 if (can_free_funccal(*pfc, copyID))
7140 {
7141 fc = *pfc;
7142 *pfc = fc->caller;
7143 free_funccal(fc, TRUE);
7144 did_free = TRUE;
7145 did_free_funccal = TRUE;
7146 }
7147 else
7148 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007149 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 if (did_free_funccal)
7151 /* When a funccal was freed some more items might be garbage
7152 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007153 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007154 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 else if (p_verbose > 0)
7156 {
7157 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7158 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007159
7160 return did_free;
7161}
7162
7163/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007164 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007165 */
7166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007168{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007169 dict_T *dd, *dd_next;
7170 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007171 int did_free = FALSE;
7172
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007173 /* Let all "free" functions know that we are here. This means no
7174 * dictionaries, lists, channels or jobs are to be freed, because we will
7175 * do that here. */
7176 in_free_unref_items = TRUE;
7177
7178 /*
7179 * PASS 1: free the contents of the items. We don't free the items
7180 * themselves yet, so that it is possible to decrement refcount counters
7181 */
7182
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007183 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007184 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007186 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007187 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007189 /* Free the Dictionary and ordinary items it contains, but don't
7190 * recurse into Lists and Dictionaries, they will be in the list
7191 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007192 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195
7196 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007197 * Go through the list of lists and free items without the copyID.
7198 * But don't free a list that has a watcher (used in a for loop), these
7199 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007201 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7202 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7203 && ll->lv_watch == NULL)
7204 {
7205 /* Free the List and ordinary items it contains, but don't recurse
7206 * into Lists and Dictionaries, they will be in the list of dicts
7207 * or list of lists. */
7208 list_free_contents(ll);
7209 did_free = TRUE;
7210 }
7211
7212#ifdef FEAT_JOB_CHANNEL
7213 /* Go through the list of jobs and free items without the copyID. This
7214 * must happen before doing channels, because jobs refer to channels, but
7215 * the reference from the channel to the job isn't tracked. */
7216 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7217
7218 /* Go through the list of channels and free items without the copyID. */
7219 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7220#endif
7221
7222 /*
7223 * PASS 2: free the items themselves.
7224 */
7225 for (dd = first_dict; dd != NULL; dd = dd_next)
7226 {
7227 dd_next = dd->dv_used_next;
7228 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7229 dict_free_dict(dd);
7230 }
7231
7232 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007233 {
7234 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007235 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7236 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007237 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007238 /* Free the List and ordinary items it contains, but don't recurse
7239 * into Lists and Dictionaries, they will be in the list of dicts
7240 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007241 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007243 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007244
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007245#ifdef FEAT_JOB_CHANNEL
7246 /* Go through the list of jobs and free items without the copyID. This
7247 * must happen before doing channels, because jobs refer to channels, but
7248 * the reference from the channel to the job isn't tracked. */
7249 free_unused_jobs(copyID, COPYID_MASK);
7250
7251 /* Go through the list of channels and free items without the copyID. */
7252 free_unused_channels(copyID, COPYID_MASK);
7253#endif
7254
7255 in_free_unref_items = FALSE;
7256
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007257 return did_free;
7258}
7259
7260/*
7261 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007262 * "list_stack" is used to add lists to be marked. Can be NULL.
7263 *
7264 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007268{
7269 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007270 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007271 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007272 hashtab_T *cur_ht;
7273 ht_stack_T *ht_stack = NULL;
7274 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007275
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007276 cur_ht = ht;
7277 for (;;)
7278 {
7279 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007281 /* Mark each item in the hashtab. If the item contains a hashtab
7282 * it is added to ht_stack, if it contains a list it is added to
7283 * list_stack. */
7284 todo = (int)cur_ht->ht_used;
7285 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7286 if (!HASHITEM_EMPTY(hi))
7287 {
7288 --todo;
7289 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7290 &ht_stack, list_stack);
7291 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007292 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293
7294 if (ht_stack == NULL)
7295 break;
7296
7297 /* take an item from the stack */
7298 cur_ht = ht_stack->ht;
7299 tempitem = ht_stack;
7300 ht_stack = ht_stack->prev;
7301 free(tempitem);
7302 }
7303
7304 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007305}
7306
7307/*
7308 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007309 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7310 *
7311 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007312 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007313 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007314set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007315{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007316 listitem_T *li;
7317 int abort = FALSE;
7318 list_T *cur_l;
7319 list_stack_T *list_stack = NULL;
7320 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007321
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007322 cur_l = l;
7323 for (;;)
7324 {
7325 if (!abort)
7326 /* Mark each item in the list. If the item contains a hashtab
7327 * it is added to ht_stack, if it contains a list it is added to
7328 * list_stack. */
7329 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7330 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7331 ht_stack, &list_stack);
7332 if (list_stack == NULL)
7333 break;
7334
7335 /* take an item from the stack */
7336 cur_l = list_stack->list;
7337 tempitem = list_stack;
7338 list_stack = list_stack->prev;
7339 free(tempitem);
7340 }
7341
7342 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007343}
7344
7345/*
7346 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007347 * "list_stack" is used to add lists to be marked. Can be NULL.
7348 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7349 *
7350 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007351 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007352 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007353set_ref_in_item(
7354 typval_T *tv,
7355 int copyID,
7356 ht_stack_T **ht_stack,
7357 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007358{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007359 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007360
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007361 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007362 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007363 dict_T *dd = tv->vval.v_dict;
7364
Bram Moolenaara03f2332016-02-06 18:09:59 +01007365 if (dd != NULL && dd->dv_copyID != copyID)
7366 {
7367 /* Didn't see this dict yet. */
7368 dd->dv_copyID = copyID;
7369 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007370 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007371 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7372 }
7373 else
7374 {
7375 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7376 if (newitem == NULL)
7377 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007378 else
7379 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007380 newitem->ht = &dd->dv_hashtab;
7381 newitem->prev = *ht_stack;
7382 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007383 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007384 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007385 }
7386 }
7387 else if (tv->v_type == VAR_LIST)
7388 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007389 list_T *ll = tv->vval.v_list;
7390
Bram Moolenaara03f2332016-02-06 18:09:59 +01007391 if (ll != NULL && ll->lv_copyID != copyID)
7392 {
7393 /* Didn't see this list yet. */
7394 ll->lv_copyID = copyID;
7395 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007396 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007397 abort = set_ref_in_list(ll, copyID, ht_stack);
7398 }
7399 else
7400 {
7401 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007402 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007403 if (newitem == NULL)
7404 abort = TRUE;
7405 else
7406 {
7407 newitem->list = ll;
7408 newitem->prev = *list_stack;
7409 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007410 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007411 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007412 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007413 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007414 else if (tv->v_type == VAR_PARTIAL)
7415 {
7416 partial_T *pt = tv->vval.v_partial;
7417 int i;
7418
7419 /* A partial does not have a copyID, because it cannot contain itself.
7420 */
7421 if (pt != NULL)
7422 {
7423 if (pt->pt_dict != NULL)
7424 {
7425 typval_T dtv;
7426
7427 dtv.v_type = VAR_DICT;
7428 dtv.vval.v_dict = pt->pt_dict;
7429 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7430 }
7431
7432 for (i = 0; i < pt->pt_argc; ++i)
7433 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7434 ht_stack, list_stack);
7435 }
7436 }
7437#ifdef FEAT_JOB_CHANNEL
7438 else if (tv->v_type == VAR_JOB)
7439 {
7440 job_T *job = tv->vval.v_job;
7441 typval_T dtv;
7442
7443 if (job != NULL && job->jv_copyID != copyID)
7444 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007445 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007446 if (job->jv_channel != NULL)
7447 {
7448 dtv.v_type = VAR_CHANNEL;
7449 dtv.vval.v_channel = job->jv_channel;
7450 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7451 }
7452 if (job->jv_exit_partial != NULL)
7453 {
7454 dtv.v_type = VAR_PARTIAL;
7455 dtv.vval.v_partial = job->jv_exit_partial;
7456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7457 }
7458 }
7459 }
7460 else if (tv->v_type == VAR_CHANNEL)
7461 {
7462 channel_T *ch =tv->vval.v_channel;
7463 int part;
7464 typval_T dtv;
7465 jsonq_T *jq;
7466 cbq_T *cq;
7467
7468 if (ch != NULL && ch->ch_copyID != copyID)
7469 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007470 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007471 for (part = PART_SOCK; part <= PART_IN; ++part)
7472 {
7473 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7474 jq = jq->jq_next)
7475 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7476 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7477 cq = cq->cq_next)
7478 if (cq->cq_partial != NULL)
7479 {
7480 dtv.v_type = VAR_PARTIAL;
7481 dtv.vval.v_partial = cq->cq_partial;
7482 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7483 }
7484 if (ch->ch_part[part].ch_partial != NULL)
7485 {
7486 dtv.v_type = VAR_PARTIAL;
7487 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7488 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7489 }
7490 }
7491 if (ch->ch_partial != NULL)
7492 {
7493 dtv.v_type = VAR_PARTIAL;
7494 dtv.vval.v_partial = ch->ch_partial;
7495 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7496 }
7497 if (ch->ch_close_partial != NULL)
7498 {
7499 dtv.v_type = VAR_PARTIAL;
7500 dtv.vval.v_partial = ch->ch_close_partial;
7501 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7502 }
7503 }
7504 }
7505#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007506 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007507}
7508
7509/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 * Allocate an empty header for a dictionary.
7511 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007512 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514{
Bram Moolenaar33570922005-01-25 22:26:29 +00007515 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007519 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007520 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007521 if (first_dict != NULL)
7522 first_dict->dv_used_prev = d;
7523 d->dv_used_next = first_dict;
7524 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007525 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007526
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007528 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007529 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007530 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007531 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007532 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534}
7535
7536/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007537 * Allocate an empty dict for a return value.
7538 * Returns OK or FAIL.
7539 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007542{
7543 dict_T *d = dict_alloc();
7544
7545 if (d == NULL)
7546 return FAIL;
7547
7548 rettv->vval.v_dict = d;
7549 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007550 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007551 ++d->dv_refcount;
7552 return OK;
7553}
7554
7555
7556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 * Unreference a Dictionary: decrement the reference count and free it when it
7558 * becomes zero.
7559 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007563 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007564 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565}
7566
7567/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007568 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 * Ignores the reference count.
7570 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007571 static void
7572dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007576 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007578 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007579 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 if (!HASHITEM_EMPTY(hi))
7584 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007585 /* Remove the item before deleting it, just in case there is
7586 * something recursive causing trouble. */
7587 di = HI2DI(hi);
7588 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007589 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007590 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007591 --todo;
7592 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007595}
7596
7597 static void
7598dict_free_dict(dict_T *d)
7599{
7600 /* Remove the dict from the list of dicts for garbage collection. */
7601 if (d->dv_used_prev == NULL)
7602 first_dict = d->dv_used_next;
7603 else
7604 d->dv_used_prev->dv_used_next = d->dv_used_next;
7605 if (d->dv_used_next != NULL)
7606 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 vim_free(d);
7608}
7609
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007610 void
7611dict_free(dict_T *d)
7612{
7613 if (!in_free_unref_items)
7614 {
7615 dict_free_contents(d);
7616 dict_free_dict(d);
7617 }
7618}
7619
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620/*
7621 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007622 * The "key" is copied to the new item.
7623 * Note that the value of the item "di_tv" still needs to be initialized!
7624 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007626 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007627dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628{
Bram Moolenaar33570922005-01-25 22:26:29 +00007629 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007631 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007635 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007636 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007637 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638}
7639
7640/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 * Make a copy of a Dictionary item.
7642 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007643 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007644dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645{
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007648 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7649 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007650 if (di != NULL)
7651 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007653 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 copy_tv(&org->di_tv, &di->di_tv);
7655 }
7656 return di;
7657}
7658
7659/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 * Remove item "item" from Dictionary "dict" and free it.
7661 */
7662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007663dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664{
Bram Moolenaar33570922005-01-25 22:26:29 +00007665 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668 if (HASHITEM_EMPTY(hi))
7669 EMSG2(_(e_intern2), "dictitem_remove()");
7670 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 dictitem_free(item);
7673}
7674
7675/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 * Free a dict item. Also clears the value.
7677 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007679dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007682 if (item->di_flags & DI_FLAGS_ALLOC)
7683 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684}
7685
7686/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007687 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7688 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007689 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 * Returns NULL when out of memory.
7691 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007693dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007694{
Bram Moolenaar33570922005-01-25 22:26:29 +00007695 dict_T *copy;
7696 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007699
7700 if (orig == NULL)
7701 return NULL;
7702
7703 copy = dict_alloc();
7704 if (copy != NULL)
7705 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007706 if (copyID != 0)
7707 {
7708 orig->dv_copyID = copyID;
7709 orig->dv_copydict = copy;
7710 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007711 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007712 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007714 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 --todo;
7717
7718 di = dictitem_alloc(hi->hi_key);
7719 if (di == NULL)
7720 break;
7721 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007722 {
7723 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7724 copyID) == FAIL)
7725 {
7726 vim_free(di);
7727 break;
7728 }
7729 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 else
7731 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7732 if (dict_add(copy, di) == FAIL)
7733 {
7734 dictitem_free(di);
7735 break;
7736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007737 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007741 if (todo > 0)
7742 {
7743 dict_unref(copy);
7744 copy = NULL;
7745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007746 }
7747
7748 return copy;
7749}
7750
7751/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007753 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007756dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757{
Bram Moolenaar33570922005-01-25 22:26:29 +00007758 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759}
7760
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007762 * Add a number or string entry to dictionary "d".
7763 * When "str" is NULL use number "nr", otherwise use "str".
7764 * Returns FAIL when out of memory and when key already exists.
7765 */
7766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007767dict_add_nr_str(
7768 dict_T *d,
7769 char *key,
7770 long nr,
7771 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007772{
7773 dictitem_T *item;
7774
7775 item = dictitem_alloc((char_u *)key);
7776 if (item == NULL)
7777 return FAIL;
7778 item->di_tv.v_lock = 0;
7779 if (str == NULL)
7780 {
7781 item->di_tv.v_type = VAR_NUMBER;
7782 item->di_tv.vval.v_number = nr;
7783 }
7784 else
7785 {
7786 item->di_tv.v_type = VAR_STRING;
7787 item->di_tv.vval.v_string = vim_strsave(str);
7788 }
7789 if (dict_add(d, item) == FAIL)
7790 {
7791 dictitem_free(item);
7792 return FAIL;
7793 }
7794 return OK;
7795}
7796
7797/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007798 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007799 * Returns FAIL when out of memory and when key already exists.
7800 */
7801 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007802dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007803{
7804 dictitem_T *item;
7805
7806 item = dictitem_alloc((char_u *)key);
7807 if (item == NULL)
7808 return FAIL;
7809 item->di_tv.v_lock = 0;
7810 item->di_tv.v_type = VAR_LIST;
7811 item->di_tv.vval.v_list = list;
7812 if (dict_add(d, item) == FAIL)
7813 {
7814 dictitem_free(item);
7815 return FAIL;
7816 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007817 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007818 return OK;
7819}
7820
7821/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 * Get the number of items in a Dictionary.
7823 */
7824 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007827 if (d == NULL)
7828 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007829 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007830}
7831
7832/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007833 * Find item "key[len]" in Dictionary "d".
7834 * If "len" is negative use strlen(key).
7835 * Returns NULL when not found.
7836 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007837 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007840#define AKEYLEN 200
7841 char_u buf[AKEYLEN];
7842 char_u *akey;
7843 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007844 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007845
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007846 if (d == NULL)
7847 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007848 if (len < 0)
7849 akey = key;
7850 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007851 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007852 tofree = akey = vim_strnsave(key, len);
7853 if (akey == NULL)
7854 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007855 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007856 else
7857 {
7858 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007859 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007860 akey = buf;
7861 }
7862
Bram Moolenaar33570922005-01-25 22:26:29 +00007863 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007864 vim_free(tofree);
7865 if (HASHITEM_EMPTY(hi))
7866 return NULL;
7867 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868}
7869
7870/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007871 * Get a string item from a dictionary.
7872 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007873 * Returns NULL if the entry doesn't exist or out of memory.
7874 */
7875 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007876get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007877{
7878 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007879 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007880
7881 di = dict_find(d, key, -1);
7882 if (di == NULL)
7883 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007884 s = get_tv_string(&di->di_tv);
7885 if (save && s != NULL)
7886 s = vim_strsave(s);
7887 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007888}
7889
7890/*
7891 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007892 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007893 */
7894 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007896{
7897 dictitem_T *di;
7898
7899 di = dict_find(d, key, -1);
7900 if (di == NULL)
7901 return 0;
7902 return get_tv_number(&di->di_tv);
7903}
7904
7905/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007906 * Return an allocated string with the string representation of a Dictionary.
7907 * May return NULL.
7908 */
7909 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007910dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007911{
7912 garray_T ga;
7913 int first = TRUE;
7914 char_u *tofree;
7915 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007919 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007920
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007921 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922 return NULL;
7923 ga_init2(&ga, (int)sizeof(char), 80);
7924 ga_append(&ga, '{');
7925
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007926 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007927 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007929 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007931 --todo;
7932
7933 if (first)
7934 first = FALSE;
7935 else
7936 ga_concat(&ga, (char_u *)", ");
7937
7938 tofree = string_quote(hi->hi_key, FALSE);
7939 if (tofree != NULL)
7940 {
7941 ga_concat(&ga, tofree);
7942 vim_free(tofree);
7943 }
7944 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007945 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7946 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007947 if (s != NULL)
7948 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007950 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007951 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007952 line_breakcheck();
7953
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007956 if (todo > 0)
7957 {
7958 vim_free(ga.ga_data);
7959 return NULL;
7960 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961
7962 ga_append(&ga, '}');
7963 ga_append(&ga, NUL);
7964 return (char_u *)ga.ga_data;
7965}
7966
7967/*
7968 * Allocate a variable for a Dictionary and fill it from "*arg".
7969 * Return OK or FAIL. Returns NOTDONE for {expr}.
7970 */
7971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007972get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007973{
Bram Moolenaar33570922005-01-25 22:26:29 +00007974 dict_T *d = NULL;
7975 typval_T tvkey;
7976 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007977 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007978 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007980 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981
7982 /*
7983 * First check if it's not a curly-braces thing: {expr}.
7984 * Must do this without evaluating, otherwise a function may be called
7985 * twice. Unfortunately this means we need to call eval1() twice for the
7986 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007989 if (*start != '}')
7990 {
7991 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7992 return FAIL;
7993 if (*start == '}')
7994 return NOTDONE;
7995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996
7997 if (evaluate)
7998 {
7999 d = dict_alloc();
8000 if (d == NULL)
8001 return FAIL;
8002 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008003 tvkey.v_type = VAR_UNKNOWN;
8004 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008005
8006 *arg = skipwhite(*arg + 1);
8007 while (**arg != '}' && **arg != NUL)
8008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008009 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008010 goto failret;
8011 if (**arg != ':')
8012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008013 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008014 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 goto failret;
8016 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008017 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008018 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008019 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008020 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008021 {
8022 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008023 clear_tv(&tvkey);
8024 goto failret;
8025 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008026 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008027
8028 *arg = skipwhite(*arg + 1);
8029 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8030 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008031 if (evaluate)
8032 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 goto failret;
8034 }
8035 if (evaluate)
8036 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008037 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 if (item != NULL)
8039 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008040 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008041 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 clear_tv(&tv);
8043 goto failret;
8044 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008045 item = dictitem_alloc(key);
8046 clear_tv(&tvkey);
8047 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008048 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008050 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008051 if (dict_add(d, item) == FAIL)
8052 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 }
8054 }
8055
8056 if (**arg == '}')
8057 break;
8058 if (**arg != ',')
8059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008060 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 goto failret;
8062 }
8063 *arg = skipwhite(*arg + 1);
8064 }
8065
8066 if (**arg != '}')
8067 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008068 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069failret:
8070 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008071 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008072 return FAIL;
8073 }
8074
8075 *arg = skipwhite(*arg + 1);
8076 if (evaluate)
8077 {
8078 rettv->v_type = VAR_DICT;
8079 rettv->vval.v_dict = d;
8080 ++d->dv_refcount;
8081 }
8082
8083 return OK;
8084}
8085
Bram Moolenaar17a13432016-01-24 14:22:10 +01008086 static char *
8087get_var_special_name(int nr)
8088{
8089 switch (nr)
8090 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008091 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008092 case VVAL_TRUE: return "v:true";
8093 case VVAL_NONE: return "v:none";
8094 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008095 }
8096 EMSG2(_(e_intern2), "get_var_special_name()");
8097 return "42";
8098}
8099
Bram Moolenaar8c711452005-01-14 21:53:12 +00008100/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 * Return a string with the string representation of a variable.
8102 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008103 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008104 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008105 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8106 * "string()", otherwise does not put quotes around strings, as ":echo"
8107 * displays values.
8108 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8109 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008110 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008111 */
8112 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008113echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008114 typval_T *tv,
8115 char_u **tofree,
8116 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008117 int copyID,
8118 int echo_style,
8119 int restore_copyID,
8120 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008122 static int recurse = 0;
8123 char_u *r = NULL;
8124
Bram Moolenaar33570922005-01-25 22:26:29 +00008125 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008126 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008127 if (!did_echo_string_emsg)
8128 {
8129 /* Only give this message once for a recursive call to avoid
8130 * flooding the user with errors. And stop iterating over lists
8131 * and dicts. */
8132 did_echo_string_emsg = TRUE;
8133 EMSG(_("E724: variable nested too deep for displaying"));
8134 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008136 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 }
8138 ++recurse;
8139
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008140 switch (tv->v_type)
8141 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008142 case VAR_STRING:
8143 if (echo_style && !dict_val)
8144 {
8145 *tofree = NULL;
8146 r = get_tv_string_buf(tv, numbuf);
8147 }
8148 else
8149 {
8150 *tofree = string_quote(tv->vval.v_string, FALSE);
8151 r = *tofree;
8152 }
8153 break;
8154
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008155 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008156 if (echo_style)
8157 {
8158 *tofree = NULL;
8159 r = tv->vval.v_string;
8160 }
8161 else
8162 {
8163 *tofree = string_quote(tv->vval.v_string, TRUE);
8164 r = *tofree;
8165 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008167
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008168 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008169 {
8170 partial_T *pt = tv->vval.v_partial;
8171 char_u *fname = string_quote(pt == NULL ? NULL
8172 : pt->pt_name, FALSE);
8173 garray_T ga;
8174 int i;
8175 char_u *tf;
8176
8177 ga_init2(&ga, 1, 100);
8178 ga_concat(&ga, (char_u *)"function(");
8179 if (fname != NULL)
8180 {
8181 ga_concat(&ga, fname);
8182 vim_free(fname);
8183 }
8184 if (pt != NULL && pt->pt_argc > 0)
8185 {
8186 ga_concat(&ga, (char_u *)", [");
8187 for (i = 0; i < pt->pt_argc; ++i)
8188 {
8189 if (i > 0)
8190 ga_concat(&ga, (char_u *)", ");
8191 ga_concat(&ga,
8192 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8193 vim_free(tf);
8194 }
8195 ga_concat(&ga, (char_u *)"]");
8196 }
8197 if (pt != NULL && pt->pt_dict != NULL)
8198 {
8199 typval_T dtv;
8200
8201 ga_concat(&ga, (char_u *)", ");
8202 dtv.v_type = VAR_DICT;
8203 dtv.vval.v_dict = pt->pt_dict;
8204 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8205 vim_free(tf);
8206 }
8207 ga_concat(&ga, (char_u *)")");
8208
8209 *tofree = ga.ga_data;
8210 r = *tofree;
8211 break;
8212 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008213
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008214 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008215 if (tv->vval.v_list == NULL)
8216 {
8217 *tofree = NULL;
8218 r = NULL;
8219 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008220 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8221 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008222 {
8223 *tofree = NULL;
8224 r = (char_u *)"[...]";
8225 }
8226 else
8227 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008228 int old_copyID = tv->vval.v_list->lv_copyID;
8229
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008230 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008231 *tofree = list2string(tv, copyID, restore_copyID);
8232 if (restore_copyID)
8233 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008234 r = *tofree;
8235 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008236 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008237
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008239 if (tv->vval.v_dict == NULL)
8240 {
8241 *tofree = NULL;
8242 r = NULL;
8243 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008244 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8245 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008246 {
8247 *tofree = NULL;
8248 r = (char_u *)"{...}";
8249 }
8250 else
8251 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008252 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008253 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008254 *tofree = dict2string(tv, copyID, restore_copyID);
8255 if (restore_copyID)
8256 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 r = *tofree;
8258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008260
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008261 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008262 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008263 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008264 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008265 *tofree = NULL;
8266 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008267 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008270#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008271 *tofree = NULL;
8272 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8273 r = numbuf;
8274 break;
8275#endif
8276
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008277 case VAR_SPECIAL:
8278 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008279 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008280 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282
Bram Moolenaar8502c702014-06-17 12:51:16 +02008283 if (--recurse == 0)
8284 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008285 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008286}
8287
8288/*
8289 * Return a string with the string representation of a variable.
8290 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8291 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008292 * Does not put quotes around strings, as ":echo" displays values.
8293 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8294 * May return NULL.
8295 */
8296 static char_u *
8297echo_string(
8298 typval_T *tv,
8299 char_u **tofree,
8300 char_u *numbuf,
8301 int copyID)
8302{
8303 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8304}
8305
8306/*
8307 * Return a string with the string representation of a variable.
8308 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8309 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008310 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008311 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008312 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008313 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008314tv2string(
8315 typval_T *tv,
8316 char_u **tofree,
8317 char_u *numbuf,
8318 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008319{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008320 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321}
8322
8323/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008324 * Return string "str" in ' quotes, doubling ' characters.
8325 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008326 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008327 */
8328 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008329string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008330{
Bram Moolenaar33570922005-01-25 22:26:29 +00008331 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332 char_u *p, *r, *s;
8333
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 len = (function ? 13 : 3);
8335 if (str != NULL)
8336 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008337 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008338 for (p = str; *p != NUL; mb_ptr_adv(p))
8339 if (*p == '\'')
8340 ++len;
8341 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342 s = r = alloc(len);
8343 if (r != NULL)
8344 {
8345 if (function)
8346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008347 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008348 r += 10;
8349 }
8350 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008351 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 if (str != NULL)
8353 for (p = str; *p != NUL; )
8354 {
8355 if (*p == '\'')
8356 *r++ = '\'';
8357 MB_COPY_CHAR(p, r);
8358 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008359 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008360 if (function)
8361 *r++ = ')';
8362 *r++ = NUL;
8363 }
8364 return s;
8365}
8366
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008367#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368/*
8369 * Convert the string "text" to a floating point number.
8370 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8371 * this always uses a decimal point.
8372 * Returns the length of the text that was consumed.
8373 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008374 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008375string2float(
8376 char_u *text,
8377 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008378{
8379 char *s = (char *)text;
8380 float_T f;
8381
8382 f = strtod(s, &s);
8383 *value = f;
8384 return (int)((char_u *)s - text);
8385}
8386#endif
8387
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 * Get the value of an environment variable.
8390 * "arg" is pointing to the '$'. It is advanced to after the name.
8391 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008392 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 */
8394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008395get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396{
8397 char_u *string = NULL;
8398 int len;
8399 int cc;
8400 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008401 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
8403 ++*arg;
8404 name = *arg;
8405 len = get_env_len(arg);
8406 if (evaluate)
8407 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008408 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008409 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008410
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008411 cc = name[len];
8412 name[len] = NUL;
8413 /* first try vim_getenv(), fast for normal environment vars */
8414 string = vim_getenv(name, &mustfree);
8415 if (string != NULL && *string != NUL)
8416 {
8417 if (!mustfree)
8418 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008420 else
8421 {
8422 if (mustfree)
8423 vim_free(string);
8424
8425 /* next try expanding things like $VIM and ${HOME} */
8426 string = expand_env_save(name - 1);
8427 if (string != NULL && *string == '$')
8428 {
8429 vim_free(string);
8430 string = NULL;
8431 }
8432 }
8433 name[len] = cc;
8434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008435 rettv->v_type = VAR_STRING;
8436 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 }
8438
8439 return OK;
8440}
8441
8442/*
8443 * Array with names and number of arguments of all internal functions
8444 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8445 */
8446static struct fst
8447{
8448 char *f_name; /* function name */
8449 char f_min_argc; /* minimal number of arguments */
8450 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008451 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008452 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453} functions[] =
8454{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455#ifdef FEAT_FLOAT
8456 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008457 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008458#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008459 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008460 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"append", 2, 2, f_append},
8462 {"argc", 0, 0, f_argc},
8463 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008464 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008465 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008466#ifdef FEAT_FLOAT
8467 {"asin", 1, 1, f_asin}, /* WJMc */
8468#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008469 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008470 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008471 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008472 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008473 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008474 {"assert_notequal", 2, 3, f_assert_notequal},
8475 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008476 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008477#ifdef FEAT_FLOAT
8478 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008479 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008482 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"bufexists", 1, 1, f_bufexists},
8484 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8485 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8486 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8487 {"buflisted", 1, 1, f_buflisted},
8488 {"bufloaded", 1, 1, f_bufloaded},
8489 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008490 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {"bufwinnr", 1, 1, f_bufwinnr},
8492 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008493 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008494 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008495 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008496#ifdef FEAT_FLOAT
8497 {"ceil", 1, 1, f_ceil},
8498#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008500 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008501 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8502 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008503 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008504 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008505 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008506 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008507 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008508 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008509 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008510 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008511 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8512 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008513 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008514 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008515#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008516 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008517 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008519 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008521#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008522 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008523 {"complete_add", 1, 1, f_complete_add},
8524 {"complete_check", 0, 0, f_complete_check},
8525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008528#ifdef FEAT_FLOAT
8529 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008530 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008531#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008532 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008534 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008535 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008536 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008538 {"diff_filler", 1, 1, f_diff_filler},
8539 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008540 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008542 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {"eventhandler", 0, 0, f_eventhandler},
8544 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008545 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008547#ifdef FEAT_FLOAT
8548 {"exp", 1, 1, f_exp},
8549#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008550 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008551 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008552 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8554 {"filereadable", 1, 1, f_filereadable},
8555 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008556 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008557 {"finddir", 1, 3, f_finddir},
8558 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008559#ifdef FEAT_FLOAT
8560 {"float2nr", 1, 1, f_float2nr},
8561 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008562 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008563#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008564 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"fnamemodify", 2, 2, f_fnamemodify},
8566 {"foldclosed", 1, 1, f_foldclosed},
8567 {"foldclosedend", 1, 1, f_foldclosedend},
8568 {"foldlevel", 1, 1, f_foldlevel},
8569 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008570 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008572 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008573 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008574 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008575 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008576 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 {"getchar", 0, 1, f_getchar},
8578 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008579 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {"getcmdline", 0, 0, f_getcmdline},
8581 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008582 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008583 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008584 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008585 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008586 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008587 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {"getfsize", 1, 1, f_getfsize},
8589 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008590 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008591 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008592 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008593 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008594 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008595 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008596 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008597 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008599 {"gettabvar", 2, 3, f_gettabvar},
8600 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 {"getwinposx", 0, 0, f_getwinposx},
8602 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008603 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008604 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008605 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008606 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008608 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008609 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008610 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8612 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8613 {"histadd", 2, 2, f_histadd},
8614 {"histdel", 1, 2, f_histdel},
8615 {"histget", 1, 2, f_histget},
8616 {"histnr", 1, 1, f_histnr},
8617 {"hlID", 1, 1, f_hlID},
8618 {"hlexists", 1, 1, f_hlexists},
8619 {"hostname", 0, 0, f_hostname},
8620 {"iconv", 3, 3, f_iconv},
8621 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008622 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008623 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008625 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"inputrestore", 0, 0, f_inputrestore},
8627 {"inputsave", 0, 0, f_inputsave},
8628 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008629 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008630 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008632 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008633#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8634 {"isnan", 1, 1, f_isnan},
8635#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008636 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008637#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008638 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008639 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008640 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008641 {"job_start", 1, 2, f_job_start},
8642 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008643 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008644#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008645 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008646 {"js_decode", 1, 1, f_js_decode},
8647 {"js_encode", 1, 1, f_js_encode},
8648 {"json_decode", 1, 1, f_json_decode},
8649 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008650 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 {"libcall", 3, 3, f_libcall},
8654 {"libcallnr", 3, 3, f_libcallnr},
8655 {"line", 1, 1, f_line},
8656 {"line2byte", 1, 1, f_line2byte},
8657 {"lispindent", 1, 1, f_lispindent},
8658 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008659#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008660 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008661 {"log10", 1, 1, f_log10},
8662#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008663#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008664 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008665#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008666 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008667 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008668 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008669 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008670 {"matchadd", 2, 5, f_matchadd},
8671 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008672 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008673 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008674 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008675 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008676 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008677 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008678 {"max", 1, 1, f_max},
8679 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008680#ifdef vim_mkdir
8681 {"mkdir", 1, 3, f_mkdir},
8682#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008684#ifdef FEAT_MZSCHEME
8685 {"mzeval", 1, 1, f_mzeval},
8686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008688 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008689 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008690 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008691#ifdef FEAT_PERL
8692 {"perleval", 1, 1, f_perleval},
8693#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#ifdef FEAT_FLOAT
8695 {"pow", 2, 2, f_pow},
8696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008698 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008699 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008700#ifdef FEAT_PYTHON3
8701 {"py3eval", 1, 1, f_py3eval},
8702#endif
8703#ifdef FEAT_PYTHON
8704 {"pyeval", 1, 1, f_pyeval},
8705#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008706 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008707 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008708 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008709#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008710 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008711#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008712 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"remote_expr", 2, 3, f_remote_expr},
8714 {"remote_foreground", 1, 1, f_remote_foreground},
8715 {"remote_peek", 1, 2, f_remote_peek},
8716 {"remote_read", 1, 1, f_remote_read},
8717 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008718 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008720 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008723#ifdef FEAT_FLOAT
8724 {"round", 1, 1, f_round},
8725#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008726 {"screenattr", 2, 2, f_screenattr},
8727 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008728 {"screencol", 0, 0, f_screencol},
8729 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008730 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008731 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008732 {"searchpair", 3, 7, f_searchpair},
8733 {"searchpairpos", 3, 7, f_searchpairpos},
8734 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {"server2client", 2, 2, f_server2client},
8736 {"serverlist", 0, 0, f_serverlist},
8737 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008738 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008740 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008742 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008743 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008744 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008745 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008747 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008748 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008750#ifdef FEAT_CRYPT
8751 {"sha256", 1, 1, f_sha256},
8752#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008753 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008754 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008756#ifdef FEAT_FLOAT
8757 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008758 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008759#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008760 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008761 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008762 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008763 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008764 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008765#ifdef FEAT_FLOAT
8766 {"sqrt", 1, 1, f_sqrt},
8767 {"str2float", 1, 1, f_str2float},
8768#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008769 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008770 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008771 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008772 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773#ifdef HAVE_STRFTIME
8774 {"strftime", 1, 2, f_strftime},
8775#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008776 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008778 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 {"strlen", 1, 1, f_strlen},
8780 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008781 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008783 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008784 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {"substitute", 4, 4, f_substitute},
8786 {"synID", 3, 3, f_synID},
8787 {"synIDattr", 2, 3, f_synIDattr},
8788 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008789 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008790 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008791 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008792 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008793 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008794 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008795 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008796 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008797 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008798#ifdef FEAT_FLOAT
8799 {"tan", 1, 1, f_tan},
8800 {"tanh", 1, 1, f_tanh},
8801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008803 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8804 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008805 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8806#ifdef FEAT_JOB_CHANNEL
8807 {"test_null_channel", 0, 0, f_test_null_channel},
8808#endif
8809 {"test_null_dict", 0, 0, f_test_null_dict},
8810#ifdef FEAT_JOB_CHANNEL
8811 {"test_null_job", 0, 0, f_test_null_job},
8812#endif
8813 {"test_null_list", 0, 0, f_test_null_list},
8814 {"test_null_partial", 0, 0, f_test_null_partial},
8815 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008816#ifdef FEAT_TIMERS
8817 {"timer_start", 2, 3, f_timer_start},
8818 {"timer_stop", 1, 1, f_timer_stop},
8819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {"tolower", 1, 1, f_tolower},
8821 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008822 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008823#ifdef FEAT_FLOAT
8824 {"trunc", 1, 1, f_trunc},
8825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008827 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008828 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008829 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008830 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {"virtcol", 1, 1, f_virtcol},
8832 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008833 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008834 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008835 {"win_getid", 0, 2, f_win_getid},
8836 {"win_gotoid", 1, 1, f_win_gotoid},
8837 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8838 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 {"winbufnr", 1, 1, f_winbufnr},
8840 {"wincol", 0, 0, f_wincol},
8841 {"winheight", 1, 1, f_winheight},
8842 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008843 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008845 {"winrestview", 1, 1, f_winrestview},
8846 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008848 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008849 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008850 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851};
8852
8853#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8854
8855/*
8856 * Function given to ExpandGeneric() to obtain the list of internal
8857 * or user defined function names.
8858 */
8859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008860get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 static int intidx = -1;
8863 char_u *name;
8864
8865 if (idx == 0)
8866 intidx = -1;
8867 if (intidx < 0)
8868 {
8869 name = get_user_func_name(xp, idx);
8870 if (name != NULL)
8871 return name;
8872 }
8873 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8874 {
8875 STRCPY(IObuff, functions[intidx].f_name);
8876 STRCAT(IObuff, "(");
8877 if (functions[intidx].f_max_argc == 0)
8878 STRCAT(IObuff, ")");
8879 return IObuff;
8880 }
8881
8882 return NULL;
8883}
8884
8885/*
8886 * Function given to ExpandGeneric() to obtain the list of internal or
8887 * user defined variable or function names.
8888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008890get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891{
8892 static int intidx = -1;
8893 char_u *name;
8894
8895 if (idx == 0)
8896 intidx = -1;
8897 if (intidx < 0)
8898 {
8899 name = get_function_name(xp, idx);
8900 if (name != NULL)
8901 return name;
8902 }
8903 return get_user_var_name(xp, ++intidx);
8904}
8905
8906#endif /* FEAT_CMDL_COMPL */
8907
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008908#if defined(EBCDIC) || defined(PROTO)
8909/*
8910 * Compare struct fst by function name.
8911 */
8912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008913compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008914{
8915 struct fst *p1 = (struct fst *)s1;
8916 struct fst *p2 = (struct fst *)s2;
8917
8918 return STRCMP(p1->f_name, p2->f_name);
8919}
8920
8921/*
8922 * Sort the function table by function name.
8923 * The sorting of the table above is ASCII dependant.
8924 * On machines using EBCDIC we have to sort it.
8925 */
8926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008927sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008928{
8929 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8930
8931 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8932}
8933#endif
8934
8935
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936/*
8937 * Find internal function in table above.
8938 * Return index, or -1 if not found
8939 */
8940 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008941find_internal_func(
8942 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 int first = 0;
8945 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8946 int cmp;
8947 int x;
8948
8949 /*
8950 * Find the function name in the table. Binary search.
8951 */
8952 while (first <= last)
8953 {
8954 x = first + ((unsigned)(last - first) >> 1);
8955 cmp = STRCMP(name, functions[x].f_name);
8956 if (cmp < 0)
8957 last = x - 1;
8958 else if (cmp > 0)
8959 first = x + 1;
8960 else
8961 return x;
8962 }
8963 return -1;
8964}
8965
8966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8968 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008969 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8970 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 */
8972 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008973deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008974{
Bram Moolenaar33570922005-01-25 22:26:29 +00008975 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 int cc;
8977
Bram Moolenaar65639032016-03-16 21:40:30 +01008978 if (partialp != NULL)
8979 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008980
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008981 cc = name[*lenp];
8982 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008983 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008986 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008988 {
8989 *lenp = 0;
8990 return (char_u *)""; /* just in case */
8991 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008992 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008993 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 }
8995
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008996 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8997 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008998 partial_T *pt = v->di_tv.vval.v_partial;
8999
9000 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009001 {
9002 *lenp = 0;
9003 return (char_u *)""; /* just in case */
9004 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009005 if (partialp != NULL)
9006 *partialp = pt;
9007 *lenp = (int)STRLEN(pt->pt_name);
9008 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009009 }
9010
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009011 return name;
9012}
9013
9014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 * Allocate a variable for the result of a function.
9016 * Return OK or FAIL.
9017 */
9018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009019get_func_tv(
9020 char_u *name, /* name of the function */
9021 int len, /* length of "name" */
9022 typval_T *rettv,
9023 char_u **arg, /* argument, pointing to the '(' */
9024 linenr_T firstline, /* first line of range */
9025 linenr_T lastline, /* last line of range */
9026 int *doesrange, /* return: function handled range */
9027 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009028 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009029 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 char_u *argp;
9032 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009033 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 int argcount = 0; /* number of arguments found */
9035
9036 /*
9037 * Get the arguments.
9038 */
9039 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009040 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 {
9042 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9043 if (*argp == ')' || *argp == ',' || *argp == NUL)
9044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9046 {
9047 ret = FAIL;
9048 break;
9049 }
9050 ++argcount;
9051 if (*argp != ',')
9052 break;
9053 }
9054 if (*argp == ')')
9055 ++argp;
9056 else
9057 ret = FAIL;
9058
9059 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009060 {
9061 int i = 0;
9062
9063 if (get_vim_var_nr(VV_TESTING))
9064 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009065 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009066 * what variables are used on the call stack. */
9067 if (funcargs.ga_itemsize == 0)
9068 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9069 for (i = 0; i < argcount; ++i)
9070 if (ga_grow(&funcargs, 1) == OK)
9071 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9072 &argvars[i];
9073 }
9074
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009076 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009077
9078 funcargs.ga_len -= i;
9079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 {
9082 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009083 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009085 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
9088 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090
9091 *arg = skipwhite(argp);
9092 return ret;
9093}
9094
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009095#define ERROR_UNKNOWN 0
9096#define ERROR_TOOMANY 1
9097#define ERROR_TOOFEW 2
9098#define ERROR_SCRIPT 3
9099#define ERROR_DICT 4
9100#define ERROR_NONE 5
9101#define ERROR_OTHER 6
9102#define FLEN_FIXED 40
9103
9104/*
9105 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9106 * Change <SNR>123_name() to K_SNR 123_name().
9107 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9108 * (slow).
9109 */
9110 static char_u *
9111fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9112{
9113 int llen;
9114 char_u *fname;
9115 int i;
9116
9117 llen = eval_fname_script(name);
9118 if (llen > 0)
9119 {
9120 fname_buf[0] = K_SPECIAL;
9121 fname_buf[1] = KS_EXTRA;
9122 fname_buf[2] = (int)KE_SNR;
9123 i = 3;
9124 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9125 {
9126 if (current_SID <= 0)
9127 *error = ERROR_SCRIPT;
9128 else
9129 {
9130 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9131 i = (int)STRLEN(fname_buf);
9132 }
9133 }
9134 if (i + STRLEN(name + llen) < FLEN_FIXED)
9135 {
9136 STRCPY(fname_buf + i, name + llen);
9137 fname = fname_buf;
9138 }
9139 else
9140 {
9141 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9142 if (fname == NULL)
9143 *error = ERROR_OTHER;
9144 else
9145 {
9146 *tofree = fname;
9147 mch_memmove(fname, fname_buf, (size_t)i);
9148 STRCPY(fname + i, name + llen);
9149 }
9150 }
9151 }
9152 else
9153 fname = name;
9154 return fname;
9155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
9157/*
9158 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009159 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009160 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009163call_func(
9164 char_u *funcname, /* name of the function */
9165 int len, /* length of "name" */
9166 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009167 int argcount_in, /* number of "argvars" */
9168 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009169 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009170 linenr_T firstline, /* first line of range */
9171 linenr_T lastline, /* last line of range */
9172 int *doesrange, /* return: function handled range */
9173 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009174 partial_T *partial, /* optional, can be NULL */
9175 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 int error = ERROR_NONE;
9179 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009182 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009184 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009185 int argcount = argcount_in;
9186 typval_T *argvars = argvars_in;
9187 dict_T *selfdict = selfdict_in;
9188 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9189 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009190
9191 /* Make a copy of the name, if it comes from a funcref variable it could
9192 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009193 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009194 if (name == NULL)
9195 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009197 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198
9199 *doesrange = FALSE;
9200
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009201 if (partial != NULL)
9202 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009203 /* When the function has a partial with a dict and there is a dict
9204 * argument, use the dict argument. That is backwards compatible.
9205 * When the dict was bound explicitly use the one from the partial. */
9206 if (partial->pt_dict != NULL
9207 && (selfdict_in == NULL || !partial->pt_auto))
9208 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009209 if (error == ERROR_NONE && partial->pt_argc > 0)
9210 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009211 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9212 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9213 for (i = 0; i < argcount_in; ++i)
9214 argv[i + argv_clear] = argvars_in[i];
9215 argvars = argv;
9216 argcount = partial->pt_argc + argcount_in;
9217 }
9218 }
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220
9221 /* execute the function if no errors detected and executing */
9222 if (evaluate && error == ERROR_NONE)
9223 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009224 char_u *rfname = fname;
9225
9226 /* Ignore "g:" before a function name. */
9227 if (fname[0] == 'g' && fname[1] == ':')
9228 rfname = fname + 2;
9229
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009230 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9231 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 error = ERROR_UNKNOWN;
9233
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009234 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 {
9236 /*
9237 * User defined function.
9238 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009239 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009240
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009242 /* Trigger FuncUndefined event, may load the function. */
9243 if (fp == NULL
9244 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009245 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009246 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009248 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009249 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 }
9251#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009252 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009253 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009254 {
9255 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009256 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009257 }
9258
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 if (fp != NULL)
9260 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009261 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009263 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009265 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009267 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009268 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 else
9270 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009271 int did_save_redo = FALSE;
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 /*
9274 * Call the user function.
9275 * Save and restore search patterns, script variables and
9276 * redo buffer.
9277 */
9278 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009279#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009280 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009281#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009282 {
9283 saveRedobuff();
9284 did_save_redo = TRUE;
9285 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009286 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009287 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009289 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9290 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9291 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009292 /* Function was unreferenced while being used, free it
9293 * now. */
9294 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009295 if (did_save_redo)
9296 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 restore_search_patterns();
9298 error = ERROR_NONE;
9299 }
9300 }
9301 }
9302 else
9303 {
9304 /*
9305 * Find the function name in the table, call its implementation.
9306 */
9307 i = find_internal_func(fname);
9308 if (i >= 0)
9309 {
9310 if (argcount < functions[i].f_min_argc)
9311 error = ERROR_TOOFEW;
9312 else if (argcount > functions[i].f_max_argc)
9313 error = ERROR_TOOMANY;
9314 else
9315 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009316 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009317 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 error = ERROR_NONE;
9319 }
9320 }
9321 }
9322 /*
9323 * The function call (or "FuncUndefined" autocommand sequence) might
9324 * have been aborted by an error, an interrupt, or an explicitly thrown
9325 * exception that has not been caught so far. This situation can be
9326 * tested for by calling aborting(). For an error in an internal
9327 * function or for the "E132" error in call_user_func(), however, the
9328 * throw point at which the "force_abort" flag (temporarily reset by
9329 * emsg()) is normally updated has not been reached yet. We need to
9330 * update that flag first to make aborting() reliable.
9331 */
9332 update_force_abort();
9333 }
9334 if (error == ERROR_NONE)
9335 ret = OK;
9336
9337 /*
9338 * Report an error unless the argument evaluation or function call has been
9339 * cancelled due to an aborting error, an interrupt, or an exception.
9340 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009341 if (!aborting())
9342 {
9343 switch (error)
9344 {
9345 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009346 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009347 break;
9348 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009349 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009350 break;
9351 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009352 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009353 name);
9354 break;
9355 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009356 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009357 name);
9358 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009359 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009360 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009361 name);
9362 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009363 }
9364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009366 while (argv_clear > 0)
9367 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009368 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009369 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370
9371 return ret;
9372}
9373
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009374/*
9375 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009376 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009377 */
9378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009379emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009380{
9381 char_u *p;
9382
9383 if (*name == K_SPECIAL)
9384 p = concat_str((char_u *)"<SNR>", name + 3);
9385 else
9386 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009387 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009388 if (p != name)
9389 vim_free(p);
9390}
9391
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009392/*
9393 * Return TRUE for a non-zero Number and a non-empty String.
9394 */
9395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009396non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009397{
9398 return ((argvars[0].v_type == VAR_NUMBER
9399 && argvars[0].vval.v_number != 0)
9400 || (argvars[0].v_type == VAR_STRING
9401 && argvars[0].vval.v_string != NULL
9402 && *argvars[0].vval.v_string != NUL));
9403}
9404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405/*********************************************
9406 * Implementation of the built-in functions
9407 */
9408
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009409#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009410static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009411
9412/*
9413 * Get the float value of "argvars[0]" into "f".
9414 * Returns FAIL when the argument is not a Number or Float.
9415 */
9416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009417get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009418{
9419 if (argvars[0].v_type == VAR_FLOAT)
9420 {
9421 *f = argvars[0].vval.v_float;
9422 return OK;
9423 }
9424 if (argvars[0].v_type == VAR_NUMBER)
9425 {
9426 *f = (float_T)argvars[0].vval.v_number;
9427 return OK;
9428 }
9429 EMSG(_("E808: Number or Float required"));
9430 return FAIL;
9431}
9432
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009433/*
9434 * "abs(expr)" function
9435 */
9436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009438{
9439 if (argvars[0].v_type == VAR_FLOAT)
9440 {
9441 rettv->v_type = VAR_FLOAT;
9442 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9443 }
9444 else
9445 {
9446 varnumber_T n;
9447 int error = FALSE;
9448
9449 n = get_tv_number_chk(&argvars[0], &error);
9450 if (error)
9451 rettv->vval.v_number = -1;
9452 else if (n > 0)
9453 rettv->vval.v_number = n;
9454 else
9455 rettv->vval.v_number = -n;
9456 }
9457}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009458
9459/*
9460 * "acos()" function
9461 */
9462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009464{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009465 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009466
9467 rettv->v_type = VAR_FLOAT;
9468 if (get_float_arg(argvars, &f) == OK)
9469 rettv->vval.v_float = acos(f);
9470 else
9471 rettv->vval.v_float = 0.0;
9472}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473#endif
9474
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009476 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 */
9478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009479f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480{
Bram Moolenaar33570922005-01-25 22:26:29 +00009481 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009483 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009484 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009486 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009487 && !tv_check_lock(l->lv_lock,
9488 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009489 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009491 }
9492 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009493 EMSG(_(e_listreq));
9494}
9495
9496/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009497 * "and(expr, expr)" function
9498 */
9499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009500f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009501{
9502 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9503 & get_tv_number_chk(&argvars[1], NULL);
9504}
9505
9506/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009507 * "append(lnum, string/list)" function
9508 */
9509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009511{
9512 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009514 list_T *l = NULL;
9515 listitem_T *li = NULL;
9516 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009517 long added = 0;
9518
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009519 /* When coming here from Insert mode, sync undo, so that this can be
9520 * undone separately from what was previously inserted. */
9521 if (u_sync_once == 2)
9522 {
9523 u_sync_once = 1; /* notify that u_sync() was called */
9524 u_sync(TRUE);
9525 }
9526
Bram Moolenaar0d660222005-01-07 21:51:51 +00009527 lnum = get_tv_lnum(argvars);
9528 if (lnum >= 0
9529 && lnum <= curbuf->b_ml.ml_line_count
9530 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009531 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009532 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009534 l = argvars[1].vval.v_list;
9535 if (l == NULL)
9536 return;
9537 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009538 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539 for (;;)
9540 {
9541 if (l == NULL)
9542 tv = &argvars[1]; /* append a string */
9543 else if (li == NULL)
9544 break; /* end of list */
9545 else
9546 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 line = get_tv_string_chk(tv);
9548 if (line == NULL) /* type error */
9549 {
9550 rettv->vval.v_number = 1; /* Failed */
9551 break;
9552 }
9553 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009554 ++added;
9555 if (l == NULL)
9556 break;
9557 li = li->li_next;
9558 }
9559
9560 appended_lines_mark(lnum, added);
9561 if (curwin->w_cursor.lnum > lnum)
9562 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 else
9565 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566}
9567
9568/*
9569 * "argc()" function
9570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009572f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
9577/*
9578 * "argidx()" function
9579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009581f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
9585
9586/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009587 * "arglistid()" function
9588 */
9589 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009590f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009591{
9592 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009593
9594 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009595 wp = find_tabwin(&argvars[0], &argvars[1]);
9596 if (wp != NULL)
9597 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009598}
9599
9600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 * "argv(nr)" function
9602 */
9603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009604f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605{
9606 int idx;
9607
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009608 if (argvars[0].v_type != VAR_UNKNOWN)
9609 {
9610 idx = get_tv_number_chk(&argvars[0], NULL);
9611 if (idx >= 0 && idx < ARGCOUNT)
9612 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9613 else
9614 rettv->vval.v_string = NULL;
9615 rettv->v_type = VAR_STRING;
9616 }
9617 else if (rettv_list_alloc(rettv) == OK)
9618 for (idx = 0; idx < ARGCOUNT; ++idx)
9619 list_append_string(rettv->vval.v_list,
9620 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621}
9622
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009623typedef enum
9624{
9625 ASSERT_EQUAL,
9626 ASSERT_NOTEQUAL,
9627 ASSERT_MATCH,
9628 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009629 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009630} assert_type_T;
9631
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009632static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009633static 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 +01009634static void assert_error(garray_T *gap);
9635static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009636
9637/*
9638 * Prepare "gap" for an assert error and add the sourcing position.
9639 */
9640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009641prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009642{
9643 char buf[NUMBUFLEN];
9644
9645 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009646 if (sourcing_name != NULL)
9647 {
9648 ga_concat(gap, sourcing_name);
9649 if (sourcing_lnum > 0)
9650 ga_concat(gap, (char_u *)" ");
9651 }
9652 if (sourcing_lnum > 0)
9653 {
9654 sprintf(buf, "line %ld", (long)sourcing_lnum);
9655 ga_concat(gap, (char_u *)buf);
9656 }
9657 if (sourcing_name != NULL || sourcing_lnum > 0)
9658 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009659}
9660
9661/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009662 * Append "str" to "gap", escaping unprintable characters.
9663 * Changes NL to \n, CR to \r, etc.
9664 */
9665 static void
9666ga_concat_esc(garray_T *gap, char_u *str)
9667{
9668 char_u *p;
9669 char_u buf[NUMBUFLEN];
9670
Bram Moolenaarf1551962016-03-15 12:55:58 +01009671 if (str == NULL)
9672 {
9673 ga_concat(gap, (char_u *)"NULL");
9674 return;
9675 }
9676
Bram Moolenaar23689172016-02-15 22:37:37 +01009677 for (p = str; *p != NUL; ++p)
9678 switch (*p)
9679 {
9680 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9681 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9682 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9683 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9684 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9685 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9686 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9687 default:
9688 if (*p < ' ')
9689 {
9690 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9691 ga_concat(gap, buf);
9692 }
9693 else
9694 ga_append(gap, *p);
9695 break;
9696 }
9697}
9698
9699/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009700 * Fill "gap" with information about an assert error.
9701 */
9702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009703fill_assert_error(
9704 garray_T *gap,
9705 typval_T *opt_msg_tv,
9706 char_u *exp_str,
9707 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009708 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009709 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009710{
9711 char_u numbuf[NUMBUFLEN];
9712 char_u *tofree;
9713
9714 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9715 {
9716 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9717 vim_free(tofree);
9718 }
9719 else
9720 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009721 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009722 ga_concat(gap, (char_u *)"Pattern ");
9723 else
9724 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009725 if (exp_str == NULL)
9726 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009727 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009728 vim_free(tofree);
9729 }
9730 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009731 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009732 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009733 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009734 else if (atype == ASSERT_NOTMATCH)
9735 ga_concat(gap, (char_u *)" does match ");
9736 else if (atype == ASSERT_NOTEQUAL)
9737 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009738 else
9739 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009740 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009741 vim_free(tofree);
9742 }
9743}
Bram Moolenaar43345542015-11-29 17:35:35 +01009744
9745/*
9746 * Add an assert error to v:errors.
9747 */
9748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009749assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009750{
9751 struct vimvar *vp = &vimvars[VV_ERRORS];
9752
9753 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9754 /* Make sure v:errors is a list. */
9755 set_vim_var_list(VV_ERRORS, list_alloc());
9756 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9757}
9758
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009759 static void
9760assert_equal_common(typval_T *argvars, assert_type_T atype)
9761{
9762 garray_T ga;
9763
9764 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9765 != (atype == ASSERT_EQUAL))
9766 {
9767 prepare_assert_error(&ga);
9768 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9769 atype);
9770 assert_error(&ga);
9771 ga_clear(&ga);
9772 }
9773}
9774
Bram Moolenaar43345542015-11-29 17:35:35 +01009775/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009777 */
9778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009780{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009781 assert_equal_common(argvars, ASSERT_EQUAL);
9782}
Bram Moolenaar43345542015-11-29 17:35:35 +01009783
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009784/*
9785 * "assert_notequal(expected, actual[, msg])" function
9786 */
9787 static void
9788f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9789{
9790 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009791}
9792
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009793/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009794 * "assert_exception(string[, msg])" function
9795 */
9796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009797f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009798{
9799 garray_T ga;
9800 char *error;
9801
9802 error = (char *)get_tv_string_chk(&argvars[0]);
9803 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9804 {
9805 prepare_assert_error(&ga);
9806 ga_concat(&ga, (char_u *)"v:exception is not set");
9807 assert_error(&ga);
9808 ga_clear(&ga);
9809 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009810 else if (error != NULL
9811 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009812 {
9813 prepare_assert_error(&ga);
9814 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009815 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009816 assert_error(&ga);
9817 ga_clear(&ga);
9818 }
9819}
9820
9821/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009822 * "assert_fails(cmd [, error])" function
9823 */
9824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009825f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009826{
9827 char_u *cmd = get_tv_string_chk(&argvars[0]);
9828 garray_T ga;
9829
9830 called_emsg = FALSE;
9831 suppress_errthrow = TRUE;
9832 emsg_silent = TRUE;
9833 do_cmdline_cmd(cmd);
9834 if (!called_emsg)
9835 {
9836 prepare_assert_error(&ga);
9837 ga_concat(&ga, (char_u *)"command did not fail: ");
9838 ga_concat(&ga, cmd);
9839 assert_error(&ga);
9840 ga_clear(&ga);
9841 }
9842 else if (argvars[1].v_type != VAR_UNKNOWN)
9843 {
9844 char_u buf[NUMBUFLEN];
9845 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9846
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009847 if (error == NULL
9848 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009849 {
9850 prepare_assert_error(&ga);
9851 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009852 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009853 assert_error(&ga);
9854 ga_clear(&ga);
9855 }
9856 }
9857
9858 called_emsg = FALSE;
9859 suppress_errthrow = FALSE;
9860 emsg_silent = FALSE;
9861 emsg_on_display = FALSE;
9862 set_vim_var_string(VV_ERRMSG, NULL, 0);
9863}
9864
9865/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009866 * Common for assert_true() and assert_false().
9867 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009869assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009870{
9871 int error = FALSE;
9872 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009873
Bram Moolenaar37127922016-02-06 20:29:28 +01009874 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009875 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009876 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009877 if (argvars[0].v_type != VAR_NUMBER
9878 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9879 || error)
9880 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009881 prepare_assert_error(&ga);
9882 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009883 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009884 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009885 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009886 ga_clear(&ga);
9887 }
9888}
9889
9890/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009891 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009892 */
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009895{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009896 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009897}
9898
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009899 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009900assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009901{
9902 garray_T ga;
9903 char_u buf1[NUMBUFLEN];
9904 char_u buf2[NUMBUFLEN];
9905 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9906 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9907
Bram Moolenaar72188e92016-03-28 22:48:29 +02009908 if (pat == NULL || text == NULL)
9909 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009910 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009911 {
9912 prepare_assert_error(&ga);
9913 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009914 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009915 assert_error(&ga);
9916 ga_clear(&ga);
9917 }
9918}
9919
9920/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009921 * "assert_match(pattern, actual[, msg])" function
9922 */
9923 static void
9924f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9925{
9926 assert_match_common(argvars, ASSERT_MATCH);
9927}
9928
9929/*
9930 * "assert_notmatch(pattern, actual[, msg])" function
9931 */
9932 static void
9933f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9934{
9935 assert_match_common(argvars, ASSERT_NOTMATCH);
9936}
9937
9938/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009939 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009940 */
9941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009942f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009943{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009944 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009945}
9946
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009947#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009948/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009949 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009950 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009953{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009954 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009955
9956 rettv->v_type = VAR_FLOAT;
9957 if (get_float_arg(argvars, &f) == OK)
9958 rettv->vval.v_float = asin(f);
9959 else
9960 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009961}
9962
9963/*
9964 * "atan()" function
9965 */
9966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009967f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009968{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009969 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009970
9971 rettv->v_type = VAR_FLOAT;
9972 if (get_float_arg(argvars, &f) == OK)
9973 rettv->vval.v_float = atan(f);
9974 else
9975 rettv->vval.v_float = 0.0;
9976}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009977
9978/*
9979 * "atan2()" function
9980 */
9981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009982f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009983{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009984 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009985
9986 rettv->v_type = VAR_FLOAT;
9987 if (get_float_arg(argvars, &fx) == OK
9988 && get_float_arg(&argvars[1], &fy) == OK)
9989 rettv->vval.v_float = atan2(fx, fy);
9990 else
9991 rettv->vval.v_float = 0.0;
9992}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009993#endif
9994
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995/*
9996 * "browse(save, title, initdir, default)" function
9997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009999f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
10001#ifdef FEAT_BROWSE
10002 int save;
10003 char_u *title;
10004 char_u *initdir;
10005 char_u *defname;
10006 char_u buf[NUMBUFLEN];
10007 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 save = get_tv_number_chk(&argvars[0], &error);
10011 title = get_tv_string_chk(&argvars[1]);
10012 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10013 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 if (error || title == NULL || initdir == NULL || defname == NULL)
10016 rettv->vval.v_string = NULL;
10017 else
10018 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010019 do_browse(save ? BROWSE_SAVE : 0,
10020 title, defname, NULL, initdir, NULL, curbuf);
10021#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010022 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010023#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010025}
10026
10027/*
10028 * "browsedir(title, initdir)" function
10029 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010031f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010032{
10033#ifdef FEAT_BROWSE
10034 char_u *title;
10035 char_u *initdir;
10036 char_u buf[NUMBUFLEN];
10037
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010038 title = get_tv_string_chk(&argvars[0]);
10039 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 if (title == NULL || initdir == NULL)
10042 rettv->vval.v_string = NULL;
10043 else
10044 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010045 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050}
10051
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010052static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010053
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054/*
10055 * Find a buffer by number or exact name.
10056 */
10057 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010058find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059{
10060 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010062 if (avar->v_type == VAR_NUMBER)
10063 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010064 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010066 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010067 if (buf == NULL)
10068 {
10069 /* No full path name match, try a match with a URL or a "nofile"
10070 * buffer, these don't use the full path. */
10071 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10072 if (buf->b_fname != NULL
10073 && (path_with_url(buf->b_fname)
10074#ifdef FEAT_QUICKFIX
10075 || bt_nofile(buf)
10076#endif
10077 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010078 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010079 break;
10080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 }
10082 return buf;
10083}
10084
10085/*
10086 * "bufexists(expr)" function
10087 */
10088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010089f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092}
10093
10094/*
10095 * "buflisted(expr)" function
10096 */
10097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010098f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099{
10100 buf_T *buf;
10101
10102 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010103 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104}
10105
10106/*
10107 * "bufloaded(expr)" function
10108 */
10109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010110f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 buf_T *buf;
10113
10114 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010115 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010118 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010119buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 int save_magic;
10122 char_u *save_cpo;
10123 buf_T *buf;
10124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10126 save_magic = p_magic;
10127 p_magic = TRUE;
10128 save_cpo = p_cpo;
10129 p_cpo = (char_u *)"";
10130
10131 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010132 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133
10134 p_magic = save_magic;
10135 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010136 return buf;
10137}
10138
10139/*
10140 * Get buffer by number or pattern.
10141 */
10142 static buf_T *
10143get_buf_tv(typval_T *tv, int curtab_only)
10144{
10145 char_u *name = tv->vval.v_string;
10146 buf_T *buf;
10147
10148 if (tv->v_type == VAR_NUMBER)
10149 return buflist_findnr((int)tv->vval.v_number);
10150 if (tv->v_type != VAR_STRING)
10151 return NULL;
10152 if (name == NULL || *name == NUL)
10153 return curbuf;
10154 if (name[0] == '$' && name[1] == NUL)
10155 return lastbuf;
10156
10157 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158
10159 /* If not found, try expanding the name, like done for bufexists(). */
10160 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
10163 return buf;
10164}
10165
10166/*
10167 * "bufname(expr)" function
10168 */
10169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010170f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171{
10172 buf_T *buf;
10173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010174 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010176 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010177 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 --emsg_off;
10183}
10184
10185/*
10186 * "bufnr(expr)" function
10187 */
10188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010192 int error = FALSE;
10193 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010197 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010198 --emsg_off;
10199
10200 /* If the buffer isn't found and the second argument is not zero create a
10201 * new buffer. */
10202 if (buf == NULL
10203 && argvars[1].v_type != VAR_UNKNOWN
10204 && get_tv_number_chk(&argvars[1], &error) != 0
10205 && !error
10206 && (name = get_tv_string_chk(&argvars[0])) != NULL
10207 && !error)
10208 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10209
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010211 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214}
10215
10216/*
10217 * "bufwinnr(nr)" function
10218 */
10219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010220f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
10222#ifdef FEAT_WINDOWS
10223 win_T *wp;
10224 int winnr = 0;
10225#endif
10226 buf_T *buf;
10227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010230 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#ifdef FEAT_WINDOWS
10232 for (wp = firstwin; wp; wp = wp->w_next)
10233 {
10234 ++winnr;
10235 if (wp->w_buffer == buf)
10236 break;
10237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010238 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241#endif
10242 --emsg_off;
10243}
10244
10245/*
10246 * "byte2line(byte)" function
10247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010249f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250{
10251#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010252 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253#else
10254 long boff = 0;
10255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 (linenr_T)0, &boff);
10262#endif
10263}
10264
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010266byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010267{
10268#ifdef FEAT_MBYTE
10269 char_u *t;
10270#endif
10271 char_u *str;
10272 long idx;
10273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 str = get_tv_string_chk(&argvars[0]);
10275 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010276 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010278 return;
10279
10280#ifdef FEAT_MBYTE
10281 t = str;
10282 for ( ; idx > 0; idx--)
10283 {
10284 if (*t == NUL) /* EOL reached */
10285 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010286 if (enc_utf8 && comp)
10287 t += utf_ptr2len(t);
10288 else
10289 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010290 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010291 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010292#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010293 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010295#endif
10296}
10297
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010298/*
10299 * "byteidx()" function
10300 */
10301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010302f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010303{
10304 byteidx(argvars, rettv, FALSE);
10305}
10306
10307/*
10308 * "byteidxcomp()" function
10309 */
10310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010311f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010312{
10313 byteidx(argvars, rettv, TRUE);
10314}
10315
Bram Moolenaardb913952012-06-29 12:54:53 +020010316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010317func_call(
10318 char_u *name,
10319 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010320 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010321 dict_T *selfdict,
10322 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010323{
10324 listitem_T *item;
10325 typval_T argv[MAX_FUNC_ARGS + 1];
10326 int argc = 0;
10327 int dummy;
10328 int r = 0;
10329
10330 for (item = args->vval.v_list->lv_first; item != NULL;
10331 item = item->li_next)
10332 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010333 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010334 {
10335 EMSG(_("E699: Too many arguments"));
10336 break;
10337 }
10338 /* Make a copy of each argument. This is needed to be able to set
10339 * v_lock to VAR_FIXED in the copy without changing the original list.
10340 */
10341 copy_tv(&item->li_tv, &argv[argc++]);
10342 }
10343
10344 if (item == NULL)
10345 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10346 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010347 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010348
10349 /* Free the arguments. */
10350 while (argc > 0)
10351 clear_tv(&argv[--argc]);
10352
10353 return r;
10354}
10355
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010356/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010357 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010358 */
10359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010360f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010361{
10362 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010363 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010364 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010365
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010366 if (argvars[1].v_type != VAR_LIST)
10367 {
10368 EMSG(_(e_listreq));
10369 return;
10370 }
10371 if (argvars[1].vval.v_list == NULL)
10372 return;
10373
10374 if (argvars[0].v_type == VAR_FUNC)
10375 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010376 else if (argvars[0].v_type == VAR_PARTIAL)
10377 {
10378 partial = argvars[0].vval.v_partial;
10379 func = partial->pt_name;
10380 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010381 else
10382 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010383 if (*func == NUL)
10384 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010385
Bram Moolenaare9a41262005-01-15 22:18:47 +000010386 if (argvars[2].v_type != VAR_UNKNOWN)
10387 {
10388 if (argvars[2].v_type != VAR_DICT)
10389 {
10390 EMSG(_(e_dictreq));
10391 return;
10392 }
10393 selfdict = argvars[2].vval.v_dict;
10394 }
10395
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010396 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010397}
10398
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010399#ifdef FEAT_FLOAT
10400/*
10401 * "ceil({float})" function
10402 */
10403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010404f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010405{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010406 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010407
10408 rettv->v_type = VAR_FLOAT;
10409 if (get_float_arg(argvars, &f) == OK)
10410 rettv->vval.v_float = ceil(f);
10411 else
10412 rettv->vval.v_float = 0.0;
10413}
10414#endif
10415
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010416#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010417/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010418 * "ch_close()" function
10419 */
10420 static void
10421f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10422{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010423 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010424
10425 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010426 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010427 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010428 channel_clear(channel);
10429 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010430}
10431
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010432/*
10433 * "ch_getbufnr()" function
10434 */
10435 static void
10436f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10437{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010438 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010439
10440 rettv->vval.v_number = -1;
10441 if (channel != NULL)
10442 {
10443 char_u *what = get_tv_string(&argvars[1]);
10444 int part;
10445
10446 if (STRCMP(what, "err") == 0)
10447 part = PART_ERR;
10448 else if (STRCMP(what, "out") == 0)
10449 part = PART_OUT;
10450 else if (STRCMP(what, "in") == 0)
10451 part = PART_IN;
10452 else
10453 part = PART_SOCK;
10454 if (channel->ch_part[part].ch_buffer != NULL)
10455 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10456 }
10457}
10458
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010459/*
10460 * "ch_getjob()" function
10461 */
10462 static void
10463f_ch_getjob(typval_T *argvars, typval_T *rettv)
10464{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010465 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010466
10467 if (channel != NULL)
10468 {
10469 rettv->v_type = VAR_JOB;
10470 rettv->vval.v_job = channel->ch_job;
10471 if (channel->ch_job != NULL)
10472 ++channel->ch_job->jv_refcount;
10473 }
10474}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010475
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010476/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010477 * "ch_info()" function
10478 */
10479 static void
10480f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10481{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010482 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010483
10484 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10485 channel_info(channel, rettv->vval.v_dict);
10486}
10487
10488/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010489 * "ch_log()" function
10490 */
10491 static void
10492f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10493{
10494 char_u *msg = get_tv_string(&argvars[0]);
10495 channel_T *channel = NULL;
10496
10497 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010498 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010499
10500 ch_log(channel, (char *)msg);
10501}
10502
10503/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010504 * "ch_logfile()" function
10505 */
10506 static void
10507f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10508{
10509 char_u *fname;
10510 char_u *opt = (char_u *)"";
10511 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010512
10513 fname = get_tv_string(&argvars[0]);
10514 if (argvars[1].v_type == VAR_STRING)
10515 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010516 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010517}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010518
10519/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010520 * "ch_open()" function
10521 */
10522 static void
10523f_ch_open(typval_T *argvars, typval_T *rettv)
10524{
Bram Moolenaar77073442016-02-13 23:23:53 +010010525 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010526 if (check_restricted() || check_secure())
10527 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010528 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010529}
10530
10531/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010532 * "ch_read()" function
10533 */
10534 static void
10535f_ch_read(typval_T *argvars, typval_T *rettv)
10536{
10537 common_channel_read(argvars, rettv, FALSE);
10538}
10539
10540/*
10541 * "ch_readraw()" function
10542 */
10543 static void
10544f_ch_readraw(typval_T *argvars, typval_T *rettv)
10545{
10546 common_channel_read(argvars, rettv, TRUE);
10547}
10548
10549/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010550 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010551 */
10552 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010553f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10554{
10555 ch_expr_common(argvars, rettv, TRUE);
10556}
10557
10558/*
10559 * "ch_sendexpr()" function
10560 */
10561 static void
10562f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10563{
10564 ch_expr_common(argvars, rettv, FALSE);
10565}
10566
10567/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010568 * "ch_evalraw()" function
10569 */
10570 static void
10571f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10572{
10573 ch_raw_common(argvars, rettv, TRUE);
10574}
10575
10576/*
10577 * "ch_sendraw()" function
10578 */
10579 static void
10580f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10581{
10582 ch_raw_common(argvars, rettv, FALSE);
10583}
10584
10585/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010586 * "ch_setoptions()" function
10587 */
10588 static void
10589f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10590{
10591 channel_T *channel;
10592 jobopt_T opt;
10593
Bram Moolenaar437905c2016-04-26 19:01:05 +020010594 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010595 if (channel == NULL)
10596 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010597 clear_job_options(&opt);
10598 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010599 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10600 channel_set_options(channel, &opt);
10601 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010602}
10603
10604/*
10605 * "ch_status()" function
10606 */
10607 static void
10608f_ch_status(typval_T *argvars, typval_T *rettv)
10609{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010610 channel_T *channel;
10611
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010612 /* return an empty string by default */
10613 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010614 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010615
Bram Moolenaar437905c2016-04-26 19:01:05 +020010616 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010617 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010618}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010619#endif
10620
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010621/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010622 * "changenr()" function
10623 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010625f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010626{
10627 rettv->vval.v_number = curbuf->b_u_seq_cur;
10628}
10629
10630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 * "char2nr(string)" function
10632 */
10633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010634f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635{
10636#ifdef FEAT_MBYTE
10637 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010638 {
10639 int utf8 = 0;
10640
10641 if (argvars[1].v_type != VAR_UNKNOWN)
10642 utf8 = get_tv_number_chk(&argvars[1], NULL);
10643
10644 if (utf8)
10645 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10646 else
10647 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 else
10650#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652}
10653
10654/*
10655 * "cindent(lnum)" function
10656 */
10657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010658f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659{
10660#ifdef FEAT_CINDENT
10661 pos_T pos;
10662 linenr_T lnum;
10663
10664 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10667 {
10668 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010669 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 curwin->w_cursor = pos;
10671 }
10672 else
10673#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675}
10676
10677/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010678 * "clearmatches()" function
10679 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010681f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010682{
10683#ifdef FEAT_SEARCH_EXTRA
10684 clear_matches(curwin);
10685#endif
10686}
10687
10688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 * "col(string)" function
10690 */
10691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010692f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
10694 colnr_T col = 0;
10695 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010696 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010698 fp = var2fpos(&argvars[0], FALSE, &fnum);
10699 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 {
10701 if (fp->col == MAXCOL)
10702 {
10703 /* '> can be MAXCOL, get the length of the line then */
10704 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010705 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 else
10707 col = MAXCOL;
10708 }
10709 else
10710 {
10711 col = fp->col + 1;
10712#ifdef FEAT_VIRTUALEDIT
10713 /* col(".") when the cursor is on the NUL at the end of the line
10714 * because of "coladd" can be seen as an extra column. */
10715 if (virtual_active() && fp == &curwin->w_cursor)
10716 {
10717 char_u *p = ml_get_cursor();
10718
10719 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10720 curwin->w_virtcol - curwin->w_cursor.coladd))
10721 {
10722# ifdef FEAT_MBYTE
10723 int l;
10724
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010725 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 col += l;
10727# else
10728 if (*p != NUL && p[1] == NUL)
10729 ++col;
10730# endif
10731 }
10732 }
10733#endif
10734 }
10735 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010736 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737}
10738
Bram Moolenaar572cb562005-08-05 21:35:02 +000010739#if defined(FEAT_INS_EXPAND)
10740/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010741 * "complete()" function
10742 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010744f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010745{
10746 int startcol;
10747
10748 if ((State & INSERT) == 0)
10749 {
10750 EMSG(_("E785: complete() can only be used in Insert mode"));
10751 return;
10752 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010753
10754 /* Check for undo allowed here, because if something was already inserted
10755 * the line was already saved for undo and this check isn't done. */
10756 if (!undo_allowed())
10757 return;
10758
Bram Moolenaarade00832006-03-10 21:46:58 +000010759 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10760 {
10761 EMSG(_(e_invarg));
10762 return;
10763 }
10764
10765 startcol = get_tv_number_chk(&argvars[0], NULL);
10766 if (startcol <= 0)
10767 return;
10768
10769 set_completion(startcol - 1, argvars[1].vval.v_list);
10770}
10771
10772/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010773 * "complete_add()" function
10774 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010777{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010778 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010779}
10780
10781/*
10782 * "complete_check()" function
10783 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010785f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010786{
10787 int saved = RedrawingDisabled;
10788
10789 RedrawingDisabled = 0;
10790 ins_compl_check_keys(0);
10791 rettv->vval.v_number = compl_interrupted;
10792 RedrawingDisabled = saved;
10793}
10794#endif
10795
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796/*
10797 * "confirm(message, buttons[, default [, type]])" function
10798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010800f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801{
10802#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10803 char_u *message;
10804 char_u *buttons = NULL;
10805 char_u buf[NUMBUFLEN];
10806 char_u buf2[NUMBUFLEN];
10807 int def = 1;
10808 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 char_u *typestr;
10810 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010812 message = get_tv_string_chk(&argvars[0]);
10813 if (message == NULL)
10814 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010815 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10818 if (buttons == NULL)
10819 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010823 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010825 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10826 if (typestr == NULL)
10827 error = TRUE;
10828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 switch (TOUPPER_ASC(*typestr))
10831 {
10832 case 'E': type = VIM_ERROR; break;
10833 case 'Q': type = VIM_QUESTION; break;
10834 case 'I': type = VIM_INFO; break;
10835 case 'W': type = VIM_WARNING; break;
10836 case 'G': type = VIM_GENERIC; break;
10837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 }
10839 }
10840 }
10841 }
10842
10843 if (buttons == NULL || *buttons == NUL)
10844 buttons = (char_u *)_("&Ok");
10845
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010846 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010848 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#endif
10850}
10851
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010852/*
10853 * "copy()" function
10854 */
10855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010856f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010857{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010858 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010861#ifdef FEAT_FLOAT
10862/*
10863 * "cos()" function
10864 */
10865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010866f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010867{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010868 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010869
10870 rettv->v_type = VAR_FLOAT;
10871 if (get_float_arg(argvars, &f) == OK)
10872 rettv->vval.v_float = cos(f);
10873 else
10874 rettv->vval.v_float = 0.0;
10875}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010876
10877/*
10878 * "cosh()" function
10879 */
10880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010881f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010882{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010883 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010884
10885 rettv->v_type = VAR_FLOAT;
10886 if (get_float_arg(argvars, &f) == OK)
10887 rettv->vval.v_float = cosh(f);
10888 else
10889 rettv->vval.v_float = 0.0;
10890}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010891#endif
10892
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010894 * "count()" function
10895 */
10896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010897f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010898{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010899 long n = 0;
10900 int ic = FALSE;
10901
Bram Moolenaare9a41262005-01-15 22:18:47 +000010902 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010903 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010904 listitem_T *li;
10905 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010906 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010907
Bram Moolenaare9a41262005-01-15 22:18:47 +000010908 if ((l = argvars[0].vval.v_list) != NULL)
10909 {
10910 li = l->lv_first;
10911 if (argvars[2].v_type != VAR_UNKNOWN)
10912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010913 int error = FALSE;
10914
10915 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010916 if (argvars[3].v_type != VAR_UNKNOWN)
10917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918 idx = get_tv_number_chk(&argvars[3], &error);
10919 if (!error)
10920 {
10921 li = list_find(l, idx);
10922 if (li == NULL)
10923 EMSGN(_(e_listidx), idx);
10924 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010925 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926 if (error)
10927 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010928 }
10929
10930 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010931 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 ++n;
10933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010935 else if (argvars[0].v_type == VAR_DICT)
10936 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 int todo;
10938 dict_T *d;
10939 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010940
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010941 if ((d = argvars[0].vval.v_dict) != NULL)
10942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010943 int error = FALSE;
10944
Bram Moolenaare9a41262005-01-15 22:18:47 +000010945 if (argvars[2].v_type != VAR_UNKNOWN)
10946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010948 if (argvars[3].v_type != VAR_UNKNOWN)
10949 EMSG(_(e_invarg));
10950 }
10951
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010952 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010953 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010954 {
10955 if (!HASHITEM_EMPTY(hi))
10956 {
10957 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010958 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010959 ++n;
10960 }
10961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010962 }
10963 }
10964 else
10965 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010966 rettv->vval.v_number = n;
10967}
10968
10969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10971 *
10972 * Checks the existence of a cscope connection.
10973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010975f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
10977#ifdef FEAT_CSCOPE
10978 int num = 0;
10979 char_u *dbpath = NULL;
10980 char_u *prepend = NULL;
10981 char_u buf[NUMBUFLEN];
10982
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010983 if (argvars[0].v_type != VAR_UNKNOWN
10984 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986 num = (int)get_tv_number(&argvars[0]);
10987 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 }
10991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993#endif
10994}
10995
10996/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010997 * "cursor(lnum, col)" function, or
10998 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011000 * Moves the cursor to the specified line and column.
11001 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011004f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005{
11006 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011007#ifdef FEAT_VIRTUALEDIT
11008 long coladd = 0;
11009#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011010 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011012 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011013 if (argvars[1].v_type == VAR_UNKNOWN)
11014 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011015 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011016 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011017
Bram Moolenaar493c1782014-05-28 14:34:46 +020011018 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011019 {
11020 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011021 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011022 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011023 line = pos.lnum;
11024 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011025#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011026 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011027#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011028 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011029 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011030 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011031 set_curswant = FALSE;
11032 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011033 }
11034 else
11035 {
11036 line = get_tv_lnum(argvars);
11037 col = get_tv_number_chk(&argvars[1], NULL);
11038#ifdef FEAT_VIRTUALEDIT
11039 if (argvars[2].v_type != VAR_UNKNOWN)
11040 coladd = get_tv_number_chk(&argvars[2], NULL);
11041#endif
11042 }
11043 if (line < 0 || col < 0
11044#ifdef FEAT_VIRTUALEDIT
11045 || coladd < 0
11046#endif
11047 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 if (line > 0)
11050 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 if (col > 0)
11052 curwin->w_cursor.col = col - 1;
11053#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011054 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055#endif
11056
11057 /* Make sure the cursor is in a valid position. */
11058 check_cursor();
11059#ifdef FEAT_MBYTE
11060 /* Correct cursor for multi-byte character. */
11061 if (has_mbyte)
11062 mb_adjust_cursor();
11063#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011064
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011065 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011066 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067}
11068
11069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011070 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 */
11072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011073f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011075 int noref = 0;
11076
11077 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011079 if (noref < 0 || noref > 1)
11080 EMSG(_(e_invarg));
11081 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011082 {
11083 current_copyID += COPYID_INC;
11084 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086}
11087
11088/*
11089 * "delete()" function
11090 */
11091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011092f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011094 char_u nbuf[NUMBUFLEN];
11095 char_u *name;
11096 char_u *flags;
11097
11098 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011100 return;
11101
11102 name = get_tv_string(&argvars[0]);
11103 if (name == NULL || *name == NUL)
11104 {
11105 EMSG(_(e_invarg));
11106 return;
11107 }
11108
11109 if (argvars[1].v_type != VAR_UNKNOWN)
11110 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011112 flags = (char_u *)"";
11113
11114 if (*flags == NUL)
11115 /* delete a file */
11116 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11117 else if (STRCMP(flags, "d") == 0)
11118 /* delete an empty directory */
11119 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11120 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011121 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011122 rettv->vval.v_number = delete_recursive(name);
11123 else
11124 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125}
11126
11127/*
11128 * "did_filetype()" function
11129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011131f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132{
11133#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135#endif
11136}
11137
11138/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011139 * "diff_filler()" function
11140 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011142f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011143{
11144#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011145 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011146#endif
11147}
11148
11149/*
11150 * "diff_hlID()" function
11151 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011153f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011154{
11155#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011157 static linenr_T prev_lnum = 0;
11158 static int changedtick = 0;
11159 static int fnum = 0;
11160 static int change_start = 0;
11161 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011162 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011163 int filler_lines;
11164 int col;
11165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 if (lnum < 0) /* ignore type error in {lnum} arg */
11167 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011168 if (lnum != prev_lnum
11169 || changedtick != curbuf->b_changedtick
11170 || fnum != curbuf->b_fnum)
11171 {
11172 /* New line, buffer, change: need to get the values. */
11173 filler_lines = diff_check(curwin, lnum);
11174 if (filler_lines < 0)
11175 {
11176 if (filler_lines == -1)
11177 {
11178 change_start = MAXCOL;
11179 change_end = -1;
11180 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11181 hlID = HLF_ADD; /* added line */
11182 else
11183 hlID = HLF_CHD; /* changed line */
11184 }
11185 else
11186 hlID = HLF_ADD; /* added line */
11187 }
11188 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011189 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011190 prev_lnum = lnum;
11191 changedtick = curbuf->b_changedtick;
11192 fnum = curbuf->b_fnum;
11193 }
11194
11195 if (hlID == HLF_CHD || hlID == HLF_TXD)
11196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011198 if (col >= change_start && col <= change_end)
11199 hlID = HLF_TXD; /* changed text */
11200 else
11201 hlID = HLF_CHD; /* changed line */
11202 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011203 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011204#endif
11205}
11206
11207/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011208 * "empty({expr})" function
11209 */
11210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011211f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011212{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011213 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011214
11215 switch (argvars[0].v_type)
11216 {
11217 case VAR_STRING:
11218 case VAR_FUNC:
11219 n = argvars[0].vval.v_string == NULL
11220 || *argvars[0].vval.v_string == NUL;
11221 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011222 case VAR_PARTIAL:
11223 n = FALSE;
11224 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011225 case VAR_NUMBER:
11226 n = argvars[0].vval.v_number == 0;
11227 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011228 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011229#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011230 n = argvars[0].vval.v_float == 0.0;
11231 break;
11232#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011233 case VAR_LIST:
11234 n = argvars[0].vval.v_list == NULL
11235 || argvars[0].vval.v_list->lv_first == NULL;
11236 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 case VAR_DICT:
11238 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011239 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011240 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011241 case VAR_SPECIAL:
11242 n = argvars[0].vval.v_number != VVAL_TRUE;
11243 break;
11244
Bram Moolenaar835dc632016-02-07 14:27:38 +010011245 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011246#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011247 n = argvars[0].vval.v_job == NULL
11248 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11249 break;
11250#endif
11251 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011252#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011253 n = argvars[0].vval.v_channel == NULL
11254 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011255 break;
11256#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011257 case VAR_UNKNOWN:
11258 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11259 n = TRUE;
11260 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011261 }
11262
11263 rettv->vval.v_number = n;
11264}
11265
11266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267 * "escape({string}, {chars})" function
11268 */
11269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011270f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271{
11272 char_u buf[NUMBUFLEN];
11273
Bram Moolenaar758711c2005-02-02 23:11:38 +000011274 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11275 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011276 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277}
11278
11279/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011280 * "eval()" function
11281 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011283f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011284{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011285 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 s = get_tv_string_chk(&argvars[0]);
11288 if (s != NULL)
11289 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011290
Bram Moolenaar615b9972015-01-14 17:15:05 +010011291 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11293 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011294 if (p != NULL && !aborting())
11295 EMSG2(_(e_invexpr2), p);
11296 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011297 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011298 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011300 else if (*s != NUL)
11301 EMSG(_(e_trailing));
11302}
11303
11304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 * "eventhandler()" function
11306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011308f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011310 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311}
11312
11313/*
11314 * "executable()" function
11315 */
11316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011317f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011319 char_u *name = get_tv_string(&argvars[0]);
11320
11321 /* Check in $PATH and also check directly if there is a directory name. */
11322 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11323 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011324}
11325
11326/*
11327 * "exepath()" function
11328 */
11329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011330f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011331{
11332 char_u *p = NULL;
11333
Bram Moolenaarb5971142015-03-21 17:32:19 +010011334 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011335 rettv->v_type = VAR_STRING;
11336 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337}
11338
11339/*
11340 * "exists()" function
11341 */
11342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011343f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344{
11345 char_u *p;
11346 char_u *name;
11347 int n = FALSE;
11348 int len = 0;
11349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351 if (*p == '$') /* environment variable */
11352 {
11353 /* first try "normal" environment variables (fast) */
11354 if (mch_getenv(p + 1) != NULL)
11355 n = TRUE;
11356 else
11357 {
11358 /* try expanding things like $VIM and ${HOME} */
11359 p = expand_env_save(p);
11360 if (p != NULL && *p != '$')
11361 n = TRUE;
11362 vim_free(p);
11363 }
11364 }
11365 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011367 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011368 if (*skipwhite(p) != NUL)
11369 n = FALSE; /* trailing garbage */
11370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 else if (*p == '*') /* internal or user defined function */
11372 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011373 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 }
11375 else if (*p == ':')
11376 {
11377 n = cmd_exists(p + 1);
11378 }
11379 else if (*p == '#')
11380 {
11381#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011382 if (p[1] == '#')
11383 n = autocmd_supported(p + 2);
11384 else
11385 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386#endif
11387 }
11388 else /* internal variable */
11389 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011390 char_u *tofree;
11391 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011393 /* get_name_len() takes care of expanding curly braces */
11394 name = p;
11395 len = get_name_len(&p, &tofree, TRUE, FALSE);
11396 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011398 if (tofree != NULL)
11399 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011400 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011401 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011403 /* handle d.key, l[idx], f(expr) */
11404 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11405 if (n)
11406 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 }
11408 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011409 if (*p != NUL)
11410 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011412 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 }
11414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011415 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416}
11417
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011418#ifdef FEAT_FLOAT
11419/*
11420 * "exp()" function
11421 */
11422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011423f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011424{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011425 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011426
11427 rettv->v_type = VAR_FLOAT;
11428 if (get_float_arg(argvars, &f) == OK)
11429 rettv->vval.v_float = exp(f);
11430 else
11431 rettv->vval.v_float = 0.0;
11432}
11433#endif
11434
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435/*
11436 * "expand()" function
11437 */
11438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011439f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440{
11441 char_u *s;
11442 int len;
11443 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011444 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011447 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011450 if (argvars[1].v_type != VAR_UNKNOWN
11451 && argvars[2].v_type != VAR_UNKNOWN
11452 && get_tv_number_chk(&argvars[2], &error)
11453 && !error)
11454 {
11455 rettv->v_type = VAR_LIST;
11456 rettv->vval.v_list = NULL;
11457 }
11458
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 if (*s == '%' || *s == '#' || *s == '<')
11461 {
11462 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011463 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011465 if (rettv->v_type == VAR_LIST)
11466 {
11467 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11468 list_append_string(rettv->vval.v_list, result, -1);
11469 else
11470 vim_free(result);
11471 }
11472 else
11473 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 }
11475 else
11476 {
11477 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011478 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 if (argvars[1].v_type != VAR_UNKNOWN
11480 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011481 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482 if (!error)
11483 {
11484 ExpandInit(&xpc);
11485 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011486 if (p_wic)
11487 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011488 if (rettv->v_type == VAR_STRING)
11489 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11490 options, WILD_ALL);
11491 else if (rettv_list_alloc(rettv) != FAIL)
11492 {
11493 int i;
11494
11495 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11496 for (i = 0; i < xpc.xp_numfiles; i++)
11497 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11498 ExpandCleanup(&xpc);
11499 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 }
11501 else
11502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 }
11504}
11505
11506/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011507 * Go over all entries in "d2" and add them to "d1".
11508 * When "action" is "error" then a duplicate key is an error.
11509 * When "action" is "force" then a duplicate key is overwritten.
11510 * Otherwise duplicate keys are ignored ("action" is "keep").
11511 */
11512 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011514{
11515 dictitem_T *di1;
11516 hashitem_T *hi2;
11517 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011518 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011519
11520 todo = (int)d2->dv_hashtab.ht_used;
11521 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11522 {
11523 if (!HASHITEM_EMPTY(hi2))
11524 {
11525 --todo;
11526 di1 = dict_find(d1, hi2->hi_key, -1);
11527 if (d1->dv_scope != 0)
11528 {
11529 /* Disallow replacing a builtin function in l: and g:.
11530 * Check the key to be valid when adding to any
11531 * scope. */
11532 if (d1->dv_scope == VAR_DEF_SCOPE
11533 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11534 && var_check_func_name(hi2->hi_key,
11535 di1 == NULL))
11536 break;
11537 if (!valid_varname(hi2->hi_key))
11538 break;
11539 }
11540 if (di1 == NULL)
11541 {
11542 di1 = dictitem_copy(HI2DI(hi2));
11543 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11544 dictitem_free(di1);
11545 }
11546 else if (*action == 'e')
11547 {
11548 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11549 break;
11550 }
11551 else if (*action == 'f' && HI2DI(hi2) != di1)
11552 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011553 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11554 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011555 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011556 clear_tv(&di1->di_tv);
11557 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11558 }
11559 }
11560 }
11561}
11562
11563/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011565 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011566 */
11567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011568f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011569{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011570 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011571
Bram Moolenaare9a41262005-01-15 22:18:47 +000011572 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011573 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011574 list_T *l1, *l2;
11575 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011578
Bram Moolenaare9a41262005-01-15 22:18:47 +000011579 l1 = argvars[0].vval.v_list;
11580 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011581 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011582 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011583 {
11584 if (argvars[2].v_type != VAR_UNKNOWN)
11585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011586 before = get_tv_number_chk(&argvars[2], &error);
11587 if (error)
11588 return; /* type error; errmsg already given */
11589
Bram Moolenaar758711c2005-02-02 23:11:38 +000011590 if (before == l1->lv_len)
11591 item = NULL;
11592 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011594 item = list_find(l1, before);
11595 if (item == NULL)
11596 {
11597 EMSGN(_(e_listidx), before);
11598 return;
11599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 }
11601 }
11602 else
11603 item = NULL;
11604 list_extend(l1, l2, item);
11605
Bram Moolenaare9a41262005-01-15 22:18:47 +000011606 copy_tv(&argvars[0], rettv);
11607 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011608 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011609 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11610 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011611 dict_T *d1, *d2;
11612 char_u *action;
11613 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011614
11615 d1 = argvars[0].vval.v_dict;
11616 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011617 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011618 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011619 {
11620 /* Check the third argument. */
11621 if (argvars[2].v_type != VAR_UNKNOWN)
11622 {
11623 static char *(av[]) = {"keep", "force", "error"};
11624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 action = get_tv_string_chk(&argvars[2]);
11626 if (action == NULL)
11627 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 for (i = 0; i < 3; ++i)
11629 if (STRCMP(action, av[i]) == 0)
11630 break;
11631 if (i == 3)
11632 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011633 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011634 return;
11635 }
11636 }
11637 else
11638 action = (char_u *)"force";
11639
Bram Moolenaara9922d62013-05-30 13:01:18 +020011640 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011641
Bram Moolenaare9a41262005-01-15 22:18:47 +000011642 copy_tv(&argvars[0], rettv);
11643 }
11644 }
11645 else
11646 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011647}
11648
11649/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011650 * "feedkeys()" function
11651 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011653f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011654{
11655 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011656 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011657 char_u *keys, *flags;
11658 char_u nbuf[NUMBUFLEN];
11659 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011660 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011661 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011662 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011663
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011664 /* This is not allowed in the sandbox. If the commands would still be
11665 * executed in the sandbox it would be OK, but it probably happens later,
11666 * when "sandbox" is no longer set. */
11667 if (check_secure())
11668 return;
11669
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011670 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011671
11672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011673 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011674 flags = get_tv_string_buf(&argvars[1], nbuf);
11675 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011676 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011677 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011678 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011679 case 'n': remap = FALSE; break;
11680 case 'm': remap = TRUE; break;
11681 case 't': typed = TRUE; break;
11682 case 'i': insert = TRUE; break;
11683 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011684 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011685 }
11686 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011687 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011688
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011689 if (*keys != NUL || execute)
11690 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011691 /* Need to escape K_SPECIAL and CSI before putting the string in the
11692 * typeahead buffer. */
11693 keys_esc = vim_strsave_escape_csi(keys);
11694 if (keys_esc != NULL)
11695 {
11696 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011697 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011698 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011699 if (vgetc_busy)
11700 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011701 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011702 {
11703 int save_msg_scroll = msg_scroll;
11704
11705 /* Avoid a 1 second delay when the keys start Insert mode. */
11706 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011707
Bram Moolenaar245c4102016-04-20 17:37:41 +020011708 if (!dangerous)
11709 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011710 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011711 if (!dangerous)
11712 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011713 msg_scroll |= save_msg_scroll;
11714 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011715 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011716 }
11717}
11718
11719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720 * "filereadable()" function
11721 */
11722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011723f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011725 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 char_u *p;
11727 int n;
11728
Bram Moolenaarc236c162008-07-13 17:41:49 +000011729#ifndef O_NONBLOCK
11730# define O_NONBLOCK 0
11731#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011732 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011733 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11734 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 {
11736 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011737 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 }
11739 else
11740 n = FALSE;
11741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011742 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743}
11744
11745/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011746 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 * rights to write into.
11748 */
11749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011750f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011752 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753}
11754
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011755 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011756findfilendir(
11757 typval_T *argvars UNUSED,
11758 typval_T *rettv,
11759 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011760{
11761#ifdef FEAT_SEARCHPATH
11762 char_u *fname;
11763 char_u *fresult = NULL;
11764 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11765 char_u *p;
11766 char_u pathbuf[NUMBUFLEN];
11767 int count = 1;
11768 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011769 int error = FALSE;
11770#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011771
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011772 rettv->vval.v_string = NULL;
11773 rettv->v_type = VAR_STRING;
11774
11775#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011777
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011778 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11781 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011782 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 else
11784 {
11785 if (*p != NUL)
11786 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011789 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011791 }
11792
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011793 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11794 error = TRUE;
11795
11796 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 do
11799 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011800 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011801 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 fresult = find_file_in_path_option(first ? fname : NULL,
11803 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011804 0, first, path,
11805 find_what,
11806 curbuf->b_ffname,
11807 find_what == FINDFILE_DIR
11808 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011810
11811 if (fresult != NULL && rettv->v_type == VAR_LIST)
11812 list_append_string(rettv->vval.v_list, fresult, -1);
11813
11814 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011816
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011817 if (rettv->v_type == VAR_STRING)
11818 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011819#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011820}
11821
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011822static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11823static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011824
11825/*
11826 * Implementation of map() and filter().
11827 */
11828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011829filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011830{
11831 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011832 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011833 listitem_T *li, *nli;
11834 list_T *l = NULL;
11835 dictitem_T *di;
11836 hashtab_T *ht;
11837 hashitem_T *hi;
11838 dict_T *d = NULL;
11839 typval_T save_val;
11840 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011841 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011842 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011843 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011844 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011845 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011846 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011847 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011848
Bram Moolenaare9a41262005-01-15 22:18:47 +000011849 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011850 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011851 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011852 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 return;
11854 }
11855 else if (argvars[0].v_type == VAR_DICT)
11856 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011857 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011858 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011859 return;
11860 }
11861 else
11862 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011863 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864 return;
11865 }
11866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011867 expr = get_tv_string_buf_chk(&argvars[1], buf);
11868 /* On type errors, the preceding call has already displayed an error
11869 * message. Avoid a misleading error message for an empty string that
11870 * was not passed as argument. */
11871 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873 prepare_vimvar(VV_VAL, &save_val);
11874 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011875
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011876 /* We reset "did_emsg" to be able to detect whether an error
11877 * occurred during evaluation of the expression. */
11878 save_did_emsg = did_emsg;
11879 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011880
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011881 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011882 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011884 vimvars[VV_KEY].vv_type = VAR_STRING;
11885
11886 ht = &d->dv_hashtab;
11887 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011888 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 if (!HASHITEM_EMPTY(hi))
11892 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011893 int r;
11894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011895 --todo;
11896 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011897 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011898 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11899 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 break;
11901 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011902 r = filter_map_one(&di->di_tv, expr, map, &rem);
11903 clear_tv(&vimvars[VV_KEY].vv_tv);
11904 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 break;
11906 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011907 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011908 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11909 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011910 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011911 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011912 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913 }
11914 }
11915 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 }
11917 else
11918 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011919 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011921 for (li = l->lv_first; li != NULL; li = nli)
11922 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011923 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011924 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011925 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011926 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011927 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011928 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011929 break;
11930 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011931 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011932 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011933 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011935
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011936 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011938
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011939 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011940 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011941
11942 copy_tv(&argvars[0], rettv);
11943}
11944
11945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011946filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011947{
Bram Moolenaar33570922005-01-25 22:26:29 +000011948 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011949 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011950 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011953 s = expr;
11954 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011955 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011956 if (*s != NUL) /* check for trailing chars after expr */
11957 {
11958 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011959 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011960 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011961 }
11962 if (map)
11963 {
11964 /* map(): replace the list item value */
11965 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011966 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011967 *tv = rettv;
11968 }
11969 else
11970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 int error = FALSE;
11972
Bram Moolenaare9a41262005-01-15 22:18:47 +000011973 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011975 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 /* On type error, nothing has been removed; return FAIL to stop the
11977 * loop. The error message was given by get_tv_number_chk(). */
11978 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011979 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011980 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011981 retval = OK;
11982theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011983 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011984 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011985}
11986
11987/*
11988 * "filter()" function
11989 */
11990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011991f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011992{
11993 filter_map(argvars, rettv, FALSE);
11994}
11995
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011996/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011997 * "finddir({fname}[, {path}[, {count}]])" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012001{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012002 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012003}
12004
12005/*
12006 * "findfile({fname}[, {path}[, {count}]])" function
12007 */
12008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012009f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012011 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012012}
12013
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012014#ifdef FEAT_FLOAT
12015/*
12016 * "float2nr({float})" function
12017 */
12018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012019f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012020{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012021 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012022
12023 if (get_float_arg(argvars, &f) == OK)
12024 {
12025 if (f < -0x7fffffff)
12026 rettv->vval.v_number = -0x7fffffff;
12027 else if (f > 0x7fffffff)
12028 rettv->vval.v_number = 0x7fffffff;
12029 else
12030 rettv->vval.v_number = (varnumber_T)f;
12031 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012032}
12033
12034/*
12035 * "floor({float})" function
12036 */
12037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012038f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012039{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012040 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012041
12042 rettv->v_type = VAR_FLOAT;
12043 if (get_float_arg(argvars, &f) == OK)
12044 rettv->vval.v_float = floor(f);
12045 else
12046 rettv->vval.v_float = 0.0;
12047}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012048
12049/*
12050 * "fmod()" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012054{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012055 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012056
12057 rettv->v_type = VAR_FLOAT;
12058 if (get_float_arg(argvars, &fx) == OK
12059 && get_float_arg(&argvars[1], &fy) == OK)
12060 rettv->vval.v_float = fmod(fx, fy);
12061 else
12062 rettv->vval.v_float = 0.0;
12063}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012064#endif
12065
Bram Moolenaar0d660222005-01-07 21:51:51 +000012066/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012067 * "fnameescape({string})" function
12068 */
12069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012070f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012071{
12072 rettv->vval.v_string = vim_strsave_fnameescape(
12073 get_tv_string(&argvars[0]), FALSE);
12074 rettv->v_type = VAR_STRING;
12075}
12076
12077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 * "fnamemodify({fname}, {mods})" function
12079 */
12080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012081f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082{
12083 char_u *fname;
12084 char_u *mods;
12085 int usedlen = 0;
12086 int len;
12087 char_u *fbuf = NULL;
12088 char_u buf[NUMBUFLEN];
12089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012090 fname = get_tv_string_chk(&argvars[0]);
12091 mods = get_tv_string_buf_chk(&argvars[1], buf);
12092 if (fname == NULL || mods == NULL)
12093 fname = NULL;
12094 else
12095 {
12096 len = (int)STRLEN(fname);
12097 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012102 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012105 vim_free(fbuf);
12106}
12107
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012108static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109
12110/*
12111 * "foldclosed()" function
12112 */
12113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012114foldclosed_both(
12115 typval_T *argvars UNUSED,
12116 typval_T *rettv,
12117 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118{
12119#ifdef FEAT_FOLDING
12120 linenr_T lnum;
12121 linenr_T first, last;
12122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12125 {
12126 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12127 {
12128 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012129 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 return;
12133 }
12134 }
12135#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137}
12138
12139/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 * "foldclosed()" function
12141 */
12142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012143f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144{
12145 foldclosed_both(argvars, rettv, FALSE);
12146}
12147
12148/*
12149 * "foldclosedend()" function
12150 */
12151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012152f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012153{
12154 foldclosed_both(argvars, rettv, TRUE);
12155}
12156
12157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158 * "foldlevel()" function
12159 */
12160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012161f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162{
12163#ifdef FEAT_FOLDING
12164 linenr_T lnum;
12165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170}
12171
12172/*
12173 * "foldtext()" function
12174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012176f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177{
12178#ifdef FEAT_FOLDING
12179 linenr_T lnum;
12180 char_u *s;
12181 char_u *r;
12182 int len;
12183 char *txt;
12184#endif
12185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 rettv->v_type = VAR_STRING;
12187 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012189 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12190 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12191 <= curbuf->b_ml.ml_line_count
12192 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 {
12194 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012195 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12196 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 {
12198 if (!linewhite(lnum))
12199 break;
12200 ++lnum;
12201 }
12202
12203 /* Find interesting text in this line. */
12204 s = skipwhite(ml_get(lnum));
12205 /* skip C comment-start */
12206 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012207 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012209 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012210 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012211 {
12212 s = skipwhite(ml_get(lnum + 1));
12213 if (*s == '*')
12214 s = skipwhite(s + 1);
12215 }
12216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012217 txt = _("+-%s%3ld lines: ");
12218 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012219 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 + 20 /* for %3ld */
12221 + STRLEN(s))); /* concatenated */
12222 if (r != NULL)
12223 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012224 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12225 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12226 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 len = (int)STRLEN(r);
12228 STRCAT(r, s);
12229 /* remove 'foldmarker' and 'commentstring' */
12230 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232 }
12233 }
12234#endif
12235}
12236
12237/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012238 * "foldtextresult(lnum)" function
12239 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012241f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012242{
12243#ifdef FEAT_FOLDING
12244 linenr_T lnum;
12245 char_u *text;
12246 char_u buf[51];
12247 foldinfo_T foldinfo;
12248 int fold_count;
12249#endif
12250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012251 rettv->v_type = VAR_STRING;
12252 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012253#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012255 /* treat illegal types and illegal string values for {lnum} the same */
12256 if (lnum < 0)
12257 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012258 fold_count = foldedCount(curwin, lnum, &foldinfo);
12259 if (fold_count > 0)
12260 {
12261 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12262 &foldinfo, buf);
12263 if (text == buf)
12264 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012266 }
12267#endif
12268}
12269
12270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 * "foreground()" function
12272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012274f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012276#ifdef FEAT_GUI
12277 if (gui.in_use)
12278 gui_mch_set_foreground();
12279#else
12280# ifdef WIN32
12281 win32_set_foreground();
12282# endif
12283#endif
12284}
12285
12286/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012287 * "function()" function
12288 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012290f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012291{
12292 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012293 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012294 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012295 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012296
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012297 if (argvars[0].v_type == VAR_FUNC)
12298 {
12299 /* function(MyFunc, [arg], dict) */
12300 s = argvars[0].vval.v_string;
12301 }
12302 else if (argvars[0].v_type == VAR_PARTIAL
12303 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012304 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012305 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012306 arg_pt = argvars[0].vval.v_partial;
12307 s = arg_pt->pt_name;
12308 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012309 else
12310 {
12311 /* function('MyFunc', [arg], dict) */
12312 s = get_tv_string(&argvars[0]);
12313 use_string = TRUE;
12314 }
12315
12316 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012317 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012318 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012319 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12320 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012321 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012322 else
12323 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012324 int dict_idx = 0;
12325 int arg_idx = 0;
12326 list_T *list = NULL;
12327
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012328 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012329 {
12330 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012331 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012332
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012333 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12334 * also be called from another script. Using trans_function_name()
12335 * would also work, but some plugins depend on the name being
12336 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012337 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012338 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12339 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012340 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012341 STRCPY(name, sid_buf);
12342 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012343 }
12344 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012345 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012346 name = vim_strsave(s);
12347
12348 if (argvars[1].v_type != VAR_UNKNOWN)
12349 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012350 if (argvars[2].v_type != VAR_UNKNOWN)
12351 {
12352 /* function(name, [args], dict) */
12353 arg_idx = 1;
12354 dict_idx = 2;
12355 }
12356 else if (argvars[1].v_type == VAR_DICT)
12357 /* function(name, dict) */
12358 dict_idx = 1;
12359 else
12360 /* function(name, [args]) */
12361 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012362 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012363 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012364 if (argvars[dict_idx].v_type != VAR_DICT)
12365 {
12366 EMSG(_("E922: expected a dict"));
12367 vim_free(name);
12368 return;
12369 }
12370 if (argvars[dict_idx].vval.v_dict == NULL)
12371 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012372 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012373 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012374 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012375 if (argvars[arg_idx].v_type != VAR_LIST)
12376 {
12377 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12378 vim_free(name);
12379 return;
12380 }
12381 list = argvars[arg_idx].vval.v_list;
12382 if (list == NULL || list->lv_len == 0)
12383 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012384 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012385 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012386 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012387 {
12388 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012389
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012390 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012391 if (pt == NULL)
12392 vim_free(name);
12393 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012394 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012395 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012396 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012397 listitem_T *li;
12398 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012399 int arg_len = 0;
12400 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012401
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012402 if (arg_pt != NULL)
12403 arg_len = arg_pt->pt_argc;
12404 if (list != NULL)
12405 lv_len = list->lv_len;
12406 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012407 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012408 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012409 if (pt->pt_argv == NULL)
12410 {
12411 vim_free(pt);
12412 vim_free(name);
12413 return;
12414 }
12415 else
12416 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012417 for (i = 0; i < arg_len; i++)
12418 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12419 if (lv_len > 0)
12420 for (li = list->lv_first; li != NULL;
12421 li = li->li_next)
12422 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012423 }
12424 }
12425
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012426 /* For "function(dict.func, [], dict)" and "func" is a partial
12427 * use "dict". That is backwards compatible. */
12428 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012429 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012430 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012431 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12432 ++pt->pt_dict->dv_refcount;
12433 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012434 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012435 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012436 /* If the dict was bound automatically the result is also
12437 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012438 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012439 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012440 if (pt->pt_dict != NULL)
12441 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012442 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012443
12444 pt->pt_refcount = 1;
12445 pt->pt_name = name;
12446 func_ref(pt->pt_name);
12447 }
12448 rettv->v_type = VAR_PARTIAL;
12449 rettv->vval.v_partial = pt;
12450 }
12451 else
12452 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012453 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012454 rettv->v_type = VAR_FUNC;
12455 rettv->vval.v_string = name;
12456 func_ref(name);
12457 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012458 }
12459}
12460
12461/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012462 * "garbagecollect()" function
12463 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012465f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012466{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012467 /* This is postponed until we are back at the toplevel, because we may be
12468 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12469 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012470
12471 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12472 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012473}
12474
12475/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476 * "get()" function
12477 */
12478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012479f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480{
Bram Moolenaar33570922005-01-25 22:26:29 +000012481 listitem_T *li;
12482 list_T *l;
12483 dictitem_T *di;
12484 dict_T *d;
12485 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486
Bram Moolenaare9a41262005-01-15 22:18:47 +000012487 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012488 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012489 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012491 int error = FALSE;
12492
12493 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12494 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012495 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012498 else if (argvars[0].v_type == VAR_DICT)
12499 {
12500 if ((d = argvars[0].vval.v_dict) != NULL)
12501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012502 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012503 if (di != NULL)
12504 tv = &di->di_tv;
12505 }
12506 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012507 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012508 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012509 partial_T *pt;
12510 partial_T fref_pt;
12511
12512 if (argvars[0].v_type == VAR_PARTIAL)
12513 pt = argvars[0].vval.v_partial;
12514 else
12515 {
12516 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12517 fref_pt.pt_name = argvars[0].vval.v_string;
12518 pt = &fref_pt;
12519 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012520
12521 if (pt != NULL)
12522 {
12523 char_u *what = get_tv_string(&argvars[1]);
12524
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012525 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012526 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012527 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012528 if (pt->pt_name == NULL)
12529 rettv->vval.v_string = NULL;
12530 else
12531 rettv->vval.v_string = vim_strsave(pt->pt_name);
12532 }
12533 else if (STRCMP(what, "dict") == 0)
12534 {
12535 rettv->v_type = VAR_DICT;
12536 rettv->vval.v_dict = pt->pt_dict;
12537 if (pt->pt_dict != NULL)
12538 ++pt->pt_dict->dv_refcount;
12539 }
12540 else if (STRCMP(what, "args") == 0)
12541 {
12542 rettv->v_type = VAR_LIST;
12543 if (rettv_list_alloc(rettv) == OK)
12544 {
12545 int i;
12546
12547 for (i = 0; i < pt->pt_argc; ++i)
12548 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12549 }
12550 }
12551 else
12552 EMSG2(_(e_invarg2), what);
12553 return;
12554 }
12555 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012556 else
12557 EMSG2(_(e_listdictarg), "get()");
12558
12559 if (tv == NULL)
12560 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012561 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012562 copy_tv(&argvars[2], rettv);
12563 }
12564 else
12565 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566}
12567
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012568static 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 +000012569
12570/*
12571 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012572 * Return a range (from start to end) of lines in rettv from the specified
12573 * buffer.
12574 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012575 */
12576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012577get_buffer_lines(
12578 buf_T *buf,
12579 linenr_T start,
12580 linenr_T end,
12581 int retlist,
12582 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012583{
12584 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012585
Bram Moolenaar959a1432013-12-14 12:17:38 +010012586 rettv->v_type = VAR_STRING;
12587 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012588 if (retlist && rettv_list_alloc(rettv) == FAIL)
12589 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012590
12591 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12592 return;
12593
12594 if (!retlist)
12595 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012596 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12597 p = ml_get_buf(buf, start, FALSE);
12598 else
12599 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012600 rettv->vval.v_string = vim_strsave(p);
12601 }
12602 else
12603 {
12604 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012605 return;
12606
12607 if (start < 1)
12608 start = 1;
12609 if (end > buf->b_ml.ml_line_count)
12610 end = buf->b_ml.ml_line_count;
12611 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012612 if (list_append_string(rettv->vval.v_list,
12613 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012614 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012615 }
12616}
12617
12618/*
12619 * "getbufline()" function
12620 */
12621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012622f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012623{
12624 linenr_T lnum;
12625 linenr_T end;
12626 buf_T *buf;
12627
12628 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12629 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012630 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012631 --emsg_off;
12632
Bram Moolenaar661b1822005-07-28 22:36:45 +000012633 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012634 if (argvars[2].v_type == VAR_UNKNOWN)
12635 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012636 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012637 end = get_tv_lnum_buf(&argvars[2], buf);
12638
Bram Moolenaar342337a2005-07-21 21:11:17 +000012639 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012640}
12641
Bram Moolenaar0d660222005-01-07 21:51:51 +000012642/*
12643 * "getbufvar()" function
12644 */
12645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012646f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012647{
12648 buf_T *buf;
12649 buf_T *save_curbuf;
12650 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012651 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012652 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012654 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12655 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012656 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012657 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012659 rettv->v_type = VAR_STRING;
12660 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661
12662 if (buf != NULL && varname != NULL)
12663 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012664 /* set curbuf to be our buf, temporarily */
12665 save_curbuf = curbuf;
12666 curbuf = buf;
12667
Bram Moolenaar0d660222005-01-07 21:51:51 +000012668 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012669 {
12670 if (get_option_tv(&varname, rettv, TRUE) == OK)
12671 done = TRUE;
12672 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012673 else if (STRCMP(varname, "changedtick") == 0)
12674 {
12675 rettv->v_type = VAR_NUMBER;
12676 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012677 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679 else
12680 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012681 /* Look up the variable. */
12682 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12683 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12684 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012685 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012686 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012687 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012688 done = TRUE;
12689 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012690 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012691
12692 /* restore previous notion of curbuf */
12693 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012694 }
12695
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012696 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12697 /* use the default value */
12698 copy_tv(&argvars[2], rettv);
12699
Bram Moolenaar0d660222005-01-07 21:51:51 +000012700 --emsg_off;
12701}
12702
12703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 * "getchar()" function
12705 */
12706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012707f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708{
12709 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012710 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012712 /* Position the cursor. Needed after a message that ends in a space. */
12713 windgoto(msg_row, msg_col);
12714
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 ++no_mapping;
12716 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012717 for (;;)
12718 {
12719 if (argvars[0].v_type == VAR_UNKNOWN)
12720 /* getchar(): blocking wait. */
12721 n = safe_vgetc();
12722 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12723 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012724 n = vpeekc_any();
12725 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012726 /* illegal argument or getchar(0) and no char avail: return zero */
12727 n = 0;
12728 else
12729 /* getchar(0) and char avail: return char */
12730 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012731
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012732 if (n == K_IGNORE)
12733 continue;
12734 break;
12735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 --no_mapping;
12737 --allow_keys;
12738
Bram Moolenaar219b8702006-11-01 14:32:36 +000012739 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12740 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12741 vimvars[VV_MOUSE_COL].vv_nr = 0;
12742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 if (IS_SPECIAL(n) || mod_mask != 0)
12745 {
12746 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12747 int i = 0;
12748
12749 /* Turn a special key into three bytes, plus modifier. */
12750 if (mod_mask != 0)
12751 {
12752 temp[i++] = K_SPECIAL;
12753 temp[i++] = KS_MODIFIER;
12754 temp[i++] = mod_mask;
12755 }
12756 if (IS_SPECIAL(n))
12757 {
12758 temp[i++] = K_SPECIAL;
12759 temp[i++] = K_SECOND(n);
12760 temp[i++] = K_THIRD(n);
12761 }
12762#ifdef FEAT_MBYTE
12763 else if (has_mbyte)
12764 i += (*mb_char2bytes)(n, temp + i);
12765#endif
12766 else
12767 temp[i++] = n;
12768 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->v_type = VAR_STRING;
12770 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012771
12772#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012773 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012774 {
12775 int row = mouse_row;
12776 int col = mouse_col;
12777 win_T *win;
12778 linenr_T lnum;
12779# ifdef FEAT_WINDOWS
12780 win_T *wp;
12781# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012782 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012783
12784 if (row >= 0 && col >= 0)
12785 {
12786 /* Find the window at the mouse coordinates and compute the
12787 * text position. */
12788 win = mouse_find_win(&row, &col);
12789 (void)mouse_comp_pos(win, &row, &col, &lnum);
12790# ifdef FEAT_WINDOWS
12791 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012792 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012793# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012794 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012795 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12796 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12797 }
12798 }
12799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 }
12801}
12802
12803/*
12804 * "getcharmod()" function
12805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012807f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810}
12811
12812/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012813 * "getcharsearch()" function
12814 */
12815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012816f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012817{
12818 if (rettv_dict_alloc(rettv) != FAIL)
12819 {
12820 dict_T *dict = rettv->vval.v_dict;
12821
12822 dict_add_nr_str(dict, "char", 0L, last_csearch());
12823 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12824 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12825 }
12826}
12827
12828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 * "getcmdline()" function
12830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012832f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 rettv->v_type = VAR_STRING;
12835 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
12838/*
12839 * "getcmdpos()" function
12840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012842f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845}
12846
12847/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012848 * "getcmdtype()" function
12849 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012851f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012852{
12853 rettv->v_type = VAR_STRING;
12854 rettv->vval.v_string = alloc(2);
12855 if (rettv->vval.v_string != NULL)
12856 {
12857 rettv->vval.v_string[0] = get_cmdline_type();
12858 rettv->vval.v_string[1] = NUL;
12859 }
12860}
12861
12862/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012863 * "getcmdwintype()" function
12864 */
12865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012866f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012867{
12868 rettv->v_type = VAR_STRING;
12869 rettv->vval.v_string = NULL;
12870#ifdef FEAT_CMDWIN
12871 rettv->vval.v_string = alloc(2);
12872 if (rettv->vval.v_string != NULL)
12873 {
12874 rettv->vval.v_string[0] = cmdwin_type;
12875 rettv->vval.v_string[1] = NUL;
12876 }
12877#endif
12878}
12879
12880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 * "getcwd()" function
12882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012884f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012886 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012887 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012890 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012891
12892 wp = find_tabwin(&argvars[0], &argvars[1]);
12893 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012895 if (wp->w_localdir != NULL)
12896 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12897 else if(globaldir != NULL)
12898 rettv->vval.v_string = vim_strsave(globaldir);
12899 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012900 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012901 cwd = alloc(MAXPATHL);
12902 if (cwd != NULL)
12903 {
12904 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12905 rettv->vval.v_string = vim_strsave(cwd);
12906 vim_free(cwd);
12907 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012908 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012909#ifdef BACKSLASH_IN_FILENAME
12910 if (rettv->vval.v_string != NULL)
12911 slash_adjust(rettv->vval.v_string);
12912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 }
12914}
12915
12916/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012917 * "getfontname()" function
12918 */
12919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012920f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012921{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->v_type = VAR_STRING;
12923 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012924#ifdef FEAT_GUI
12925 if (gui.in_use)
12926 {
12927 GuiFont font;
12928 char_u *name = NULL;
12929
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012930 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012931 {
12932 /* Get the "Normal" font. Either the name saved by
12933 * hl_set_font_name() or from the font ID. */
12934 font = gui.norm_font;
12935 name = hl_get_font_name();
12936 }
12937 else
12938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012940 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12941 return;
12942 font = gui_mch_get_font(name, FALSE);
12943 if (font == NOFONT)
12944 return; /* Invalid font name, return empty string. */
12945 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012947 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012948 gui_mch_free_font(font);
12949 }
12950#endif
12951}
12952
12953/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012954 * "getfperm({fname})" function
12955 */
12956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012957f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012958{
12959 char_u *fname;
12960 struct stat st;
12961 char_u *perm = NULL;
12962 char_u flags[] = "rwx";
12963 int i;
12964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012965 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012968 if (mch_stat((char *)fname, &st) >= 0)
12969 {
12970 perm = vim_strsave((char_u *)"---------");
12971 if (perm != NULL)
12972 {
12973 for (i = 0; i < 9; i++)
12974 {
12975 if (st.st_mode & (1 << (8 - i)))
12976 perm[i] = flags[i % 3];
12977 }
12978 }
12979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012981}
12982
12983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 * "getfsize({fname})" function
12985 */
12986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012987f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988{
12989 char_u *fname;
12990 struct stat st;
12991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012994 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995
12996 if (mch_stat((char *)fname, &st) >= 0)
12997 {
12998 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012999 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013003
13004 /* non-perfect check for overflow */
13005 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
13006 rettv->vval.v_number = -2;
13007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 }
13009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011}
13012
13013/*
13014 * "getftime({fname})" function
13015 */
13016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013017f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018{
13019 char_u *fname;
13020 struct stat st;
13021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023
13024 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013025 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028}
13029
13030/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013031 * "getftype({fname})" function
13032 */
13033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013034f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013035{
13036 char_u *fname;
13037 struct stat st;
13038 char_u *type = NULL;
13039 char *t;
13040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013044 if (mch_lstat((char *)fname, &st) >= 0)
13045 {
13046#ifdef S_ISREG
13047 if (S_ISREG(st.st_mode))
13048 t = "file";
13049 else if (S_ISDIR(st.st_mode))
13050 t = "dir";
13051# ifdef S_ISLNK
13052 else if (S_ISLNK(st.st_mode))
13053 t = "link";
13054# endif
13055# ifdef S_ISBLK
13056 else if (S_ISBLK(st.st_mode))
13057 t = "bdev";
13058# endif
13059# ifdef S_ISCHR
13060 else if (S_ISCHR(st.st_mode))
13061 t = "cdev";
13062# endif
13063# ifdef S_ISFIFO
13064 else if (S_ISFIFO(st.st_mode))
13065 t = "fifo";
13066# endif
13067# ifdef S_ISSOCK
13068 else if (S_ISSOCK(st.st_mode))
13069 t = "fifo";
13070# endif
13071 else
13072 t = "other";
13073#else
13074# ifdef S_IFMT
13075 switch (st.st_mode & S_IFMT)
13076 {
13077 case S_IFREG: t = "file"; break;
13078 case S_IFDIR: t = "dir"; break;
13079# ifdef S_IFLNK
13080 case S_IFLNK: t = "link"; break;
13081# endif
13082# ifdef S_IFBLK
13083 case S_IFBLK: t = "bdev"; break;
13084# endif
13085# ifdef S_IFCHR
13086 case S_IFCHR: t = "cdev"; break;
13087# endif
13088# ifdef S_IFIFO
13089 case S_IFIFO: t = "fifo"; break;
13090# endif
13091# ifdef S_IFSOCK
13092 case S_IFSOCK: t = "socket"; break;
13093# endif
13094 default: t = "other";
13095 }
13096# else
13097 if (mch_isdir(fname))
13098 t = "dir";
13099 else
13100 t = "file";
13101# endif
13102#endif
13103 type = vim_strsave((char_u *)t);
13104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013106}
13107
13108/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013109 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013110 */
13111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013112f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013113{
13114 linenr_T lnum;
13115 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013116 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013117
13118 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013119 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013120 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013121 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013122 retlist = FALSE;
13123 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013124 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013125 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013126 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013127 retlist = TRUE;
13128 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013129
Bram Moolenaar342337a2005-07-21 21:11:17 +000013130 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131}
13132
13133/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013134 * "getmatches()" function
13135 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013137f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013138{
13139#ifdef FEAT_SEARCH_EXTRA
13140 dict_T *dict;
13141 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013142 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013143
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013144 if (rettv_list_alloc(rettv) == OK)
13145 {
13146 while (cur != NULL)
13147 {
13148 dict = dict_alloc();
13149 if (dict == NULL)
13150 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013151 if (cur->match.regprog == NULL)
13152 {
13153 /* match added with matchaddpos() */
13154 for (i = 0; i < MAXPOSMATCH; ++i)
13155 {
13156 llpos_T *llpos;
13157 char buf[6];
13158 list_T *l;
13159
13160 llpos = &cur->pos.pos[i];
13161 if (llpos->lnum == 0)
13162 break;
13163 l = list_alloc();
13164 if (l == NULL)
13165 break;
13166 list_append_number(l, (varnumber_T)llpos->lnum);
13167 if (llpos->col > 0)
13168 {
13169 list_append_number(l, (varnumber_T)llpos->col);
13170 list_append_number(l, (varnumber_T)llpos->len);
13171 }
13172 sprintf(buf, "pos%d", i + 1);
13173 dict_add_list(dict, buf, l);
13174 }
13175 }
13176 else
13177 {
13178 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13179 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013180 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013181 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13182 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013183# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013184 if (cur->conceal_char)
13185 {
13186 char_u buf[MB_MAXBYTES + 1];
13187
13188 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13189 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13190 }
13191# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013192 list_append_dict(rettv->vval.v_list, dict);
13193 cur = cur->next;
13194 }
13195 }
13196#endif
13197}
13198
13199/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013200 * "getpid()" function
13201 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013203f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013204{
13205 rettv->vval.v_number = mch_get_pid();
13206}
13207
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013209getpos_both(
13210 typval_T *argvars,
13211 typval_T *rettv,
13212 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013213{
Bram Moolenaara5525202006-03-02 22:52:09 +000013214 pos_T *fp;
13215 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013216 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013217
13218 if (rettv_list_alloc(rettv) == OK)
13219 {
13220 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013221 if (getcurpos)
13222 fp = &curwin->w_cursor;
13223 else
13224 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013225 if (fnum != -1)
13226 list_append_number(l, (varnumber_T)fnum);
13227 else
13228 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013229 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13230 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013231 list_append_number(l, (fp != NULL)
13232 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013233 : (varnumber_T)0);
13234 list_append_number(l,
13235#ifdef FEAT_VIRTUALEDIT
13236 (fp != NULL) ? (varnumber_T)fp->coladd :
13237#endif
13238 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013239 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013240 {
13241 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013242 list_append_number(l, curwin->w_curswant == MAXCOL ?
13243 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013244 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013245 }
13246 else
13247 rettv->vval.v_number = FALSE;
13248}
13249
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013250
13251/*
13252 * "getcurpos()" function
13253 */
13254 static void
13255f_getcurpos(typval_T *argvars, typval_T *rettv)
13256{
13257 getpos_both(argvars, rettv, TRUE);
13258}
13259
13260/*
13261 * "getpos(string)" function
13262 */
13263 static void
13264f_getpos(typval_T *argvars, typval_T *rettv)
13265{
13266 getpos_both(argvars, rettv, FALSE);
13267}
13268
Bram Moolenaara5525202006-03-02 22:52:09 +000013269/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013270 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013271 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013273f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013274{
13275#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013276 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013277#endif
13278
Bram Moolenaar2641f772005-03-25 21:58:17 +000013279#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013280 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013281 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013282 wp = NULL;
13283 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13284 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013285 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013286 if (wp == NULL)
13287 return;
13288 }
13289
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013290 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013291 }
13292#endif
13293}
13294
13295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 * "getreg()" function
13297 */
13298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013299f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300{
13301 char_u *strregname;
13302 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013303 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013304 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013307 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 strregname = get_tv_string_chk(&argvars[0]);
13310 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013311 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013314 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13315 return_list = get_tv_number_chk(&argvars[2], &error);
13316 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013319 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013320
13321 if (error)
13322 return;
13323
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324 regname = (strregname == NULL ? '"' : *strregname);
13325 if (regname == 0)
13326 regname = '"';
13327
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013328 if (return_list)
13329 {
13330 rettv->v_type = VAR_LIST;
13331 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13332 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013333 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013334 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013335 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013336 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013337 }
13338 else
13339 {
13340 rettv->v_type = VAR_STRING;
13341 rettv->vval.v_string = get_reg_contents(regname,
13342 arg2 ? GREG_EXPR_SRC : 0);
13343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344}
13345
13346/*
13347 * "getregtype()" function
13348 */
13349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013350f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351{
13352 char_u *strregname;
13353 int regname;
13354 char_u buf[NUMBUFLEN + 2];
13355 long reglen = 0;
13356
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013357 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013358 {
13359 strregname = get_tv_string_chk(&argvars[0]);
13360 if (strregname == NULL) /* type error; errmsg already given */
13361 {
13362 rettv->v_type = VAR_STRING;
13363 rettv->vval.v_string = NULL;
13364 return;
13365 }
13366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 else
13368 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013369 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370
13371 regname = (strregname == NULL ? '"' : *strregname);
13372 if (regname == 0)
13373 regname = '"';
13374
13375 buf[0] = NUL;
13376 buf[1] = NUL;
13377 switch (get_reg_type(regname, &reglen))
13378 {
13379 case MLINE: buf[0] = 'V'; break;
13380 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381 case MBLOCK:
13382 buf[0] = Ctrl_V;
13383 sprintf((char *)buf + 1, "%ld", reglen + 1);
13384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013386 rettv->v_type = VAR_STRING;
13387 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388}
13389
13390/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013391 * "gettabvar()" function
13392 */
13393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013394f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013395{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013396 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013397 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013398 dictitem_T *v;
13399 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013400 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013401
13402 rettv->v_type = VAR_STRING;
13403 rettv->vval.v_string = NULL;
13404
13405 varname = get_tv_string_chk(&argvars[1]);
13406 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13407 if (tp != NULL && varname != NULL)
13408 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013409 /* Set tp to be our tabpage, temporarily. Also set the window to the
13410 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013411 if (switch_win(&oldcurwin, &oldtabpage,
13412 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013413 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013414 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013415 /* look up the variable */
13416 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13417 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13418 if (v != NULL)
13419 {
13420 copy_tv(&v->di_tv, rettv);
13421 done = TRUE;
13422 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013423 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013424
13425 /* restore previous notion of curwin */
13426 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013427 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013428
13429 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013430 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013431}
13432
13433/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013434 * "gettabwinvar()" function
13435 */
13436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013437f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013438{
13439 getwinvar(argvars, rettv, 1);
13440}
13441
13442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 * "getwinposx()" function
13444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013446f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449#ifdef FEAT_GUI
13450 if (gui.in_use)
13451 {
13452 int x, y;
13453
13454 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 }
13457#endif
13458}
13459
13460/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013461 * "win_findbuf()" function
13462 */
13463 static void
13464f_win_findbuf(typval_T *argvars, typval_T *rettv)
13465{
13466 if (rettv_list_alloc(rettv) != FAIL)
13467 win_findbuf(argvars, rettv->vval.v_list);
13468}
13469
13470/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013471 * "win_getid()" function
13472 */
13473 static void
13474f_win_getid(typval_T *argvars, typval_T *rettv)
13475{
13476 rettv->vval.v_number = win_getid(argvars);
13477}
13478
13479/*
13480 * "win_gotoid()" function
13481 */
13482 static void
13483f_win_gotoid(typval_T *argvars, typval_T *rettv)
13484{
13485 rettv->vval.v_number = win_gotoid(argvars);
13486}
13487
13488/*
13489 * "win_id2tabwin()" function
13490 */
13491 static void
13492f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13493{
13494 if (rettv_list_alloc(rettv) != FAIL)
13495 win_id2tabwin(argvars, rettv->vval.v_list);
13496}
13497
13498/*
13499 * "win_id2win()" function
13500 */
13501 static void
13502f_win_id2win(typval_T *argvars, typval_T *rettv)
13503{
13504 rettv->vval.v_number = win_id2win(argvars);
13505}
13506
13507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508 * "getwinposy()" function
13509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013511f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514#ifdef FEAT_GUI
13515 if (gui.in_use)
13516 {
13517 int x, y;
13518
13519 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 }
13522#endif
13523}
13524
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013525/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013526 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013527 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013528 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013529find_win_by_nr(
13530 typval_T *vp,
13531 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013532{
13533#ifdef FEAT_WINDOWS
13534 win_T *wp;
13535#endif
13536 int nr;
13537
13538 nr = get_tv_number_chk(vp, NULL);
13539
13540#ifdef FEAT_WINDOWS
13541 if (nr < 0)
13542 return NULL;
13543 if (nr == 0)
13544 return curwin;
13545
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013546 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13547 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013548 if (--nr <= 0)
13549 break;
13550 return wp;
13551#else
13552 if (nr == 0 || nr == 1)
13553 return curwin;
13554 return NULL;
13555#endif
13556}
13557
Bram Moolenaar071d4272004-06-13 20:20:40 +000013558/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013559 * Find window specified by "wvp" in tabpage "tvp".
13560 */
13561 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013562find_tabwin(
13563 typval_T *wvp, /* VAR_UNKNOWN for current window */
13564 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013565{
13566 win_T *wp = NULL;
13567 tabpage_T *tp = NULL;
13568 long n;
13569
13570 if (wvp->v_type != VAR_UNKNOWN)
13571 {
13572 if (tvp->v_type != VAR_UNKNOWN)
13573 {
13574 n = get_tv_number(tvp);
13575 if (n >= 0)
13576 tp = find_tabpage(n);
13577 }
13578 else
13579 tp = curtab;
13580
13581 if (tp != NULL)
13582 wp = find_win_by_nr(wvp, tp);
13583 }
13584 else
13585 wp = curwin;
13586
13587 return wp;
13588}
13589
13590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 * "getwinvar()" function
13592 */
13593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013594f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013596 getwinvar(argvars, rettv, 0);
13597}
13598
13599/*
13600 * getwinvar() and gettabwinvar()
13601 */
13602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013603getwinvar(
13604 typval_T *argvars,
13605 typval_T *rettv,
13606 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013607{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013608 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013610 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013611 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013612 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013613#ifdef FEAT_WINDOWS
13614 win_T *oldcurwin;
13615 tabpage_T *oldtabpage;
13616 int need_switch_win;
13617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013619#ifdef FEAT_WINDOWS
13620 if (off == 1)
13621 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13622 else
13623 tp = curtab;
13624#endif
13625 win = find_win_by_nr(&argvars[off], tp);
13626 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013627 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013629 rettv->v_type = VAR_STRING;
13630 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631
13632 if (win != NULL && varname != NULL)
13633 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013634#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013635 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013636 * otherwise the window is not valid. Only do this when needed,
13637 * autocommands get blocked. */
13638 need_switch_win = !(tp == curtab && win == curwin);
13639 if (!need_switch_win
13640 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13641#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013642 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013643 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013644 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013645 if (get_option_tv(&varname, rettv, 1) == OK)
13646 done = TRUE;
13647 }
13648 else
13649 {
13650 /* Look up the variable. */
13651 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13652 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13653 varname, FALSE);
13654 if (v != NULL)
13655 {
13656 copy_tv(&v->di_tv, rettv);
13657 done = TRUE;
13658 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013661
Bram Moolenaarba117c22015-09-29 16:53:22 +020013662#ifdef FEAT_WINDOWS
13663 if (need_switch_win)
13664 /* restore previous notion of curwin */
13665 restore_win(oldcurwin, oldtabpage, TRUE);
13666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 }
13668
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013669 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13670 /* use the default return value */
13671 copy_tv(&argvars[off + 2], rettv);
13672
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 --emsg_off;
13674}
13675
13676/*
13677 * "glob()" function
13678 */
13679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013680f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013682 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013684 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013686 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013687 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013689 if (argvars[1].v_type != VAR_UNKNOWN)
13690 {
13691 if (get_tv_number_chk(&argvars[1], &error))
13692 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013694 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013695 if (get_tv_number_chk(&argvars[2], &error))
13696 {
13697 rettv->v_type = VAR_LIST;
13698 rettv->vval.v_list = NULL;
13699 }
13700 if (argvars[3].v_type != VAR_UNKNOWN
13701 && get_tv_number_chk(&argvars[3], &error))
13702 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013703 }
13704 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013705 if (!error)
13706 {
13707 ExpandInit(&xpc);
13708 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013709 if (p_wic)
13710 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013711 if (rettv->v_type == VAR_STRING)
13712 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013713 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013714 else if (rettv_list_alloc(rettv) != FAIL)
13715 {
13716 int i;
13717
13718 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13719 NULL, options, WILD_ALL_KEEP);
13720 for (i = 0; i < xpc.xp_numfiles; i++)
13721 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13722
13723 ExpandCleanup(&xpc);
13724 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013725 }
13726 else
13727 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728}
13729
13730/*
13731 * "globpath()" function
13732 */
13733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013734f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013736 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013738 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013739 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013740 garray_T ga;
13741 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013743 /* When the optional second argument is non-zero, don't remove matches
13744 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013745 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013746 if (argvars[2].v_type != VAR_UNKNOWN)
13747 {
13748 if (get_tv_number_chk(&argvars[2], &error))
13749 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013750 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013751 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013752 if (get_tv_number_chk(&argvars[3], &error))
13753 {
13754 rettv->v_type = VAR_LIST;
13755 rettv->vval.v_list = NULL;
13756 }
13757 if (argvars[4].v_type != VAR_UNKNOWN
13758 && get_tv_number_chk(&argvars[4], &error))
13759 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013760 }
13761 }
13762 if (file != NULL && !error)
13763 {
13764 ga_init2(&ga, (int)sizeof(char_u *), 10);
13765 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13766 if (rettv->v_type == VAR_STRING)
13767 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13768 else if (rettv_list_alloc(rettv) != FAIL)
13769 for (i = 0; i < ga.ga_len; ++i)
13770 list_append_string(rettv->vval.v_list,
13771 ((char_u **)(ga.ga_data))[i], -1);
13772 ga_clear_strings(&ga);
13773 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013775 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776}
13777
13778/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013779 * "glob2regpat()" function
13780 */
13781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013782f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013783{
13784 char_u *pat = get_tv_string_chk(&argvars[0]);
13785
13786 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013787 rettv->vval.v_string = (pat == NULL)
13788 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013789}
13790
13791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792 * "has()" function
13793 */
13794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013795f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796{
13797 int i;
13798 char_u *name;
13799 int n = FALSE;
13800 static char *(has_list[]) =
13801 {
13802#ifdef AMIGA
13803 "amiga",
13804# ifdef FEAT_ARP
13805 "arp",
13806# endif
13807#endif
13808#ifdef __BEOS__
13809 "beos",
13810#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013811#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 "mac",
13813#endif
13814#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013815 "macunix", /* built with 'darwin' enabled */
13816#endif
13817#if defined(__APPLE__) && __APPLE__ == 1
13818 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820#ifdef __QNX__
13821 "qnx",
13822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823#ifdef UNIX
13824 "unix",
13825#endif
13826#ifdef VMS
13827 "vms",
13828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829#ifdef WIN32
13830 "win32",
13831#endif
13832#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13833 "win32unix",
13834#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013835#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836 "win64",
13837#endif
13838#ifdef EBCDIC
13839 "ebcdic",
13840#endif
13841#ifndef CASE_INSENSITIVE_FILENAME
13842 "fname_case",
13843#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013844#ifdef HAVE_ACL
13845 "acl",
13846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#ifdef FEAT_ARABIC
13848 "arabic",
13849#endif
13850#ifdef FEAT_AUTOCMD
13851 "autocmd",
13852#endif
13853#ifdef FEAT_BEVAL
13854 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013855# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13856 "balloon_multiline",
13857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#endif
13859#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13860 "builtin_terms",
13861# ifdef ALL_BUILTIN_TCAPS
13862 "all_builtin_terms",
13863# endif
13864#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013865#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13866 || defined(FEAT_GUI_W32) \
13867 || defined(FEAT_GUI_MOTIF))
13868 "browsefilter",
13869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870#ifdef FEAT_BYTEOFF
13871 "byte_offset",
13872#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013873#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013874 "channel",
13875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876#ifdef FEAT_CINDENT
13877 "cindent",
13878#endif
13879#ifdef FEAT_CLIENTSERVER
13880 "clientserver",
13881#endif
13882#ifdef FEAT_CLIPBOARD
13883 "clipboard",
13884#endif
13885#ifdef FEAT_CMDL_COMPL
13886 "cmdline_compl",
13887#endif
13888#ifdef FEAT_CMDHIST
13889 "cmdline_hist",
13890#endif
13891#ifdef FEAT_COMMENTS
13892 "comments",
13893#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013894#ifdef FEAT_CONCEAL
13895 "conceal",
13896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897#ifdef FEAT_CRYPT
13898 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013899 "crypt-blowfish",
13900 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901#endif
13902#ifdef FEAT_CSCOPE
13903 "cscope",
13904#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013905#ifdef FEAT_CURSORBIND
13906 "cursorbind",
13907#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013908#ifdef CURSOR_SHAPE
13909 "cursorshape",
13910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911#ifdef DEBUG
13912 "debug",
13913#endif
13914#ifdef FEAT_CON_DIALOG
13915 "dialog_con",
13916#endif
13917#ifdef FEAT_GUI_DIALOG
13918 "dialog_gui",
13919#endif
13920#ifdef FEAT_DIFF
13921 "diff",
13922#endif
13923#ifdef FEAT_DIGRAPHS
13924 "digraphs",
13925#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013926#ifdef FEAT_DIRECTX
13927 "directx",
13928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929#ifdef FEAT_DND
13930 "dnd",
13931#endif
13932#ifdef FEAT_EMACS_TAGS
13933 "emacs_tags",
13934#endif
13935 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013936 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937#ifdef FEAT_SEARCH_EXTRA
13938 "extra_search",
13939#endif
13940#ifdef FEAT_FKMAP
13941 "farsi",
13942#endif
13943#ifdef FEAT_SEARCHPATH
13944 "file_in_path",
13945#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013946#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013947 "filterpipe",
13948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949#ifdef FEAT_FIND_ID
13950 "find_in_path",
13951#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013952#ifdef FEAT_FLOAT
13953 "float",
13954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955#ifdef FEAT_FOLDING
13956 "folding",
13957#endif
13958#ifdef FEAT_FOOTER
13959 "footer",
13960#endif
13961#if !defined(USE_SYSTEM) && defined(UNIX)
13962 "fork",
13963#endif
13964#ifdef FEAT_GETTEXT
13965 "gettext",
13966#endif
13967#ifdef FEAT_GUI
13968 "gui",
13969#endif
13970#ifdef FEAT_GUI_ATHENA
13971# ifdef FEAT_GUI_NEXTAW
13972 "gui_neXtaw",
13973# else
13974 "gui_athena",
13975# endif
13976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977#ifdef FEAT_GUI_GTK
13978 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013979# ifdef USE_GTK3
13980 "gui_gtk3",
13981# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013985#ifdef FEAT_GUI_GNOME
13986 "gui_gnome",
13987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988#ifdef FEAT_GUI_MAC
13989 "gui_mac",
13990#endif
13991#ifdef FEAT_GUI_MOTIF
13992 "gui_motif",
13993#endif
13994#ifdef FEAT_GUI_PHOTON
13995 "gui_photon",
13996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997#ifdef FEAT_GUI_W32
13998 "gui_win32",
13999#endif
14000#ifdef FEAT_HANGULIN
14001 "hangul_input",
14002#endif
14003#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14004 "iconv",
14005#endif
14006#ifdef FEAT_INS_EXPAND
14007 "insert_expand",
14008#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014009#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014010 "job",
14011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012#ifdef FEAT_JUMPLIST
14013 "jumplist",
14014#endif
14015#ifdef FEAT_KEYMAP
14016 "keymap",
14017#endif
14018#ifdef FEAT_LANGMAP
14019 "langmap",
14020#endif
14021#ifdef FEAT_LIBCALL
14022 "libcall",
14023#endif
14024#ifdef FEAT_LINEBREAK
14025 "linebreak",
14026#endif
14027#ifdef FEAT_LISP
14028 "lispindent",
14029#endif
14030#ifdef FEAT_LISTCMDS
14031 "listcmds",
14032#endif
14033#ifdef FEAT_LOCALMAP
14034 "localmap",
14035#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014036#ifdef FEAT_LUA
14037# ifndef DYNAMIC_LUA
14038 "lua",
14039# endif
14040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041#ifdef FEAT_MENU
14042 "menu",
14043#endif
14044#ifdef FEAT_SESSION
14045 "mksession",
14046#endif
14047#ifdef FEAT_MODIFY_FNAME
14048 "modify_fname",
14049#endif
14050#ifdef FEAT_MOUSE
14051 "mouse",
14052#endif
14053#ifdef FEAT_MOUSESHAPE
14054 "mouseshape",
14055#endif
14056#if defined(UNIX) || defined(VMS)
14057# ifdef FEAT_MOUSE_DEC
14058 "mouse_dec",
14059# endif
14060# ifdef FEAT_MOUSE_GPM
14061 "mouse_gpm",
14062# endif
14063# ifdef FEAT_MOUSE_JSB
14064 "mouse_jsbterm",
14065# endif
14066# ifdef FEAT_MOUSE_NET
14067 "mouse_netterm",
14068# endif
14069# ifdef FEAT_MOUSE_PTERM
14070 "mouse_pterm",
14071# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014072# ifdef FEAT_MOUSE_SGR
14073 "mouse_sgr",
14074# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014075# ifdef FEAT_SYSMOUSE
14076 "mouse_sysmouse",
14077# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014078# ifdef FEAT_MOUSE_URXVT
14079 "mouse_urxvt",
14080# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081# ifdef FEAT_MOUSE_XTERM
14082 "mouse_xterm",
14083# endif
14084#endif
14085#ifdef FEAT_MBYTE
14086 "multi_byte",
14087#endif
14088#ifdef FEAT_MBYTE_IME
14089 "multi_byte_ime",
14090#endif
14091#ifdef FEAT_MULTI_LANG
14092 "multi_lang",
14093#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014094#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014095#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014096 "mzscheme",
14097#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099#ifdef FEAT_OLE
14100 "ole",
14101#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014102 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103#ifdef FEAT_PATH_EXTRA
14104 "path_extra",
14105#endif
14106#ifdef FEAT_PERL
14107#ifndef DYNAMIC_PERL
14108 "perl",
14109#endif
14110#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014111#ifdef FEAT_PERSISTENT_UNDO
14112 "persistent_undo",
14113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114#ifdef FEAT_PYTHON
14115#ifndef DYNAMIC_PYTHON
14116 "python",
14117#endif
14118#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014119#ifdef FEAT_PYTHON3
14120#ifndef DYNAMIC_PYTHON3
14121 "python3",
14122#endif
14123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124#ifdef FEAT_POSTSCRIPT
14125 "postscript",
14126#endif
14127#ifdef FEAT_PRINTER
14128 "printer",
14129#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014130#ifdef FEAT_PROFILE
14131 "profile",
14132#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014133#ifdef FEAT_RELTIME
14134 "reltime",
14135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136#ifdef FEAT_QUICKFIX
14137 "quickfix",
14138#endif
14139#ifdef FEAT_RIGHTLEFT
14140 "rightleft",
14141#endif
14142#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14143 "ruby",
14144#endif
14145#ifdef FEAT_SCROLLBIND
14146 "scrollbind",
14147#endif
14148#ifdef FEAT_CMDL_INFO
14149 "showcmd",
14150 "cmdline_info",
14151#endif
14152#ifdef FEAT_SIGNS
14153 "signs",
14154#endif
14155#ifdef FEAT_SMARTINDENT
14156 "smartindent",
14157#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014158#ifdef STARTUPTIME
14159 "startuptime",
14160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161#ifdef FEAT_STL_OPT
14162 "statusline",
14163#endif
14164#ifdef FEAT_SUN_WORKSHOP
14165 "sun_workshop",
14166#endif
14167#ifdef FEAT_NETBEANS_INTG
14168 "netbeans_intg",
14169#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014170#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014171 "spell",
14172#endif
14173#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 "syntax",
14175#endif
14176#if defined(USE_SYSTEM) || !defined(UNIX)
14177 "system",
14178#endif
14179#ifdef FEAT_TAG_BINS
14180 "tag_binary",
14181#endif
14182#ifdef FEAT_TAG_OLDSTATIC
14183 "tag_old_static",
14184#endif
14185#ifdef FEAT_TAG_ANYWHITE
14186 "tag_any_white",
14187#endif
14188#ifdef FEAT_TCL
14189# ifndef DYNAMIC_TCL
14190 "tcl",
14191# endif
14192#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014193#ifdef FEAT_TERMGUICOLORS
14194 "termguicolors",
14195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196#ifdef TERMINFO
14197 "terminfo",
14198#endif
14199#ifdef FEAT_TERMRESPONSE
14200 "termresponse",
14201#endif
14202#ifdef FEAT_TEXTOBJ
14203 "textobjects",
14204#endif
14205#ifdef HAVE_TGETENT
14206 "tgetent",
14207#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014208#ifdef FEAT_TIMERS
14209 "timers",
14210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211#ifdef FEAT_TITLE
14212 "title",
14213#endif
14214#ifdef FEAT_TOOLBAR
14215 "toolbar",
14216#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014217#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14218 "unnamedplus",
14219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220#ifdef FEAT_USR_CMDS
14221 "user-commands", /* was accidentally included in 5.4 */
14222 "user_commands",
14223#endif
14224#ifdef FEAT_VIMINFO
14225 "viminfo",
14226#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014227#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 "vertsplit",
14229#endif
14230#ifdef FEAT_VIRTUALEDIT
14231 "virtualedit",
14232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234#ifdef FEAT_VISUALEXTRA
14235 "visualextra",
14236#endif
14237#ifdef FEAT_VREPLACE
14238 "vreplace",
14239#endif
14240#ifdef FEAT_WILDIGN
14241 "wildignore",
14242#endif
14243#ifdef FEAT_WILDMENU
14244 "wildmenu",
14245#endif
14246#ifdef FEAT_WINDOWS
14247 "windows",
14248#endif
14249#ifdef FEAT_WAK
14250 "winaltkeys",
14251#endif
14252#ifdef FEAT_WRITEBACKUP
14253 "writebackup",
14254#endif
14255#ifdef FEAT_XIM
14256 "xim",
14257#endif
14258#ifdef FEAT_XFONTSET
14259 "xfontset",
14260#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014261#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014262 "xpm",
14263 "xpm_w32", /* for backward compatibility */
14264#else
14265# if defined(HAVE_XPM)
14266 "xpm",
14267# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269#ifdef USE_XSMP
14270 "xsmp",
14271#endif
14272#ifdef USE_XSMP_INTERACT
14273 "xsmp_interact",
14274#endif
14275#ifdef FEAT_XCLIPBOARD
14276 "xterm_clipboard",
14277#endif
14278#ifdef FEAT_XTERM_SAVE
14279 "xterm_save",
14280#endif
14281#if defined(UNIX) && defined(FEAT_X11)
14282 "X11",
14283#endif
14284 NULL
14285 };
14286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014287 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 for (i = 0; has_list[i] != NULL; ++i)
14289 if (STRICMP(name, has_list[i]) == 0)
14290 {
14291 n = TRUE;
14292 break;
14293 }
14294
14295 if (n == FALSE)
14296 {
14297 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014298 {
14299 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014300 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014301 && vim_isdigit(name[6])
14302 && vim_isdigit(name[8])
14303 && vim_isdigit(name[10]))
14304 {
14305 int major = atoi((char *)name + 6);
14306 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014307
14308 /* Expect "patch-9.9.01234". */
14309 n = (major < VIM_VERSION_MAJOR
14310 || (major == VIM_VERSION_MAJOR
14311 && (minor < VIM_VERSION_MINOR
14312 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014313 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014314 }
14315 else
14316 n = has_patch(atoi((char *)name + 5));
14317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 else if (STRICMP(name, "vim_starting") == 0)
14319 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014320#ifdef FEAT_MBYTE
14321 else if (STRICMP(name, "multi_byte_encoding") == 0)
14322 n = has_mbyte;
14323#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014324#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14325 else if (STRICMP(name, "balloon_multiline") == 0)
14326 n = multiline_balloon_available();
14327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328#ifdef DYNAMIC_TCL
14329 else if (STRICMP(name, "tcl") == 0)
14330 n = tcl_enabled(FALSE);
14331#endif
14332#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14333 else if (STRICMP(name, "iconv") == 0)
14334 n = iconv_enabled(FALSE);
14335#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014336#ifdef DYNAMIC_LUA
14337 else if (STRICMP(name, "lua") == 0)
14338 n = lua_enabled(FALSE);
14339#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014340#ifdef DYNAMIC_MZSCHEME
14341 else if (STRICMP(name, "mzscheme") == 0)
14342 n = mzscheme_enabled(FALSE);
14343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344#ifdef DYNAMIC_RUBY
14345 else if (STRICMP(name, "ruby") == 0)
14346 n = ruby_enabled(FALSE);
14347#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014348#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349#ifdef DYNAMIC_PYTHON
14350 else if (STRICMP(name, "python") == 0)
14351 n = python_enabled(FALSE);
14352#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014353#endif
14354#ifdef FEAT_PYTHON3
14355#ifdef DYNAMIC_PYTHON3
14356 else if (STRICMP(name, "python3") == 0)
14357 n = python3_enabled(FALSE);
14358#endif
14359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360#ifdef DYNAMIC_PERL
14361 else if (STRICMP(name, "perl") == 0)
14362 n = perl_enabled(FALSE);
14363#endif
14364#ifdef FEAT_GUI
14365 else if (STRICMP(name, "gui_running") == 0)
14366 n = (gui.in_use || gui.starting);
14367# ifdef FEAT_GUI_W32
14368 else if (STRICMP(name, "gui_win32s") == 0)
14369 n = gui_is_win32s();
14370# endif
14371# ifdef FEAT_BROWSE
14372 else if (STRICMP(name, "browse") == 0)
14373 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14374# endif
14375#endif
14376#ifdef FEAT_SYN_HL
14377 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014378 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#endif
14380#if defined(WIN3264)
14381 else if (STRICMP(name, "win95") == 0)
14382 n = mch_windows95();
14383#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014384#ifdef FEAT_NETBEANS_INTG
14385 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014386 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388 }
14389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391}
14392
14393/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014394 * "has_key()" function
14395 */
14396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014397f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014398{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014399 if (argvars[0].v_type != VAR_DICT)
14400 {
14401 EMSG(_(e_dictreq));
14402 return;
14403 }
14404 if (argvars[0].vval.v_dict == NULL)
14405 return;
14406
14407 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014408 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014409}
14410
14411/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014412 * "haslocaldir()" function
14413 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014416{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014417 win_T *wp = NULL;
14418
14419 wp = find_tabwin(&argvars[0], &argvars[1]);
14420 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014421}
14422
14423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 * "hasmapto()" function
14425 */
14426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014427f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428{
14429 char_u *name;
14430 char_u *mode;
14431 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014432 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014435 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 mode = (char_u *)"nvo";
14437 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014440 if (argvars[2].v_type != VAR_UNKNOWN)
14441 abbr = get_tv_number(&argvars[2]);
14442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443
Bram Moolenaar2c932302006-03-18 21:42:09 +000014444 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448}
14449
14450/*
14451 * "histadd()" function
14452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014454f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455{
14456#ifdef FEAT_CMDHIST
14457 int histype;
14458 char_u *str;
14459 char_u buf[NUMBUFLEN];
14460#endif
14461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 if (check_restricted() || check_secure())
14464 return;
14465#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014466 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14467 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 if (histype >= 0)
14469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471 if (*str != NUL)
14472 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014473 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 return;
14477 }
14478 }
14479#endif
14480}
14481
14482/*
14483 * "histdel()" function
14484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014486f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487{
14488#ifdef FEAT_CMDHIST
14489 int n;
14490 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014491 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014493 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14494 if (str == NULL)
14495 n = 0;
14496 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014499 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014500 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014501 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 else
14504 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 get_tv_string_buf(&argvars[1], buf));
14507 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508#endif
14509}
14510
14511/*
14512 * "histget()" function
14513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014515f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516{
14517#ifdef FEAT_CMDHIST
14518 int type;
14519 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14523 if (str == NULL)
14524 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 {
14527 type = get_histtype(str);
14528 if (argvars[1].v_type == VAR_UNKNOWN)
14529 idx = get_history_idx(type);
14530 else
14531 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14532 /* -1 on type error */
14533 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539}
14540
14541/*
14542 * "histnr()" function
14543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
14547 int i;
14548
14549#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 char_u *history = get_tv_string_chk(&argvars[0]);
14551
14552 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 if (i >= HIST_CMD && i < HIST_COUNT)
14554 i = get_history_idx(i);
14555 else
14556#endif
14557 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559}
14560
14561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 * "highlightID(name)" function
14563 */
14564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014565f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014567 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568}
14569
14570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 * "highlight_exists()" function
14572 */
14573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014574f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575{
14576 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14577}
14578
14579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 * "hostname()" function
14581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014583f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
14585 char_u hostname[256];
14586
14587 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->v_type = VAR_STRING;
14589 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590}
14591
14592/*
14593 * iconv() function
14594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014596f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597{
14598#ifdef FEAT_MBYTE
14599 char_u buf1[NUMBUFLEN];
14600 char_u buf2[NUMBUFLEN];
14601 char_u *from, *to, *str;
14602 vimconv_T vimconv;
14603#endif
14604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->v_type = VAR_STRING;
14606 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607
14608#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 str = get_tv_string(&argvars[0]);
14610 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14611 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612 vimconv.vc_type = CONV_NONE;
14613 convert_setup(&vimconv, from, to);
14614
14615 /* If the encodings are equal, no conversion needed. */
14616 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014617 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014619 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620
14621 convert_setup(&vimconv, NULL, NULL);
14622 vim_free(from);
14623 vim_free(to);
14624#endif
14625}
14626
14627/*
14628 * "indent()" function
14629 */
14630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014631f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632{
14633 linenr_T lnum;
14634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014635 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014637 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640}
14641
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014642/*
14643 * "index()" function
14644 */
14645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014646f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014647{
Bram Moolenaar33570922005-01-25 22:26:29 +000014648 list_T *l;
14649 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014650 long idx = 0;
14651 int ic = FALSE;
14652
14653 rettv->vval.v_number = -1;
14654 if (argvars[0].v_type != VAR_LIST)
14655 {
14656 EMSG(_(e_listreq));
14657 return;
14658 }
14659 l = argvars[0].vval.v_list;
14660 if (l != NULL)
14661 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014662 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014663 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014664 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 int error = FALSE;
14666
Bram Moolenaar758711c2005-02-02 23:11:38 +000014667 /* Start at specified item. Use the cached index that list_find()
14668 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014669 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014670 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014671 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014672 ic = get_tv_number_chk(&argvars[3], &error);
14673 if (error)
14674 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014675 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014676
Bram Moolenaar758711c2005-02-02 23:11:38 +000014677 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014678 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014679 {
14680 rettv->vval.v_number = idx;
14681 break;
14682 }
14683 }
14684}
14685
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686static int inputsecret_flag = 0;
14687
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014688static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014689
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014691 * This function is used by f_input() and f_inputdialog() functions. The third
14692 * argument to f_input() specifies the type of completion to use at the
14693 * prompt. The third argument to f_inputdialog() specifies the value to return
14694 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 */
14696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014697get_user_input(
14698 typval_T *argvars,
14699 typval_T *rettv,
14700 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014701{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 char_u *p = NULL;
14704 int c;
14705 char_u buf[NUMBUFLEN];
14706 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014707 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014708 int xp_type = EXPAND_NOTHING;
14709 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014711 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014712 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713
14714#ifdef NO_CONSOLE_INPUT
14715 /* While starting up, there is no place to enter text. */
14716 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718#endif
14719
14720 cmd_silent = FALSE; /* Want to see the prompt. */
14721 if (prompt != NULL)
14722 {
14723 /* Only the part of the message after the last NL is considered as
14724 * prompt for the command line */
14725 p = vim_strrchr(prompt, '\n');
14726 if (p == NULL)
14727 p = prompt;
14728 else
14729 {
14730 ++p;
14731 c = *p;
14732 *p = NUL;
14733 msg_start();
14734 msg_clr_eos();
14735 msg_puts_attr(prompt, echo_attr);
14736 msg_didout = FALSE;
14737 msg_starthere();
14738 *p = c;
14739 }
14740 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 if (argvars[1].v_type != VAR_UNKNOWN)
14743 {
14744 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14745 if (defstr != NULL)
14746 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014748 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014749 {
14750 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014751 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014752 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014753
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014754 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014755 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014756
Bram Moolenaar4463f292005-09-25 22:20:24 +000014757 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14758 if (xp_name == NULL)
14759 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014760
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014761 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014762
Bram Moolenaar4463f292005-09-25 22:20:24 +000014763 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14764 &xp_arg) == FAIL)
14765 return;
14766 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014767 }
14768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014770 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014771 int save_ex_normal_busy = ex_normal_busy;
14772 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014773 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014774 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14775 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014776 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014777 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014778 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014779 && argvars[1].v_type != VAR_UNKNOWN
14780 && argvars[2].v_type != VAR_UNKNOWN)
14781 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14782 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014783
14784 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014786 /* since the user typed this, no need to wait for return */
14787 need_wait_return = FALSE;
14788 msg_didout = FALSE;
14789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 cmd_silent = cmd_silent_save;
14791}
14792
14793/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014794 * "input()" function
14795 * Also handles inputsecret() when inputsecret is set.
14796 */
14797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014798f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014799{
14800 get_user_input(argvars, rettv, FALSE);
14801}
14802
14803/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014804 * "inputdialog()" function
14805 */
14806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014807f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808{
14809#if defined(FEAT_GUI_TEXTDIALOG)
14810 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14811 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14812 {
14813 char_u *message;
14814 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014815 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 message = get_tv_string_chk(&argvars[0]);
14818 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014819 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014820 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014821 else
14822 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 if (message != NULL && defstr != NULL
14824 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014825 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 else
14828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014829 if (message != NULL && defstr != NULL
14830 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014831 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 rettv->vval.v_string = vim_strsave(
14833 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014835 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014837 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 }
14839 else
14840#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014841 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842}
14843
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014844/*
14845 * "inputlist()" function
14846 */
14847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014848f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014849{
14850 listitem_T *li;
14851 int selected;
14852 int mouse_used;
14853
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014854#ifdef NO_CONSOLE_INPUT
14855 /* While starting up, there is no place to enter text. */
14856 if (no_console_input())
14857 return;
14858#endif
14859 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14860 {
14861 EMSG2(_(e_listarg), "inputlist()");
14862 return;
14863 }
14864
14865 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014866 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014867 lines_left = Rows; /* avoid more prompt */
14868 msg_scroll = TRUE;
14869 msg_clr_eos();
14870
14871 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14872 {
14873 msg_puts(get_tv_string(&li->li_tv));
14874 msg_putchar('\n');
14875 }
14876
14877 /* Ask for choice. */
14878 selected = prompt_for_number(&mouse_used);
14879 if (mouse_used)
14880 selected -= lines_left;
14881
14882 rettv->vval.v_number = selected;
14883}
14884
14885
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14887
14888/*
14889 * "inputrestore()" function
14890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014892f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893{
14894 if (ga_userinput.ga_len > 0)
14895 {
14896 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014897 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14898 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014899 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 }
14901 else if (p_verbose > 1)
14902 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014903 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014904 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 }
14906}
14907
14908/*
14909 * "inputsave()" function
14910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014912f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014913{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014914 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915 if (ga_grow(&ga_userinput, 1) == OK)
14916 {
14917 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14918 + ga_userinput.ga_len);
14919 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014920 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 }
14922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924}
14925
14926/*
14927 * "inputsecret()" function
14928 */
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014931{
14932 ++cmdline_star;
14933 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014934 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 --cmdline_star;
14936 --inputsecret_flag;
14937}
14938
14939/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014940 * "insert()" function
14941 */
14942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014943f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014944{
14945 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014946 listitem_T *item;
14947 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014948 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014949
14950 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014952 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014953 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014954 {
14955 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014956 before = get_tv_number_chk(&argvars[2], &error);
14957 if (error)
14958 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014959
Bram Moolenaar758711c2005-02-02 23:11:38 +000014960 if (before == l->lv_len)
14961 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014962 else
14963 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014964 item = list_find(l, before);
14965 if (item == NULL)
14966 {
14967 EMSGN(_(e_listidx), before);
14968 l = NULL;
14969 }
14970 }
14971 if (l != NULL)
14972 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014973 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014974 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014975 }
14976 }
14977}
14978
14979/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014980 * "invert(expr)" function
14981 */
14982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014983f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014984{
14985 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14986}
14987
14988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989 * "isdirectory()" function
14990 */
14991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014992f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995}
14996
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014997/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014998 * "islocked()" function
14999 */
15000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015001f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015002{
15003 lval_T lv;
15004 char_u *end;
15005 dictitem_T *di;
15006
15007 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015008 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15009 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015010 if (end != NULL && lv.ll_name != NULL)
15011 {
15012 if (*end != NUL)
15013 EMSG(_(e_trailing));
15014 else
15015 {
15016 if (lv.ll_tv == NULL)
15017 {
15018 if (check_changedtick(lv.ll_name))
15019 rettv->vval.v_number = 1; /* always locked */
15020 else
15021 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015022 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015023 if (di != NULL)
15024 {
15025 /* Consider a variable locked when:
15026 * 1. the variable itself is locked
15027 * 2. the value of the variable is locked.
15028 * 3. the List or Dict value is locked.
15029 */
15030 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15031 || tv_islocked(&di->di_tv));
15032 }
15033 }
15034 }
15035 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015036 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015037 else if (lv.ll_newkey != NULL)
15038 EMSG2(_(e_dictkey), lv.ll_newkey);
15039 else if (lv.ll_list != NULL)
15040 /* List item. */
15041 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15042 else
15043 /* Dictionary item. */
15044 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15045 }
15046 }
15047
15048 clear_lval(&lv);
15049}
15050
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015051#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15052/*
15053 * "isnan()" function
15054 */
15055 static void
15056f_isnan(typval_T *argvars, typval_T *rettv)
15057{
15058 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15059 && isnan(argvars[0].vval.v_float);
15060}
15061#endif
15062
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015063static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015064
15065/*
15066 * Turn a dict into a list:
15067 * "what" == 0: list of keys
15068 * "what" == 1: list of values
15069 * "what" == 2: list of items
15070 */
15071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015072dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015073{
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 list_T *l2;
15075 dictitem_T *di;
15076 hashitem_T *hi;
15077 listitem_T *li;
15078 listitem_T *li2;
15079 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015080 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015081
Bram Moolenaar8c711452005-01-14 21:53:12 +000015082 if (argvars[0].v_type != VAR_DICT)
15083 {
15084 EMSG(_(e_dictreq));
15085 return;
15086 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015087 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015088 return;
15089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015090 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015091 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015093 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015094 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015096 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015098 --todo;
15099 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015100
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015101 li = listitem_alloc();
15102 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015103 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015104 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015105
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015106 if (what == 0)
15107 {
15108 /* keys() */
15109 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015110 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015111 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15112 }
15113 else if (what == 1)
15114 {
15115 /* values() */
15116 copy_tv(&di->di_tv, &li->li_tv);
15117 }
15118 else
15119 {
15120 /* items() */
15121 l2 = list_alloc();
15122 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015123 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015124 li->li_tv.vval.v_list = l2;
15125 if (l2 == NULL)
15126 break;
15127 ++l2->lv_refcount;
15128
15129 li2 = listitem_alloc();
15130 if (li2 == NULL)
15131 break;
15132 list_append(l2, li2);
15133 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015134 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015135 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15136
15137 li2 = listitem_alloc();
15138 if (li2 == NULL)
15139 break;
15140 list_append(l2, li2);
15141 copy_tv(&di->di_tv, &li2->li_tv);
15142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015143 }
15144 }
15145}
15146
15147/*
15148 * "items(dict)" function
15149 */
15150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015151f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015152{
15153 dict_list(argvars, rettv, 2);
15154}
15155
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015156#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015157/*
15158 * Get the job from the argument.
15159 * Returns NULL if the job is invalid.
15160 */
15161 static job_T *
15162get_job_arg(typval_T *tv)
15163{
15164 job_T *job;
15165
15166 if (tv->v_type != VAR_JOB)
15167 {
15168 EMSG2(_(e_invarg2), get_tv_string(tv));
15169 return NULL;
15170 }
15171 job = tv->vval.v_job;
15172
15173 if (job == NULL)
15174 EMSG(_("E916: not a valid job"));
15175 return job;
15176}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015177
Bram Moolenaar835dc632016-02-07 14:27:38 +010015178/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015179 * "job_getchannel()" function
15180 */
15181 static void
15182f_job_getchannel(typval_T *argvars, typval_T *rettv)
15183{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015184 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015185
Bram Moolenaar65edff82016-02-21 16:40:11 +010015186 if (job != NULL)
15187 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015188 rettv->v_type = VAR_CHANNEL;
15189 rettv->vval.v_channel = job->jv_channel;
15190 if (job->jv_channel != NULL)
15191 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015192 }
15193}
15194
15195/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015196 * "job_info()" function
15197 */
15198 static void
15199f_job_info(typval_T *argvars, typval_T *rettv)
15200{
15201 job_T *job = get_job_arg(&argvars[0]);
15202
15203 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15204 job_info(job, rettv->vval.v_dict);
15205}
15206
15207/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015208 * "job_setoptions()" function
15209 */
15210 static void
15211f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15212{
15213 job_T *job = get_job_arg(&argvars[0]);
15214 jobopt_T opt;
15215
15216 if (job == NULL)
15217 return;
15218 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015219 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15220 job_set_options(job, &opt);
15221 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015222}
15223
15224/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015225 * "job_start()" function
15226 */
15227 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015228f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015229{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015230 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015231 if (check_restricted() || check_secure())
15232 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015233 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015234}
15235
15236/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015237 * "job_status()" function
15238 */
15239 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015240f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015241{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015242 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015243
Bram Moolenaar65edff82016-02-21 16:40:11 +010015244 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015245 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015246 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015247 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015248 }
15249}
15250
15251/*
15252 * "job_stop()" function
15253 */
15254 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015255f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015256{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015257 job_T *job = get_job_arg(&argvars[0]);
15258
15259 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015260 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015261}
15262#endif
15263
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015265 * "join()" function
15266 */
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015269{
15270 garray_T ga;
15271 char_u *sep;
15272
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015273 if (argvars[0].v_type != VAR_LIST)
15274 {
15275 EMSG(_(e_listreq));
15276 return;
15277 }
15278 if (argvars[0].vval.v_list == NULL)
15279 return;
15280 if (argvars[1].v_type == VAR_UNKNOWN)
15281 sep = (char_u *)" ";
15282 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015283 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015284
15285 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015286
15287 if (sep != NULL)
15288 {
15289 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015290 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 ga_append(&ga, NUL);
15292 rettv->vval.v_string = (char_u *)ga.ga_data;
15293 }
15294 else
15295 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015296}
15297
15298/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015299 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015300 */
15301 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015302f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015303{
15304 js_read_T reader;
15305
15306 reader.js_buf = get_tv_string(&argvars[0]);
15307 reader.js_fill = NULL;
15308 reader.js_used = 0;
15309 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15310 EMSG(_(e_invarg));
15311}
15312
15313/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015314 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015315 */
15316 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015317f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015318{
15319 rettv->v_type = VAR_STRING;
15320 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15321}
15322
15323/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015324 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015325 */
15326 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015327f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015328{
15329 js_read_T reader;
15330
15331 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015332 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015333 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015334 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015335 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015336}
15337
15338/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015339 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015340 */
15341 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015342f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015343{
15344 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015345 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015346}
15347
15348/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015349 * "keys()" function
15350 */
15351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015352f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015353{
15354 dict_list(argvars, rettv, 0);
15355}
15356
15357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 * "last_buffer_nr()" function.
15359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015361f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362{
15363 int n = 0;
15364 buf_T *buf;
15365
15366 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15367 if (n < buf->b_fnum)
15368 n = buf->b_fnum;
15369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015370 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015371}
15372
15373/*
15374 * "len()" function
15375 */
15376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015377f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015378{
15379 switch (argvars[0].v_type)
15380 {
15381 case VAR_STRING:
15382 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->vval.v_number = (varnumber_T)STRLEN(
15384 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015385 break;
15386 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015387 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015388 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015389 case VAR_DICT:
15390 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15391 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015392 case VAR_UNKNOWN:
15393 case VAR_SPECIAL:
15394 case VAR_FLOAT:
15395 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015396 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015397 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015398 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015399 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015400 break;
15401 }
15402}
15403
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015404static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015405
15406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015407libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015408{
15409#ifdef FEAT_LIBCALL
15410 char_u *string_in;
15411 char_u **string_result;
15412 int nr_result;
15413#endif
15414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015416 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418
15419 if (check_restricted() || check_secure())
15420 return;
15421
15422#ifdef FEAT_LIBCALL
15423 /* The first two args must be strings, otherwise its meaningless */
15424 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15425 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015426 string_in = NULL;
15427 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015428 string_in = argvars[2].vval.v_string;
15429 if (type == VAR_NUMBER)
15430 string_result = NULL;
15431 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433 if (mch_libcall(argvars[0].vval.v_string,
15434 argvars[1].vval.v_string,
15435 string_in,
15436 argvars[2].vval.v_number,
15437 string_result,
15438 &nr_result) == OK
15439 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015441 }
15442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443}
15444
15445/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015446 * "libcall()" function
15447 */
15448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015449f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015450{
15451 libcall_common(argvars, rettv, VAR_STRING);
15452}
15453
15454/*
15455 * "libcallnr()" function
15456 */
15457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015458f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015459{
15460 libcall_common(argvars, rettv, VAR_NUMBER);
15461}
15462
15463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 * "line(string)" function
15465 */
15466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015467f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468{
15469 linenr_T lnum = 0;
15470 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015471 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015473 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 if (fp != NULL)
15475 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477}
15478
15479/*
15480 * "line2byte(lnum)" function
15481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015483f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484{
15485#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487#else
15488 linenr_T lnum;
15489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15495 if (rettv->vval.v_number >= 0)
15496 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497#endif
15498}
15499
15500/*
15501 * "lispindent(lnum)" function
15502 */
15503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015504f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505{
15506#ifdef FEAT_LISP
15507 pos_T pos;
15508 linenr_T lnum;
15509
15510 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15513 {
15514 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 curwin->w_cursor = pos;
15517 }
15518 else
15519#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521}
15522
15523/*
15524 * "localtime()" function
15525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530}
15531
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015532static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533
15534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015535get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536{
15537 char_u *keys;
15538 char_u *which;
15539 char_u buf[NUMBUFLEN];
15540 char_u *keys_buf = NULL;
15541 char_u *rhs;
15542 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015543 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015544 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015545 mapblock_T *mp;
15546 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547
15548 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015549 rettv->v_type = VAR_STRING;
15550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 if (*keys == NUL)
15554 return;
15555
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015558 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015560 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015561 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015562 if (argvars[3].v_type != VAR_UNKNOWN)
15563 get_dict = get_tv_number(&argvars[3]);
15564 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 else
15567 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015568 if (which == NULL)
15569 return;
15570
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 mode = get_map_mode(&which, 0);
15572
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015573 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015574 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015576
15577 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015578 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015579 /* Return a string. */
15580 if (rhs != NULL)
15581 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582
Bram Moolenaarbd743252010-10-20 21:23:33 +020015583 }
15584 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15585 {
15586 /* Return a dictionary. */
15587 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15588 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15589 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590
Bram Moolenaarbd743252010-10-20 21:23:33 +020015591 dict_add_nr_str(dict, "lhs", 0L, lhs);
15592 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15593 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15594 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15595 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15596 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15597 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015598 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015599 dict_add_nr_str(dict, "mode", 0L, mapmode);
15600
15601 vim_free(lhs);
15602 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 }
15604}
15605
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015606#ifdef FEAT_FLOAT
15607/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015608 * "log()" function
15609 */
15610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015611f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015612{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015613 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015614
15615 rettv->v_type = VAR_FLOAT;
15616 if (get_float_arg(argvars, &f) == OK)
15617 rettv->vval.v_float = log(f);
15618 else
15619 rettv->vval.v_float = 0.0;
15620}
15621
15622/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015623 * "log10()" function
15624 */
15625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015627{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015628 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015629
15630 rettv->v_type = VAR_FLOAT;
15631 if (get_float_arg(argvars, &f) == OK)
15632 rettv->vval.v_float = log10(f);
15633 else
15634 rettv->vval.v_float = 0.0;
15635}
15636#endif
15637
Bram Moolenaar1dced572012-04-05 16:54:08 +020015638#ifdef FEAT_LUA
15639/*
15640 * "luaeval()" function
15641 */
15642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015643f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015644{
15645 char_u *str;
15646 char_u buf[NUMBUFLEN];
15647
15648 str = get_tv_string_buf(&argvars[0], buf);
15649 do_luaeval(str, argvars + 1, rettv);
15650}
15651#endif
15652
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015654 * "map()" function
15655 */
15656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015657f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658{
15659 filter_map(argvars, rettv, TRUE);
15660}
15661
15662/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 */
15665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015666f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015668 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669}
15670
15671/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015672 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 */
15674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015675f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015677 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678}
15679
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015680static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
15682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015683find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015685 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015686 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015687 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 char_u *pat;
15689 regmatch_T regmatch;
15690 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015691 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 char_u *save_cpo;
15693 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015694 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015695 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015696 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015697 list_T *l = NULL;
15698 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015700 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701
15702 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15703 save_cpo = p_cpo;
15704 p_cpo = (char_u *)"";
15705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015706 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015707 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015708 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015709 /* type 3: return empty list when there are no matches.
15710 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015711 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015712 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015713 if (type == 4
15714 && (list_append_string(rettv->vval.v_list,
15715 (char_u *)"", 0) == FAIL
15716 || list_append_number(rettv->vval.v_list,
15717 (varnumber_T)-1) == FAIL
15718 || list_append_number(rettv->vval.v_list,
15719 (varnumber_T)-1) == FAIL
15720 || list_append_number(rettv->vval.v_list,
15721 (varnumber_T)-1) == FAIL))
15722 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015723 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015724 rettv->vval.v_list = NULL;
15725 goto theend;
15726 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015727 }
15728 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 rettv->v_type = VAR_STRING;
15731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015734 if (argvars[0].v_type == VAR_LIST)
15735 {
15736 if ((l = argvars[0].vval.v_list) == NULL)
15737 goto theend;
15738 li = l->lv_first;
15739 }
15740 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015741 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015742 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015743 len = (long)STRLEN(str);
15744 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015746 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15747 if (pat == NULL)
15748 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015750 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015752 int error = FALSE;
15753
15754 start = get_tv_number_chk(&argvars[2], &error);
15755 if (error)
15756 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015757 if (l != NULL)
15758 {
15759 li = list_find(l, start);
15760 if (li == NULL)
15761 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015762 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015763 }
15764 else
15765 {
15766 if (start < 0)
15767 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015768 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015769 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015770 /* When "count" argument is there ignore matches before "start",
15771 * otherwise skip part of the string. Differs when pattern is "^"
15772 * or "\<". */
15773 if (argvars[3].v_type != VAR_UNKNOWN)
15774 startcol = start;
15775 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015776 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015777 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015778 len -= start;
15779 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015780 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015781
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015782 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015783 nth = get_tv_number_chk(&argvars[3], &error);
15784 if (error)
15785 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786 }
15787
15788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15789 if (regmatch.regprog != NULL)
15790 {
15791 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015792
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015793 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015794 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015795 if (l != NULL)
15796 {
15797 if (li == NULL)
15798 {
15799 match = FALSE;
15800 break;
15801 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015802 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015803 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015804 if (str == NULL)
15805 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015806 }
15807
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015808 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015809
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015810 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015811 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015812 if (l == NULL && !match)
15813 break;
15814
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015815 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015816 if (l != NULL)
15817 {
15818 li = li->li_next;
15819 ++idx;
15820 }
15821 else
15822 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015823#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015824 startcol = (colnr_T)(regmatch.startp[0]
15825 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015826#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015827 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015828#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015829 if (startcol > (colnr_T)len
15830 || str + startcol <= regmatch.startp[0])
15831 {
15832 match = FALSE;
15833 break;
15834 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015835 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015836 }
15837
15838 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015840 if (type == 4)
15841 {
15842 listitem_T *li1 = rettv->vval.v_list->lv_first;
15843 listitem_T *li2 = li1->li_next;
15844 listitem_T *li3 = li2->li_next;
15845 listitem_T *li4 = li3->li_next;
15846
Bram Moolenaar3c809342016-06-01 22:34:48 +020015847 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015848 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15849 (int)(regmatch.endp[0] - regmatch.startp[0]));
15850 li3->li_tv.vval.v_number =
15851 (varnumber_T)(regmatch.startp[0] - expr);
15852 li4->li_tv.vval.v_number =
15853 (varnumber_T)(regmatch.endp[0] - expr);
15854 if (l != NULL)
15855 li2->li_tv.vval.v_number = (varnumber_T)idx;
15856 }
15857 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015859 int i;
15860
15861 /* return list with matched string and submatches */
15862 for (i = 0; i < NSUBEXP; ++i)
15863 {
15864 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015865 {
15866 if (list_append_string(rettv->vval.v_list,
15867 (char_u *)"", 0) == FAIL)
15868 break;
15869 }
15870 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015871 regmatch.startp[i],
15872 (int)(regmatch.endp[i] - regmatch.startp[i]))
15873 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015874 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015875 }
15876 }
15877 else if (type == 2)
15878 {
15879 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015880 if (l != NULL)
15881 copy_tv(&li->li_tv, rettv);
15882 else
15883 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015885 }
15886 else if (l != NULL)
15887 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 else
15889 {
15890 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 (varnumber_T)(regmatch.startp[0] - str);
15893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015896 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 }
15898 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015899 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 }
15901
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015902 if (type == 4 && l == NULL)
15903 /* matchstrpos() without a list: drop the second item. */
15904 listitem_remove(rettv->vval.v_list,
15905 rettv->vval.v_list->lv_first->li_next);
15906
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015908 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 p_cpo = save_cpo;
15910}
15911
15912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015913 * "match()" function
15914 */
15915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015916f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917{
15918 find_some_match(argvars, rettv, 1);
15919}
15920
15921/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015922 * "matchadd()" function
15923 */
15924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015925f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015926{
15927#ifdef FEAT_SEARCH_EXTRA
15928 char_u buf[NUMBUFLEN];
15929 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15930 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15931 int prio = 10; /* default priority */
15932 int id = -1;
15933 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015934 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015935
15936 rettv->vval.v_number = -1;
15937
15938 if (grp == NULL || pat == NULL)
15939 return;
15940 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015941 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015942 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015943 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015944 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015945 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015946 if (argvars[4].v_type != VAR_UNKNOWN)
15947 {
15948 if (argvars[4].v_type != VAR_DICT)
15949 {
15950 EMSG(_(e_dictreq));
15951 return;
15952 }
15953 if (dict_find(argvars[4].vval.v_dict,
15954 (char_u *)"conceal", -1) != NULL)
15955 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15956 (char_u *)"conceal", FALSE);
15957 }
15958 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015959 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015960 if (error == TRUE)
15961 return;
15962 if (id >= 1 && id <= 3)
15963 {
15964 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15965 return;
15966 }
15967
Bram Moolenaar6561d522015-07-21 15:48:27 +020015968 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15969 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015970#endif
15971}
15972
15973/*
15974 * "matchaddpos()" function
15975 */
15976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015977f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015978{
15979#ifdef FEAT_SEARCH_EXTRA
15980 char_u buf[NUMBUFLEN];
15981 char_u *group;
15982 int prio = 10;
15983 int id = -1;
15984 int error = FALSE;
15985 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015986 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015987
15988 rettv->vval.v_number = -1;
15989
15990 group = get_tv_string_buf_chk(&argvars[0], buf);
15991 if (group == NULL)
15992 return;
15993
15994 if (argvars[1].v_type != VAR_LIST)
15995 {
15996 EMSG2(_(e_listarg), "matchaddpos()");
15997 return;
15998 }
15999 l = argvars[1].vval.v_list;
16000 if (l == NULL)
16001 return;
16002
16003 if (argvars[2].v_type != VAR_UNKNOWN)
16004 {
16005 prio = get_tv_number_chk(&argvars[2], &error);
16006 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016007 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016008 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016009 if (argvars[4].v_type != VAR_UNKNOWN)
16010 {
16011 if (argvars[4].v_type != VAR_DICT)
16012 {
16013 EMSG(_(e_dictreq));
16014 return;
16015 }
16016 if (dict_find(argvars[4].vval.v_dict,
16017 (char_u *)"conceal", -1) != NULL)
16018 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16019 (char_u *)"conceal", FALSE);
16020 }
16021 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016022 }
16023 if (error == TRUE)
16024 return;
16025
16026 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16027 if (id == 1 || id == 2)
16028 {
16029 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16030 return;
16031 }
16032
Bram Moolenaar6561d522015-07-21 15:48:27 +020016033 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16034 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016035#endif
16036}
16037
16038/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016039 * "matcharg()" function
16040 */
16041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016042f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016043{
16044 if (rettv_list_alloc(rettv) == OK)
16045 {
16046#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016047 int id = get_tv_number(&argvars[0]);
16048 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016049
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016050 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016051 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016052 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16053 {
16054 list_append_string(rettv->vval.v_list,
16055 syn_id2name(m->hlg_id), -1);
16056 list_append_string(rettv->vval.v_list, m->pattern, -1);
16057 }
16058 else
16059 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016060 list_append_string(rettv->vval.v_list, NULL, -1);
16061 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016062 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016063 }
16064#endif
16065 }
16066}
16067
16068/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016069 * "matchdelete()" function
16070 */
16071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016072f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016073{
16074#ifdef FEAT_SEARCH_EXTRA
16075 rettv->vval.v_number = match_delete(curwin,
16076 (int)get_tv_number(&argvars[0]), TRUE);
16077#endif
16078}
16079
16080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016081 * "matchend()" function
16082 */
16083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016084f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085{
16086 find_some_match(argvars, rettv, 0);
16087}
16088
16089/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016090 * "matchlist()" function
16091 */
16092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016094{
16095 find_some_match(argvars, rettv, 3);
16096}
16097
16098/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016099 * "matchstr()" function
16100 */
16101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016102f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016103{
16104 find_some_match(argvars, rettv, 2);
16105}
16106
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016107/*
16108 * "matchstrpos()" function
16109 */
16110 static void
16111f_matchstrpos(typval_T *argvars, typval_T *rettv)
16112{
16113 find_some_match(argvars, rettv, 4);
16114}
16115
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016116static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016117
16118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016119max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016120{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016121 long n = 0;
16122 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016123 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016124
16125 if (argvars[0].v_type == VAR_LIST)
16126 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016127 list_T *l;
16128 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016129
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016130 l = argvars[0].vval.v_list;
16131 if (l != NULL)
16132 {
16133 li = l->lv_first;
16134 if (li != NULL)
16135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016136 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016137 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016138 {
16139 li = li->li_next;
16140 if (li == NULL)
16141 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016143 if (domax ? i > n : i < n)
16144 n = i;
16145 }
16146 }
16147 }
16148 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016149 else if (argvars[0].v_type == VAR_DICT)
16150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016151 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016152 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016154 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016155
16156 d = argvars[0].vval.v_dict;
16157 if (d != NULL)
16158 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016159 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016160 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016161 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016162 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016163 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016164 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016165 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016166 if (first)
16167 {
16168 n = i;
16169 first = FALSE;
16170 }
16171 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016172 n = i;
16173 }
16174 }
16175 }
16176 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016177 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016178 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016179 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016180}
16181
16182/*
16183 * "max()" function
16184 */
16185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016186f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016187{
16188 max_min(argvars, rettv, TRUE);
16189}
16190
16191/*
16192 * "min()" function
16193 */
16194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016195f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016196{
16197 max_min(argvars, rettv, FALSE);
16198}
16199
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016200static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016201
16202/*
16203 * Create the directory in which "dir" is located, and higher levels when
16204 * needed.
16205 */
16206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016207mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016208{
16209 char_u *p;
16210 char_u *updir;
16211 int r = FAIL;
16212
16213 /* Get end of directory name in "dir".
16214 * We're done when it's "/" or "c:/". */
16215 p = gettail_sep(dir);
16216 if (p <= get_past_head(dir))
16217 return OK;
16218
16219 /* If the directory exists we're done. Otherwise: create it.*/
16220 updir = vim_strnsave(dir, (int)(p - dir));
16221 if (updir == NULL)
16222 return FAIL;
16223 if (mch_isdir(updir))
16224 r = OK;
16225 else if (mkdir_recurse(updir, prot) == OK)
16226 r = vim_mkdir_emsg(updir, prot);
16227 vim_free(updir);
16228 return r;
16229}
16230
16231#ifdef vim_mkdir
16232/*
16233 * "mkdir()" function
16234 */
16235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016236f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016237{
16238 char_u *dir;
16239 char_u buf[NUMBUFLEN];
16240 int prot = 0755;
16241
16242 rettv->vval.v_number = FAIL;
16243 if (check_restricted() || check_secure())
16244 return;
16245
16246 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016247 if (*dir == NUL)
16248 rettv->vval.v_number = FAIL;
16249 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016250 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016251 if (*gettail(dir) == NUL)
16252 /* remove trailing slashes */
16253 *gettail_sep(dir) = NUL;
16254
16255 if (argvars[1].v_type != VAR_UNKNOWN)
16256 {
16257 if (argvars[2].v_type != VAR_UNKNOWN)
16258 prot = get_tv_number_chk(&argvars[2], NULL);
16259 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16260 mkdir_recurse(dir, prot);
16261 }
16262 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016263 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016264}
16265#endif
16266
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 * "mode()" function
16269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016271f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016273 char_u buf[3];
16274
16275 buf[1] = NUL;
16276 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278 if (VIsual_active)
16279 {
16280 if (VIsual_select)
16281 buf[0] = VIsual_mode + 's' - 'v';
16282 else
16283 buf[0] = VIsual_mode;
16284 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016285 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016286 || State == CONFIRM)
16287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016289 if (State == ASKMORE)
16290 buf[1] = 'm';
16291 else if (State == CONFIRM)
16292 buf[1] = '?';
16293 }
16294 else if (State == EXTERNCMD)
16295 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 else if (State & INSERT)
16297 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016298#ifdef FEAT_VREPLACE
16299 if (State & VREPLACE_FLAG)
16300 {
16301 buf[0] = 'R';
16302 buf[1] = 'v';
16303 }
16304 else
16305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 if (State & REPLACE_FLAG)
16307 buf[0] = 'R';
16308 else
16309 buf[0] = 'i';
16310 }
16311 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016313 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016314 if (exmode_active)
16315 buf[1] = 'v';
16316 }
16317 else if (exmode_active)
16318 {
16319 buf[0] = 'c';
16320 buf[1] = 'e';
16321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016325 if (finish_op)
16326 buf[1] = 'o';
16327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016329 /* Clear out the minor mode when the argument is not a non-zero number or
16330 * non-empty string. */
16331 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016332 buf[1] = NUL;
16333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016334 rettv->vval.v_string = vim_strsave(buf);
16335 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336}
16337
Bram Moolenaar429fa852013-04-15 12:27:36 +020016338#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016339/*
16340 * "mzeval()" function
16341 */
16342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016343f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016344{
16345 char_u *str;
16346 char_u buf[NUMBUFLEN];
16347
16348 str = get_tv_string_buf(&argvars[0], buf);
16349 do_mzeval(str, rettv);
16350}
Bram Moolenaar75676462013-01-30 14:55:42 +010016351
16352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016353mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016354{
16355 typval_T argvars[3];
16356
16357 argvars[0].v_type = VAR_STRING;
16358 argvars[0].vval.v_string = name;
16359 copy_tv(args, &argvars[1]);
16360 argvars[2].v_type = VAR_UNKNOWN;
16361 f_call(argvars, rettv);
16362 clear_tv(&argvars[1]);
16363}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016364#endif
16365
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367 * "nextnonblank()" function
16368 */
16369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016370f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371{
16372 linenr_T lnum;
16373
16374 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016376 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016377 {
16378 lnum = 0;
16379 break;
16380 }
16381 if (*skipwhite(ml_get(lnum)) != NUL)
16382 break;
16383 }
16384 rettv->vval.v_number = lnum;
16385}
16386
16387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 * "nr2char()" function
16389 */
16390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016391f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392{
16393 char_u buf[NUMBUFLEN];
16394
16395#ifdef FEAT_MBYTE
16396 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016397 {
16398 int utf8 = 0;
16399
16400 if (argvars[1].v_type != VAR_UNKNOWN)
16401 utf8 = get_tv_number_chk(&argvars[1], NULL);
16402 if (utf8)
16403 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16404 else
16405 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407 else
16408#endif
16409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016410 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411 buf[1] = NUL;
16412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016413 rettv->v_type = VAR_STRING;
16414 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016415}
16416
16417/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016418 * "or(expr, expr)" function
16419 */
16420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016421f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016422{
16423 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16424 | get_tv_number_chk(&argvars[1], NULL);
16425}
16426
16427/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016428 * "pathshorten()" function
16429 */
16430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016431f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016432{
16433 char_u *p;
16434
16435 rettv->v_type = VAR_STRING;
16436 p = get_tv_string_chk(&argvars[0]);
16437 if (p == NULL)
16438 rettv->vval.v_string = NULL;
16439 else
16440 {
16441 p = vim_strsave(p);
16442 rettv->vval.v_string = p;
16443 if (p != NULL)
16444 shorten_dir(p);
16445 }
16446}
16447
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016448#ifdef FEAT_PERL
16449/*
16450 * "perleval()" function
16451 */
16452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016453f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016454{
16455 char_u *str;
16456 char_u buf[NUMBUFLEN];
16457
16458 str = get_tv_string_buf(&argvars[0], buf);
16459 do_perleval(str, rettv);
16460}
16461#endif
16462
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016463#ifdef FEAT_FLOAT
16464/*
16465 * "pow()" function
16466 */
16467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016468f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016469{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016470 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016471
16472 rettv->v_type = VAR_FLOAT;
16473 if (get_float_arg(argvars, &fx) == OK
16474 && get_float_arg(&argvars[1], &fy) == OK)
16475 rettv->vval.v_float = pow(fx, fy);
16476 else
16477 rettv->vval.v_float = 0.0;
16478}
16479#endif
16480
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016481/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016482 * "prevnonblank()" function
16483 */
16484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016485f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486{
16487 linenr_T lnum;
16488
16489 lnum = get_tv_lnum(argvars);
16490 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16491 lnum = 0;
16492 else
16493 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16494 --lnum;
16495 rettv->vval.v_number = lnum;
16496}
16497
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016498/* This dummy va_list is here because:
16499 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16500 * - locally in the function results in a "used before set" warning
16501 * - using va_start() to initialize it gives "function with fixed args" error */
16502static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016503
Bram Moolenaar8c711452005-01-14 21:53:12 +000016504/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016505 * "printf()" function
16506 */
16507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016508f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016509{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016510 char_u buf[NUMBUFLEN];
16511 int len;
16512 char_u *s;
16513 int saved_did_emsg = did_emsg;
16514 char *fmt;
16515
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016516 rettv->v_type = VAR_STRING;
16517 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016518
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016519 /* Get the required length, allocate the buffer and do it for real. */
16520 did_emsg = FALSE;
16521 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16522 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16523 if (!did_emsg)
16524 {
16525 s = alloc(len + 1);
16526 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016527 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016528 rettv->vval.v_string = s;
16529 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016530 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016531 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016532 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016533}
16534
16535/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016536 * "pumvisible()" function
16537 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016539f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016540{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016541#ifdef FEAT_INS_EXPAND
16542 if (pum_visible())
16543 rettv->vval.v_number = 1;
16544#endif
16545}
16546
Bram Moolenaardb913952012-06-29 12:54:53 +020016547#ifdef FEAT_PYTHON3
16548/*
16549 * "py3eval()" function
16550 */
16551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016552f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016553{
16554 char_u *str;
16555 char_u buf[NUMBUFLEN];
16556
16557 str = get_tv_string_buf(&argvars[0], buf);
16558 do_py3eval(str, rettv);
16559}
16560#endif
16561
16562#ifdef FEAT_PYTHON
16563/*
16564 * "pyeval()" function
16565 */
16566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016567f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016568{
16569 char_u *str;
16570 char_u buf[NUMBUFLEN];
16571
16572 str = get_tv_string_buf(&argvars[0], buf);
16573 do_pyeval(str, rettv);
16574}
16575#endif
16576
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016578 * "range()" function
16579 */
16580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016581f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016582{
16583 long start;
16584 long end;
16585 long stride = 1;
16586 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016587 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016589 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016590 if (argvars[1].v_type == VAR_UNKNOWN)
16591 {
16592 end = start - 1;
16593 start = 0;
16594 }
16595 else
16596 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016597 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016598 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016599 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016600 }
16601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016602 if (error)
16603 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016604 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016605 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016606 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016607 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016608 else
16609 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016610 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016611 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016612 if (list_append_number(rettv->vval.v_list,
16613 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016614 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016615 }
16616}
16617
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016618/*
16619 * "readfile()" function
16620 */
16621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016622f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016623{
16624 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016625 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016626 char_u *fname;
16627 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16629 int io_size = sizeof(buf);
16630 int readlen; /* size of last fread() */
16631 char_u *prev = NULL; /* previously read bytes, if any */
16632 long prevlen = 0; /* length of data in prev */
16633 long prevsize = 0; /* size of prev buffer */
16634 long maxline = MAXLNUM;
16635 long cnt = 0;
16636 char_u *p; /* position in buf */
16637 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016639 if (argvars[1].v_type != VAR_UNKNOWN)
16640 {
16641 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16642 binary = TRUE;
16643 if (argvars[2].v_type != VAR_UNKNOWN)
16644 maxline = get_tv_number(&argvars[2]);
16645 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016646
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016647 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016649
16650 /* Always open the file in binary mode, library functions have a mind of
16651 * their own about CR-LF conversion. */
16652 fname = get_tv_string(&argvars[0]);
16653 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16654 {
16655 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16656 return;
16657 }
16658
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016659 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016660 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016661 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016662
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016663 /* This for loop processes what was read, but is also entered at end
16664 * of file so that either:
16665 * - an incomplete line gets written
16666 * - a "binary" file gets an empty line at the end if it ends in a
16667 * newline. */
16668 for (p = buf, start = buf;
16669 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16670 ++p)
16671 {
16672 if (*p == '\n' || readlen <= 0)
16673 {
16674 listitem_T *li;
16675 char_u *s = NULL;
16676 long_u len = p - start;
16677
16678 /* Finished a line. Remove CRs before NL. */
16679 if (readlen > 0 && !binary)
16680 {
16681 while (len > 0 && start[len - 1] == '\r')
16682 --len;
16683 /* removal may cross back to the "prev" string */
16684 if (len == 0)
16685 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16686 --prevlen;
16687 }
16688 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016689 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016690 else
16691 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016692 /* Change "prev" buffer to be the right size. This way
16693 * the bytes are only copied once, and very long lines are
16694 * allocated only once. */
16695 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016697 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016698 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016699 prev = NULL; /* the list will own the string */
16700 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016701 }
16702 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016703 if (s == NULL)
16704 {
16705 do_outofmem_msg((long_u) prevlen + len + 1);
16706 failed = TRUE;
16707 break;
16708 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016709
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016710 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016711 {
16712 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016713 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016714 break;
16715 }
16716 li->li_tv.v_type = VAR_STRING;
16717 li->li_tv.v_lock = 0;
16718 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016719 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016720
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016721 start = p + 1; /* step over newline */
16722 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016723 break;
16724 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016725 else if (*p == NUL)
16726 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016727#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016728 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16729 * when finding the BF and check the previous two bytes. */
16730 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016731 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016732 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16733 * + 1, these may be in the "prev" string. */
16734 char_u back1 = p >= buf + 1 ? p[-1]
16735 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16736 char_u back2 = p >= buf + 2 ? p[-2]
16737 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16738 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016739
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016740 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016741 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016742 char_u *dest = p - 2;
16743
16744 /* Usually a BOM is at the beginning of a file, and so at
16745 * the beginning of a line; then we can just step over it.
16746 */
16747 if (start == dest)
16748 start = p + 1;
16749 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016750 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016751 /* have to shuffle buf to close gap */
16752 int adjust_prevlen = 0;
16753
16754 if (dest < buf)
16755 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016756 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016757 dest = buf;
16758 }
16759 if (readlen > p - buf + 1)
16760 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16761 readlen -= 3 - adjust_prevlen;
16762 prevlen -= adjust_prevlen;
16763 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016764 }
16765 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016766 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016767#endif
16768 } /* for */
16769
16770 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16771 break;
16772 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016773 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016774 /* There's part of a line in buf, store it in "prev". */
16775 if (p - start + prevlen >= prevsize)
16776 {
16777 /* need bigger "prev" buffer */
16778 char_u *newprev;
16779
16780 /* A common use case is ordinary text files and "prev" gets a
16781 * fragment of a line, so the first allocation is made
16782 * small, to avoid repeatedly 'allocing' large and
16783 * 'reallocing' small. */
16784 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016785 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016786 else
16787 {
16788 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016789 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016790 prevsize = grow50pc > growmin ? grow50pc : growmin;
16791 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016792 newprev = prev == NULL ? alloc(prevsize)
16793 : vim_realloc(prev, prevsize);
16794 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016795 {
16796 do_outofmem_msg((long_u)prevsize);
16797 failed = TRUE;
16798 break;
16799 }
16800 prev = newprev;
16801 }
16802 /* Add the line part to end of "prev". */
16803 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016804 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016805 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016806 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016807
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016808 /*
16809 * For a negative line count use only the lines at the end of the file,
16810 * free the rest.
16811 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016812 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016813 while (cnt > -maxline)
16814 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016815 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016816 --cnt;
16817 }
16818
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016819 if (failed)
16820 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016821 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016822 /* readfile doc says an empty list is returned on error */
16823 rettv->vval.v_list = list_alloc();
16824 }
16825
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016826 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016827 fclose(fd);
16828}
16829
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016830#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016831static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016832
16833/*
16834 * Convert a List to proftime_T.
16835 * Return FAIL when there is something wrong.
16836 */
16837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016838list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016839{
16840 long n1, n2;
16841 int error = FALSE;
16842
16843 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16844 || arg->vval.v_list->lv_len != 2)
16845 return FAIL;
16846 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16847 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16848# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016849 tm->HighPart = n1;
16850 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016851# else
16852 tm->tv_sec = n1;
16853 tm->tv_usec = n2;
16854# endif
16855 return error ? FAIL : OK;
16856}
16857#endif /* FEAT_RELTIME */
16858
16859/*
16860 * "reltime()" function
16861 */
16862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016863f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016864{
16865#ifdef FEAT_RELTIME
16866 proftime_T res;
16867 proftime_T start;
16868
16869 if (argvars[0].v_type == VAR_UNKNOWN)
16870 {
16871 /* No arguments: get current time. */
16872 profile_start(&res);
16873 }
16874 else if (argvars[1].v_type == VAR_UNKNOWN)
16875 {
16876 if (list2proftime(&argvars[0], &res) == FAIL)
16877 return;
16878 profile_end(&res);
16879 }
16880 else
16881 {
16882 /* Two arguments: compute the difference. */
16883 if (list2proftime(&argvars[0], &start) == FAIL
16884 || list2proftime(&argvars[1], &res) == FAIL)
16885 return;
16886 profile_sub(&res, &start);
16887 }
16888
16889 if (rettv_list_alloc(rettv) == OK)
16890 {
16891 long n1, n2;
16892
16893# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016894 n1 = res.HighPart;
16895 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016896# else
16897 n1 = res.tv_sec;
16898 n2 = res.tv_usec;
16899# endif
16900 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16901 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16902 }
16903#endif
16904}
16905
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016906#ifdef FEAT_FLOAT
16907/*
16908 * "reltimefloat()" function
16909 */
16910 static void
16911f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16912{
16913# ifdef FEAT_RELTIME
16914 proftime_T tm;
16915# endif
16916
16917 rettv->v_type = VAR_FLOAT;
16918 rettv->vval.v_float = 0;
16919# ifdef FEAT_RELTIME
16920 if (list2proftime(&argvars[0], &tm) == OK)
16921 rettv->vval.v_float = profile_float(&tm);
16922# endif
16923}
16924#endif
16925
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016926/*
16927 * "reltimestr()" function
16928 */
16929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016930f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016931{
16932#ifdef FEAT_RELTIME
16933 proftime_T tm;
16934#endif
16935
16936 rettv->v_type = VAR_STRING;
16937 rettv->vval.v_string = NULL;
16938#ifdef FEAT_RELTIME
16939 if (list2proftime(&argvars[0], &tm) == OK)
16940 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16941#endif
16942}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016943
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016945static void make_connection(void);
16946static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947
16948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016949make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950{
16951 if (X_DISPLAY == NULL
16952# ifdef FEAT_GUI
16953 && !gui.in_use
16954# endif
16955 )
16956 {
16957 x_force_connect = TRUE;
16958 setup_term_clip();
16959 x_force_connect = FALSE;
16960 }
16961}
16962
16963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016964check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965{
16966 make_connection();
16967 if (X_DISPLAY == NULL)
16968 {
16969 EMSG(_("E240: No connection to Vim server"));
16970 return FAIL;
16971 }
16972 return OK;
16973}
16974#endif
16975
16976#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016978remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979{
16980 char_u *server_name;
16981 char_u *keys;
16982 char_u *r = NULL;
16983 char_u buf[NUMBUFLEN];
16984# ifdef WIN32
16985 HWND w;
16986# else
16987 Window w;
16988# endif
16989
16990 if (check_restricted() || check_secure())
16991 return;
16992
16993# ifdef FEAT_X11
16994 if (check_connection() == FAIL)
16995 return;
16996# endif
16997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 server_name = get_tv_string_chk(&argvars[0]);
16999 if (server_name == NULL)
17000 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001 keys = get_tv_string_buf(&argvars[1], buf);
17002# ifdef WIN32
17003 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17004# else
17005 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17006 < 0)
17007# endif
17008 {
17009 if (r != NULL)
17010 EMSG(r); /* sending worked but evaluation failed */
17011 else
17012 EMSG2(_("E241: Unable to send to %s"), server_name);
17013 return;
17014 }
17015
17016 rettv->vval.v_string = r;
17017
17018 if (argvars[2].v_type != VAR_UNKNOWN)
17019 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017020 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017021 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017022 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017024 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 v.di_tv.v_type = VAR_STRING;
17026 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 idvar = get_tv_string_chk(&argvars[2]);
17028 if (idvar != NULL)
17029 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017030 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 }
17032}
17033#endif
17034
17035/*
17036 * "remote_expr()" function
17037 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017039f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040{
17041 rettv->v_type = VAR_STRING;
17042 rettv->vval.v_string = NULL;
17043#ifdef FEAT_CLIENTSERVER
17044 remote_common(argvars, rettv, TRUE);
17045#endif
17046}
17047
17048/*
17049 * "remote_foreground()" function
17050 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017052f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054#ifdef FEAT_CLIENTSERVER
17055# ifdef WIN32
17056 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 {
17058 char_u *server_name = get_tv_string_chk(&argvars[0]);
17059
17060 if (server_name != NULL)
17061 serverForeground(server_name);
17062 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063# else
17064 /* Send a foreground() expression to the server. */
17065 argvars[1].v_type = VAR_STRING;
17066 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17067 argvars[2].v_type = VAR_UNKNOWN;
17068 remote_common(argvars, rettv, TRUE);
17069 vim_free(argvars[1].vval.v_string);
17070# endif
17071#endif
17072}
17073
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017075f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076{
17077#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017078 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 char_u *s = NULL;
17080# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017081 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017083 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084
17085 if (check_restricted() || check_secure())
17086 {
17087 rettv->vval.v_number = -1;
17088 return;
17089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017090 serverid = get_tv_string_chk(&argvars[0]);
17091 if (serverid == NULL)
17092 {
17093 rettv->vval.v_number = -1;
17094 return; /* type error; errmsg already given */
17095 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017097 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 if (n == 0)
17099 rettv->vval.v_number = -1;
17100 else
17101 {
17102 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17103 rettv->vval.v_number = (s != NULL);
17104 }
17105# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 if (check_connection() == FAIL)
17107 return;
17108
17109 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111# endif
17112
17113 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017115 char_u *retvar;
17116
Bram Moolenaar33570922005-01-25 22:26:29 +000017117 v.di_tv.v_type = VAR_STRING;
17118 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017119 retvar = get_tv_string_chk(&argvars[1]);
17120 if (retvar != NULL)
17121 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017122 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 }
17124#else
17125 rettv->vval.v_number = -1;
17126#endif
17127}
17128
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017130f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131{
17132 char_u *r = NULL;
17133
17134#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017135 char_u *serverid = get_tv_string_chk(&argvars[0]);
17136
17137 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138 {
17139# ifdef WIN32
17140 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017141 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017142
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017143 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 if (n != 0)
17145 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17146 if (r == NULL)
17147# else
17148 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150# endif
17151 EMSG(_("E277: Unable to read a server reply"));
17152 }
17153#endif
17154 rettv->v_type = VAR_STRING;
17155 rettv->vval.v_string = r;
17156}
17157
17158/*
17159 * "remote_send()" function
17160 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017162f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163{
17164 rettv->v_type = VAR_STRING;
17165 rettv->vval.v_string = NULL;
17166#ifdef FEAT_CLIENTSERVER
17167 remote_common(argvars, rettv, FALSE);
17168#endif
17169}
17170
17171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017172 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017173 */
17174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017175f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017176{
Bram Moolenaar33570922005-01-25 22:26:29 +000017177 list_T *l;
17178 listitem_T *item, *item2;
17179 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017180 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017181 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017182 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017183 dict_T *d;
17184 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017185 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017186
Bram Moolenaar8c711452005-01-14 21:53:12 +000017187 if (argvars[0].v_type == VAR_DICT)
17188 {
17189 if (argvars[2].v_type != VAR_UNKNOWN)
17190 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017191 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017192 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017194 key = get_tv_string_chk(&argvars[1]);
17195 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017197 di = dict_find(d, key, -1);
17198 if (di == NULL)
17199 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017200 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17201 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017202 {
17203 *rettv = di->di_tv;
17204 init_tv(&di->di_tv);
17205 dictitem_remove(d, di);
17206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017207 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017208 }
17209 }
17210 else if (argvars[0].v_type != VAR_LIST)
17211 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017212 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017213 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 int error = FALSE;
17216
17217 idx = get_tv_number_chk(&argvars[1], &error);
17218 if (error)
17219 ; /* type error: do nothing, errmsg already given */
17220 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017221 EMSGN(_(e_listidx), idx);
17222 else
17223 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017224 if (argvars[2].v_type == VAR_UNKNOWN)
17225 {
17226 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017227 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017228 *rettv = item->li_tv;
17229 vim_free(item);
17230 }
17231 else
17232 {
17233 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 end = get_tv_number_chk(&argvars[2], &error);
17235 if (error)
17236 ; /* type error: do nothing */
17237 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017238 EMSGN(_(e_listidx), end);
17239 else
17240 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017241 int cnt = 0;
17242
17243 for (li = item; li != NULL; li = li->li_next)
17244 {
17245 ++cnt;
17246 if (li == item2)
17247 break;
17248 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017249 if (li == NULL) /* didn't find "item2" after "item" */
17250 EMSG(_(e_invrange));
17251 else
17252 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017253 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017254 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017255 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017256 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017257 l->lv_first = item;
17258 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017259 item->li_prev = NULL;
17260 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017261 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017262 }
17263 }
17264 }
17265 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017266 }
17267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268}
17269
17270/*
17271 * "rename({from}, {to})" function
17272 */
17273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017274f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275{
17276 char_u buf[NUMBUFLEN];
17277
17278 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017281 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17282 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283}
17284
17285/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017286 * "repeat()" function
17287 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017289f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017290{
17291 char_u *p;
17292 int n;
17293 int slen;
17294 int len;
17295 char_u *r;
17296 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017297
17298 n = get_tv_number(&argvars[1]);
17299 if (argvars[0].v_type == VAR_LIST)
17300 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017301 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017302 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017303 if (list_extend(rettv->vval.v_list,
17304 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017305 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017306 }
17307 else
17308 {
17309 p = get_tv_string(&argvars[0]);
17310 rettv->v_type = VAR_STRING;
17311 rettv->vval.v_string = NULL;
17312
17313 slen = (int)STRLEN(p);
17314 len = slen * n;
17315 if (len <= 0)
17316 return;
17317
17318 r = alloc(len + 1);
17319 if (r != NULL)
17320 {
17321 for (i = 0; i < n; i++)
17322 mch_memmove(r + i * slen, p, (size_t)slen);
17323 r[len] = NUL;
17324 }
17325
17326 rettv->vval.v_string = r;
17327 }
17328}
17329
17330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331 * "resolve()" function
17332 */
17333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017334f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335{
17336 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017337#ifdef HAVE_READLINK
17338 char_u *buf = NULL;
17339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017341 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342#ifdef FEAT_SHORTCUT
17343 {
17344 char_u *v = NULL;
17345
17346 v = mch_resolve_shortcut(p);
17347 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017350 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 }
17352#else
17353# ifdef HAVE_READLINK
17354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 char_u *cpy;
17356 int len;
17357 char_u *remain = NULL;
17358 char_u *q;
17359 int is_relative_to_current = FALSE;
17360 int has_trailing_pathsep = FALSE;
17361 int limit = 100;
17362
17363 p = vim_strsave(p);
17364
17365 if (p[0] == '.' && (vim_ispathsep(p[1])
17366 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17367 is_relative_to_current = TRUE;
17368
17369 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017370 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017373 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
17376 q = getnextcomp(p);
17377 if (*q != NUL)
17378 {
17379 /* Separate the first path component in "p", and keep the
17380 * remainder (beginning with the path separator). */
17381 remain = vim_strsave(q - 1);
17382 q[-1] = NUL;
17383 }
17384
Bram Moolenaard9462e32011-04-11 21:35:11 +020017385 buf = alloc(MAXPATHL + 1);
17386 if (buf == NULL)
17387 goto fail;
17388
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 for (;;)
17390 {
17391 for (;;)
17392 {
17393 len = readlink((char *)p, (char *)buf, MAXPATHL);
17394 if (len <= 0)
17395 break;
17396 buf[len] = NUL;
17397
17398 if (limit-- == 0)
17399 {
17400 vim_free(p);
17401 vim_free(remain);
17402 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 goto fail;
17405 }
17406
17407 /* Ensure that the result will have a trailing path separator
17408 * if the argument has one. */
17409 if (remain == NULL && has_trailing_pathsep)
17410 add_pathsep(buf);
17411
17412 /* Separate the first path component in the link value and
17413 * concatenate the remainders. */
17414 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17415 if (*q != NUL)
17416 {
17417 if (remain == NULL)
17418 remain = vim_strsave(q - 1);
17419 else
17420 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017421 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 if (cpy != NULL)
17423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424 vim_free(remain);
17425 remain = cpy;
17426 }
17427 }
17428 q[-1] = NUL;
17429 }
17430
17431 q = gettail(p);
17432 if (q > p && *q == NUL)
17433 {
17434 /* Ignore trailing path separator. */
17435 q[-1] = NUL;
17436 q = gettail(p);
17437 }
17438 if (q > p && !mch_isFullName(buf))
17439 {
17440 /* symlink is relative to directory of argument */
17441 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17442 if (cpy != NULL)
17443 {
17444 STRCPY(cpy, p);
17445 STRCPY(gettail(cpy), buf);
17446 vim_free(p);
17447 p = cpy;
17448 }
17449 }
17450 else
17451 {
17452 vim_free(p);
17453 p = vim_strsave(buf);
17454 }
17455 }
17456
17457 if (remain == NULL)
17458 break;
17459
17460 /* Append the first path component of "remain" to "p". */
17461 q = getnextcomp(remain + 1);
17462 len = q - remain - (*q != NUL);
17463 cpy = vim_strnsave(p, STRLEN(p) + len);
17464 if (cpy != NULL)
17465 {
17466 STRNCAT(cpy, remain, len);
17467 vim_free(p);
17468 p = cpy;
17469 }
17470 /* Shorten "remain". */
17471 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017472 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 else
17474 {
17475 vim_free(remain);
17476 remain = NULL;
17477 }
17478 }
17479
17480 /* If the result is a relative path name, make it explicitly relative to
17481 * the current directory if and only if the argument had this form. */
17482 if (!vim_ispathsep(*p))
17483 {
17484 if (is_relative_to_current
17485 && *p != NUL
17486 && !(p[0] == '.'
17487 && (p[1] == NUL
17488 || vim_ispathsep(p[1])
17489 || (p[1] == '.'
17490 && (p[2] == NUL
17491 || vim_ispathsep(p[2]))))))
17492 {
17493 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017494 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 if (cpy != NULL)
17496 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 vim_free(p);
17498 p = cpy;
17499 }
17500 }
17501 else if (!is_relative_to_current)
17502 {
17503 /* Strip leading "./". */
17504 q = p;
17505 while (q[0] == '.' && vim_ispathsep(q[1]))
17506 q += 2;
17507 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017508 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 }
17510 }
17511
17512 /* Ensure that the result will have no trailing path separator
17513 * if the argument had none. But keep "/" or "//". */
17514 if (!has_trailing_pathsep)
17515 {
17516 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017517 if (after_pathsep(p, q))
17518 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 }
17520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017521 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 }
17523# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525# endif
17526#endif
17527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017528 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529
17530#ifdef HAVE_READLINK
17531fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017532 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017534 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535}
17536
17537/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 */
17540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017541f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542{
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 list_T *l;
17544 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545
Bram Moolenaar0d660222005-01-07 21:51:51 +000017546 if (argvars[0].v_type != VAR_LIST)
17547 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017548 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017549 && !tv_check_lock(l->lv_lock,
17550 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 {
17552 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017553 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017554 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 while (li != NULL)
17556 {
17557 ni = li->li_prev;
17558 list_append(l, li);
17559 li = ni;
17560 }
17561 rettv->vval.v_list = l;
17562 rettv->v_type = VAR_LIST;
17563 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017564 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566}
17567
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017568#define SP_NOMOVE 0x01 /* don't move cursor */
17569#define SP_REPEAT 0x02 /* repeat to find outer pair */
17570#define SP_RETCOUNT 0x04 /* return matchcount */
17571#define SP_SETPCMARK 0x08 /* set previous context mark */
17572#define SP_START 0x10 /* accept match at start position */
17573#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17574#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017575#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017576
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017577static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578
17579/*
17580 * Get flags for a search function.
17581 * Possibly sets "p_ws".
17582 * Returns BACKWARD, FORWARD or zero (for an error).
17583 */
17584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017585get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586{
17587 int dir = FORWARD;
17588 char_u *flags;
17589 char_u nbuf[NUMBUFLEN];
17590 int mask;
17591
17592 if (varp->v_type != VAR_UNKNOWN)
17593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017594 flags = get_tv_string_buf_chk(varp, nbuf);
17595 if (flags == NULL)
17596 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017597 while (*flags != NUL)
17598 {
17599 switch (*flags)
17600 {
17601 case 'b': dir = BACKWARD; break;
17602 case 'w': p_ws = TRUE; break;
17603 case 'W': p_ws = FALSE; break;
17604 default: mask = 0;
17605 if (flagsp != NULL)
17606 switch (*flags)
17607 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017608 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017609 case 'e': mask = SP_END; break;
17610 case 'm': mask = SP_RETCOUNT; break;
17611 case 'n': mask = SP_NOMOVE; break;
17612 case 'p': mask = SP_SUBPAT; break;
17613 case 'r': mask = SP_REPEAT; break;
17614 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017615 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017616 }
17617 if (mask == 0)
17618 {
17619 EMSG2(_(e_invarg2), flags);
17620 dir = 0;
17621 }
17622 else
17623 *flagsp |= mask;
17624 }
17625 if (dir == 0)
17626 break;
17627 ++flags;
17628 }
17629 }
17630 return dir;
17631}
17632
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017634 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017637search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017638{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017639 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017640 char_u *pat;
17641 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017642 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 int save_p_ws = p_ws;
17644 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017645 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017646 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017647 proftime_T tm;
17648#ifdef FEAT_RELTIME
17649 long time_limit = 0;
17650#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017651 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017652 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017654 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017655 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017656 if (dir == 0)
17657 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017658 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017659 if (flags & SP_START)
17660 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017661 if (flags & SP_END)
17662 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017663 if (flags & SP_COLUMN)
17664 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017665
Bram Moolenaar76929292008-01-06 19:07:36 +000017666 /* Optional arguments: line number to stop searching and timeout. */
17667 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017668 {
17669 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17670 if (lnum_stop < 0)
17671 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017672#ifdef FEAT_RELTIME
17673 if (argvars[3].v_type != VAR_UNKNOWN)
17674 {
17675 time_limit = get_tv_number_chk(&argvars[3], NULL);
17676 if (time_limit < 0)
17677 goto theend;
17678 }
17679#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017680 }
17681
Bram Moolenaar76929292008-01-06 19:07:36 +000017682#ifdef FEAT_RELTIME
17683 /* Set the time limit, if there is one. */
17684 profile_setlimit(time_limit, &tm);
17685#endif
17686
Bram Moolenaar231334e2005-07-25 20:46:57 +000017687 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017688 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017689 * Check to make sure only those flags are set.
17690 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17691 * flags cannot be set. Check for that condition also.
17692 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017693 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017694 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017697 goto theend;
17698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017700 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017701 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017702 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017703 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017705 if (flags & SP_SUBPAT)
17706 retval = subpatnum;
17707 else
17708 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017709 if (flags & SP_SETPCMARK)
17710 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017712 if (match_pos != NULL)
17713 {
17714 /* Store the match cursor position */
17715 match_pos->lnum = pos.lnum;
17716 match_pos->col = pos.col + 1;
17717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 /* "/$" will put the cursor after the end of the line, may need to
17719 * correct that here */
17720 check_cursor();
17721 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017722
17723 /* If 'n' flag is used: restore cursor position. */
17724 if (flags & SP_NOMOVE)
17725 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017726 else
17727 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017728theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017730
17731 return retval;
17732}
17733
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017734#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017735
17736/*
17737 * round() is not in C90, use ceil() or floor() instead.
17738 */
17739 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017740vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017741{
17742 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17743}
17744
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017745/*
17746 * "round({float})" function
17747 */
17748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017749f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017750{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017751 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017752
17753 rettv->v_type = VAR_FLOAT;
17754 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017755 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017756 else
17757 rettv->vval.v_float = 0.0;
17758}
17759#endif
17760
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017761/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017762 * "screenattr()" function
17763 */
17764 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017765f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017766{
17767 int row;
17768 int col;
17769 int c;
17770
17771 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17772 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17773 if (row < 0 || row >= screen_Rows
17774 || col < 0 || col >= screen_Columns)
17775 c = -1;
17776 else
17777 c = ScreenAttrs[LineOffset[row] + col];
17778 rettv->vval.v_number = c;
17779}
17780
17781/*
17782 * "screenchar()" function
17783 */
17784 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017785f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017786{
17787 int row;
17788 int col;
17789 int off;
17790 int c;
17791
17792 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17793 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17794 if (row < 0 || row >= screen_Rows
17795 || col < 0 || col >= screen_Columns)
17796 c = -1;
17797 else
17798 {
17799 off = LineOffset[row] + col;
17800#ifdef FEAT_MBYTE
17801 if (enc_utf8 && ScreenLinesUC[off] != 0)
17802 c = ScreenLinesUC[off];
17803 else
17804#endif
17805 c = ScreenLines[off];
17806 }
17807 rettv->vval.v_number = c;
17808}
17809
17810/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017811 * "screencol()" function
17812 *
17813 * First column is 1 to be consistent with virtcol().
17814 */
17815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017816f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017817{
17818 rettv->vval.v_number = screen_screencol() + 1;
17819}
17820
17821/*
17822 * "screenrow()" function
17823 */
17824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017825f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017826{
17827 rettv->vval.v_number = screen_screenrow() + 1;
17828}
17829
17830/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017831 * "search()" function
17832 */
17833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017834f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017835{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017836 int flags = 0;
17837
17838 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839}
17840
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017842 * "searchdecl()" function
17843 */
17844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017845f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017846{
17847 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017848 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017849 int error = FALSE;
17850 char_u *name;
17851
17852 rettv->vval.v_number = 1; /* default: FAIL */
17853
17854 name = get_tv_string_chk(&argvars[0]);
17855 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017856 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017857 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017858 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17859 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17860 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017861 if (!error && name != NULL)
17862 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017863 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017864}
17865
17866/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017870searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871{
17872 char_u *spat, *mpat, *epat;
17873 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 int dir;
17876 int flags = 0;
17877 char_u nbuf1[NUMBUFLEN];
17878 char_u nbuf2[NUMBUFLEN];
17879 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017880 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017881 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017882 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017885 spat = get_tv_string_chk(&argvars[0]);
17886 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17887 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17888 if (spat == NULL || mpat == NULL || epat == NULL)
17889 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 /* Handle the optional fourth argument: flags */
17892 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017893 if (dir == 0)
17894 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017895
17896 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017897 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17898 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017899 if ((flags & (SP_END | SP_SUBPAT)) != 0
17900 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017901 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017902 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017903 goto theend;
17904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017906 /* Using 'r' implies 'W', otherwise it doesn't work. */
17907 if (flags & SP_REPEAT)
17908 p_ws = FALSE;
17909
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017910 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017911 if (argvars[3].v_type == VAR_UNKNOWN
17912 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 skip = (char_u *)"";
17914 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017916 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017917 if (argvars[5].v_type != VAR_UNKNOWN)
17918 {
17919 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17920 if (lnum_stop < 0)
17921 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017922#ifdef FEAT_RELTIME
17923 if (argvars[6].v_type != VAR_UNKNOWN)
17924 {
17925 time_limit = get_tv_number_chk(&argvars[6], NULL);
17926 if (time_limit < 0)
17927 goto theend;
17928 }
17929#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017930 }
17931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017932 if (skip == NULL)
17933 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017935 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017936 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017937
17938theend:
17939 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017940
17941 return retval;
17942}
17943
17944/*
17945 * "searchpair()" function
17946 */
17947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017948f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017949{
17950 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17951}
17952
17953/*
17954 * "searchpairpos()" function
17955 */
17956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017957f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017958{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017959 pos_T match_pos;
17960 int lnum = 0;
17961 int col = 0;
17962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017963 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017964 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017965
17966 if (searchpair_cmn(argvars, &match_pos) > 0)
17967 {
17968 lnum = match_pos.lnum;
17969 col = match_pos.col;
17970 }
17971
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017972 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17973 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017974}
17975
17976/*
17977 * Search for a start/middle/end thing.
17978 * Used by searchpair(), see its documentation for the details.
17979 * Returns 0 or -1 for no match,
17980 */
17981 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017982do_searchpair(
17983 char_u *spat, /* start pattern */
17984 char_u *mpat, /* middle pattern */
17985 char_u *epat, /* end pattern */
17986 int dir, /* BACKWARD or FORWARD */
17987 char_u *skip, /* skip expression */
17988 int flags, /* SP_SETPCMARK and other SP_ values */
17989 pos_T *match_pos,
17990 linenr_T lnum_stop, /* stop at this line if not zero */
17991 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017992{
17993 char_u *save_cpo;
17994 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17995 long retval = 0;
17996 pos_T pos;
17997 pos_T firstpos;
17998 pos_T foundpos;
17999 pos_T save_cursor;
18000 pos_T save_pos;
18001 int n;
18002 int r;
18003 int nest = 1;
18004 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018005 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018006 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018007
18008 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18009 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018010 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018011
Bram Moolenaar76929292008-01-06 19:07:36 +000018012#ifdef FEAT_RELTIME
18013 /* Set the time limit, if there is one. */
18014 profile_setlimit(time_limit, &tm);
18015#endif
18016
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018017 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18018 * start/middle/end (pat3, for the top pair). */
18019 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18020 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18021 if (pat2 == NULL || pat3 == NULL)
18022 goto theend;
18023 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18024 if (*mpat == NUL)
18025 STRCPY(pat3, pat2);
18026 else
18027 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18028 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018029 if (flags & SP_START)
18030 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018031
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 save_cursor = curwin->w_cursor;
18033 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018034 clearpos(&firstpos);
18035 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 pat = pat3;
18037 for (;;)
18038 {
18039 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018040 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18042 /* didn't find it or found the first match again: FAIL */
18043 break;
18044
18045 if (firstpos.lnum == 0)
18046 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018047 if (equalpos(pos, foundpos))
18048 {
18049 /* Found the same position again. Can happen with a pattern that
18050 * has "\zs" at the end and searching backwards. Advance one
18051 * character and try again. */
18052 if (dir == BACKWARD)
18053 decl(&pos);
18054 else
18055 incl(&pos);
18056 }
18057 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018059 /* clear the start flag to avoid getting stuck here */
18060 options &= ~SEARCH_START;
18061
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 /* If the skip pattern matches, ignore this match. */
18063 if (*skip != NUL)
18064 {
18065 save_pos = curwin->w_cursor;
18066 curwin->w_cursor = pos;
18067 r = eval_to_bool(skip, &err, NULL, FALSE);
18068 curwin->w_cursor = save_pos;
18069 if (err)
18070 {
18071 /* Evaluating {skip} caused an error, break here. */
18072 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018073 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 break;
18075 }
18076 if (r)
18077 continue;
18078 }
18079
18080 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18081 {
18082 /* Found end when searching backwards or start when searching
18083 * forward: nested pair. */
18084 ++nest;
18085 pat = pat2; /* nested, don't search for middle */
18086 }
18087 else
18088 {
18089 /* Found end when searching forward or start when searching
18090 * backward: end of (nested) pair; or found middle in outer pair. */
18091 if (--nest == 1)
18092 pat = pat3; /* outer level, search for middle */
18093 }
18094
18095 if (nest == 0)
18096 {
18097 /* Found the match: return matchcount or line number. */
18098 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018099 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018101 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018102 if (flags & SP_SETPCMARK)
18103 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 curwin->w_cursor = pos;
18105 if (!(flags & SP_REPEAT))
18106 break;
18107 nest = 1; /* search for next unmatched */
18108 }
18109 }
18110
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018111 if (match_pos != NULL)
18112 {
18113 /* Store the match cursor position */
18114 match_pos->lnum = curwin->w_cursor.lnum;
18115 match_pos->col = curwin->w_cursor.col + 1;
18116 }
18117
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018119 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120 curwin->w_cursor = save_cursor;
18121
18122theend:
18123 vim_free(pat2);
18124 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018125 if (p_cpo == empty_option)
18126 p_cpo = save_cpo;
18127 else
18128 /* Darn, evaluating the {skip} expression changed the value. */
18129 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018130
18131 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132}
18133
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018134/*
18135 * "searchpos()" function
18136 */
18137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018138f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018139{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018140 pos_T match_pos;
18141 int lnum = 0;
18142 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018143 int n;
18144 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018145
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018146 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018147 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018148
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018149 n = search_cmn(argvars, &match_pos, &flags);
18150 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018151 {
18152 lnum = match_pos.lnum;
18153 col = match_pos.col;
18154 }
18155
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018156 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18157 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018158 if (flags & SP_SUBPAT)
18159 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018160}
18161
Bram Moolenaar0d660222005-01-07 21:51:51 +000018162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018163f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018165#ifdef FEAT_CLIENTSERVER
18166 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 char_u *server = get_tv_string_chk(&argvars[0]);
18168 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169
Bram Moolenaar0d660222005-01-07 21:51:51 +000018170 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018171 if (server == NULL || reply == NULL)
18172 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 if (check_restricted() || check_secure())
18174 return;
18175# ifdef FEAT_X11
18176 if (check_connection() == FAIL)
18177 return;
18178# endif
18179
18180 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018182 EMSG(_("E258: Unable to send to client"));
18183 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018185 rettv->vval.v_number = 0;
18186#else
18187 rettv->vval.v_number = -1;
18188#endif
18189}
18190
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018192f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018193{
18194 char_u *r = NULL;
18195
18196#ifdef FEAT_CLIENTSERVER
18197# ifdef WIN32
18198 r = serverGetVimNames();
18199# else
18200 make_connection();
18201 if (X_DISPLAY != NULL)
18202 r = serverGetVimNames(X_DISPLAY);
18203# endif
18204#endif
18205 rettv->v_type = VAR_STRING;
18206 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207}
18208
18209/*
18210 * "setbufvar()" function
18211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018213f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214{
18215 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018218 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 char_u nbuf[NUMBUFLEN];
18220
18221 if (check_restricted() || check_secure())
18222 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018223 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18224 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018225 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 varp = &argvars[2];
18227
18228 if (buf != NULL && varname != NULL && varp != NULL)
18229 {
18230 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232
18233 if (*varname == '&')
18234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018235 long numval;
18236 char_u *strval;
18237 int error = FALSE;
18238
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018240 numval = get_tv_number_chk(varp, &error);
18241 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018242 if (!error && strval != NULL)
18243 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 }
18245 else
18246 {
18247 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18248 if (bufvarname != NULL)
18249 {
18250 STRCPY(bufvarname, "b:");
18251 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018252 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 vim_free(bufvarname);
18254 }
18255 }
18256
18257 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260}
18261
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018263f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018264{
18265 dict_T *d;
18266 dictitem_T *di;
18267 char_u *csearch;
18268
18269 if (argvars[0].v_type != VAR_DICT)
18270 {
18271 EMSG(_(e_dictreq));
18272 return;
18273 }
18274
18275 if ((d = argvars[0].vval.v_dict) != NULL)
18276 {
18277 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18278 if (csearch != NULL)
18279 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018280#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018281 if (enc_utf8)
18282 {
18283 int pcc[MAX_MCO];
18284 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018285
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018286 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18287 }
18288 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018289#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018290 set_last_csearch(PTR2CHAR(csearch),
18291 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018292 }
18293
18294 di = dict_find(d, (char_u *)"forward", -1);
18295 if (di != NULL)
18296 set_csearch_direction(get_tv_number(&di->di_tv)
18297 ? FORWARD : BACKWARD);
18298
18299 di = dict_find(d, (char_u *)"until", -1);
18300 if (di != NULL)
18301 set_csearch_until(!!get_tv_number(&di->di_tv));
18302 }
18303}
18304
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305/*
18306 * "setcmdpos()" function
18307 */
18308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018309f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 int pos = (int)get_tv_number(&argvars[0]) - 1;
18312
18313 if (pos >= 0)
18314 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315}
18316
18317/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018318 * "setfperm({fname}, {mode})" function
18319 */
18320 static void
18321f_setfperm(typval_T *argvars, typval_T *rettv)
18322{
18323 char_u *fname;
18324 char_u modebuf[NUMBUFLEN];
18325 char_u *mode_str;
18326 int i;
18327 int mask;
18328 int mode = 0;
18329
18330 rettv->vval.v_number = 0;
18331 fname = get_tv_string_chk(&argvars[0]);
18332 if (fname == NULL)
18333 return;
18334 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18335 if (mode_str == NULL)
18336 return;
18337 if (STRLEN(mode_str) != 9)
18338 {
18339 EMSG2(_(e_invarg2), mode_str);
18340 return;
18341 }
18342
18343 mask = 1;
18344 for (i = 8; i >= 0; --i)
18345 {
18346 if (mode_str[i] != '-')
18347 mode |= mask;
18348 mask = mask << 1;
18349 }
18350 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18351}
18352
18353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354 * "setline()" function
18355 */
18356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018357f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358{
18359 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018360 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018361 list_T *l = NULL;
18362 listitem_T *li = NULL;
18363 long added = 0;
18364 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018366 lnum = get_tv_lnum(&argvars[0]);
18367 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018369 l = argvars[1].vval.v_list;
18370 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018372 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018373 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018374
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018375 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018376 for (;;)
18377 {
18378 if (l != NULL)
18379 {
18380 /* list argument, get next string */
18381 if (li == NULL)
18382 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018383 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018384 li = li->li_next;
18385 }
18386
18387 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018388 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018389 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018390
18391 /* When coming here from Insert mode, sync undo, so that this can be
18392 * undone separately from what was previously inserted. */
18393 if (u_sync_once == 2)
18394 {
18395 u_sync_once = 1; /* notify that u_sync() was called */
18396 u_sync(TRUE);
18397 }
18398
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018399 if (lnum <= curbuf->b_ml.ml_line_count)
18400 {
18401 /* existing line, replace it */
18402 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18403 {
18404 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018405 if (lnum == curwin->w_cursor.lnum)
18406 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018407 rettv->vval.v_number = 0; /* OK */
18408 }
18409 }
18410 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18411 {
18412 /* lnum is one past the last line, append the line */
18413 ++added;
18414 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18415 rettv->vval.v_number = 0; /* OK */
18416 }
18417
18418 if (l == NULL) /* only one string argument */
18419 break;
18420 ++lnum;
18421 }
18422
18423 if (added > 0)
18424 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425}
18426
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018427static 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 +000018428
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018430 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018431 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018433set_qf_ll_list(
18434 win_T *wp UNUSED,
18435 typval_T *list_arg UNUSED,
18436 typval_T *action_arg UNUSED,
18437 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018438{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018439#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018440 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018441 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018442 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018443#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018444
Bram Moolenaar2641f772005-03-25 21:58:17 +000018445 rettv->vval.v_number = -1;
18446
18447#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018448 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018449 EMSG(_(e_listreq));
18450 else
18451 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018452 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018453
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018454 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018455 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018456 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018457 if (act == NULL)
18458 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018459 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018460 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018461 else
18462 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018463 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018464 else if (action_arg->v_type == VAR_UNKNOWN)
18465 action = ' ';
18466 else
18467 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018468
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018469 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018470 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018471 rettv->vval.v_number = 0;
18472 }
18473#endif
18474}
18475
18476/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018477 * "setloclist()" function
18478 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018480f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018481{
18482 win_T *win;
18483
18484 rettv->vval.v_number = -1;
18485
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018486 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018487 if (win != NULL)
18488 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18489}
18490
18491/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018492 * "setmatches()" function
18493 */
18494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018495f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018496{
18497#ifdef FEAT_SEARCH_EXTRA
18498 list_T *l;
18499 listitem_T *li;
18500 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018501 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018502
18503 rettv->vval.v_number = -1;
18504 if (argvars[0].v_type != VAR_LIST)
18505 {
18506 EMSG(_(e_listreq));
18507 return;
18508 }
18509 if ((l = argvars[0].vval.v_list) != NULL)
18510 {
18511
18512 /* To some extent make sure that we are dealing with a list from
18513 * "getmatches()". */
18514 li = l->lv_first;
18515 while (li != NULL)
18516 {
18517 if (li->li_tv.v_type != VAR_DICT
18518 || (d = li->li_tv.vval.v_dict) == NULL)
18519 {
18520 EMSG(_(e_invarg));
18521 return;
18522 }
18523 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018524 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18525 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018526 && dict_find(d, (char_u *)"priority", -1) != NULL
18527 && dict_find(d, (char_u *)"id", -1) != NULL))
18528 {
18529 EMSG(_(e_invarg));
18530 return;
18531 }
18532 li = li->li_next;
18533 }
18534
18535 clear_matches(curwin);
18536 li = l->lv_first;
18537 while (li != NULL)
18538 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018539 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018540 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018541 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018542 char_u *group;
18543 int priority;
18544 int id;
18545 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018546
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018547 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018548 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18549 {
18550 if (s == NULL)
18551 {
18552 s = list_alloc();
18553 if (s == NULL)
18554 return;
18555 }
18556
18557 /* match from matchaddpos() */
18558 for (i = 1; i < 9; i++)
18559 {
18560 sprintf((char *)buf, (char *)"pos%d", i);
18561 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18562 {
18563 if (di->di_tv.v_type != VAR_LIST)
18564 return;
18565
18566 list_append_tv(s, &di->di_tv);
18567 s->lv_refcount++;
18568 }
18569 else
18570 break;
18571 }
18572 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018573
18574 group = get_dict_string(d, (char_u *)"group", FALSE);
18575 priority = (int)get_dict_number(d, (char_u *)"priority");
18576 id = (int)get_dict_number(d, (char_u *)"id");
18577 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18578 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18579 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018580 if (i == 0)
18581 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018582 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018583 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018584 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018585 }
18586 else
18587 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018588 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018589 list_unref(s);
18590 s = NULL;
18591 }
18592
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018593 li = li->li_next;
18594 }
18595 rettv->vval.v_number = 0;
18596 }
18597#endif
18598}
18599
18600/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018601 * "setpos()" function
18602 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018604f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018605{
18606 pos_T pos;
18607 int fnum;
18608 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018609 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018610
Bram Moolenaar08250432008-02-13 11:42:46 +000018611 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018612 name = get_tv_string_chk(argvars);
18613 if (name != NULL)
18614 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018615 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018616 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018617 if (--pos.col < 0)
18618 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018619 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018620 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018621 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018622 if (fnum == curbuf->b_fnum)
18623 {
18624 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018625 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018626 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018627 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018628 curwin->w_set_curswant = FALSE;
18629 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018630 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018631 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018632 }
18633 else
18634 EMSG(_(e_invarg));
18635 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018636 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18637 {
18638 /* set mark */
18639 if (setmark_pos(name[1], &pos, fnum) == OK)
18640 rettv->vval.v_number = 0;
18641 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018642 else
18643 EMSG(_(e_invarg));
18644 }
18645 }
18646}
18647
18648/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018649 * "setqflist()" function
18650 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018652f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018653{
18654 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18655}
18656
18657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 * "setreg()" function
18659 */
18660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018661f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662{
18663 int regname;
18664 char_u *strregname;
18665 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018666 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667 int append;
18668 char_u yank_type;
18669 long block_len;
18670
18671 block_len = -1;
18672 yank_type = MAUTO;
18673 append = FALSE;
18674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018675 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018678 if (strregname == NULL)
18679 return; /* type error; errmsg already given */
18680 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 if (regname == 0 || regname == '@')
18682 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018684 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018686 stropt = get_tv_string_chk(&argvars[2]);
18687 if (stropt == NULL)
18688 return; /* type error */
18689 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 switch (*stropt)
18691 {
18692 case 'a': case 'A': /* append */
18693 append = TRUE;
18694 break;
18695 case 'v': case 'c': /* character-wise selection */
18696 yank_type = MCHAR;
18697 break;
18698 case 'V': case 'l': /* line-wise selection */
18699 yank_type = MLINE;
18700 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 case 'b': case Ctrl_V: /* block-wise selection */
18702 yank_type = MBLOCK;
18703 if (VIM_ISDIGIT(stropt[1]))
18704 {
18705 ++stropt;
18706 block_len = getdigits(&stropt) - 1;
18707 --stropt;
18708 }
18709 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710 }
18711 }
18712
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018713 if (argvars[1].v_type == VAR_LIST)
18714 {
18715 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018716 char_u **allocval;
18717 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018718 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018719 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018720 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018721 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018722 int len;
18723
18724 /* If the list is NULL handle like an empty list. */
18725 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018726
Bram Moolenaar7d647822014-04-05 21:28:56 +020018727 /* First half: use for pointers to result lines; second half: use for
18728 * pointers to allocated copies. */
18729 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018730 if (lstval == NULL)
18731 return;
18732 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018733 allocval = lstval + len + 2;
18734 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018735
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018736 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018737 li = li->li_next)
18738 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018739 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018740 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018741 goto free_lstval;
18742 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018743 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018744 /* Need to make a copy, next get_tv_string_buf_chk() will
18745 * overwrite the string. */
18746 strval = vim_strsave(buf);
18747 if (strval == NULL)
18748 goto free_lstval;
18749 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018750 }
18751 *curval++ = strval;
18752 }
18753 *curval++ = NULL;
18754
18755 write_reg_contents_lst(regname, lstval, -1,
18756 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018757free_lstval:
18758 while (curallocval > allocval)
18759 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018760 vim_free(lstval);
18761 }
18762 else
18763 {
18764 strval = get_tv_string_chk(&argvars[1]);
18765 if (strval == NULL)
18766 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018767 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018770 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771}
18772
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018773/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018774 * "settabvar()" function
18775 */
18776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018777f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018778{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018779#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018780 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018781 tabpage_T *tp;
18782#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018783 char_u *varname, *tabvarname;
18784 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018785
18786 rettv->vval.v_number = 0;
18787
18788 if (check_restricted() || check_secure())
18789 return;
18790
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018791#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018792 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018793#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018794 varname = get_tv_string_chk(&argvars[1]);
18795 varp = &argvars[2];
18796
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018797 if (varname != NULL && varp != NULL
18798#ifdef FEAT_WINDOWS
18799 && tp != NULL
18800#endif
18801 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018802 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018803#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018804 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018805 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018806#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018807
18808 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18809 if (tabvarname != NULL)
18810 {
18811 STRCPY(tabvarname, "t:");
18812 STRCPY(tabvarname + 2, varname);
18813 set_var(tabvarname, varp, TRUE);
18814 vim_free(tabvarname);
18815 }
18816
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018817#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018818 /* Restore current tabpage */
18819 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018820 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018821#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018822 }
18823}
18824
18825/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018826 * "settabwinvar()" function
18827 */
18828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018830{
18831 setwinvar(argvars, rettv, 1);
18832}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833
18834/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018835 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018838f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018840 setwinvar(argvars, rettv, 0);
18841}
18842
18843/*
18844 * "setwinvar()" and "settabwinvar()" functions
18845 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018846
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018848setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018849{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850 win_T *win;
18851#ifdef FEAT_WINDOWS
18852 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018853 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018854 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018855#endif
18856 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018857 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018859 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860
18861 if (check_restricted() || check_secure())
18862 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018863
18864#ifdef FEAT_WINDOWS
18865 if (off == 1)
18866 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18867 else
18868 tp = curtab;
18869#endif
18870 win = find_win_by_nr(&argvars[off], tp);
18871 varname = get_tv_string_chk(&argvars[off + 1]);
18872 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873
18874 if (win != NULL && varname != NULL && varp != NULL)
18875 {
18876#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018877 need_switch_win = !(tp == curtab && win == curwin);
18878 if (!need_switch_win
18879 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018882 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018884 long numval;
18885 char_u *strval;
18886 int error = FALSE;
18887
18888 ++varname;
18889 numval = get_tv_number_chk(varp, &error);
18890 strval = get_tv_string_buf_chk(varp, nbuf);
18891 if (!error && strval != NULL)
18892 set_option_value(varname, numval, strval, OPT_LOCAL);
18893 }
18894 else
18895 {
18896 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18897 if (winvarname != NULL)
18898 {
18899 STRCPY(winvarname, "w:");
18900 STRCPY(winvarname + 2, varname);
18901 set_var(winvarname, varp, TRUE);
18902 vim_free(winvarname);
18903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 }
18905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018906#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018907 if (need_switch_win)
18908 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909#endif
18910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911}
18912
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018913#ifdef FEAT_CRYPT
18914/*
18915 * "sha256({string})" function
18916 */
18917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018918f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018919{
18920 char_u *p;
18921
18922 p = get_tv_string(&argvars[0]);
18923 rettv->vval.v_string = vim_strsave(
18924 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18925 rettv->v_type = VAR_STRING;
18926}
18927#endif /* FEAT_CRYPT */
18928
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018930 * "shellescape({string})" function
18931 */
18932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018933f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018934{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018935 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018936 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018937 rettv->v_type = VAR_STRING;
18938}
18939
18940/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018941 * shiftwidth() function
18942 */
18943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018944f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018945{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018946 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018947}
18948
18949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018950 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 */
18952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018953f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018955 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018956
Bram Moolenaar0d660222005-01-07 21:51:51 +000018957 p = get_tv_string(&argvars[0]);
18958 rettv->vval.v_string = vim_strsave(p);
18959 simplify_filename(rettv->vval.v_string); /* simplify in place */
18960 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961}
18962
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018963#ifdef FEAT_FLOAT
18964/*
18965 * "sin()" function
18966 */
18967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018968f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018969{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018970 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018971
18972 rettv->v_type = VAR_FLOAT;
18973 if (get_float_arg(argvars, &f) == OK)
18974 rettv->vval.v_float = sin(f);
18975 else
18976 rettv->vval.v_float = 0.0;
18977}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018978
18979/*
18980 * "sinh()" function
18981 */
18982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018983f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018984{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018985 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018986
18987 rettv->v_type = VAR_FLOAT;
18988 if (get_float_arg(argvars, &f) == OK)
18989 rettv->vval.v_float = sinh(f);
18990 else
18991 rettv->vval.v_float = 0.0;
18992}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018993#endif
18994
Bram Moolenaar0d660222005-01-07 21:51:51 +000018995static int
18996#ifdef __BORLANDC__
18997 _RTLENTRYF
18998#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018999 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019000static int
19001#ifdef __BORLANDC__
19002 _RTLENTRYF
19003#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019004 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019006/* struct used in the array that's given to qsort() */
19007typedef struct
19008{
19009 listitem_T *item;
19010 int idx;
19011} sortItem_T;
19012
Bram Moolenaar0b962472016-02-22 22:51:33 +010019013/* struct storing information about current sort */
19014typedef struct
19015{
19016 int item_compare_ic;
19017 int item_compare_numeric;
19018 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019019#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019020 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019021#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019022 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019023 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019024 dict_T *item_compare_selfdict;
19025 int item_compare_func_err;
19026 int item_compare_keep_zero;
19027} sortinfo_T;
19028static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019029static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030#define ITEM_COMPARE_FAIL 999
19031
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019033 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035 static int
19036#ifdef __BORLANDC__
19037_RTLENTRYF
19038#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019039item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019041 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019042 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019044 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 int res;
19046 char_u numbuf1[NUMBUFLEN];
19047 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019049 si1 = (sortItem_T *)s1;
19050 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019051 tv1 = &si1->item->li_tv;
19052 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019053
Bram Moolenaar0b962472016-02-22 22:51:33 +010019054 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019055 {
19056 long v1 = get_tv_number(tv1);
19057 long v2 = get_tv_number(tv2);
19058
19059 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19060 }
19061
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019062#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019063 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019064 {
19065 float_T v1 = get_tv_float(tv1);
19066 float_T v2 = get_tv_float(tv2);
19067
19068 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19069 }
19070#endif
19071
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019072 /* tv2string() puts quotes around a string and allocates memory. Don't do
19073 * that for string variables. Use a single quote when comparing with a
19074 * non-string to do what the docs promise. */
19075 if (tv1->v_type == VAR_STRING)
19076 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019077 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019078 p1 = (char_u *)"'";
19079 else
19080 p1 = tv1->vval.v_string;
19081 }
19082 else
19083 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19084 if (tv2->v_type == VAR_STRING)
19085 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019086 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019087 p2 = (char_u *)"'";
19088 else
19089 p2 = tv2->vval.v_string;
19090 }
19091 else
19092 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019093 if (p1 == NULL)
19094 p1 = (char_u *)"";
19095 if (p2 == NULL)
19096 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019097 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019098 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019099 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019100 res = STRICMP(p1, p2);
19101 else
19102 res = STRCMP(p1, p2);
19103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019105 {
19106 double n1, n2;
19107 n1 = strtod((char *)p1, (char **)&p1);
19108 n2 = strtod((char *)p2, (char **)&p2);
19109 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19110 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019111
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019112 /* When the result would be zero, compare the item indexes. Makes the
19113 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019115 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019116
Bram Moolenaar0d660222005-01-07 21:51:51 +000019117 vim_free(tofree1);
19118 vim_free(tofree2);
19119 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120}
19121
19122 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019123#ifdef __BORLANDC__
19124_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019126item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019127{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019128 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019130 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019131 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019132 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019133 char_u *func_name;
19134 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019136 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019137 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019138 return 0;
19139
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019140 si1 = (sortItem_T *)s1;
19141 si2 = (sortItem_T *)s2;
19142
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019143 if (partial == NULL)
19144 func_name = sortinfo->item_compare_func;
19145 else
19146 func_name = partial->pt_name;
19147
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019148 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019149 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019150 copy_tv(&si1->item->li_tv, &argv[0]);
19151 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152
19153 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019154 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019155 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019156 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 clear_tv(&argv[0]);
19158 clear_tv(&argv[1]);
19159
19160 if (res == FAIL)
19161 res = ITEM_COMPARE_FAIL;
19162 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019163 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19164 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019165 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019166 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019167
19168 /* When the result would be zero, compare the pointers themselves. Makes
19169 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019170 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019172
Bram Moolenaar0d660222005-01-07 21:51:51 +000019173 return res;
19174}
19175
19176/*
19177 * "sort({list})" function
19178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019180do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019181{
Bram Moolenaar33570922005-01-25 22:26:29 +000019182 list_T *l;
19183 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019184 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 sortinfo_T *old_sortinfo;
19186 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019187 long len;
19188 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189
Bram Moolenaar0b962472016-02-22 22:51:33 +010019190 /* Pointer to current info struct used in compare function. Save and
19191 * restore the current one for nested calls. */
19192 old_sortinfo = sortinfo;
19193 sortinfo = &info;
19194
Bram Moolenaar0d660222005-01-07 21:51:51 +000019195 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019196 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019197 else
19198 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019199 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019200 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019201 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19202 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019203 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019204 rettv->vval.v_list = l;
19205 rettv->v_type = VAR_LIST;
19206 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207
Bram Moolenaar0d660222005-01-07 21:51:51 +000019208 len = list_len(l);
19209 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019210 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 info.item_compare_ic = FALSE;
19213 info.item_compare_numeric = FALSE;
19214 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019215#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019216 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019217#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019219 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019221 if (argvars[1].v_type != VAR_UNKNOWN)
19222 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019223 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019224 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019225 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019226 else if (argvars[1].v_type == VAR_PARTIAL)
19227 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019228 else
19229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019230 int error = FALSE;
19231
19232 i = get_tv_number_chk(&argvars[1], &error);
19233 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019234 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019235 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019236 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019237 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019238 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019239 else if (i != 0)
19240 {
19241 EMSG(_(e_invarg));
19242 goto theend;
19243 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019245 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019246 if (*info.item_compare_func == NUL)
19247 {
19248 /* empty string means default sort */
19249 info.item_compare_func = NULL;
19250 }
19251 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019252 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019253 info.item_compare_func = NULL;
19254 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019255 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019256 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019257 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019258 info.item_compare_func = NULL;
19259 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019260 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019261#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019262 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019263 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019264 info.item_compare_func = NULL;
19265 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019266 }
19267#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019268 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019269 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019270 info.item_compare_func = NULL;
19271 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019272 }
19273 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019274 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019275
19276 if (argvars[2].v_type != VAR_UNKNOWN)
19277 {
19278 /* optional third argument: {dict} */
19279 if (argvars[2].v_type != VAR_DICT)
19280 {
19281 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019282 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019283 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019284 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019285 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287
Bram Moolenaar0d660222005-01-07 21:51:51 +000019288 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019289 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019290 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019291 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292
Bram Moolenaar327aa022014-03-25 18:24:23 +010019293 i = 0;
19294 if (sort)
19295 {
19296 /* sort(): ptrs will be the list to sort */
19297 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019298 {
19299 ptrs[i].item = li;
19300 ptrs[i].idx = i;
19301 ++i;
19302 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019303
Bram Moolenaar0b962472016-02-22 22:51:33 +010019304 info.item_compare_func_err = FALSE;
19305 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019306 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019307 if ((info.item_compare_func != NULL
19308 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019309 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019311 EMSG(_("E702: Sort compare function failed"));
19312 else
19313 {
19314 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019315 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019316 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019317 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019318 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019319
Bram Moolenaar0b962472016-02-22 22:51:33 +010019320 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019321 {
19322 /* Clear the List and append the items in sorted order. */
19323 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19324 l->lv_len = 0;
19325 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019326 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019327 }
19328 }
19329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019331 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019332 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019333
19334 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019335 info.item_compare_func_err = FALSE;
19336 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019337 item_compare_func_ptr = info.item_compare_func != NULL
19338 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019339 ? item_compare2 : item_compare;
19340
19341 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19342 li = li->li_next)
19343 {
19344 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19345 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019346 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019347 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019348 {
19349 EMSG(_("E882: Uniq compare function failed"));
19350 break;
19351 }
19352 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019353
Bram Moolenaar0b962472016-02-22 22:51:33 +010019354 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019355 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019356 while (--i >= 0)
19357 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019358 li = ptrs[i].item->li_next;
19359 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019360 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019361 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019362 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019363 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019364 list_fix_watch(l, li);
19365 listitem_free(li);
19366 l->lv_len--;
19367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019368 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369 }
19370
19371 vim_free(ptrs);
19372 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019373theend:
19374 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375}
19376
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019377/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019378 * "sort({list})" function
19379 */
19380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019381f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019382{
19383 do_sort_uniq(argvars, rettv, TRUE);
19384}
19385
19386/*
19387 * "uniq({list})" function
19388 */
19389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019390f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019391{
19392 do_sort_uniq(argvars, rettv, FALSE);
19393}
19394
19395/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019396 * "soundfold({word})" function
19397 */
19398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019399f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019400{
19401 char_u *s;
19402
19403 rettv->v_type = VAR_STRING;
19404 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019405#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019406 rettv->vval.v_string = eval_soundfold(s);
19407#else
19408 rettv->vval.v_string = vim_strsave(s);
19409#endif
19410}
19411
19412/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019413 * "spellbadword()" function
19414 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019416f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019417{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019418 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019419 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019420 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019421
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019422 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019423 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019424
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019425#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019426 if (argvars[0].v_type == VAR_UNKNOWN)
19427 {
19428 /* Find the start and length of the badly spelled word. */
19429 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19430 if (len != 0)
19431 word = ml_get_cursor();
19432 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019433 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019434 {
19435 char_u *str = get_tv_string_chk(&argvars[0]);
19436 int capcol = -1;
19437
19438 if (str != NULL)
19439 {
19440 /* Check the argument for spelling. */
19441 while (*str != NUL)
19442 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019443 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019444 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019445 {
19446 word = str;
19447 break;
19448 }
19449 str += len;
19450 }
19451 }
19452 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019453#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019454
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019455 list_append_string(rettv->vval.v_list, word, len);
19456 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019457 attr == HLF_SPB ? "bad" :
19458 attr == HLF_SPR ? "rare" :
19459 attr == HLF_SPL ? "local" :
19460 attr == HLF_SPC ? "caps" :
19461 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019462}
19463
19464/*
19465 * "spellsuggest()" function
19466 */
19467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019468f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019469{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019470#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019471 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019472 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019473 int maxcount;
19474 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019475 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019476 listitem_T *li;
19477 int need_capital = FALSE;
19478#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019480 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019481 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019482
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019483#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019484 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019485 {
19486 str = get_tv_string(&argvars[0]);
19487 if (argvars[1].v_type != VAR_UNKNOWN)
19488 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019489 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019490 if (maxcount <= 0)
19491 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019492 if (argvars[2].v_type != VAR_UNKNOWN)
19493 {
19494 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19495 if (typeerr)
19496 return;
19497 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019498 }
19499 else
19500 maxcount = 25;
19501
Bram Moolenaar4770d092006-01-12 23:22:24 +000019502 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019503
19504 for (i = 0; i < ga.ga_len; ++i)
19505 {
19506 str = ((char_u **)ga.ga_data)[i];
19507
19508 li = listitem_alloc();
19509 if (li == NULL)
19510 vim_free(str);
19511 else
19512 {
19513 li->li_tv.v_type = VAR_STRING;
19514 li->li_tv.v_lock = 0;
19515 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019516 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019517 }
19518 }
19519 ga_clear(&ga);
19520 }
19521#endif
19522}
19523
Bram Moolenaar0d660222005-01-07 21:51:51 +000019524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019525f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019526{
19527 char_u *str;
19528 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019529 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019530 regmatch_T regmatch;
19531 char_u patbuf[NUMBUFLEN];
19532 char_u *save_cpo;
19533 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019534 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019535 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019536 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019537
19538 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19539 save_cpo = p_cpo;
19540 p_cpo = (char_u *)"";
19541
19542 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019543 if (argvars[1].v_type != VAR_UNKNOWN)
19544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019545 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19546 if (pat == NULL)
19547 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019548 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019549 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019550 }
19551 if (pat == NULL || *pat == NUL)
19552 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019553
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019554 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019556 if (typeerr)
19557 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558
Bram Moolenaar0d660222005-01-07 21:51:51 +000019559 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19560 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019562 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019563 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019564 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019565 if (*str == NUL)
19566 match = FALSE; /* empty item at the end */
19567 else
19568 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019569 if (match)
19570 end = regmatch.startp[0];
19571 else
19572 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019573 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19574 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019575 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019576 if (list_append_string(rettv->vval.v_list, str,
19577 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019578 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019579 }
19580 if (!match)
19581 break;
19582 /* Advance to just after the match. */
19583 if (regmatch.endp[0] > str)
19584 col = 0;
19585 else
19586 {
19587 /* Don't get stuck at the same match. */
19588#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019589 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019590#else
19591 col = 1;
19592#endif
19593 }
19594 str = regmatch.endp[0];
19595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596
Bram Moolenaar473de612013-06-08 18:19:48 +020019597 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599
Bram Moolenaar0d660222005-01-07 21:51:51 +000019600 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601}
19602
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019603#ifdef FEAT_FLOAT
19604/*
19605 * "sqrt()" function
19606 */
19607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019608f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019609{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019610 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019611
19612 rettv->v_type = VAR_FLOAT;
19613 if (get_float_arg(argvars, &f) == OK)
19614 rettv->vval.v_float = sqrt(f);
19615 else
19616 rettv->vval.v_float = 0.0;
19617}
19618
19619/*
19620 * "str2float()" function
19621 */
19622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019623f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019624{
19625 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19626
19627 if (*p == '+')
19628 p = skipwhite(p + 1);
19629 (void)string2float(p, &rettv->vval.v_float);
19630 rettv->v_type = VAR_FLOAT;
19631}
19632#endif
19633
Bram Moolenaar2c932302006-03-18 21:42:09 +000019634/*
19635 * "str2nr()" function
19636 */
19637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019639{
19640 int base = 10;
19641 char_u *p;
19642 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019643 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019644
19645 if (argvars[1].v_type != VAR_UNKNOWN)
19646 {
19647 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019648 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019649 {
19650 EMSG(_(e_invarg));
19651 return;
19652 }
19653 }
19654
19655 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019656 if (*p == '+')
19657 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019658 switch (base)
19659 {
19660 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19661 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19662 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19663 default: what = 0;
19664 }
19665 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019666 rettv->vval.v_number = n;
19667}
19668
Bram Moolenaar071d4272004-06-13 20:20:40 +000019669#ifdef HAVE_STRFTIME
19670/*
19671 * "strftime({format}[, {time}])" function
19672 */
19673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019674f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675{
19676 char_u result_buf[256];
19677 struct tm *curtime;
19678 time_t seconds;
19679 char_u *p;
19680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019681 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019684 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 seconds = time(NULL);
19686 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019687 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 curtime = localtime(&seconds);
19689 /* MSVC returns NULL for an invalid value of seconds. */
19690 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 else
19693 {
19694# ifdef FEAT_MBYTE
19695 vimconv_T conv;
19696 char_u *enc;
19697
19698 conv.vc_type = CONV_NONE;
19699 enc = enc_locale();
19700 convert_setup(&conv, p_enc, enc);
19701 if (conv.vc_type != CONV_NONE)
19702 p = string_convert(&conv, p, NULL);
19703# endif
19704 if (p != NULL)
19705 (void)strftime((char *)result_buf, sizeof(result_buf),
19706 (char *)p, curtime);
19707 else
19708 result_buf[0] = NUL;
19709
19710# ifdef FEAT_MBYTE
19711 if (conv.vc_type != CONV_NONE)
19712 vim_free(p);
19713 convert_setup(&conv, enc, p_enc);
19714 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 else
19717# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019718 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719
19720# ifdef FEAT_MBYTE
19721 /* Release conversion descriptors */
19722 convert_setup(&conv, NULL, NULL);
19723 vim_free(enc);
19724# endif
19725 }
19726}
19727#endif
19728
19729/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019730 * "strgetchar()" function
19731 */
19732 static void
19733f_strgetchar(typval_T *argvars, typval_T *rettv)
19734{
19735 char_u *str;
19736 int len;
19737 int error = FALSE;
19738 int charidx;
19739
19740 rettv->vval.v_number = -1;
19741 str = get_tv_string_chk(&argvars[0]);
19742 if (str == NULL)
19743 return;
19744 len = (int)STRLEN(str);
19745 charidx = get_tv_number_chk(&argvars[1], &error);
19746 if (error)
19747 return;
19748#ifdef FEAT_MBYTE
19749 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019750 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019751
19752 while (charidx >= 0 && byteidx < len)
19753 {
19754 if (charidx == 0)
19755 {
19756 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19757 break;
19758 }
19759 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019760 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019761 }
19762 }
19763#else
19764 if (charidx < len)
19765 rettv->vval.v_number = str[charidx];
19766#endif
19767}
19768
19769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 * "stridx()" function
19771 */
19772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019773f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774{
19775 char_u buf[NUMBUFLEN];
19776 char_u *needle;
19777 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019780 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019782 needle = get_tv_string_chk(&argvars[1]);
19783 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019784 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019785 if (needle == NULL || haystack == NULL)
19786 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787
Bram Moolenaar33570922005-01-25 22:26:29 +000019788 if (argvars[2].v_type != VAR_UNKNOWN)
19789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019790 int error = FALSE;
19791
19792 start_idx = get_tv_number_chk(&argvars[2], &error);
19793 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019794 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019795 if (start_idx >= 0)
19796 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019797 }
19798
19799 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19800 if (pos != NULL)
19801 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802}
19803
19804/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019805 * "string()" function
19806 */
19807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019808f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019809{
19810 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019811 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019814 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19815 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019816 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019817 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019818 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819}
19820
19821/*
19822 * "strlen()" function
19823 */
19824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019825f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019827 rettv->vval.v_number = (varnumber_T)(STRLEN(
19828 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829}
19830
19831/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019832 * "strchars()" function
19833 */
19834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019835f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019836{
19837 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019838 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019839#ifdef FEAT_MBYTE
19840 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019841 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019842#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019843
19844 if (argvars[1].v_type != VAR_UNKNOWN)
19845 skipcc = get_tv_number_chk(&argvars[1], NULL);
19846 if (skipcc < 0 || skipcc > 1)
19847 EMSG(_(e_invarg));
19848 else
19849 {
19850#ifdef FEAT_MBYTE
19851 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19852 while (*s != NUL)
19853 {
19854 func_mb_ptr2char_adv(&s);
19855 ++len;
19856 }
19857 rettv->vval.v_number = len;
19858#else
19859 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19860#endif
19861 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019862}
19863
19864/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019865 * "strdisplaywidth()" function
19866 */
19867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019868f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019869{
19870 char_u *s = get_tv_string(&argvars[0]);
19871 int col = 0;
19872
19873 if (argvars[1].v_type != VAR_UNKNOWN)
19874 col = get_tv_number(&argvars[1]);
19875
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019876 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019877}
19878
19879/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019880 * "strwidth()" function
19881 */
19882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019883f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019884{
19885 char_u *s = get_tv_string(&argvars[0]);
19886
19887 rettv->vval.v_number = (varnumber_T)(
19888#ifdef FEAT_MBYTE
19889 mb_string2cells(s, -1)
19890#else
19891 STRLEN(s)
19892#endif
19893 );
19894}
19895
19896/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019897 * "strcharpart()" function
19898 */
19899 static void
19900f_strcharpart(typval_T *argvars, typval_T *rettv)
19901{
19902#ifdef FEAT_MBYTE
19903 char_u *p;
19904 int nchar;
19905 int nbyte = 0;
19906 int charlen;
19907 int len = 0;
19908 int slen;
19909 int error = FALSE;
19910
19911 p = get_tv_string(&argvars[0]);
19912 slen = (int)STRLEN(p);
19913
19914 nchar = get_tv_number_chk(&argvars[1], &error);
19915 if (!error)
19916 {
19917 if (nchar > 0)
19918 while (nchar > 0 && nbyte < slen)
19919 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019920 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019921 --nchar;
19922 }
19923 else
19924 nbyte = nchar;
19925 if (argvars[2].v_type != VAR_UNKNOWN)
19926 {
19927 charlen = get_tv_number(&argvars[2]);
19928 while (charlen > 0 && nbyte + len < slen)
19929 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019930 int off = nbyte + len;
19931
19932 if (off < 0)
19933 len += 1;
19934 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019935 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019936 --charlen;
19937 }
19938 }
19939 else
19940 len = slen - nbyte; /* default: all bytes that are available. */
19941 }
19942
19943 /*
19944 * Only return the overlap between the specified part and the actual
19945 * string.
19946 */
19947 if (nbyte < 0)
19948 {
19949 len += nbyte;
19950 nbyte = 0;
19951 }
19952 else if (nbyte > slen)
19953 nbyte = slen;
19954 if (len < 0)
19955 len = 0;
19956 else if (nbyte + len > slen)
19957 len = slen - nbyte;
19958
19959 rettv->v_type = VAR_STRING;
19960 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19961#else
19962 f_strpart(argvars, rettv);
19963#endif
19964}
19965
19966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 * "strpart()" function
19968 */
19969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019970f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971{
19972 char_u *p;
19973 int n;
19974 int len;
19975 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019976 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979 slen = (int)STRLEN(p);
19980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019981 n = get_tv_number_chk(&argvars[1], &error);
19982 if (error)
19983 len = 0;
19984 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 else
19987 len = slen - n; /* default len: all bytes that are available. */
19988
19989 /*
19990 * Only return the overlap between the specified part and the actual
19991 * string.
19992 */
19993 if (n < 0)
19994 {
19995 len += n;
19996 n = 0;
19997 }
19998 else if (n > slen)
19999 n = slen;
20000 if (len < 0)
20001 len = 0;
20002 else if (n + len > slen)
20003 len = slen - n;
20004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020005 rettv->v_type = VAR_STRING;
20006 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007}
20008
20009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020010 * "strridx()" function
20011 */
20012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020013f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020014{
20015 char_u buf[NUMBUFLEN];
20016 char_u *needle;
20017 char_u *haystack;
20018 char_u *rest;
20019 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020020 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020022 needle = get_tv_string_chk(&argvars[1]);
20023 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020024
20025 rettv->vval.v_number = -1;
20026 if (needle == NULL || haystack == NULL)
20027 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020028
20029 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020030 if (argvars[2].v_type != VAR_UNKNOWN)
20031 {
20032 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020033 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020034 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020035 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020036 }
20037 else
20038 end_idx = haystack_len;
20039
Bram Moolenaar0d660222005-01-07 21:51:51 +000020040 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020041 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020042 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020043 lastmatch = haystack + end_idx;
20044 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020045 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020046 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020047 for (rest = haystack; *rest != '\0'; ++rest)
20048 {
20049 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020050 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020051 break;
20052 lastmatch = rest;
20053 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020054 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020055
20056 if (lastmatch == NULL)
20057 rettv->vval.v_number = -1;
20058 else
20059 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20060}
20061
20062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 * "strtrans()" function
20064 */
20065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020066f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020068 rettv->v_type = VAR_STRING;
20069 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070}
20071
20072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020073 * "submatch()" function
20074 */
20075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020076f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020077{
Bram Moolenaar41571762014-04-02 19:00:58 +020020078 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020079 int no;
20080 int retList = 0;
20081
20082 no = (int)get_tv_number_chk(&argvars[0], &error);
20083 if (error)
20084 return;
20085 error = FALSE;
20086 if (argvars[1].v_type != VAR_UNKNOWN)
20087 retList = get_tv_number_chk(&argvars[1], &error);
20088 if (error)
20089 return;
20090
20091 if (retList == 0)
20092 {
20093 rettv->v_type = VAR_STRING;
20094 rettv->vval.v_string = reg_submatch(no);
20095 }
20096 else
20097 {
20098 rettv->v_type = VAR_LIST;
20099 rettv->vval.v_list = reg_submatch_list(no);
20100 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020101}
20102
20103/*
20104 * "substitute()" function
20105 */
20106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020107f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020108{
20109 char_u patbuf[NUMBUFLEN];
20110 char_u subbuf[NUMBUFLEN];
20111 char_u flagsbuf[NUMBUFLEN];
20112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020113 char_u *str = get_tv_string_chk(&argvars[0]);
20114 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20115 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20116 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20117
Bram Moolenaar0d660222005-01-07 21:51:51 +000020118 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020119 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20120 rettv->vval.v_string = NULL;
20121 else
20122 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020123}
20124
20125/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020126 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020129f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130{
20131 int id = 0;
20132#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020133 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 long col;
20135 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020136 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020138 lnum = get_tv_lnum(argvars); /* -1 on type error */
20139 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20140 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020142 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020143 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020144 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145#endif
20146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020147 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148}
20149
20150/*
20151 * "synIDattr(id, what [, mode])" function
20152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020154f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155{
20156 char_u *p = NULL;
20157#ifdef FEAT_SYN_HL
20158 int id;
20159 char_u *what;
20160 char_u *mode;
20161 char_u modebuf[NUMBUFLEN];
20162 int modec;
20163
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020164 id = get_tv_number(&argvars[0]);
20165 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020166 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020168 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020170 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 modec = 0; /* replace invalid with current */
20172 }
20173 else
20174 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020175#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020176 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 modec = 'g';
20178 else
20179#endif
20180 if (t_colors > 1)
20181 modec = 'c';
20182 else
20183 modec = 't';
20184 }
20185
20186
20187 switch (TOLOWER_ASC(what[0]))
20188 {
20189 case 'b':
20190 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20191 p = highlight_color(id, what, modec);
20192 else /* bold */
20193 p = highlight_has_attr(id, HL_BOLD, modec);
20194 break;
20195
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020196 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 p = highlight_color(id, what, modec);
20198 break;
20199
20200 case 'i':
20201 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20202 p = highlight_has_attr(id, HL_INVERSE, modec);
20203 else /* italic */
20204 p = highlight_has_attr(id, HL_ITALIC, modec);
20205 break;
20206
20207 case 'n': /* name */
20208 p = get_highlight_name(NULL, id - 1);
20209 break;
20210
20211 case 'r': /* reverse */
20212 p = highlight_has_attr(id, HL_INVERSE, modec);
20213 break;
20214
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020215 case 's':
20216 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20217 p = highlight_color(id, what, modec);
20218 else /* standout */
20219 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 break;
20221
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020222 case 'u':
20223 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20224 /* underline */
20225 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20226 else
20227 /* undercurl */
20228 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 break;
20230 }
20231
20232 if (p != NULL)
20233 p = vim_strsave(p);
20234#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020235 rettv->v_type = VAR_STRING;
20236 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237}
20238
20239/*
20240 * "synIDtrans(id)" function
20241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020243f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244{
20245 int id;
20246
20247#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020248 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249
20250 if (id > 0)
20251 id = syn_get_final_id(id);
20252 else
20253#endif
20254 id = 0;
20255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257}
20258
20259/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020260 * "synconcealed(lnum, col)" function
20261 */
20262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020263f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020264{
20265#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20266 long lnum;
20267 long col;
20268 int syntax_flags = 0;
20269 int cchar;
20270 int matchid = 0;
20271 char_u str[NUMBUFLEN];
20272#endif
20273
20274 rettv->v_type = VAR_LIST;
20275 rettv->vval.v_list = NULL;
20276
20277#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20278 lnum = get_tv_lnum(argvars); /* -1 on type error */
20279 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20280
20281 vim_memset(str, NUL, sizeof(str));
20282
20283 if (rettv_list_alloc(rettv) != FAIL)
20284 {
20285 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20286 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20287 && curwin->w_p_cole > 0)
20288 {
20289 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20290 syntax_flags = get_syntax_info(&matchid);
20291
20292 /* get the conceal character */
20293 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20294 {
20295 cchar = syn_get_sub_char();
20296 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20297 cchar = lcs_conceal;
20298 if (cchar != NUL)
20299 {
20300# ifdef FEAT_MBYTE
20301 if (has_mbyte)
20302 (*mb_char2bytes)(cchar, str);
20303 else
20304# endif
20305 str[0] = cchar;
20306 }
20307 }
20308 }
20309
20310 list_append_number(rettv->vval.v_list,
20311 (syntax_flags & HL_CONCEAL) != 0);
20312 /* -1 to auto-determine strlen */
20313 list_append_string(rettv->vval.v_list, str, -1);
20314 list_append_number(rettv->vval.v_list, matchid);
20315 }
20316#endif
20317}
20318
20319/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020320 * "synstack(lnum, col)" function
20321 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020323f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020324{
20325#ifdef FEAT_SYN_HL
20326 long lnum;
20327 long col;
20328 int i;
20329 int id;
20330#endif
20331
20332 rettv->v_type = VAR_LIST;
20333 rettv->vval.v_list = NULL;
20334
20335#ifdef FEAT_SYN_HL
20336 lnum = get_tv_lnum(argvars); /* -1 on type error */
20337 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20338
20339 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020340 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020341 && rettv_list_alloc(rettv) != FAIL)
20342 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020343 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020344 for (i = 0; ; ++i)
20345 {
20346 id = syn_get_stack_item(i);
20347 if (id < 0)
20348 break;
20349 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20350 break;
20351 }
20352 }
20353#endif
20354}
20355
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020357get_cmd_output_as_rettv(
20358 typval_T *argvars,
20359 typval_T *rettv,
20360 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020362 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020364 char_u *infile = NULL;
20365 char_u buf[NUMBUFLEN];
20366 int err = FALSE;
20367 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020368 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020369 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020371 rettv->v_type = VAR_STRING;
20372 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020373 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020374 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020375
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020376 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020377 {
20378 /*
20379 * Write the string to a temp file, to be used for input of the shell
20380 * command.
20381 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020382 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020383 {
20384 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020385 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020386 }
20387
20388 fd = mch_fopen((char *)infile, WRITEBIN);
20389 if (fd == NULL)
20390 {
20391 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020392 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020393 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020394 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020395 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020396 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20397 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020398 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020399 else
20400 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020401 size_t len;
20402
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020403 p = get_tv_string_buf_chk(&argvars[1], buf);
20404 if (p == NULL)
20405 {
20406 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020407 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020408 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020409 len = STRLEN(p);
20410 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020411 err = TRUE;
20412 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020413 if (fclose(fd) != 0)
20414 err = TRUE;
20415 if (err)
20416 {
20417 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020418 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020419 }
20420 }
20421
Bram Moolenaar52a72462014-08-29 15:53:52 +020020422 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20423 * echoes typeahead, that messes up the display. */
20424 if (!msg_silent)
20425 flags += SHELL_COOKED;
20426
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020427 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020429 int len;
20430 listitem_T *li;
20431 char_u *s = NULL;
20432 char_u *start;
20433 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020434 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435
Bram Moolenaar52a72462014-08-29 15:53:52 +020020436 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020437 if (res == NULL)
20438 goto errret;
20439
20440 list = list_alloc();
20441 if (list == NULL)
20442 goto errret;
20443
20444 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020446 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020447 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020448 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020449 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020450
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020451 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020452 if (s == NULL)
20453 goto errret;
20454
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020455 for (p = s; start < end; ++p, ++start)
20456 *p = *start == NUL ? NL : *start;
20457 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020458
20459 li = listitem_alloc();
20460 if (li == NULL)
20461 {
20462 vim_free(s);
20463 goto errret;
20464 }
20465 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020466 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020467 li->li_tv.vval.v_string = s;
20468 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020470
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020471 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020472 rettv->v_type = VAR_LIST;
20473 rettv->vval.v_list = list;
20474 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020476 else
20477 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020478 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020479#ifdef USE_CR
20480 /* translate <CR> into <NL> */
20481 if (res != NULL)
20482 {
20483 char_u *s;
20484
20485 for (s = res; *s; ++s)
20486 {
20487 if (*s == CAR)
20488 *s = NL;
20489 }
20490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491#else
20492# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020493 /* translate <CR><NL> into <NL> */
20494 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020496 char_u *s, *d;
20497
20498 d = res;
20499 for (s = res; *s; ++s)
20500 {
20501 if (s[0] == CAR && s[1] == NL)
20502 ++s;
20503 *d++ = *s;
20504 }
20505 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507# endif
20508#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020509 rettv->vval.v_string = res;
20510 res = NULL;
20511 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020512
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020513errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020514 if (infile != NULL)
20515 {
20516 mch_remove(infile);
20517 vim_free(infile);
20518 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020519 if (res != NULL)
20520 vim_free(res);
20521 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020522 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020523}
20524
20525/*
20526 * "system()" function
20527 */
20528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020529f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020530{
20531 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20532}
20533
20534/*
20535 * "systemlist()" function
20536 */
20537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020538f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020539{
20540 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541}
20542
20543/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020544 * "tabpagebuflist()" function
20545 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020548{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020549#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020550 tabpage_T *tp;
20551 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020552
20553 if (argvars[0].v_type == VAR_UNKNOWN)
20554 wp = firstwin;
20555 else
20556 {
20557 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20558 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020559 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020560 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020561 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020562 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020563 for (; wp != NULL; wp = wp->w_next)
20564 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020565 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020566 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020567 }
20568#endif
20569}
20570
20571
20572/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020573 * "tabpagenr()" function
20574 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020576f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020577{
20578 int nr = 1;
20579#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020580 char_u *arg;
20581
20582 if (argvars[0].v_type != VAR_UNKNOWN)
20583 {
20584 arg = get_tv_string_chk(&argvars[0]);
20585 nr = 0;
20586 if (arg != NULL)
20587 {
20588 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020589 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020590 else
20591 EMSG2(_(e_invexpr2), arg);
20592 }
20593 }
20594 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020595 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020596#endif
20597 rettv->vval.v_number = nr;
20598}
20599
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020600
20601#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020602static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020603
20604/*
20605 * Common code for tabpagewinnr() and winnr().
20606 */
20607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020608get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020609{
20610 win_T *twin;
20611 int nr = 1;
20612 win_T *wp;
20613 char_u *arg;
20614
20615 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20616 if (argvar->v_type != VAR_UNKNOWN)
20617 {
20618 arg = get_tv_string_chk(argvar);
20619 if (arg == NULL)
20620 nr = 0; /* type error; errmsg already given */
20621 else if (STRCMP(arg, "$") == 0)
20622 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20623 else if (STRCMP(arg, "#") == 0)
20624 {
20625 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20626 if (twin == NULL)
20627 nr = 0;
20628 }
20629 else
20630 {
20631 EMSG2(_(e_invexpr2), arg);
20632 nr = 0;
20633 }
20634 }
20635
20636 if (nr > 0)
20637 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20638 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020639 {
20640 if (wp == NULL)
20641 {
20642 /* didn't find it in this tabpage */
20643 nr = 0;
20644 break;
20645 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020646 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020647 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020648 return nr;
20649}
20650#endif
20651
20652/*
20653 * "tabpagewinnr()" function
20654 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020656f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020657{
20658 int nr = 1;
20659#ifdef FEAT_WINDOWS
20660 tabpage_T *tp;
20661
20662 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20663 if (tp == NULL)
20664 nr = 0;
20665 else
20666 nr = get_winnr(tp, &argvars[1]);
20667#endif
20668 rettv->vval.v_number = nr;
20669}
20670
20671
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020672/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020673 * "tagfiles()" function
20674 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020676f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020677{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020678 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020679 tagname_T tn;
20680 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020681
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020682 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020683 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020684 fname = alloc(MAXPATHL);
20685 if (fname == NULL)
20686 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020687
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020688 for (first = TRUE; ; first = FALSE)
20689 if (get_tagfname(&tn, first, fname) == FAIL
20690 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020691 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020692 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020693 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020694}
20695
20696/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020697 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020698 */
20699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020700f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020701{
20702 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020703
20704 tag_pattern = get_tv_string(&argvars[0]);
20705
20706 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020707 if (*tag_pattern == NUL)
20708 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020710 if (rettv_list_alloc(rettv) == OK)
20711 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020712}
20713
20714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715 * "tempname()" function
20716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020718f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719{
20720 static int x = 'A';
20721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020723 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020724
20725 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20726 * names. Skip 'I' and 'O', they are used for shell redirection. */
20727 do
20728 {
20729 if (x == 'Z')
20730 x = '0';
20731 else if (x == '9')
20732 x = 'A';
20733 else
20734 {
20735#ifdef EBCDIC
20736 if (x == 'I')
20737 x = 'J';
20738 else if (x == 'R')
20739 x = 'S';
20740 else
20741#endif
20742 ++x;
20743 }
20744 } while (x == 'I' || x == 'O');
20745}
20746
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020747#ifdef FEAT_FLOAT
20748/*
20749 * "tan()" function
20750 */
20751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020752f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020753{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020754 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020755
20756 rettv->v_type = VAR_FLOAT;
20757 if (get_float_arg(argvars, &f) == OK)
20758 rettv->vval.v_float = tan(f);
20759 else
20760 rettv->vval.v_float = 0.0;
20761}
20762
20763/*
20764 * "tanh()" function
20765 */
20766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020767f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020768{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020769 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020770
20771 rettv->v_type = VAR_FLOAT;
20772 if (get_float_arg(argvars, &f) == OK)
20773 rettv->vval.v_float = tanh(f);
20774 else
20775 rettv->vval.v_float = 0.0;
20776}
20777#endif
20778
Bram Moolenaar574860b2016-05-24 17:33:34 +020020779/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020780 * "test_alloc_fail(id, countdown, repeat)" function
20781 */
20782 static void
20783f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20784{
20785 if (argvars[0].v_type != VAR_NUMBER
20786 || argvars[0].vval.v_number <= 0
20787 || argvars[1].v_type != VAR_NUMBER
20788 || argvars[1].vval.v_number < 0
20789 || argvars[2].v_type != VAR_NUMBER)
20790 EMSG(_(e_invarg));
20791 else
20792 {
20793 alloc_fail_id = argvars[0].vval.v_number;
20794 if (alloc_fail_id >= aid_last)
20795 EMSG(_(e_invarg));
20796 alloc_fail_countdown = argvars[1].vval.v_number;
20797 alloc_fail_repeat = argvars[2].vval.v_number;
20798 did_outofmem_msg = FALSE;
20799 }
20800}
20801
20802/*
20803 * "test_disable_char_avail({expr})" function
20804 */
20805 static void
20806f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20807{
20808 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20809}
20810
20811/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020812 * "test_garbagecollect_now()" function
20813 */
20814 static void
20815f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20816{
20817 /* This is dangerous, any Lists and Dicts used internally may be freed
20818 * while still in use. */
20819 garbage_collect(TRUE);
20820}
20821
20822#ifdef FEAT_JOB_CHANNEL
20823 static void
20824f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20825{
20826 rettv->v_type = VAR_CHANNEL;
20827 rettv->vval.v_channel = NULL;
20828}
20829#endif
20830
20831 static void
20832f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20833{
20834 rettv->v_type = VAR_DICT;
20835 rettv->vval.v_dict = NULL;
20836}
20837
20838#ifdef FEAT_JOB_CHANNEL
20839 static void
20840f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20841{
20842 rettv->v_type = VAR_JOB;
20843 rettv->vval.v_job = NULL;
20844}
20845#endif
20846
20847 static void
20848f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20849{
20850 rettv->v_type = VAR_LIST;
20851 rettv->vval.v_list = NULL;
20852}
20853
20854 static void
20855f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20856{
20857 rettv->v_type = VAR_PARTIAL;
20858 rettv->vval.v_partial = NULL;
20859}
20860
20861 static void
20862f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20863{
20864 rettv->v_type = VAR_STRING;
20865 rettv->vval.v_string = NULL;
20866}
20867
Bram Moolenaar975b5272016-03-15 23:10:59 +010020868#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20869/*
20870 * Get a callback from "arg". It can be a Funcref or a function name.
20871 * When "arg" is zero return an empty string.
20872 * Return NULL for an invalid argument.
20873 */
20874 char_u *
20875get_callback(typval_T *arg, partial_T **pp)
20876{
20877 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20878 {
20879 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020880 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020881 return (*pp)->pt_name;
20882 }
20883 *pp = NULL;
20884 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20885 return arg->vval.v_string;
20886 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20887 return (char_u *)"";
20888 EMSG(_("E921: Invalid callback argument"));
20889 return NULL;
20890}
20891#endif
20892
20893#ifdef FEAT_TIMERS
20894/*
20895 * "timer_start(time, callback [, options])" function
20896 */
20897 static void
20898f_timer_start(typval_T *argvars, typval_T *rettv)
20899{
20900 long msec = get_tv_number(&argvars[0]);
20901 timer_T *timer;
20902 int repeat = 0;
20903 char_u *callback;
20904 dict_T *dict;
20905
Bram Moolenaar38499922016-04-22 20:46:52 +020020906 if (check_secure())
20907 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020908 if (argvars[2].v_type != VAR_UNKNOWN)
20909 {
20910 if (argvars[2].v_type != VAR_DICT
20911 || (dict = argvars[2].vval.v_dict) == NULL)
20912 {
20913 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20914 return;
20915 }
20916 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20917 repeat = get_dict_number(dict, (char_u *)"repeat");
20918 }
20919
20920 timer = create_timer(msec, repeat);
20921 callback = get_callback(&argvars[1], &timer->tr_partial);
20922 if (callback == NULL)
20923 {
20924 stop_timer(timer);
20925 rettv->vval.v_number = -1;
20926 }
20927 else
20928 {
20929 timer->tr_callback = vim_strsave(callback);
20930 rettv->vval.v_number = timer->tr_id;
20931 }
20932}
20933
20934/*
20935 * "timer_stop(timer)" function
20936 */
20937 static void
20938f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20939{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020940 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020941
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020942 if (argvars[0].v_type != VAR_NUMBER)
20943 {
20944 EMSG(_(e_number_exp));
20945 return;
20946 }
20947 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020948 if (timer != NULL)
20949 stop_timer(timer);
20950}
20951#endif
20952
Bram Moolenaard52d9742005-08-21 22:20:28 +000020953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 * "tolower(string)" function
20955 */
20956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020957f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958{
20959 char_u *p;
20960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020961 p = vim_strsave(get_tv_string(&argvars[0]));
20962 rettv->v_type = VAR_STRING;
20963 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964
20965 if (p != NULL)
20966 while (*p != NUL)
20967 {
20968#ifdef FEAT_MBYTE
20969 int l;
20970
20971 if (enc_utf8)
20972 {
20973 int c, lc;
20974
20975 c = utf_ptr2char(p);
20976 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020977 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 /* TODO: reallocate string when byte count changes. */
20979 if (utf_char2len(lc) == l)
20980 utf_char2bytes(lc, p);
20981 p += l;
20982 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020983 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 p += l; /* skip multi-byte character */
20985 else
20986#endif
20987 {
20988 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20989 ++p;
20990 }
20991 }
20992}
20993
20994/*
20995 * "toupper(string)" function
20996 */
20997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020998f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021000 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021001 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002}
21003
21004/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021005 * "tr(string, fromstr, tostr)" function
21006 */
21007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021008f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021009{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021010 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021011 char_u *fromstr;
21012 char_u *tostr;
21013 char_u *p;
21014#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021015 int inlen;
21016 int fromlen;
21017 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021018 int idx;
21019 char_u *cpstr;
21020 int cplen;
21021 int first = TRUE;
21022#endif
21023 char_u buf[NUMBUFLEN];
21024 char_u buf2[NUMBUFLEN];
21025 garray_T ga;
21026
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021027 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021028 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21029 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021030
21031 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 rettv->v_type = VAR_STRING;
21033 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021034 if (fromstr == NULL || tostr == NULL)
21035 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021036 ga_init2(&ga, (int)sizeof(char), 80);
21037
21038#ifdef FEAT_MBYTE
21039 if (!has_mbyte)
21040#endif
21041 /* not multi-byte: fromstr and tostr must be the same length */
21042 if (STRLEN(fromstr) != STRLEN(tostr))
21043 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021044#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021045error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021046#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021047 EMSG2(_(e_invarg2), fromstr);
21048 ga_clear(&ga);
21049 return;
21050 }
21051
21052 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021053 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021054 {
21055#ifdef FEAT_MBYTE
21056 if (has_mbyte)
21057 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021058 inlen = (*mb_ptr2len)(in_str);
21059 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021060 cplen = inlen;
21061 idx = 0;
21062 for (p = fromstr; *p != NUL; p += fromlen)
21063 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021064 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021065 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021066 {
21067 for (p = tostr; *p != NUL; p += tolen)
21068 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021069 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021070 if (idx-- == 0)
21071 {
21072 cplen = tolen;
21073 cpstr = p;
21074 break;
21075 }
21076 }
21077 if (*p == NUL) /* tostr is shorter than fromstr */
21078 goto error;
21079 break;
21080 }
21081 ++idx;
21082 }
21083
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021084 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021085 {
21086 /* Check that fromstr and tostr have the same number of
21087 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021088 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021089 first = FALSE;
21090 for (p = tostr; *p != NUL; p += tolen)
21091 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021092 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021093 --idx;
21094 }
21095 if (idx != 0)
21096 goto error;
21097 }
21098
Bram Moolenaarcde88542015-08-11 19:14:00 +020021099 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021100 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021101 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021102
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021103 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021104 }
21105 else
21106#endif
21107 {
21108 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021109 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021110 if (p != NULL)
21111 ga_append(&ga, tostr[p - fromstr]);
21112 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021113 ga_append(&ga, *in_str);
21114 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021115 }
21116 }
21117
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021118 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021119 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021120 ga_append(&ga, NUL);
21121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021122 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021123}
21124
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021125#ifdef FEAT_FLOAT
21126/*
21127 * "trunc({float})" function
21128 */
21129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021131{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021132 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021133
21134 rettv->v_type = VAR_FLOAT;
21135 if (get_float_arg(argvars, &f) == OK)
21136 /* trunc() is not in C90, use floor() or ceil() instead. */
21137 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21138 else
21139 rettv->vval.v_float = 0.0;
21140}
21141#endif
21142
Bram Moolenaar8299df92004-07-10 09:47:34 +000021143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 * "type(expr)" function
21145 */
21146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021149 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021150
21151 switch (argvars[0].v_type)
21152 {
21153 case VAR_NUMBER: n = 0; break;
21154 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021155 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021156 case VAR_FUNC: n = 2; break;
21157 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021158 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021159 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021160 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021161 if (argvars[0].vval.v_number == VVAL_FALSE
21162 || argvars[0].vval.v_number == VVAL_TRUE)
21163 n = 6;
21164 else
21165 n = 7;
21166 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021167 case VAR_JOB: n = 8; break;
21168 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021169 case VAR_UNKNOWN:
21170 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21171 n = -1;
21172 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021173 }
21174 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175}
21176
21177/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021178 * "undofile(name)" function
21179 */
21180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021182{
21183 rettv->v_type = VAR_STRING;
21184#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021185 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021186 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021187
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021188 if (*fname == NUL)
21189 {
21190 /* If there is no file name there will be no undo file. */
21191 rettv->vval.v_string = NULL;
21192 }
21193 else
21194 {
21195 char_u *ffname = FullName_save(fname, FALSE);
21196
21197 if (ffname != NULL)
21198 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21199 vim_free(ffname);
21200 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021201 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021202#else
21203 rettv->vval.v_string = NULL;
21204#endif
21205}
21206
21207/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021208 * "undotree()" function
21209 */
21210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021211f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021212{
21213 if (rettv_dict_alloc(rettv) == OK)
21214 {
21215 dict_T *dict = rettv->vval.v_dict;
21216 list_T *list;
21217
Bram Moolenaar730cde92010-06-27 05:18:54 +020021218 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021219 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021220 dict_add_nr_str(dict, "save_last",
21221 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021222 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21223 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021224 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021225
21226 list = list_alloc();
21227 if (list != NULL)
21228 {
21229 u_eval_tree(curbuf->b_u_oldhead, list);
21230 dict_add_list(dict, "entries", list);
21231 }
21232 }
21233}
21234
21235/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021236 * "values(dict)" function
21237 */
21238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021239f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021240{
21241 dict_list(argvars, rettv, 1);
21242}
21243
21244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 * "virtcol(string)" function
21246 */
21247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021248f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249{
21250 colnr_T vcol = 0;
21251 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021252 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021254 fp = var2fpos(&argvars[0], FALSE, &fnum);
21255 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21256 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 {
21258 getvvcol(curwin, fp, NULL, NULL, &vcol);
21259 ++vcol;
21260 }
21261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021262 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263}
21264
21265/*
21266 * "visualmode()" function
21267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021269f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 char_u str[2];
21272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021273 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 str[0] = curbuf->b_visual_mode_eval;
21275 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021276 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277
21278 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021279 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281}
21282
21283/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021284 * "wildmenumode()" function
21285 */
21286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021287f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021288{
21289#ifdef FEAT_WILDMENU
21290 if (wild_menu_showing)
21291 rettv->vval.v_number = 1;
21292#endif
21293}
21294
21295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 * "winbufnr(nr)" function
21297 */
21298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300{
21301 win_T *wp;
21302
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021303 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021305 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021307 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308}
21309
21310/*
21311 * "wincol()" function
21312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021314f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315{
21316 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021317 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318}
21319
21320/*
21321 * "winheight(nr)" function
21322 */
21323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021324f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325{
21326 win_T *wp;
21327
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021328 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021330 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021332 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333}
21334
21335/*
21336 * "winline()" function
21337 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340{
21341 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021342 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343}
21344
21345/*
21346 * "winnr()" function
21347 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021349f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350{
21351 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021352
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021354 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021356 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357}
21358
21359/*
21360 * "winrestcmd()" function
21361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021363f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364{
21365#ifdef FEAT_WINDOWS
21366 win_T *wp;
21367 int winnr = 1;
21368 garray_T ga;
21369 char_u buf[50];
21370
21371 ga_init2(&ga, (int)sizeof(char), 70);
21372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21373 {
21374 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21375 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21377 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 ++winnr;
21379 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021380 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021382 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021384 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021386 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387}
21388
21389/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021390 * "winrestview()" function
21391 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021393f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021394{
21395 dict_T *dict;
21396
21397 if (argvars[0].v_type != VAR_DICT
21398 || (dict = argvars[0].vval.v_dict) == NULL)
21399 EMSG(_(e_invarg));
21400 else
21401 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021402 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21403 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21404 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21405 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021406#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021407 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21408 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021409#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021410 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21411 {
21412 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21413 curwin->w_set_curswant = FALSE;
21414 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021415
Bram Moolenaar82c25852014-05-28 16:47:16 +020021416 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21417 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021418#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021419 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21420 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021421#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021422 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21423 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21424 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21425 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021426
21427 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021428 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021429# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021430 win_new_width(curwin, W_WIDTH(curwin));
21431# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021432 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021433
Bram Moolenaarb851a962014-10-31 15:45:52 +010021434 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021435 curwin->w_topline = 1;
21436 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21437 curwin->w_topline = curbuf->b_ml.ml_line_count;
21438#ifdef FEAT_DIFF
21439 check_topfill(curwin, TRUE);
21440#endif
21441 }
21442}
21443
21444/*
21445 * "winsaveview()" function
21446 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021448f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021449{
21450 dict_T *dict;
21451
Bram Moolenaara800b422010-06-27 01:15:55 +020021452 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021453 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021454 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021455
21456 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21457 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21458#ifdef FEAT_VIRTUALEDIT
21459 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21460#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021461 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021462 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21463
21464 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21465#ifdef FEAT_DIFF
21466 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21467#endif
21468 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21469 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21470}
21471
21472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473 * "winwidth(nr)" function
21474 */
21475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021476f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477{
21478 win_T *wp;
21479
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021480 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021482 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021484#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021485 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021487 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488#endif
21489}
21490
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021492 * "wordcount()" function
21493 */
21494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021495f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021496{
21497 if (rettv_dict_alloc(rettv) == FAIL)
21498 return;
21499 cursor_pos_info(rettv->vval.v_dict);
21500}
21501
21502/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021503 * Write list of strings to file
21504 */
21505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021506write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021507{
21508 listitem_T *li;
21509 int c;
21510 int ret = OK;
21511 char_u *s;
21512
21513 for (li = list->lv_first; li != NULL; li = li->li_next)
21514 {
21515 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21516 {
21517 if (*s == '\n')
21518 c = putc(NUL, fd);
21519 else
21520 c = putc(*s, fd);
21521 if (c == EOF)
21522 {
21523 ret = FAIL;
21524 break;
21525 }
21526 }
21527 if (!binary || li->li_next != NULL)
21528 if (putc('\n', fd) == EOF)
21529 {
21530 ret = FAIL;
21531 break;
21532 }
21533 if (ret == FAIL)
21534 {
21535 EMSG(_(e_write));
21536 break;
21537 }
21538 }
21539 return ret;
21540}
21541
21542/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021543 * "writefile()" function
21544 */
21545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021546f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021547{
21548 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021549 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021550 char_u *fname;
21551 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021552 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021553
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021554 if (check_restricted() || check_secure())
21555 return;
21556
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021557 if (argvars[0].v_type != VAR_LIST)
21558 {
21559 EMSG2(_(e_listarg), "writefile()");
21560 return;
21561 }
21562 if (argvars[0].vval.v_list == NULL)
21563 return;
21564
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021565 if (argvars[2].v_type != VAR_UNKNOWN)
21566 {
21567 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21568 binary = TRUE;
21569 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21570 append = TRUE;
21571 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021572
21573 /* Always open the file in binary mode, library functions have a mind of
21574 * their own about CR-LF conversion. */
21575 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021576 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21577 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021578 {
21579 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21580 ret = -1;
21581 }
21582 else
21583 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021584 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21585 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021586 fclose(fd);
21587 }
21588
21589 rettv->vval.v_number = ret;
21590}
21591
21592/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021593 * "xor(expr, expr)" function
21594 */
21595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021596f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021597{
21598 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21599 ^ get_tv_number_chk(&argvars[1], NULL);
21600}
21601
21602
21603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021605 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 */
21607 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021608var2fpos(
21609 typval_T *varp,
21610 int dollar_lnum, /* TRUE when $ is last line */
21611 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021613 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021615 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616
Bram Moolenaara5525202006-03-02 22:52:09 +000021617 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021618 if (varp->v_type == VAR_LIST)
21619 {
21620 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021621 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021622 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021623 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021624
21625 l = varp->vval.v_list;
21626 if (l == NULL)
21627 return NULL;
21628
21629 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021630 pos.lnum = list_find_nr(l, 0L, &error);
21631 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021632 return NULL; /* invalid line number */
21633
21634 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021635 pos.col = list_find_nr(l, 1L, &error);
21636 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021637 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021638 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021639
21640 /* We accept "$" for the column number: last column. */
21641 li = list_find(l, 1L);
21642 if (li != NULL && li->li_tv.v_type == VAR_STRING
21643 && li->li_tv.vval.v_string != NULL
21644 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21645 pos.col = len + 1;
21646
Bram Moolenaara5525202006-03-02 22:52:09 +000021647 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021648 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021649 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021650 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021651
Bram Moolenaara5525202006-03-02 22:52:09 +000021652#ifdef FEAT_VIRTUALEDIT
21653 /* Get the virtual offset. Defaults to zero. */
21654 pos.coladd = list_find_nr(l, 2L, &error);
21655 if (error)
21656 pos.coladd = 0;
21657#endif
21658
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021659 return &pos;
21660 }
21661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021662 name = get_tv_string_chk(varp);
21663 if (name == NULL)
21664 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021665 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021667 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21668 {
21669 if (VIsual_active)
21670 return &VIsual;
21671 return &curwin->w_cursor;
21672 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021673 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021675 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21677 return NULL;
21678 return pp;
21679 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021680
21681#ifdef FEAT_VIRTUALEDIT
21682 pos.coladd = 0;
21683#endif
21684
Bram Moolenaar477933c2007-07-17 14:32:23 +000021685 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021686 {
21687 pos.col = 0;
21688 if (name[1] == '0') /* "w0": first visible line */
21689 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021690 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021691 pos.lnum = curwin->w_topline;
21692 return &pos;
21693 }
21694 else if (name[1] == '$') /* "w$": last visible line */
21695 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021696 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021697 pos.lnum = curwin->w_botline - 1;
21698 return &pos;
21699 }
21700 }
21701 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021703 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 {
21705 pos.lnum = curbuf->b_ml.ml_line_count;
21706 pos.col = 0;
21707 }
21708 else
21709 {
21710 pos.lnum = curwin->w_cursor.lnum;
21711 pos.col = (colnr_T)STRLEN(ml_get_curline());
21712 }
21713 return &pos;
21714 }
21715 return NULL;
21716}
21717
21718/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021719 * Convert list in "arg" into a position and optional file number.
21720 * When "fnump" is NULL there is no file number, only 3 items.
21721 * Note that the column is passed on as-is, the caller may want to decrement
21722 * it to use 1 for the first column.
21723 * Return FAIL when conversion is not possible, doesn't check the position for
21724 * validity.
21725 */
21726 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021727list2fpos(
21728 typval_T *arg,
21729 pos_T *posp,
21730 int *fnump,
21731 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021732{
21733 list_T *l = arg->vval.v_list;
21734 long i = 0;
21735 long n;
21736
Bram Moolenaar493c1782014-05-28 14:34:46 +020021737 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21738 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021739 if (arg->v_type != VAR_LIST
21740 || l == NULL
21741 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021742 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021743 return FAIL;
21744
21745 if (fnump != NULL)
21746 {
21747 n = list_find_nr(l, i++, NULL); /* fnum */
21748 if (n < 0)
21749 return FAIL;
21750 if (n == 0)
21751 n = curbuf->b_fnum; /* current buffer */
21752 *fnump = n;
21753 }
21754
21755 n = list_find_nr(l, i++, NULL); /* lnum */
21756 if (n < 0)
21757 return FAIL;
21758 posp->lnum = n;
21759
21760 n = list_find_nr(l, i++, NULL); /* col */
21761 if (n < 0)
21762 return FAIL;
21763 posp->col = n;
21764
21765#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021766 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021767 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021768 posp->coladd = 0;
21769 else
21770 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021771#endif
21772
Bram Moolenaar493c1782014-05-28 14:34:46 +020021773 if (curswantp != NULL)
21774 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21775
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021776 return OK;
21777}
21778
21779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 * Get the length of an environment variable name.
21781 * Advance "arg" to the first character after the name.
21782 * Return 0 for error.
21783 */
21784 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021785get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
21787 char_u *p;
21788 int len;
21789
21790 for (p = *arg; vim_isIDc(*p); ++p)
21791 ;
21792 if (p == *arg) /* no name found */
21793 return 0;
21794
21795 len = (int)(p - *arg);
21796 *arg = p;
21797 return len;
21798}
21799
21800/*
21801 * Get the length of the name of a function or internal variable.
21802 * "arg" is advanced to the first non-white character after the name.
21803 * Return 0 if something is wrong.
21804 */
21805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021806get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807{
21808 char_u *p;
21809 int len;
21810
21811 /* Find the end of the name. */
21812 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021813 {
21814 if (*p == ':')
21815 {
21816 /* "s:" is start of "s:var", but "n:" is not and can be used in
21817 * slice "[n:]". Also "xx:" is not a namespace. */
21818 len = (int)(p - *arg);
21819 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21820 || len > 1)
21821 break;
21822 }
21823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 if (p == *arg) /* no name found */
21825 return 0;
21826
21827 len = (int)(p - *arg);
21828 *arg = skipwhite(p);
21829
21830 return len;
21831}
21832
21833/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021834 * Get the length of the name of a variable or function.
21835 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021837 * Return -1 if curly braces expansion failed.
21838 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 * If the name contains 'magic' {}'s, expand them and return the
21840 * expanded name in an allocated string via 'alias' - caller must free.
21841 */
21842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021843get_name_len(
21844 char_u **arg,
21845 char_u **alias,
21846 int evaluate,
21847 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848{
21849 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 char_u *p;
21851 char_u *expr_start;
21852 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853
21854 *alias = NULL; /* default to no alias */
21855
21856 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21857 && (*arg)[2] == (int)KE_SNR)
21858 {
21859 /* hard coded <SNR>, already translated */
21860 *arg += 3;
21861 return get_id_len(arg) + 3;
21862 }
21863 len = eval_fname_script(*arg);
21864 if (len > 0)
21865 {
21866 /* literal "<SID>", "s:" or "<SNR>" */
21867 *arg += len;
21868 }
21869
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021871 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021873 p = find_name_end(*arg, &expr_start, &expr_end,
21874 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 if (expr_start != NULL)
21876 {
21877 char_u *temp_string;
21878
21879 if (!evaluate)
21880 {
21881 len += (int)(p - *arg);
21882 *arg = skipwhite(p);
21883 return len;
21884 }
21885
21886 /*
21887 * Include any <SID> etc in the expanded string:
21888 * Thus the -len here.
21889 */
21890 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21891 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021892 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 *alias = temp_string;
21894 *arg = skipwhite(p);
21895 return (int)STRLEN(temp_string);
21896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897
21898 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021899 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 EMSG2(_(e_invexpr2), *arg);
21901
21902 return len;
21903}
21904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905/*
21906 * Find the end of a variable or function name, taking care of magic braces.
21907 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21908 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021909 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021910 * Return a pointer to just after the name. Equal to "arg" if there is no
21911 * valid name.
21912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021914find_name_end(
21915 char_u *arg,
21916 char_u **expr_start,
21917 char_u **expr_end,
21918 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021920 int mb_nest = 0;
21921 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021923 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021925 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021927 *expr_start = NULL;
21928 *expr_end = NULL;
21929 }
21930
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021931 /* Quick check for valid starting character. */
21932 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21933 return arg;
21934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021935 for (p = arg; *p != NUL
21936 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021937 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021938 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021939 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021940 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021941 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021942 if (*p == '\'')
21943 {
21944 /* skip over 'string' to avoid counting [ and ] inside it. */
21945 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21946 ;
21947 if (*p == NUL)
21948 break;
21949 }
21950 else if (*p == '"')
21951 {
21952 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21953 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21954 if (*p == '\\' && p[1] != NUL)
21955 ++p;
21956 if (*p == NUL)
21957 break;
21958 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021959 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21960 {
21961 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021962 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021963 len = (int)(p - arg);
21964 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021965 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021966 break;
21967 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021969 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021971 if (*p == '[')
21972 ++br_nest;
21973 else if (*p == ']')
21974 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021977 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021979 if (*p == '{')
21980 {
21981 mb_nest++;
21982 if (expr_start != NULL && *expr_start == NULL)
21983 *expr_start = p;
21984 }
21985 else if (*p == '}')
21986 {
21987 mb_nest--;
21988 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21989 *expr_end = p;
21990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 }
21993
21994 return p;
21995}
21996
21997/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021998 * Expands out the 'magic' {}'s in a variable/function name.
21999 * Note that this can call itself recursively, to deal with
22000 * constructs like foo{bar}{baz}{bam}
22001 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22002 * "in_start" ^
22003 * "expr_start" ^
22004 * "expr_end" ^
22005 * "in_end" ^
22006 *
22007 * Returns a new allocated string, which the caller must free.
22008 * Returns NULL for failure.
22009 */
22010 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022011make_expanded_name(
22012 char_u *in_start,
22013 char_u *expr_start,
22014 char_u *expr_end,
22015 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022016{
22017 char_u c1;
22018 char_u *retval = NULL;
22019 char_u *temp_result;
22020 char_u *nextcmd = NULL;
22021
22022 if (expr_end == NULL || in_end == NULL)
22023 return NULL;
22024 *expr_start = NUL;
22025 *expr_end = NUL;
22026 c1 = *in_end;
22027 *in_end = NUL;
22028
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022029 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022030 if (temp_result != NULL && nextcmd == NULL)
22031 {
22032 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22033 + (in_end - expr_end) + 1));
22034 if (retval != NULL)
22035 {
22036 STRCPY(retval, in_start);
22037 STRCAT(retval, temp_result);
22038 STRCAT(retval, expr_end + 1);
22039 }
22040 }
22041 vim_free(temp_result);
22042
22043 *in_end = c1; /* put char back for error messages */
22044 *expr_start = '{';
22045 *expr_end = '}';
22046
22047 if (retval != NULL)
22048 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022049 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022050 if (expr_start != NULL)
22051 {
22052 /* Further expansion! */
22053 temp_result = make_expanded_name(retval, expr_start,
22054 expr_end, temp_result);
22055 vim_free(retval);
22056 retval = temp_result;
22057 }
22058 }
22059
22060 return retval;
22061}
22062
22063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022065 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022066 */
22067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022068eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022070 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22071}
22072
22073/*
22074 * Return TRUE if character "c" can be used as the first character in a
22075 * variable or function name (excluding '{' and '}').
22076 */
22077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022078eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022079{
22080 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081}
22082
22083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084 * Set number v: variable to "val".
22085 */
22086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022087set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022089 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090}
22091
22092/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022093 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 */
22095 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022096get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022098 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099}
22100
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022101/*
22102 * Get string v: variable value. Uses a static buffer, can only be used once.
22103 */
22104 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022105get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022106{
22107 return get_tv_string(&vimvars[idx].vv_tv);
22108}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022109
Bram Moolenaar071d4272004-06-13 20:20:40 +000022110/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022111 * Get List v: variable value. Caller must take care of reference count when
22112 * needed.
22113 */
22114 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022115get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022116{
22117 return vimvars[idx].vv_list;
22118}
22119
22120/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022121 * Set v:char to character "c".
22122 */
22123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022124set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022125{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022126 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022127
22128#ifdef FEAT_MBYTE
22129 if (has_mbyte)
22130 buf[(*mb_char2bytes)(c, buf)] = NUL;
22131 else
22132#endif
22133 {
22134 buf[0] = c;
22135 buf[1] = NUL;
22136 }
22137 set_vim_var_string(VV_CHAR, buf, -1);
22138}
22139
22140/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022141 * Set v:count to "count" and v:count1 to "count1".
22142 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 */
22144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022145set_vcount(
22146 long count,
22147 long count1,
22148 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022150 if (set_prevcount)
22151 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022152 vimvars[VV_COUNT].vv_nr = count;
22153 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154}
22155
22156/*
22157 * Set string v: variable to a copy of "val".
22158 */
22159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022160set_vim_var_string(
22161 int idx,
22162 char_u *val,
22163 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164{
Bram Moolenaara542c682016-01-31 16:28:04 +010022165 clear_tv(&vimvars[idx].vv_di.di_tv);
22166 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022168 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022170 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022172 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173}
22174
22175/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022176 * Set List v: variable to "val".
22177 */
22178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022179set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022180{
Bram Moolenaara542c682016-01-31 16:28:04 +010022181 clear_tv(&vimvars[idx].vv_di.di_tv);
22182 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022183 vimvars[idx].vv_list = val;
22184 if (val != NULL)
22185 ++val->lv_refcount;
22186}
22187
22188/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022189 * Set Dictionary v: variable to "val".
22190 */
22191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022192set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022193{
22194 int todo;
22195 hashitem_T *hi;
22196
Bram Moolenaara542c682016-01-31 16:28:04 +010022197 clear_tv(&vimvars[idx].vv_di.di_tv);
22198 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022199 vimvars[idx].vv_dict = val;
22200 if (val != NULL)
22201 {
22202 ++val->dv_refcount;
22203
22204 /* Set readonly */
22205 todo = (int)val->dv_hashtab.ht_used;
22206 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22207 {
22208 if (HASHITEM_EMPTY(hi))
22209 continue;
22210 --todo;
22211 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22212 }
22213 }
22214}
22215
22216/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 * Set v:register if needed.
22218 */
22219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022220set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221{
22222 char_u regname;
22223
22224 if (c == 0 || c == ' ')
22225 regname = '"';
22226 else
22227 regname = c;
22228 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022229 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 set_vim_var_string(VV_REG, &regname, 1);
22231}
22232
22233/*
22234 * Get or set v:exception. If "oldval" == NULL, return the current value.
22235 * Otherwise, restore the value to "oldval" and return NULL.
22236 * Must always be called in pairs to save and restore v:exception! Does not
22237 * take care of memory allocations.
22238 */
22239 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022240v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241{
22242 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022243 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244
Bram Moolenaare9a41262005-01-15 22:18:47 +000022245 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 return NULL;
22247}
22248
22249/*
22250 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22251 * Otherwise, restore the value to "oldval" and return NULL.
22252 * Must always be called in pairs to save and restore v:throwpoint! Does not
22253 * take care of memory allocations.
22254 */
22255 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022256v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022257{
22258 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022259 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260
Bram Moolenaare9a41262005-01-15 22:18:47 +000022261 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 return NULL;
22263}
22264
22265#if defined(FEAT_AUTOCMD) || defined(PROTO)
22266/*
22267 * Set v:cmdarg.
22268 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22269 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22270 * Must always be called in pairs!
22271 */
22272 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022273set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274{
22275 char_u *oldval;
22276 char_u *newval;
22277 unsigned len;
22278
Bram Moolenaare9a41262005-01-15 22:18:47 +000022279 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022280 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022282 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022283 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022284 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 }
22286
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022287 if (eap->force_bin == FORCE_BIN)
22288 len = 6;
22289 else if (eap->force_bin == FORCE_NOBIN)
22290 len = 8;
22291 else
22292 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022293
22294 if (eap->read_edit)
22295 len += 7;
22296
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022297 if (eap->force_ff != 0)
22298 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22299# ifdef FEAT_MBYTE
22300 if (eap->force_enc != 0)
22301 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022302 if (eap->bad_char != 0)
22303 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022304# endif
22305
22306 newval = alloc(len + 1);
22307 if (newval == NULL)
22308 return NULL;
22309
22310 if (eap->force_bin == FORCE_BIN)
22311 sprintf((char *)newval, " ++bin");
22312 else if (eap->force_bin == FORCE_NOBIN)
22313 sprintf((char *)newval, " ++nobin");
22314 else
22315 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022316
22317 if (eap->read_edit)
22318 STRCAT(newval, " ++edit");
22319
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022320 if (eap->force_ff != 0)
22321 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22322 eap->cmd + eap->force_ff);
22323# ifdef FEAT_MBYTE
22324 if (eap->force_enc != 0)
22325 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22326 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022327 if (eap->bad_char == BAD_KEEP)
22328 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22329 else if (eap->bad_char == BAD_DROP)
22330 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22331 else if (eap->bad_char != 0)
22332 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022333# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022334 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022335 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336}
22337#endif
22338
22339/*
22340 * Get the value of internal variable "name".
22341 * Return OK or FAIL.
22342 */
22343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344get_var_tv(
22345 char_u *name,
22346 int len, /* length of "name" */
22347 typval_T *rettv, /* NULL when only checking existence */
22348 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22349 int verbose, /* may give error message */
22350 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351{
22352 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022353 typval_T *tv = NULL;
22354 typval_T atv;
22355 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357
22358 /* truncate the name, so that we can use strcmp() */
22359 cc = name[len];
22360 name[len] = NUL;
22361
22362 /*
22363 * Check for "b:changedtick".
22364 */
22365 if (STRCMP(name, "b:changedtick") == 0)
22366 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022367 atv.v_type = VAR_NUMBER;
22368 atv.vval.v_number = curbuf->b_changedtick;
22369 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 }
22371
22372 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 * Check for user-defined variables.
22374 */
22375 else
22376 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022377 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022379 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022380 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022381 if (dip != NULL)
22382 *dip = v;
22383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 }
22385
Bram Moolenaare9a41262005-01-15 22:18:47 +000022386 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022388 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022389 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 ret = FAIL;
22391 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022392 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022393 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394
22395 name[len] = cc;
22396
22397 return ret;
22398}
22399
22400/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022401 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22402 * Also handle function call with Funcref variable: func(expr)
22403 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22404 */
22405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022406handle_subscript(
22407 char_u **arg,
22408 typval_T *rettv,
22409 int evaluate, /* do more than finding the end */
22410 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022411{
22412 int ret = OK;
22413 dict_T *selfdict = NULL;
22414 char_u *s;
22415 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022416 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022417
22418 while (ret == OK
22419 && (**arg == '['
22420 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022421 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22422 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022423 && !vim_iswhite(*(*arg - 1)))
22424 {
22425 if (**arg == '(')
22426 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022427 partial_T *pt = NULL;
22428
Bram Moolenaard9fba312005-06-26 22:34:35 +000022429 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022430 if (evaluate)
22431 {
22432 functv = *rettv;
22433 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022434
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022435 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022436 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022437 {
22438 pt = functv.vval.v_partial;
22439 s = pt->pt_name;
22440 }
22441 else
22442 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022443 }
22444 else
22445 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022446 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022447 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022448 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022449
22450 /* Clear the funcref afterwards, so that deleting it while
22451 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022452 if (evaluate)
22453 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022454
22455 /* Stop the expression evaluation when immediately aborting on
22456 * error, or when an interrupt occurred or an exception was thrown
22457 * but not caught. */
22458 if (aborting())
22459 {
22460 if (ret == OK)
22461 clear_tv(rettv);
22462 ret = FAIL;
22463 }
22464 dict_unref(selfdict);
22465 selfdict = NULL;
22466 }
22467 else /* **arg == '[' || **arg == '.' */
22468 {
22469 dict_unref(selfdict);
22470 if (rettv->v_type == VAR_DICT)
22471 {
22472 selfdict = rettv->vval.v_dict;
22473 if (selfdict != NULL)
22474 ++selfdict->dv_refcount;
22475 }
22476 else
22477 selfdict = NULL;
22478 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22479 {
22480 clear_tv(rettv);
22481 ret = FAIL;
22482 }
22483 }
22484 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022485
Bram Moolenaar1d429612016-05-24 15:44:17 +020022486 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22487 * Don't do this when "Func" is already a partial that was bound
22488 * explicitly (pt_auto is FALSE). */
22489 if (selfdict != NULL
22490 && (rettv->v_type == VAR_FUNC
22491 || (rettv->v_type == VAR_PARTIAL
22492 && (rettv->vval.v_partial->pt_auto
22493 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022494 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022495 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22496 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022497 char_u *tofree = NULL;
22498 ufunc_T *fp;
22499 char_u fname_buf[FLEN_FIXED + 1];
22500 int error;
22501
22502 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022503 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022504 fp = find_func(fname);
22505 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022506
Bram Moolenaar65639032016-03-16 21:40:30 +010022507 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022508 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022509 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22510
22511 if (pt != NULL)
22512 {
22513 pt->pt_refcount = 1;
22514 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022515 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022516 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022517 if (rettv->v_type == VAR_FUNC)
22518 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022519 /* Just a function: Take over the function name and use
22520 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022521 pt->pt_name = rettv->vval.v_string;
22522 }
22523 else
22524 {
22525 partial_T *ret_pt = rettv->vval.v_partial;
22526 int i;
22527
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022528 /* Partial: copy the function name, use selfdict and copy
22529 * args. Can't take over name or args, the partial might
22530 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022531 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022532 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022533 if (ret_pt->pt_argc > 0)
22534 {
22535 pt->pt_argv = (typval_T *)alloc(
22536 sizeof(typval_T) * ret_pt->pt_argc);
22537 if (pt->pt_argv == NULL)
22538 /* out of memory: drop the arguments */
22539 pt->pt_argc = 0;
22540 else
22541 {
22542 pt->pt_argc = ret_pt->pt_argc;
22543 for (i = 0; i < pt->pt_argc; i++)
22544 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22545 }
22546 }
22547 partial_unref(ret_pt);
22548 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022549 rettv->v_type = VAR_PARTIAL;
22550 rettv->vval.v_partial = pt;
22551 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022552 }
22553 }
22554
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022555 dict_unref(selfdict);
22556 return ret;
22557}
22558
22559/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022560 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022561 * value).
22562 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022563 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022564alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022565{
Bram Moolenaar33570922005-01-25 22:26:29 +000022566 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022567}
22568
22569/*
22570 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 * The string "s" must have been allocated, it is consumed.
22572 * Return NULL for out of memory, the variable otherwise.
22573 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022575alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576{
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022579 rettv = alloc_tv();
22580 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022582 rettv->v_type = VAR_STRING;
22583 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 }
22585 else
22586 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022587 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588}
22589
22590/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022591 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022594free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595{
22596 if (varp != NULL)
22597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022598 switch (varp->v_type)
22599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022600 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022601 func_unref(varp->vval.v_string);
22602 /*FALLTHROUGH*/
22603 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022604 vim_free(varp->vval.v_string);
22605 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022606 case VAR_PARTIAL:
22607 partial_unref(varp->vval.v_partial);
22608 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022609 case VAR_LIST:
22610 list_unref(varp->vval.v_list);
22611 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022612 case VAR_DICT:
22613 dict_unref(varp->vval.v_dict);
22614 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022615 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022616#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022617 job_unref(varp->vval.v_job);
22618 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022619#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022620 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022621#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022622 channel_unref(varp->vval.v_channel);
22623 break;
22624#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022625 case VAR_NUMBER:
22626 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022627 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022628 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022629 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 vim_free(varp);
22632 }
22633}
22634
22635/*
22636 * Free the memory for a variable value and set the value to NULL or 0.
22637 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022639clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640{
22641 if (varp != NULL)
22642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022643 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022645 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022646 func_unref(varp->vval.v_string);
22647 /*FALLTHROUGH*/
22648 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649 vim_free(varp->vval.v_string);
22650 varp->vval.v_string = NULL;
22651 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022652 case VAR_PARTIAL:
22653 partial_unref(varp->vval.v_partial);
22654 varp->vval.v_partial = NULL;
22655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022656 case VAR_LIST:
22657 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022658 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022659 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022660 case VAR_DICT:
22661 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022662 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022663 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022664 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022665 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666 varp->vval.v_number = 0;
22667 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022668 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022669#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022670 varp->vval.v_float = 0.0;
22671 break;
22672#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022673 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022674#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022675 job_unref(varp->vval.v_job);
22676 varp->vval.v_job = NULL;
22677#endif
22678 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022679 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022680#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022681 channel_unref(varp->vval.v_channel);
22682 varp->vval.v_channel = NULL;
22683#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022684 case VAR_UNKNOWN:
22685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022687 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 }
22689}
22690
22691/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022692 * Set the value of a variable to NULL without freeing items.
22693 */
22694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022695init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022696{
22697 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022699}
22700
22701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 * Get the number value of a variable.
22703 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022704 * For incompatible types, return 0.
22705 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22706 * caller of incompatible types: it sets *denote to TRUE if "denote"
22707 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022709 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022710get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022712 int error = FALSE;
22713
22714 return get_tv_number_chk(varp, &error); /* return 0L on error */
22715}
22716
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022717 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022718get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022719{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022720 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022724 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022725 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022726 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022727#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022728 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022729 break;
22730#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022731 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022732 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022733 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022734 break;
22735 case VAR_STRING:
22736 if (varp->vval.v_string != NULL)
22737 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022738 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022739 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022740 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022741 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022742 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022743 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022744 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022745 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022746 case VAR_SPECIAL:
22747 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22748 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022749 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022750#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022751 EMSG(_("E910: Using a Job as a Number"));
22752 break;
22753#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022754 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022755#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022756 EMSG(_("E913: Using a Channel as a Number"));
22757 break;
22758#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022759 case VAR_UNKNOWN:
22760 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022763 if (denote == NULL) /* useful for values that must be unsigned */
22764 n = -1;
22765 else
22766 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022767 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768}
22769
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022770#ifdef FEAT_FLOAT
22771 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022773{
22774 switch (varp->v_type)
22775 {
22776 case VAR_NUMBER:
22777 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022778 case VAR_FLOAT:
22779 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022780 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022781 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022782 EMSG(_("E891: Using a Funcref as a Float"));
22783 break;
22784 case VAR_STRING:
22785 EMSG(_("E892: Using a String as a Float"));
22786 break;
22787 case VAR_LIST:
22788 EMSG(_("E893: Using a List as a Float"));
22789 break;
22790 case VAR_DICT:
22791 EMSG(_("E894: Using a Dictionary as a Float"));
22792 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022793 case VAR_SPECIAL:
22794 EMSG(_("E907: Using a special value as a Float"));
22795 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022796 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022797# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022798 EMSG(_("E911: Using a Job as a Float"));
22799 break;
22800# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022801 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022802# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022803 EMSG(_("E914: Using a Channel as a Float"));
22804 break;
22805# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022806 case VAR_UNKNOWN:
22807 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022808 break;
22809 }
22810 return 0;
22811}
22812#endif
22813
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022815 * Get the lnum from the first argument.
22816 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022817 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 */
22819 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022820get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821{
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823 linenr_T lnum;
22824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022825 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 if (lnum == 0) /* no valid number, try using line() */
22827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022828 rettv.v_type = VAR_NUMBER;
22829 f_line(argvars, &rettv);
22830 lnum = rettv.vval.v_number;
22831 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 }
22833 return lnum;
22834}
22835
22836/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022837 * Get the lnum from the first argument.
22838 * Also accepts "$", then "buf" is used.
22839 * Returns 0 on error.
22840 */
22841 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022842get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022843{
22844 if (argvars[0].v_type == VAR_STRING
22845 && argvars[0].vval.v_string != NULL
22846 && argvars[0].vval.v_string[0] == '$'
22847 && buf != NULL)
22848 return buf->b_ml.ml_line_count;
22849 return get_tv_number_chk(&argvars[0], NULL);
22850}
22851
22852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 * Get the string value of a variable.
22854 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022855 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22856 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857 * If the String variable has never been set, return an empty string.
22858 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022859 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22860 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022863get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864{
22865 static char_u mybuf[NUMBUFLEN];
22866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022867 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868}
22869
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022870 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022871get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022873 char_u *res = get_tv_string_buf_chk(varp, buf);
22874
22875 return res != NULL ? res : (char_u *)"";
22876}
22877
Bram Moolenaar7d647822014-04-05 21:28:56 +020022878/*
22879 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22880 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022881 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022882get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022883{
22884 static char_u mybuf[NUMBUFLEN];
22885
22886 return get_tv_string_buf_chk(varp, mybuf);
22887}
22888
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022889 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022890get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022891{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022894 case VAR_NUMBER:
22895 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22896 return buf;
22897 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022898 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022899 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022900 break;
22901 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022902 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022903 break;
22904 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022905 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022906 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022907 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022908#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022909 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022910 break;
22911#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022912 case VAR_STRING:
22913 if (varp->vval.v_string != NULL)
22914 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022915 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022916 case VAR_SPECIAL:
22917 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22918 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022919 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022920#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022921 {
22922 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022923 char *status;
22924
22925 if (job == NULL)
22926 return (char_u *)"no process";
22927 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022928 : job->jv_status == JOB_ENDED ? "dead"
22929 : "run";
22930# ifdef UNIX
22931 vim_snprintf((char *)buf, NUMBUFLEN,
22932 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022933# elif defined(WIN32)
22934 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022935 "process %ld %s",
22936 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022937 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022938# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022939 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022940 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22941# endif
22942 return buf;
22943 }
22944#endif
22945 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022946 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022947#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022948 {
22949 channel_T *channel = varp->vval.v_channel;
22950 char *status = channel_status(channel);
22951
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022952 if (channel == NULL)
22953 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22954 else
22955 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022956 "channel %d %s", channel->ch_id, status);
22957 return buf;
22958 }
22959#endif
22960 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022961 case VAR_UNKNOWN:
22962 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022965 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966}
22967
22968/*
22969 * Find variable "name" in the list of variables.
22970 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022971 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022972 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022973 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022975 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022976find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980
Bram Moolenaara7043832005-01-21 11:56:39 +000022981 ht = find_var_ht(name, &varname);
22982 if (htp != NULL)
22983 *htp = ht;
22984 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022986 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987}
22988
22989/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022990 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022991 * Returns NULL if not found.
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_in_ht(
22995 hashtab_T *ht,
22996 int htname,
22997 char_u *varname,
22998 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022999{
Bram Moolenaar33570922005-01-25 22:26:29 +000023000 hashitem_T *hi;
23001
23002 if (*varname == NUL)
23003 {
23004 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023005 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023006 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023007 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023008 case 'g': return &globvars_var;
23009 case 'v': return &vimvars_var;
23010 case 'b': return &curbuf->b_bufvar;
23011 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023012#ifdef FEAT_WINDOWS
23013 case 't': return &curtab->tp_winvar;
23014#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023015 case 'l': return current_funccal == NULL
23016 ? NULL : &current_funccal->l_vars_var;
23017 case 'a': return current_funccal == NULL
23018 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023019 }
23020 return NULL;
23021 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023022
23023 hi = hash_find(ht, varname);
23024 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023025 {
23026 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023027 * worked find the variable again. Don't auto-load a script if it was
23028 * loaded already, otherwise it would be loaded every time when
23029 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023030 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023031 {
23032 /* Note: script_autoload() may make "hi" invalid. It must either
23033 * be obtained again or not used. */
23034 if (!script_autoload(varname, FALSE) || aborting())
23035 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023036 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023037 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023038 if (HASHITEM_EMPTY(hi))
23039 return NULL;
23040 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023042}
23043
23044/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023045 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023046 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023047 * Set "varname" to the start of name without ':'.
23048 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023049 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023050find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023052 hashitem_T *hi;
23053
Bram Moolenaar73627d02015-08-11 15:46:09 +020023054 if (name[0] == NUL)
23055 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 if (name[1] != ':')
23057 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023058 /* The name must not start with a colon or #. */
23059 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 return NULL;
23061 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023062
23063 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023064 hi = hash_find(&compat_hashtab, name);
23065 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023066 return &compat_hashtab;
23067
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023070 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 }
23072 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023073 if (*name == 'g') /* global variable */
23074 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023075 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23076 */
23077 if (vim_strchr(name + 2, ':') != NULL
23078 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023079 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023081 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023083 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023084#ifdef FEAT_WINDOWS
23085 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023086 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023087#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023088 if (*name == 'v') /* v: variable */
23089 return &vimvarht;
23090 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023091 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023093 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 if (*name == 's' /* script variable */
23095 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23096 return &SCRIPT_VARS(current_SID);
23097 return NULL;
23098}
23099
23100/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023101 * Get function call environment based on bactrace debug level
23102 */
23103 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023104get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023105{
23106 int i;
23107 funccall_T *funccal;
23108 funccall_T *temp_funccal;
23109
23110 funccal = current_funccal;
23111 if (debug_backtrace_level > 0)
23112 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023113 for (i = 0; i < debug_backtrace_level; i++)
23114 {
23115 temp_funccal = funccal->caller;
23116 if (temp_funccal)
23117 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023118 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023119 /* backtrace level overflow. reset to max */
23120 debug_backtrace_level = i;
23121 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023122 }
23123 return funccal;
23124}
23125
23126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023128 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129 * Returns NULL when it doesn't exist.
23130 */
23131 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023132get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133{
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023136 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 if (v == NULL)
23138 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140}
23141
23142/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 * sourcing this script and when executing functions defined in the script.
23145 */
23146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023147new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148{
Bram Moolenaara7043832005-01-21 11:56:39 +000023149 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023150 hashtab_T *ht;
23151 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023152
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23154 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023155 /* Re-allocating ga_data means that an ht_array pointing to
23156 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023157 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023158 for (i = 1; i <= ga_scripts.ga_len; ++i)
23159 {
23160 ht = &SCRIPT_VARS(i);
23161 if (ht->ht_mask == HT_INIT_SIZE - 1)
23162 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023163 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023164 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023165 }
23166
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167 while (ga_scripts.ga_len < id)
23168 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023169 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023170 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023171 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 }
23174 }
23175}
23176
23177/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23179 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 */
23181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023182init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183{
Bram Moolenaar33570922005-01-25 22:26:29 +000023184 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023185 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023186 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023187 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023188 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023189 dict_var->di_tv.vval.v_dict = dict;
23190 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023191 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023192 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23193 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194}
23195
23196/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023197 * Unreference a dictionary initialized by init_var_dict().
23198 */
23199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023200unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023201{
23202 /* Now the dict needs to be freed if no one else is using it, go back to
23203 * normal reference counting. */
23204 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23205 dict_unref(dict);
23206}
23207
23208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023210 * Frees all allocated variables and the value they contain.
23211 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 */
23213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023214vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023215{
23216 vars_clear_ext(ht, TRUE);
23217}
23218
23219/*
23220 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23221 */
23222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023223vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224{
Bram Moolenaara7043832005-01-21 11:56:39 +000023225 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023226 hashitem_T *hi;
23227 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023230 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023231 for (hi = ht->ht_array; todo > 0; ++hi)
23232 {
23233 if (!HASHITEM_EMPTY(hi))
23234 {
23235 --todo;
23236
Bram Moolenaar33570922005-01-25 22:26:29 +000023237 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023238 * ht_array might change then. hash_clear() takes care of it
23239 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023240 v = HI2DI(hi);
23241 if (free_val)
23242 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023243 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023244 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023245 }
23246 }
23247 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023248 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249}
23250
Bram Moolenaara7043832005-01-21 11:56:39 +000023251/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023252 * Delete a variable from hashtab "ht" at item "hi".
23253 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023256delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023257{
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023259
23260 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023261 clear_tv(&di->di_tv);
23262 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263}
23264
23265/*
23266 * List the value of one internal variable.
23267 */
23268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023269list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023271 char_u *tofree;
23272 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023273 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023274
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023275 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023277 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023278 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279}
23280
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023282list_one_var_a(
23283 char_u *prefix,
23284 char_u *name,
23285 int type,
23286 char_u *string,
23287 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288{
Bram Moolenaar31859182007-08-14 20:41:13 +000023289 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23290 msg_start();
23291 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 if (name != NULL) /* "a:" vars don't have a name stored */
23293 msg_puts(name);
23294 msg_putchar(' ');
23295 msg_advance(22);
23296 if (type == VAR_NUMBER)
23297 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023298 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023299 msg_putchar('*');
23300 else if (type == VAR_LIST)
23301 {
23302 msg_putchar('[');
23303 if (*string == '[')
23304 ++string;
23305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023306 else if (type == VAR_DICT)
23307 {
23308 msg_putchar('{');
23309 if (*string == '{')
23310 ++string;
23311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 else
23313 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023314
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023316
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023317 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023318 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023319 if (*first)
23320 {
23321 msg_clr_eos();
23322 *first = FALSE;
23323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324}
23325
23326/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023327 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328 * If the variable already exists, the value is updated.
23329 * Otherwise the variable is created.
23330 */
23331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023332set_var(
23333 char_u *name,
23334 typval_T *tv,
23335 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336{
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023339 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023341 ht = find_var_ht(name, &varname);
23342 if (ht == NULL || *varname == NUL)
23343 {
23344 EMSG2(_(e_illvar), name);
23345 return;
23346 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023347 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023348
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023349 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23350 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023351 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023352
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023356 if (var_check_ro(v->di_flags, name, FALSE)
23357 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023358 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023359
23360 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023361 * Handle setting internal v: variables separately where needed to
23362 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023363 */
23364 if (ht == &vimvarht)
23365 {
23366 if (v->di_tv.v_type == VAR_STRING)
23367 {
23368 vim_free(v->di_tv.vval.v_string);
23369 if (copy || tv->v_type != VAR_STRING)
23370 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23371 else
23372 {
23373 /* Take over the string to avoid an extra alloc/free. */
23374 v->di_tv.vval.v_string = tv->vval.v_string;
23375 tv->vval.v_string = NULL;
23376 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023377 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023378 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023379 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023380 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023382 if (STRCMP(varname, "searchforward") == 0)
23383 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023384#ifdef FEAT_SEARCH_EXTRA
23385 else if (STRCMP(varname, "hlsearch") == 0)
23386 {
23387 no_hlsearch = !v->di_tv.vval.v_number;
23388 redraw_all_later(SOME_VALID);
23389 }
23390#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023391 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023392 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023393 else if (v->di_tv.v_type != tv->v_type)
23394 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023395 }
23396
23397 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 }
23399 else /* add a new variable */
23400 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023401 /* Can't add "v:" variable. */
23402 if (ht == &vimvarht)
23403 {
23404 EMSG2(_(e_illvar), name);
23405 return;
23406 }
23407
Bram Moolenaar92124a32005-06-17 22:03:40 +000023408 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023409 if (!valid_varname(varname))
23410 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023411
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023412 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23413 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023414 if (v == NULL)
23415 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023416 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023417 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023419 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 return;
23421 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023422 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023424
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023425 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023426 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023427 else
23428 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023430 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023431 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433}
23434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023435/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023436 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023437 * Also give an error message.
23438 */
23439 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023440var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023441{
23442 if (flags & DI_FLAGS_RO)
23443 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023444 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023445 return TRUE;
23446 }
23447 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23448 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023449 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023450 return TRUE;
23451 }
23452 return FALSE;
23453}
23454
23455/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023456 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23457 * Also give an error message.
23458 */
23459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023460var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023461{
23462 if (flags & DI_FLAGS_FIX)
23463 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023464 EMSG2(_("E795: Cannot delete variable %s"),
23465 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023466 return TRUE;
23467 }
23468 return FALSE;
23469}
23470
23471/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023472 * Check if a funcref is assigned to a valid variable name.
23473 * Return TRUE and give an error if not.
23474 */
23475 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023476var_check_func_name(
23477 char_u *name, /* points to start of variable name */
23478 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023479{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023480 /* Allow for w: b: s: and t:. */
23481 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023482 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23483 ? name[2] : name[0]))
23484 {
23485 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23486 name);
23487 return TRUE;
23488 }
23489 /* Don't allow hiding a function. When "v" is not NULL we might be
23490 * assigning another function to the same var, the type is checked
23491 * below. */
23492 if (new_var && function_exists(name))
23493 {
23494 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23495 name);
23496 return TRUE;
23497 }
23498 return FALSE;
23499}
23500
23501/*
23502 * Check if a variable name is valid.
23503 * Return FALSE and give an error if not.
23504 */
23505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023506valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023507{
23508 char_u *p;
23509
23510 for (p = varname; *p != NUL; ++p)
23511 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23512 && *p != AUTOLOAD_CHAR)
23513 {
23514 EMSG2(_(e_illvar), varname);
23515 return FALSE;
23516 }
23517 return TRUE;
23518}
23519
23520/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023521 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023522 * Also give an error message, using "name" or _("name") when use_gettext is
23523 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023524 */
23525 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023526tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023527{
23528 if (lock & VAR_LOCKED)
23529 {
23530 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023531 name == NULL ? (char_u *)_("Unknown")
23532 : use_gettext ? (char_u *)_(name)
23533 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023534 return TRUE;
23535 }
23536 if (lock & VAR_FIXED)
23537 {
23538 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023539 name == NULL ? (char_u *)_("Unknown")
23540 : use_gettext ? (char_u *)_(name)
23541 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023542 return TRUE;
23543 }
23544 return FALSE;
23545}
23546
23547/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023548 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023549 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023550 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023551 * It is OK for "from" and "to" to point to the same item. This is used to
23552 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023553 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023554 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023555copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023557 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023558 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023559 switch (from->v_type)
23560 {
23561 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023562 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023563 to->vval.v_number = from->vval.v_number;
23564 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023565 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023566#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023567 to->vval.v_float = from->vval.v_float;
23568 break;
23569#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023570 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023571#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023572 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023573 if (to->vval.v_job != NULL)
23574 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023575 break;
23576#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023577 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023578#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023579 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023580 if (to->vval.v_channel != NULL)
23581 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023582 break;
23583#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023584 case VAR_STRING:
23585 case VAR_FUNC:
23586 if (from->vval.v_string == NULL)
23587 to->vval.v_string = NULL;
23588 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023589 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023590 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023591 if (from->v_type == VAR_FUNC)
23592 func_ref(to->vval.v_string);
23593 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023594 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023595 case VAR_PARTIAL:
23596 if (from->vval.v_partial == NULL)
23597 to->vval.v_partial = NULL;
23598 else
23599 {
23600 to->vval.v_partial = from->vval.v_partial;
23601 ++to->vval.v_partial->pt_refcount;
23602 }
23603 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023604 case VAR_LIST:
23605 if (from->vval.v_list == NULL)
23606 to->vval.v_list = NULL;
23607 else
23608 {
23609 to->vval.v_list = from->vval.v_list;
23610 ++to->vval.v_list->lv_refcount;
23611 }
23612 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023613 case VAR_DICT:
23614 if (from->vval.v_dict == NULL)
23615 to->vval.v_dict = NULL;
23616 else
23617 {
23618 to->vval.v_dict = from->vval.v_dict;
23619 ++to->vval.v_dict->dv_refcount;
23620 }
23621 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023622 case VAR_UNKNOWN:
23623 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023624 break;
23625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626}
23627
23628/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023629 * Make a copy of an item.
23630 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023631 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23632 * reference to an already copied list/dict can be used.
23633 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023634 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023636item_copy(
23637 typval_T *from,
23638 typval_T *to,
23639 int deep,
23640 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023641{
23642 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023643 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023644
Bram Moolenaar33570922005-01-25 22:26:29 +000023645 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023646 {
23647 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023648 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023649 }
23650 ++recurse;
23651
23652 switch (from->v_type)
23653 {
23654 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023655 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023656 case VAR_STRING:
23657 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023658 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023659 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023660 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023661 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023662 copy_tv(from, to);
23663 break;
23664 case VAR_LIST:
23665 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023666 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023667 if (from->vval.v_list == NULL)
23668 to->vval.v_list = NULL;
23669 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23670 {
23671 /* use the copy made earlier */
23672 to->vval.v_list = from->vval.v_list->lv_copylist;
23673 ++to->vval.v_list->lv_refcount;
23674 }
23675 else
23676 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23677 if (to->vval.v_list == NULL)
23678 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023679 break;
23680 case VAR_DICT:
23681 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023682 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023683 if (from->vval.v_dict == NULL)
23684 to->vval.v_dict = NULL;
23685 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23686 {
23687 /* use the copy made earlier */
23688 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23689 ++to->vval.v_dict->dv_refcount;
23690 }
23691 else
23692 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23693 if (to->vval.v_dict == NULL)
23694 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023695 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023696 case VAR_UNKNOWN:
23697 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023698 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023699 }
23700 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023701 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023702}
23703
23704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 * ":echo expr1 ..." print each argument separated with a space, add a
23706 * newline at the end.
23707 * ":echon expr1 ..." print each argument plain.
23708 */
23709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023710ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711{
23712 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023713 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023714 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715 char_u *p;
23716 int needclr = TRUE;
23717 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023718 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719
23720 if (eap->skip)
23721 ++emsg_skip;
23722 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23723 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023724 /* If eval1() causes an error message the text from the command may
23725 * still need to be cleared. E.g., "echo 22,44". */
23726 need_clr_eos = needclr;
23727
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023729 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 {
23731 /*
23732 * Report the invalid expression unless the expression evaluation
23733 * has been cancelled due to an aborting error, an interrupt, or an
23734 * exception.
23735 */
23736 if (!aborting())
23737 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023738 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 break;
23740 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023741 need_clr_eos = FALSE;
23742
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 if (!eap->skip)
23744 {
23745 if (atstart)
23746 {
23747 atstart = FALSE;
23748 /* Call msg_start() after eval1(), evaluating the expression
23749 * may cause a message to appear. */
23750 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023751 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023752 /* Mark the saved text as finishing the line, so that what
23753 * follows is displayed on a new line when scrolling back
23754 * at the more prompt. */
23755 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758 }
23759 else if (eap->cmdidx == CMD_echo)
23760 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023761 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023762 if (p != NULL)
23763 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023765 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023767 if (*p != TAB && needclr)
23768 {
23769 /* remove any text still there from the command */
23770 msg_clr_eos();
23771 needclr = FALSE;
23772 }
23773 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 }
23775 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023776 {
23777#ifdef FEAT_MBYTE
23778 if (has_mbyte)
23779 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023780 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023781
23782 (void)msg_outtrans_len_attr(p, i, echo_attr);
23783 p += i - 1;
23784 }
23785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023787 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023790 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023792 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 arg = skipwhite(arg);
23794 }
23795 eap->nextcmd = check_nextcmd(arg);
23796
23797 if (eap->skip)
23798 --emsg_skip;
23799 else
23800 {
23801 /* remove text that may still be there from the command */
23802 if (needclr)
23803 msg_clr_eos();
23804 if (eap->cmdidx == CMD_echo)
23805 msg_end();
23806 }
23807}
23808
23809/*
23810 * ":echohl {name}".
23811 */
23812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023813ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814{
23815 int id;
23816
23817 id = syn_name2id(eap->arg);
23818 if (id == 0)
23819 echo_attr = 0;
23820 else
23821 echo_attr = syn_id2attr(id);
23822}
23823
23824/*
23825 * ":execute expr1 ..." execute the result of an expression.
23826 * ":echomsg expr1 ..." Print a message
23827 * ":echoerr expr1 ..." Print an error
23828 * Each gets spaces around each argument and a newline at the end for
23829 * echo commands
23830 */
23831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023832ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833{
23834 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023835 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 int ret = OK;
23837 char_u *p;
23838 garray_T ga;
23839 int len;
23840 int save_did_emsg;
23841
23842 ga_init2(&ga, 1, 80);
23843
23844 if (eap->skip)
23845 ++emsg_skip;
23846 while (*arg != NUL && *arg != '|' && *arg != '\n')
23847 {
23848 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023849 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 {
23851 /*
23852 * Report the invalid expression unless the expression evaluation
23853 * has been cancelled due to an aborting error, an interrupt, or an
23854 * exception.
23855 */
23856 if (!aborting())
23857 EMSG2(_(e_invexpr2), p);
23858 ret = FAIL;
23859 break;
23860 }
23861
23862 if (!eap->skip)
23863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023864 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 len = (int)STRLEN(p);
23866 if (ga_grow(&ga, len + 2) == FAIL)
23867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023868 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 ret = FAIL;
23870 break;
23871 }
23872 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 ga.ga_len += len;
23876 }
23877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023878 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 arg = skipwhite(arg);
23880 }
23881
23882 if (ret != FAIL && ga.ga_data != NULL)
23883 {
23884 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023887 out_flush();
23888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 else if (eap->cmdidx == CMD_echoerr)
23890 {
23891 /* We don't want to abort following commands, restore did_emsg. */
23892 save_did_emsg = did_emsg;
23893 EMSG((char_u *)ga.ga_data);
23894 if (!force_abort)
23895 did_emsg = save_did_emsg;
23896 }
23897 else if (eap->cmdidx == CMD_execute)
23898 do_cmdline((char_u *)ga.ga_data,
23899 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23900 }
23901
23902 ga_clear(&ga);
23903
23904 if (eap->skip)
23905 --emsg_skip;
23906
23907 eap->nextcmd = check_nextcmd(arg);
23908}
23909
23910/*
23911 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23912 * "arg" points to the "&" or '+' when called, to "option" when returning.
23913 * Returns NULL when no option name found. Otherwise pointer to the char
23914 * after the option name.
23915 */
23916 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023917find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918{
23919 char_u *p = *arg;
23920
23921 ++p;
23922 if (*p == 'g' && p[1] == ':')
23923 {
23924 *opt_flags = OPT_GLOBAL;
23925 p += 2;
23926 }
23927 else if (*p == 'l' && p[1] == ':')
23928 {
23929 *opt_flags = OPT_LOCAL;
23930 p += 2;
23931 }
23932 else
23933 *opt_flags = 0;
23934
23935 if (!ASCII_ISALPHA(*p))
23936 return NULL;
23937 *arg = p;
23938
23939 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23940 p += 4; /* termcap option */
23941 else
23942 while (ASCII_ISALPHA(*p))
23943 ++p;
23944 return p;
23945}
23946
23947/*
23948 * ":function"
23949 */
23950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023951ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952{
23953 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023954 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 int j;
23956 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023957 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023958 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 char_u *name = NULL;
23960 char_u *p;
23961 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023962 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 garray_T newargs;
23964 garray_T newlines;
23965 int varargs = FALSE;
23966 int mustend = FALSE;
23967 int flags = 0;
23968 ufunc_T *fp;
23969 int indent;
23970 int nesting;
23971 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023972 dictitem_T *v;
23973 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023974 static int func_nr = 0; /* number for nameless function */
23975 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023976 hashtab_T *ht;
23977 int todo;
23978 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023979 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980
23981 /*
23982 * ":function" without argument: list functions.
23983 */
23984 if (ends_excmd(*eap->arg))
23985 {
23986 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023987 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023988 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023989 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023990 {
23991 if (!HASHITEM_EMPTY(hi))
23992 {
23993 --todo;
23994 fp = HI2UF(hi);
23995 if (!isdigit(*fp->uf_name))
23996 list_func_head(fp, FALSE);
23997 }
23998 }
23999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 eap->nextcmd = check_nextcmd(eap->arg);
24001 return;
24002 }
24003
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024004 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024005 * ":function /pat": list functions matching pattern.
24006 */
24007 if (*eap->arg == '/')
24008 {
24009 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24010 if (!eap->skip)
24011 {
24012 regmatch_T regmatch;
24013
24014 c = *p;
24015 *p = NUL;
24016 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24017 *p = c;
24018 if (regmatch.regprog != NULL)
24019 {
24020 regmatch.rm_ic = p_ic;
24021
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024022 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024023 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24024 {
24025 if (!HASHITEM_EMPTY(hi))
24026 {
24027 --todo;
24028 fp = HI2UF(hi);
24029 if (!isdigit(*fp->uf_name)
24030 && vim_regexec(&regmatch, fp->uf_name, 0))
24031 list_func_head(fp, FALSE);
24032 }
24033 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024034 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024035 }
24036 }
24037 if (*p == '/')
24038 ++p;
24039 eap->nextcmd = check_nextcmd(p);
24040 return;
24041 }
24042
24043 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024044 * Get the function name. There are these situations:
24045 * func normal function name
24046 * "name" == func, "fudi.fd_dict" == NULL
24047 * dict.func new dictionary entry
24048 * "name" == NULL, "fudi.fd_dict" set,
24049 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24050 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024051 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24053 * dict.func existing dict entry that's not a Funcref
24054 * "name" == NULL, "fudi.fd_dict" set,
24055 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024056 * s:func script-local function name
24057 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024060 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024061 paren = (vim_strchr(p, '(') != NULL);
24062 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 {
24064 /*
24065 * Return on an invalid expression in braces, unless the expression
24066 * evaluation has been cancelled due to an aborting error, an
24067 * interrupt, or an exception.
24068 */
24069 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024071 if (!eap->skip && fudi.fd_newkey != NULL)
24072 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024073 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 else
24077 eap->skip = TRUE;
24078 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024079
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 /* An error in a function call during evaluation of an expression in magic
24081 * braces should not cause the function not to be defined. */
24082 saved_did_emsg = did_emsg;
24083 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084
24085 /*
24086 * ":function func" with only function name: list function.
24087 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024088 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089 {
24090 if (!ends_excmd(*skipwhite(p)))
24091 {
24092 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024093 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 }
24095 eap->nextcmd = check_nextcmd(p);
24096 if (eap->nextcmd != NULL)
24097 *p = NUL;
24098 if (!eap->skip && !got_int)
24099 {
24100 fp = find_func(name);
24101 if (fp != NULL)
24102 {
24103 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024104 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024106 if (FUNCLINE(fp, j) == NULL)
24107 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 msg_putchar('\n');
24109 msg_outnum((long)(j + 1));
24110 if (j < 9)
24111 msg_putchar(' ');
24112 if (j < 99)
24113 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024114 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 out_flush(); /* show a line at a time */
24116 ui_breakcheck();
24117 }
24118 if (!got_int)
24119 {
24120 msg_putchar('\n');
24121 msg_puts((char_u *)" endfunction");
24122 }
24123 }
24124 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024125 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024127 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128 }
24129
24130 /*
24131 * ":function name(arg1, arg2)" Define function.
24132 */
24133 p = skipwhite(p);
24134 if (*p != '(')
24135 {
24136 if (!eap->skip)
24137 {
24138 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024139 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 }
24141 /* attempt to continue by skipping some text */
24142 if (vim_strchr(p, '(') != NULL)
24143 p = vim_strchr(p, '(');
24144 }
24145 p = skipwhite(p + 1);
24146
24147 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24148 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24149
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024150 if (!eap->skip)
24151 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024152 /* Check the name of the function. Unless it's a dictionary function
24153 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024154 if (name != NULL)
24155 arg = name;
24156 else
24157 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024158 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024159 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24160 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024161 {
24162 if (*arg == K_SPECIAL)
24163 j = 3;
24164 else
24165 j = 0;
24166 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24167 : eval_isnamec(arg[j])))
24168 ++j;
24169 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024170 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024171 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024172 /* Disallow using the g: dict. */
24173 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24174 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024175 }
24176
Bram Moolenaar071d4272004-06-13 20:20:40 +000024177 /*
24178 * Isolate the arguments: "arg1, arg2, ...)"
24179 */
24180 while (*p != ')')
24181 {
24182 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24183 {
24184 varargs = TRUE;
24185 p += 3;
24186 mustend = TRUE;
24187 }
24188 else
24189 {
24190 arg = p;
24191 while (ASCII_ISALNUM(*p) || *p == '_')
24192 ++p;
24193 if (arg == p || isdigit(*arg)
24194 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24195 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24196 {
24197 if (!eap->skip)
24198 EMSG2(_("E125: Illegal argument: %s"), arg);
24199 break;
24200 }
24201 if (ga_grow(&newargs, 1) == FAIL)
24202 goto erret;
24203 c = *p;
24204 *p = NUL;
24205 arg = vim_strsave(arg);
24206 if (arg == NULL)
24207 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024208
24209 /* Check for duplicate argument name. */
24210 for (i = 0; i < newargs.ga_len; ++i)
24211 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24212 {
24213 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024214 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024215 goto erret;
24216 }
24217
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24219 *p = c;
24220 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 if (*p == ',')
24222 ++p;
24223 else
24224 mustend = TRUE;
24225 }
24226 p = skipwhite(p);
24227 if (mustend && *p != ')')
24228 {
24229 if (!eap->skip)
24230 EMSG2(_(e_invarg2), eap->arg);
24231 break;
24232 }
24233 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024234 if (*p != ')')
24235 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 ++p; /* skip the ')' */
24237
Bram Moolenaare9a41262005-01-15 22:18:47 +000024238 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 for (;;)
24240 {
24241 p = skipwhite(p);
24242 if (STRNCMP(p, "range", 5) == 0)
24243 {
24244 flags |= FC_RANGE;
24245 p += 5;
24246 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024247 else if (STRNCMP(p, "dict", 4) == 0)
24248 {
24249 flags |= FC_DICT;
24250 p += 4;
24251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 else if (STRNCMP(p, "abort", 5) == 0)
24253 {
24254 flags |= FC_ABORT;
24255 p += 5;
24256 }
24257 else
24258 break;
24259 }
24260
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024261 /* When there is a line break use what follows for the function body.
24262 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24263 if (*p == '\n')
24264 line_arg = p + 1;
24265 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 EMSG(_(e_trailing));
24267
24268 /*
24269 * Read the body of the function, until ":endfunction" is found.
24270 */
24271 if (KeyTyped)
24272 {
24273 /* Check if the function already exists, don't let the user type the
24274 * whole function before telling him it doesn't work! For a script we
24275 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024276 if (!eap->skip && !eap->forceit)
24277 {
24278 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24279 EMSG(_(e_funcdict));
24280 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024281 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024284 if (!eap->skip && did_emsg)
24285 goto erret;
24286
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 msg_putchar('\n'); /* don't overwrite the function name */
24288 cmdline_row = msg_row;
24289 }
24290
24291 indent = 2;
24292 nesting = 0;
24293 for (;;)
24294 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024295 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024296 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024297 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024298 saved_wait_return = FALSE;
24299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024301 sourcing_lnum_off = sourcing_lnum;
24302
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024303 if (line_arg != NULL)
24304 {
24305 /* Use eap->arg, split up in parts by line breaks. */
24306 theline = line_arg;
24307 p = vim_strchr(theline, '\n');
24308 if (p == NULL)
24309 line_arg += STRLEN(line_arg);
24310 else
24311 {
24312 *p = NUL;
24313 line_arg = p + 1;
24314 }
24315 }
24316 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 theline = getcmdline(':', 0L, indent);
24318 else
24319 theline = eap->getline(':', eap->cookie, indent);
24320 if (KeyTyped)
24321 lines_left = Rows - 1;
24322 if (theline == NULL)
24323 {
24324 EMSG(_("E126: Missing :endfunction"));
24325 goto erret;
24326 }
24327
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024328 /* Detect line continuation: sourcing_lnum increased more than one. */
24329 if (sourcing_lnum > sourcing_lnum_off + 1)
24330 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24331 else
24332 sourcing_lnum_off = 0;
24333
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334 if (skip_until != NULL)
24335 {
24336 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24337 * don't check for ":endfunc". */
24338 if (STRCMP(theline, skip_until) == 0)
24339 {
24340 vim_free(skip_until);
24341 skip_until = NULL;
24342 }
24343 }
24344 else
24345 {
24346 /* skip ':' and blanks*/
24347 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24348 ;
24349
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024350 /* Check for "endfunction". */
24351 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024353 if (line_arg == NULL)
24354 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 break;
24356 }
24357
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024358 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 * at "end". */
24360 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24361 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024362 else if (STRNCMP(p, "if", 2) == 0
24363 || STRNCMP(p, "wh", 2) == 0
24364 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 || STRNCMP(p, "try", 3) == 0)
24366 indent += 2;
24367
24368 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024369 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024371 if (*p == '!')
24372 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024374 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024375 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024377 ++nesting;
24378 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 }
24380 }
24381
24382 /* Check for ":append" or ":insert". */
24383 p = skip_range(p, NULL);
24384 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24385 || (p[0] == 'i'
24386 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24387 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24388 skip_until = vim_strsave((char_u *)".");
24389
24390 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24391 arg = skipwhite(skiptowhite(p));
24392 if (arg[0] == '<' && arg[1] =='<'
24393 && ((p[0] == 'p' && p[1] == 'y'
24394 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24395 || (p[0] == 'p' && p[1] == 'e'
24396 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24397 || (p[0] == 't' && p[1] == 'c'
24398 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024399 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24400 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24402 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024403 || (p[0] == 'm' && p[1] == 'z'
24404 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024405 ))
24406 {
24407 /* ":python <<" continues until a dot, like ":append" */
24408 p = skipwhite(arg + 2);
24409 if (*p == NUL)
24410 skip_until = vim_strsave((char_u *)".");
24411 else
24412 skip_until = vim_strsave(p);
24413 }
24414 }
24415
24416 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024417 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024418 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024419 if (line_arg == NULL)
24420 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024422 }
24423
24424 /* Copy the line to newly allocated memory. get_one_sourceline()
24425 * allocates 250 bytes per line, this saves 80% on average. The cost
24426 * is an extra alloc/free. */
24427 p = vim_strsave(theline);
24428 if (p != NULL)
24429 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024430 if (line_arg == NULL)
24431 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024432 theline = p;
24433 }
24434
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024435 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24436
24437 /* Add NULL lines for continuation lines, so that the line count is
24438 * equal to the index in the growarray. */
24439 while (sourcing_lnum_off-- > 0)
24440 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024441
24442 /* Check for end of eap->arg. */
24443 if (line_arg != NULL && *line_arg == NUL)
24444 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 }
24446
24447 /* Don't define the function when skipping commands or when an error was
24448 * detected. */
24449 if (eap->skip || did_emsg)
24450 goto erret;
24451
24452 /*
24453 * If there are no errors, add the function
24454 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024455 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024456 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024457 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024458 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024459 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024460 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024461 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024462 goto erret;
24463 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024464
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024465 fp = find_func(name);
24466 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024467 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024468 if (!eap->forceit)
24469 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024470 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024471 goto erret;
24472 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024473 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024474 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024475 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024476 name);
24477 goto erret;
24478 }
24479 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024480 ga_clear_strings(&(fp->uf_args));
24481 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024482 vim_free(name);
24483 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 }
24486 else
24487 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024488 char numbuf[20];
24489
24490 fp = NULL;
24491 if (fudi.fd_newkey == NULL && !eap->forceit)
24492 {
24493 EMSG(_(e_funcdict));
24494 goto erret;
24495 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024496 if (fudi.fd_di == NULL)
24497 {
24498 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024499 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024500 goto erret;
24501 }
24502 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024503 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024504 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024505
24506 /* Give the function a sequential number. Can only be used with a
24507 * Funcref! */
24508 vim_free(name);
24509 sprintf(numbuf, "%d", ++func_nr);
24510 name = vim_strsave((char_u *)numbuf);
24511 if (name == NULL)
24512 goto erret;
24513 }
24514
24515 if (fp == NULL)
24516 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024517 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024518 {
24519 int slen, plen;
24520 char_u *scriptname;
24521
24522 /* Check that the autoload name matches the script name. */
24523 j = FAIL;
24524 if (sourcing_name != NULL)
24525 {
24526 scriptname = autoload_name(name);
24527 if (scriptname != NULL)
24528 {
24529 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024530 plen = (int)STRLEN(p);
24531 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024532 if (slen > plen && fnamecmp(p,
24533 sourcing_name + slen - plen) == 0)
24534 j = OK;
24535 vim_free(scriptname);
24536 }
24537 }
24538 if (j == FAIL)
24539 {
24540 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24541 goto erret;
24542 }
24543 }
24544
24545 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546 if (fp == NULL)
24547 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024548
24549 if (fudi.fd_dict != NULL)
24550 {
24551 if (fudi.fd_di == NULL)
24552 {
24553 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024554 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024555 if (fudi.fd_di == NULL)
24556 {
24557 vim_free(fp);
24558 goto erret;
24559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024560 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24561 {
24562 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024563 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024564 goto erret;
24565 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024566 }
24567 else
24568 /* overwrite existing dict entry */
24569 clear_tv(&fudi.fd_di->di_tv);
24570 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024571 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024572 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024573 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024574
24575 /* behave like "dict" was used */
24576 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024577 }
24578
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024580 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024581 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24582 {
24583 vim_free(fp);
24584 goto erret;
24585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024587 fp->uf_args = newargs;
24588 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024589#ifdef FEAT_PROFILE
24590 fp->uf_tml_count = NULL;
24591 fp->uf_tml_total = NULL;
24592 fp->uf_tml_self = NULL;
24593 fp->uf_profiling = FALSE;
24594 if (prof_def_func())
24595 func_do_profile(fp);
24596#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024597 fp->uf_varargs = varargs;
24598 fp->uf_flags = flags;
24599 fp->uf_calls = 0;
24600 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024601 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024602
24603erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604 ga_clear_strings(&newargs);
24605 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024606ret_free:
24607 vim_free(skip_until);
24608 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024611 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612}
24613
24614/*
24615 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024616 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024618 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024619 * TFN_INT: internal function name OK
24620 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024621 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 * Advances "pp" to just after the function name (if no error).
24623 */
24624 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024625trans_function_name(
24626 char_u **pp,
24627 int skip, /* only find the end, don't evaluate */
24628 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024629 funcdict_T *fdp, /* return: info about dictionary used */
24630 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024632 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633 char_u *start;
24634 char_u *end;
24635 int lead;
24636 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024638 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024639
24640 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024641 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024643
24644 /* Check for hard coded <SNR>: already translated function ID (from a user
24645 * command). */
24646 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24647 && (*pp)[2] == (int)KE_SNR)
24648 {
24649 *pp += 3;
24650 len = get_id_len(pp) + 3;
24651 return vim_strnsave(start, len);
24652 }
24653
24654 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24655 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024656 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024657 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024659
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024660 /* Note that TFN_ flags use the same values as GLV_ flags. */
24661 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024662 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024663 if (end == start)
24664 {
24665 if (!skip)
24666 EMSG(_("E129: Function name required"));
24667 goto theend;
24668 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024669 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024670 {
24671 /*
24672 * Report an invalid expression in braces, unless the expression
24673 * evaluation has been cancelled due to an aborting error, an
24674 * interrupt, or an exception.
24675 */
24676 if (!aborting())
24677 {
24678 if (end != NULL)
24679 EMSG2(_(e_invarg2), start);
24680 }
24681 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024682 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024683 goto theend;
24684 }
24685
24686 if (lv.ll_tv != NULL)
24687 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688 if (fdp != NULL)
24689 {
24690 fdp->fd_dict = lv.ll_dict;
24691 fdp->fd_newkey = lv.ll_newkey;
24692 lv.ll_newkey = NULL;
24693 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024695 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24696 {
24697 name = vim_strsave(lv.ll_tv->vval.v_string);
24698 *pp = end;
24699 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024700 else if (lv.ll_tv->v_type == VAR_PARTIAL
24701 && lv.ll_tv->vval.v_partial != NULL)
24702 {
24703 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24704 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024705 if (partial != NULL)
24706 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024708 else
24709 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024710 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24711 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024712 EMSG(_(e_funcref));
24713 else
24714 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024715 name = NULL;
24716 }
24717 goto theend;
24718 }
24719
24720 if (lv.ll_name == NULL)
24721 {
24722 /* Error found, but continue after the function name. */
24723 *pp = end;
24724 goto theend;
24725 }
24726
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024727 /* Check if the name is a Funcref. If so, use the value. */
24728 if (lv.ll_exp_name != NULL)
24729 {
24730 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024731 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024732 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024733 if (name == lv.ll_exp_name)
24734 name = NULL;
24735 }
24736 else
24737 {
24738 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024739 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024740 if (name == *pp)
24741 name = NULL;
24742 }
24743 if (name != NULL)
24744 {
24745 name = vim_strsave(name);
24746 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024747 if (STRNCMP(name, "<SNR>", 5) == 0)
24748 {
24749 /* Change "<SNR>" to the byte sequence. */
24750 name[0] = K_SPECIAL;
24751 name[1] = KS_EXTRA;
24752 name[2] = (int)KE_SNR;
24753 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24754 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024755 goto theend;
24756 }
24757
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024758 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024759 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024760 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024761 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24762 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24763 {
24764 /* When there was "s:" already or the name expanded to get a
24765 * leading "s:" then remove it. */
24766 lv.ll_name += 2;
24767 len -= 2;
24768 lead = 2;
24769 }
24770 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024771 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024772 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024773 /* skip over "s:" and "g:" */
24774 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024775 lv.ll_name += 2;
24776 len = (int)(end - lv.ll_name);
24777 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024778
24779 /*
24780 * Copy the function name to allocated memory.
24781 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24782 * Accept <SNR>123_name() outside a script.
24783 */
24784 if (skip)
24785 lead = 0; /* do nothing */
24786 else if (lead > 0)
24787 {
24788 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024789 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24790 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024791 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024792 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024793 if (current_SID <= 0)
24794 {
24795 EMSG(_(e_usingsid));
24796 goto theend;
24797 }
24798 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24799 lead += (int)STRLEN(sid_buf);
24800 }
24801 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024802 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024803 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024804 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024805 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024806 goto theend;
24807 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024808 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024809 {
24810 char_u *cp = vim_strchr(lv.ll_name, ':');
24811
24812 if (cp != NULL && cp < end)
24813 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024814 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024815 goto theend;
24816 }
24817 }
24818
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024819 name = alloc((unsigned)(len + lead + 1));
24820 if (name != NULL)
24821 {
24822 if (lead > 0)
24823 {
24824 name[0] = K_SPECIAL;
24825 name[1] = KS_EXTRA;
24826 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024827 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024828 STRCPY(name + 3, sid_buf);
24829 }
24830 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024831 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024832 }
24833 *pp = end;
24834
24835theend:
24836 clear_lval(&lv);
24837 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838}
24839
24840/*
24841 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24842 * Return 2 if "p" starts with "s:".
24843 * Return 0 otherwise.
24844 */
24845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024846eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024848 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24849 * the standard library function. */
24850 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24851 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024852 return 5;
24853 if (p[0] == 's' && p[1] == ':')
24854 return 2;
24855 return 0;
24856}
24857
24858/*
24859 * Return TRUE if "p" starts with "<SID>" or "s:".
24860 * Only works if eval_fname_script() returned non-zero for "p"!
24861 */
24862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024863eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864{
24865 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24866}
24867
24868/*
24869 * List the head of the function: "name(arg1, arg2)".
24870 */
24871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024872list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873{
24874 int j;
24875
24876 msg_start();
24877 if (indent)
24878 MSG_PUTS(" ");
24879 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024880 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 {
24882 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024883 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 }
24885 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024886 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024888 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889 {
24890 if (j)
24891 MSG_PUTS(", ");
24892 msg_puts(FUNCARG(fp, j));
24893 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024894 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 {
24896 if (j)
24897 MSG_PUTS(", ");
24898 MSG_PUTS("...");
24899 }
24900 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024901 if (fp->uf_flags & FC_ABORT)
24902 MSG_PUTS(" abort");
24903 if (fp->uf_flags & FC_RANGE)
24904 MSG_PUTS(" range");
24905 if (fp->uf_flags & FC_DICT)
24906 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024907 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024908 if (p_verbose > 0)
24909 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910}
24911
24912/*
24913 * Find a function by name, return pointer to it in ufuncs.
24914 * Return NULL for unknown function.
24915 */
24916 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024917find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024919 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024921 hi = hash_find(&func_hashtab, name);
24922 if (!HASHITEM_EMPTY(hi))
24923 return HI2UF(hi);
24924 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925}
24926
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024927#if defined(EXITFREE) || defined(PROTO)
24928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024929free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024930{
24931 hashitem_T *hi;
24932
24933 /* Need to start all over every time, because func_free() may change the
24934 * hash table. */
24935 while (func_hashtab.ht_used > 0)
24936 for (hi = func_hashtab.ht_array; ; ++hi)
24937 if (!HASHITEM_EMPTY(hi))
24938 {
24939 func_free(HI2UF(hi));
24940 break;
24941 }
24942}
24943#endif
24944
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024946translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024947{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024948 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024949 return find_internal_func(name) >= 0;
24950 return find_func(name) != NULL;
24951}
24952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024953/*
24954 * Return TRUE if a function "name" exists.
24955 */
24956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024957function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024958{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024959 char_u *nm = name;
24960 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024961 int n = FALSE;
24962
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024963 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024964 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024965 nm = skipwhite(nm);
24966
24967 /* Only accept "funcname", "funcname ", "funcname (..." and
24968 * "funcname(...", not "funcname!...". */
24969 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024970 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024971 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024972 return n;
24973}
24974
Bram Moolenaara1544c02013-05-30 12:35:52 +020024975 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024976get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024977{
24978 char_u *nm = name;
24979 char_u *p;
24980
Bram Moolenaar65639032016-03-16 21:40:30 +010024981 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024982
24983 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024984 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024985 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024986
Bram Moolenaara1544c02013-05-30 12:35:52 +020024987 vim_free(p);
24988 return NULL;
24989}
24990
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024991/*
24992 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024993 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24994 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024995 */
24996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024997builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024998{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024999 char_u *p;
25000
25001 if (!ASCII_ISLOWER(name[0]))
25002 return FALSE;
25003 p = vim_strchr(name, AUTOLOAD_CHAR);
25004 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025005}
25006
Bram Moolenaar05159a02005-02-26 23:04:13 +000025007#if defined(FEAT_PROFILE) || defined(PROTO)
25008/*
25009 * Start profiling function "fp".
25010 */
25011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025012func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025013{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025014 int len = fp->uf_lines.ga_len;
25015
25016 if (len == 0)
25017 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025018 fp->uf_tm_count = 0;
25019 profile_zero(&fp->uf_tm_self);
25020 profile_zero(&fp->uf_tm_total);
25021 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025022 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025023 if (fp->uf_tml_total == NULL)
25024 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025025 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025026 if (fp->uf_tml_self == NULL)
25027 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025028 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025029 fp->uf_tml_idx = -1;
25030 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25031 || fp->uf_tml_self == NULL)
25032 return; /* out of memory */
25033
25034 fp->uf_profiling = TRUE;
25035}
25036
25037/*
25038 * Dump the profiling results for all functions in file "fd".
25039 */
25040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025041func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025042{
25043 hashitem_T *hi;
25044 int todo;
25045 ufunc_T *fp;
25046 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025047 ufunc_T **sorttab;
25048 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025049
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025050 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025051 if (todo == 0)
25052 return; /* nothing to dump */
25053
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025054 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025055
Bram Moolenaar05159a02005-02-26 23:04:13 +000025056 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25057 {
25058 if (!HASHITEM_EMPTY(hi))
25059 {
25060 --todo;
25061 fp = HI2UF(hi);
25062 if (fp->uf_profiling)
25063 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025064 if (sorttab != NULL)
25065 sorttab[st_len++] = fp;
25066
Bram Moolenaar05159a02005-02-26 23:04:13 +000025067 if (fp->uf_name[0] == K_SPECIAL)
25068 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25069 else
25070 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25071 if (fp->uf_tm_count == 1)
25072 fprintf(fd, "Called 1 time\n");
25073 else
25074 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25075 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25076 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25077 fprintf(fd, "\n");
25078 fprintf(fd, "count total (s) self (s)\n");
25079
25080 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25081 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025082 if (FUNCLINE(fp, i) == NULL)
25083 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025084 prof_func_line(fd, fp->uf_tml_count[i],
25085 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025086 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25087 }
25088 fprintf(fd, "\n");
25089 }
25090 }
25091 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025092
25093 if (sorttab != NULL && st_len > 0)
25094 {
25095 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25096 prof_total_cmp);
25097 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25098 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25099 prof_self_cmp);
25100 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25101 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025102
25103 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025104}
Bram Moolenaar73830342005-02-28 22:48:19 +000025105
25106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025107prof_sort_list(
25108 FILE *fd,
25109 ufunc_T **sorttab,
25110 int st_len,
25111 char *title,
25112 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025113{
25114 int i;
25115 ufunc_T *fp;
25116
25117 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25118 fprintf(fd, "count total (s) self (s) function\n");
25119 for (i = 0; i < 20 && i < st_len; ++i)
25120 {
25121 fp = sorttab[i];
25122 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25123 prefer_self);
25124 if (fp->uf_name[0] == K_SPECIAL)
25125 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25126 else
25127 fprintf(fd, " %s()\n", fp->uf_name);
25128 }
25129 fprintf(fd, "\n");
25130}
25131
25132/*
25133 * Print the count and times for one function or function line.
25134 */
25135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025136prof_func_line(
25137 FILE *fd,
25138 int count,
25139 proftime_T *total,
25140 proftime_T *self,
25141 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025142{
25143 if (count > 0)
25144 {
25145 fprintf(fd, "%5d ", count);
25146 if (prefer_self && profile_equal(total, self))
25147 fprintf(fd, " ");
25148 else
25149 fprintf(fd, "%s ", profile_msg(total));
25150 if (!prefer_self && profile_equal(total, self))
25151 fprintf(fd, " ");
25152 else
25153 fprintf(fd, "%s ", profile_msg(self));
25154 }
25155 else
25156 fprintf(fd, " ");
25157}
25158
25159/*
25160 * Compare function for total time sorting.
25161 */
25162 static int
25163#ifdef __BORLANDC__
25164_RTLENTRYF
25165#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025166prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025167{
25168 ufunc_T *p1, *p2;
25169
25170 p1 = *(ufunc_T **)s1;
25171 p2 = *(ufunc_T **)s2;
25172 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25173}
25174
25175/*
25176 * Compare function for self time sorting.
25177 */
25178 static int
25179#ifdef __BORLANDC__
25180_RTLENTRYF
25181#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025182prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025183{
25184 ufunc_T *p1, *p2;
25185
25186 p1 = *(ufunc_T **)s1;
25187 p2 = *(ufunc_T **)s2;
25188 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25189}
25190
Bram Moolenaar05159a02005-02-26 23:04:13 +000025191#endif
25192
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025193/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025194 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025195 * Return TRUE if a package was loaded.
25196 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025198script_autoload(
25199 char_u *name,
25200 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025201{
25202 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025203 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025204 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025205 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025206
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025207 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025208 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025209 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025210 return FALSE;
25211
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025212 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025213
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025214 /* Find the name in the list of previously loaded package names. Skip
25215 * "autoload/", it's always the same. */
25216 for (i = 0; i < ga_loaded.ga_len; ++i)
25217 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25218 break;
25219 if (!reload && i < ga_loaded.ga_len)
25220 ret = FALSE; /* was loaded already */
25221 else
25222 {
25223 /* Remember the name if it wasn't loaded already. */
25224 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25225 {
25226 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25227 tofree = NULL;
25228 }
25229
25230 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025231 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025232 ret = TRUE;
25233 }
25234
25235 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025236 return ret;
25237}
25238
25239/*
25240 * Return the autoload script name for a function or variable name.
25241 * Returns NULL when out of memory.
25242 */
25243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025244autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025245{
25246 char_u *p;
25247 char_u *scriptname;
25248
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025249 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025250 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25251 if (scriptname == NULL)
25252 return FALSE;
25253 STRCPY(scriptname, "autoload/");
25254 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025255 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025256 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025257 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025258 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025259 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025260}
25261
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25263
25264/*
25265 * Function given to ExpandGeneric() to obtain the list of user defined
25266 * function names.
25267 */
25268 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025269get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025271 static long_u done;
25272 static hashitem_T *hi;
25273 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274
25275 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025277 done = 0;
25278 hi = func_hashtab.ht_array;
25279 }
25280 if (done < func_hashtab.ht_used)
25281 {
25282 if (done++ > 0)
25283 ++hi;
25284 while (HASHITEM_EMPTY(hi))
25285 ++hi;
25286 fp = HI2UF(hi);
25287
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025288 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025289 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025290
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025291 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25292 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293
25294 cat_func_name(IObuff, fp);
25295 if (xp->xp_context != EXPAND_USER_FUNC)
25296 {
25297 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025298 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025299 STRCAT(IObuff, ")");
25300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301 return IObuff;
25302 }
25303 return NULL;
25304}
25305
25306#endif /* FEAT_CMDL_COMPL */
25307
25308/*
25309 * Copy the function name of "fp" to buffer "buf".
25310 * "buf" must be able to hold the function name plus three bytes.
25311 * Takes care of script-local function names.
25312 */
25313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025314cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025316 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 {
25318 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025319 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 }
25321 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025322 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323}
25324
25325/*
25326 * ":delfunction {name}"
25327 */
25328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025329ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025331 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 char_u *p;
25333 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025334 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335
25336 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025337 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025338 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025339 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025340 {
25341 if (fudi.fd_dict != NULL && !eap->skip)
25342 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 if (!ends_excmd(*skipwhite(p)))
25346 {
25347 vim_free(name);
25348 EMSG(_(e_trailing));
25349 return;
25350 }
25351 eap->nextcmd = check_nextcmd(p);
25352 if (eap->nextcmd != NULL)
25353 *p = NUL;
25354
25355 if (!eap->skip)
25356 fp = find_func(name);
25357 vim_free(name);
25358
25359 if (!eap->skip)
25360 {
25361 if (fp == NULL)
25362 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025363 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 return;
25365 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025366 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 {
25368 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25369 return;
25370 }
25371
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025372 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025374 /* Delete the dict item that refers to the function, it will
25375 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025376 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025378 else
25379 func_free(fp);
25380 }
25381}
25382
25383/*
25384 * Free a function and remove it from the list of functions.
25385 */
25386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025387func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025388{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025389 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025390
25391 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025392 ga_clear_strings(&(fp->uf_args));
25393 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025394#ifdef FEAT_PROFILE
25395 vim_free(fp->uf_tml_count);
25396 vim_free(fp->uf_tml_total);
25397 vim_free(fp->uf_tml_self);
25398#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025399
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025400 /* remove the function from the function hashtable */
25401 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25402 if (HASHITEM_EMPTY(hi))
25403 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025404 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025405 hash_remove(&func_hashtab, hi);
25406
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025407 vim_free(fp);
25408}
25409
25410/*
25411 * Unreference a Function: decrement the reference count and free it when it
25412 * becomes zero. Only for numbered functions.
25413 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025415func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025416{
25417 ufunc_T *fp;
25418
25419 if (name != NULL && isdigit(*name))
25420 {
25421 fp = find_func(name);
25422 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025423 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025424#ifdef EXITFREE
25425 if (!entered_free_all_mem)
25426#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025427 EMSG2(_(e_intern2), "func_unref()");
25428 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025429 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025430 {
25431 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025432 * when "uf_calls" becomes zero. */
25433 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025434 func_free(fp);
25435 }
25436 }
25437}
25438
25439/*
25440 * Count a reference to a Function.
25441 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025443func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025444{
25445 ufunc_T *fp;
25446
25447 if (name != NULL && isdigit(*name))
25448 {
25449 fp = find_func(name);
25450 if (fp == NULL)
25451 EMSG2(_(e_intern2), "func_ref()");
25452 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025453 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 }
25455}
25456
25457/*
25458 * Call a user function.
25459 */
25460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025461call_user_func(
25462 ufunc_T *fp, /* pointer to function */
25463 int argcount, /* nr of args */
25464 typval_T *argvars, /* arguments */
25465 typval_T *rettv, /* return value */
25466 linenr_T firstline, /* first line of range */
25467 linenr_T lastline, /* last line of range */
25468 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469{
Bram Moolenaar33570922005-01-25 22:26:29 +000025470 char_u *save_sourcing_name;
25471 linenr_T save_sourcing_lnum;
25472 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025473 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025474 int save_did_emsg;
25475 static int depth = 0;
25476 dictitem_T *v;
25477 int fixvar_idx = 0; /* index in fixvar[] */
25478 int i;
25479 int ai;
25480 char_u numbuf[NUMBUFLEN];
25481 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025482 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025483#ifdef FEAT_PROFILE
25484 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025485 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487
25488 /* If depth of calling is getting too high, don't execute the function */
25489 if (depth >= p_mfd)
25490 {
25491 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025492 rettv->v_type = VAR_NUMBER;
25493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 return;
25495 }
25496 ++depth;
25497
25498 line_breakcheck(); /* check for CTRL-C hit */
25499
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025500 fc = (funccall_T *)alloc(sizeof(funccall_T));
25501 fc->caller = current_funccal;
25502 current_funccal = fc;
25503 fc->func = fp;
25504 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025505 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025506 fc->linenr = 0;
25507 fc->returned = FALSE;
25508 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025510 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25511 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512
Bram Moolenaar33570922005-01-25 22:26:29 +000025513 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025514 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025515 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25516 * each argument variable and saves a lot of time.
25517 */
25518 /*
25519 * Init l: variables.
25520 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025521 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025522 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025523 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025524 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25525 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025526 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025527 name = v->di_key;
25528 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025529 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025530 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025531 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025532 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025533 v->di_tv.vval.v_dict = selfdict;
25534 ++selfdict->dv_refcount;
25535 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025536
Bram Moolenaar33570922005-01-25 22:26:29 +000025537 /*
25538 * Init a: variables.
25539 * Set a:0 to "argcount".
25540 * Set a:000 to a list with room for the "..." arguments.
25541 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025542 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025543 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025544 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025545 /* Use "name" to avoid a warning from some compiler that checks the
25546 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025547 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025548 name = v->di_key;
25549 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025550 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025551 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025552 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025553 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025554 v->di_tv.vval.v_list = &fc->l_varlist;
25555 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25556 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25557 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025558
25559 /*
25560 * Set a:firstline to "firstline" and a:lastline to "lastline".
25561 * Set a:name to named arguments.
25562 * Set a:N to the "..." arguments.
25563 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025564 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025565 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025566 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025567 (varnumber_T)lastline);
25568 for (i = 0; i < argcount; ++i)
25569 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025570 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025571 if (ai < 0)
25572 /* named argument a:name */
25573 name = FUNCARG(fp, i);
25574 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025575 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025576 /* "..." argument a:1, a:2, etc. */
25577 sprintf((char *)numbuf, "%d", ai + 1);
25578 name = numbuf;
25579 }
25580 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25581 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025582 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025583 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25584 }
25585 else
25586 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025587 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25588 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025589 if (v == NULL)
25590 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025591 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025592 }
25593 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025594 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025595
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025596 /* Note: the values are copied directly to avoid alloc/free.
25597 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025598 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025599 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025600
25601 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25602 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025603 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25604 fc->l_listitems[ai].li_tv = argvars[i];
25605 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025606 }
25607 }
25608
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 /* Don't redraw while executing the function. */
25610 ++RedrawingDisabled;
25611 save_sourcing_name = sourcing_name;
25612 save_sourcing_lnum = sourcing_lnum;
25613 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025614 /* need space for function name + ("function " + 3) or "[number]" */
25615 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25616 + STRLEN(fp->uf_name) + 20;
25617 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618 if (sourcing_name != NULL)
25619 {
25620 if (save_sourcing_name != NULL
25621 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025622 sprintf((char *)sourcing_name, "%s[%d]..",
25623 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 else
25625 STRCPY(sourcing_name, "function ");
25626 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25627
25628 if (p_verbose >= 12)
25629 {
25630 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025631 verbose_enter_scroll();
25632
Bram Moolenaar555b2802005-05-19 21:08:39 +000025633 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634 if (p_verbose >= 14)
25635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025637 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025638 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025639 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640
25641 msg_puts((char_u *)"(");
25642 for (i = 0; i < argcount; ++i)
25643 {
25644 if (i > 0)
25645 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025646 if (argvars[i].v_type == VAR_NUMBER)
25647 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 else
25649 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025650 /* Do not want errors such as E724 here. */
25651 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025652 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025653 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025654 if (s != NULL)
25655 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025656 if (vim_strsize(s) > MSG_BUF_CLEN)
25657 {
25658 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25659 s = buf;
25660 }
25661 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025662 vim_free(tofree);
25663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 }
25665 }
25666 msg_puts((char_u *)")");
25667 }
25668 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025669
25670 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 --no_wait_return;
25672 }
25673 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025674#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025675 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025676 {
25677 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25678 func_do_profile(fp);
25679 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025680 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025681 {
25682 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025683 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025684 profile_zero(&fp->uf_tm_children);
25685 }
25686 script_prof_save(&wait_start);
25687 }
25688#endif
25689
Bram Moolenaar071d4272004-06-13 20:20:40 +000025690 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025691 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 save_did_emsg = did_emsg;
25693 did_emsg = FALSE;
25694
25695 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025696 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25698
25699 --RedrawingDisabled;
25700
25701 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025702 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025704 clear_tv(rettv);
25705 rettv->v_type = VAR_NUMBER;
25706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707 }
25708
Bram Moolenaar05159a02005-02-26 23:04:13 +000025709#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025710 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025711 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025713 profile_end(&call_start);
25714 profile_sub_wait(&wait_start, &call_start);
25715 profile_add(&fp->uf_tm_total, &call_start);
25716 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025717 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025718 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025719 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25720 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025721 }
25722 }
25723#endif
25724
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 /* when being verbose, mention the return value */
25726 if (p_verbose >= 12)
25727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025729 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025730
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025732 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025733 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025734 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025735 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025736 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025738 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025739 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025740 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025741 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025742
Bram Moolenaar555b2802005-05-19 21:08:39 +000025743 /* The value may be very long. Skip the middle part, so that we
25744 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025745 * truncate it at the end. Don't want errors such as E724 here. */
25746 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025747 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025748 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025749 if (s != NULL)
25750 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025751 if (vim_strsize(s) > MSG_BUF_CLEN)
25752 {
25753 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25754 s = buf;
25755 }
25756 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025757 vim_free(tofree);
25758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 }
25760 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025761
25762 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763 --no_wait_return;
25764 }
25765
25766 vim_free(sourcing_name);
25767 sourcing_name = save_sourcing_name;
25768 sourcing_lnum = save_sourcing_lnum;
25769 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025770#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025771 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025772 script_prof_restore(&wait_start);
25773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774
25775 if (p_verbose >= 12 && sourcing_name != NULL)
25776 {
25777 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025778 verbose_enter_scroll();
25779
Bram Moolenaar555b2802005-05-19 21:08:39 +000025780 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025782
25783 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 --no_wait_return;
25785 }
25786
25787 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025788 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025790
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025791 /* If the a:000 list and the l: and a: dicts are not referenced we can
25792 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025793 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25794 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25795 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25796 {
25797 free_funccal(fc, FALSE);
25798 }
25799 else
25800 {
25801 hashitem_T *hi;
25802 listitem_T *li;
25803 int todo;
25804
25805 /* "fc" is still in use. This can happen when returning "a:000" or
25806 * assigning "l:" to a global variable.
25807 * Link "fc" in the list for garbage collection later. */
25808 fc->caller = previous_funccal;
25809 previous_funccal = fc;
25810
25811 /* Make a copy of the a: variables, since we didn't do that above. */
25812 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25813 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25814 {
25815 if (!HASHITEM_EMPTY(hi))
25816 {
25817 --todo;
25818 v = HI2DI(hi);
25819 copy_tv(&v->di_tv, &v->di_tv);
25820 }
25821 }
25822
25823 /* Make a copy of the a:000 items, since we didn't do that above. */
25824 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25825 copy_tv(&li->li_tv, &li->li_tv);
25826 }
25827}
25828
25829/*
25830 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025831 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025832 */
25833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025834can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025835{
25836 return (fc->l_varlist.lv_copyID != copyID
25837 && fc->l_vars.dv_copyID != copyID
25838 && fc->l_avars.dv_copyID != copyID);
25839}
25840
25841/*
25842 * Free "fc" and what it contains.
25843 */
25844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025845free_funccal(
25846 funccall_T *fc,
25847 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025848{
25849 listitem_T *li;
25850
25851 /* The a: variables typevals may not have been allocated, only free the
25852 * allocated variables. */
25853 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25854
25855 /* free all l: variables */
25856 vars_clear(&fc->l_vars.dv_hashtab);
25857
25858 /* Free the a:000 variables if they were allocated. */
25859 if (free_val)
25860 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25861 clear_tv(&li->li_tv);
25862
25863 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864}
25865
25866/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025867 * Add a number variable "name" to dict "dp" with value "nr".
25868 */
25869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025870add_nr_var(
25871 dict_T *dp,
25872 dictitem_T *v,
25873 char *name,
25874 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025875{
25876 STRCPY(v->di_key, name);
25877 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25878 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25879 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025880 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025881 v->di_tv.vval.v_number = nr;
25882}
25883
25884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885 * ":return [expr]"
25886 */
25887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025888ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889{
25890 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025891 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 int returning = FALSE;
25893
25894 if (current_funccal == NULL)
25895 {
25896 EMSG(_("E133: :return not inside a function"));
25897 return;
25898 }
25899
25900 if (eap->skip)
25901 ++emsg_skip;
25902
25903 eap->nextcmd = NULL;
25904 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025905 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906 {
25907 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025908 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025910 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911 }
25912 /* It's safer to return also on error. */
25913 else if (!eap->skip)
25914 {
25915 /*
25916 * Return unless the expression evaluation has been cancelled due to an
25917 * aborting error, an interrupt, or an exception.
25918 */
25919 if (!aborting())
25920 returning = do_return(eap, FALSE, TRUE, NULL);
25921 }
25922
25923 /* When skipping or the return gets pending, advance to the next command
25924 * in this line (!returning). Otherwise, ignore the rest of the line.
25925 * Following lines will be ignored by get_func_line(). */
25926 if (returning)
25927 eap->nextcmd = NULL;
25928 else if (eap->nextcmd == NULL) /* no argument */
25929 eap->nextcmd = check_nextcmd(arg);
25930
25931 if (eap->skip)
25932 --emsg_skip;
25933}
25934
25935/*
25936 * Return from a function. Possibly makes the return pending. Also called
25937 * for a pending return at the ":endtry" or after returning from an extra
25938 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025939 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025940 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941 * FALSE when the return gets pending.
25942 */
25943 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025944do_return(
25945 exarg_T *eap,
25946 int reanimate,
25947 int is_cmd,
25948 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949{
25950 int idx;
25951 struct condstack *cstack = eap->cstack;
25952
25953 if (reanimate)
25954 /* Undo the return. */
25955 current_funccal->returned = FALSE;
25956
25957 /*
25958 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25959 * not in its finally clause (which then is to be executed next) is found.
25960 * In this case, make the ":return" pending for execution at the ":endtry".
25961 * Otherwise, return normally.
25962 */
25963 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25964 if (idx >= 0)
25965 {
25966 cstack->cs_pending[idx] = CSTP_RETURN;
25967
25968 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025969 /* A pending return again gets pending. "rettv" points to an
25970 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025972 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 else
25974 {
25975 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025976 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025978 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025980 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981 {
25982 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025983 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025984 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985 else
25986 EMSG(_(e_outofmem));
25987 }
25988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025989 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990
25991 if (reanimate)
25992 {
25993 /* The pending return value could be overwritten by a ":return"
25994 * without argument in a finally clause; reset the default
25995 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025996 current_funccal->rettv->v_type = VAR_NUMBER;
25997 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 }
25999 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026000 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 }
26002 else
26003 {
26004 current_funccal->returned = TRUE;
26005
26006 /* If the return is carried out now, store the return value. For
26007 * a return immediately after reanimation, the value is already
26008 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026009 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026011 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026012 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026014 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 }
26016 }
26017
26018 return idx < 0;
26019}
26020
26021/*
26022 * Free the variable with a pending return value.
26023 */
26024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026025discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026{
Bram Moolenaar33570922005-01-25 22:26:29 +000026027 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028}
26029
26030/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026031 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032 * is an allocated string. Used by report_pending() for verbose messages.
26033 */
26034 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026035get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026037 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026038 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026041 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026042 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026043 if (s == NULL)
26044 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026045
26046 STRCPY(IObuff, ":return ");
26047 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26048 if (STRLEN(s) + 8 >= IOSIZE)
26049 STRCPY(IObuff + IOSIZE - 4, "...");
26050 vim_free(tofree);
26051 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026052}
26053
26054/*
26055 * Get next function line.
26056 * Called by do_cmdline() to get the next line.
26057 * Returns allocated string, or NULL for end of function.
26058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026060get_func_line(
26061 int c UNUSED,
26062 void *cookie,
26063 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064{
Bram Moolenaar33570922005-01-25 22:26:29 +000026065 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026066 ufunc_T *fp = fcp->func;
26067 char_u *retval;
26068 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069
26070 /* If breakpoints have been added/deleted need to check for it. */
26071 if (fcp->dbg_tick != debug_tick)
26072 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026073 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074 sourcing_lnum);
26075 fcp->dbg_tick = debug_tick;
26076 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026077#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026078 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026079 func_line_end(cookie);
26080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081
Bram Moolenaar05159a02005-02-26 23:04:13 +000026082 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026083 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26084 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 retval = NULL;
26086 else
26087 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026088 /* Skip NULL lines (continuation lines). */
26089 while (fcp->linenr < gap->ga_len
26090 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26091 ++fcp->linenr;
26092 if (fcp->linenr >= gap->ga_len)
26093 retval = NULL;
26094 else
26095 {
26096 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26097 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026098#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026099 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026100 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026101#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 }
26104
26105 /* Did we encounter a breakpoint? */
26106 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26107 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026108 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026110 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 sourcing_lnum);
26112 fcp->dbg_tick = debug_tick;
26113 }
26114
26115 return retval;
26116}
26117
Bram Moolenaar05159a02005-02-26 23:04:13 +000026118#if defined(FEAT_PROFILE) || defined(PROTO)
26119/*
26120 * Called when starting to read a function line.
26121 * "sourcing_lnum" must be correct!
26122 * When skipping lines it may not actually be executed, but we won't find out
26123 * until later and we need to store the time now.
26124 */
26125 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026126func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026127{
26128 funccall_T *fcp = (funccall_T *)cookie;
26129 ufunc_T *fp = fcp->func;
26130
26131 if (fp->uf_profiling && sourcing_lnum >= 1
26132 && sourcing_lnum <= fp->uf_lines.ga_len)
26133 {
26134 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026135 /* Skip continuation lines. */
26136 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26137 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026138 fp->uf_tml_execed = FALSE;
26139 profile_start(&fp->uf_tml_start);
26140 profile_zero(&fp->uf_tml_children);
26141 profile_get_wait(&fp->uf_tml_wait);
26142 }
26143}
26144
26145/*
26146 * Called when actually executing a function line.
26147 */
26148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026149func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026150{
26151 funccall_T *fcp = (funccall_T *)cookie;
26152 ufunc_T *fp = fcp->func;
26153
26154 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26155 fp->uf_tml_execed = TRUE;
26156}
26157
26158/*
26159 * Called when done with a function line.
26160 */
26161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026162func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026163{
26164 funccall_T *fcp = (funccall_T *)cookie;
26165 ufunc_T *fp = fcp->func;
26166
26167 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26168 {
26169 if (fp->uf_tml_execed)
26170 {
26171 ++fp->uf_tml_count[fp->uf_tml_idx];
26172 profile_end(&fp->uf_tml_start);
26173 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026174 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026175 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26176 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026177 }
26178 fp->uf_tml_idx = -1;
26179 }
26180}
26181#endif
26182
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183/*
26184 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026185 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026186 */
26187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026188func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189{
Bram Moolenaar33570922005-01-25 22:26:29 +000026190 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026191
26192 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26193 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026194 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026195 || fcp->returned);
26196}
26197
26198/*
26199 * return TRUE if cookie indicates a function which "abort"s on errors.
26200 */
26201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026202func_has_abort(
26203 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026205 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206}
26207
26208#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26209typedef enum
26210{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026211 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26212 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26213 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214} var_flavour_T;
26215
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026216static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217
26218 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026219var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026220{
26221 char_u *p = varname;
26222
26223 if (ASCII_ISUPPER(*p))
26224 {
26225 while (*(++p))
26226 if (ASCII_ISLOWER(*p))
26227 return VAR_FLAVOUR_SESSION;
26228 return VAR_FLAVOUR_VIMINFO;
26229 }
26230 else
26231 return VAR_FLAVOUR_DEFAULT;
26232}
26233#endif
26234
26235#if defined(FEAT_VIMINFO) || defined(PROTO)
26236/*
26237 * Restore global vars that start with a capital from the viminfo file
26238 */
26239 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026240read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026241{
26242 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026243 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026244 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026245 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246
26247 if (!writing && (find_viminfo_parameter('!') != NULL))
26248 {
26249 tab = vim_strchr(virp->vir_line + 1, '\t');
26250 if (tab != NULL)
26251 {
26252 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026253 switch (*tab)
26254 {
26255 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026256#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026257 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026258#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026259 case 'D': type = VAR_DICT; break;
26260 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026261 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026263
26264 tab = vim_strchr(tab, '\t');
26265 if (tab != NULL)
26266 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026267 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026268 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026269 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026270 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026271#ifdef FEAT_FLOAT
26272 else if (type == VAR_FLOAT)
26273 (void)string2float(tab + 1, &tv.vval.v_float);
26274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026276 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026277 if (type == VAR_DICT || type == VAR_LIST)
26278 {
26279 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26280
26281 if (etv == NULL)
26282 /* Failed to parse back the dict or list, use it as a
26283 * string. */
26284 tv.v_type = VAR_STRING;
26285 else
26286 {
26287 vim_free(tv.vval.v_string);
26288 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026289 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026290 }
26291 }
26292
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026293 /* when in a function use global variables */
26294 save_funccal = current_funccal;
26295 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026296 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026297 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026298
26299 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026300 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026301 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26302 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 }
26304 }
26305 }
26306
26307 return viminfo_readline(virp);
26308}
26309
26310/*
26311 * Write global vars that start with a capital to the viminfo file
26312 */
26313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026314write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026315{
Bram Moolenaar33570922005-01-25 22:26:29 +000026316 hashitem_T *hi;
26317 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026318 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026319 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026320 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026321 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026322 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026323
26324 if (find_viminfo_parameter('!') == NULL)
26325 return;
26326
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026327 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026328
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026329 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026330 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026332 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026334 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026335 this_var = HI2DI(hi);
26336 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026338 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026339 {
26340 case VAR_STRING: s = "STR"; break;
26341 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026342 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026343 case VAR_DICT: s = "DIC"; break;
26344 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026345 case VAR_SPECIAL: s = "XPL"; break;
26346
26347 case VAR_UNKNOWN:
26348 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026349 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026350 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026351 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026352 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026353 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026354 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026355 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026356 if (p != NULL)
26357 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026358 vim_free(tofree);
26359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 }
26361 }
26362}
26363#endif
26364
26365#if defined(FEAT_SESSION) || defined(PROTO)
26366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026367store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368{
Bram Moolenaar33570922005-01-25 22:26:29 +000026369 hashitem_T *hi;
26370 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026371 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372 char_u *p, *t;
26373
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026374 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026375 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026377 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026379 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026380 this_var = HI2DI(hi);
26381 if ((this_var->di_tv.v_type == VAR_NUMBER
26382 || this_var->di_tv.v_type == VAR_STRING)
26383 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026384 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026385 /* Escape special characters with a backslash. Turn a LF and
26386 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026387 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026388 (char_u *)"\\\"\n\r");
26389 if (p == NULL) /* out of memory */
26390 break;
26391 for (t = p; *t != NUL; ++t)
26392 if (*t == '\n')
26393 *t = 'n';
26394 else if (*t == '\r')
26395 *t = 'r';
26396 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026397 this_var->di_key,
26398 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26399 : ' ',
26400 p,
26401 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26402 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026403 || put_eol(fd) == FAIL)
26404 {
26405 vim_free(p);
26406 return FAIL;
26407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408 vim_free(p);
26409 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026410#ifdef FEAT_FLOAT
26411 else if (this_var->di_tv.v_type == VAR_FLOAT
26412 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26413 {
26414 float_T f = this_var->di_tv.vval.v_float;
26415 int sign = ' ';
26416
26417 if (f < 0)
26418 {
26419 f = -f;
26420 sign = '-';
26421 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026422 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026423 this_var->di_key, sign, f) < 0)
26424 || put_eol(fd) == FAIL)
26425 return FAIL;
26426 }
26427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026428 }
26429 }
26430 return OK;
26431}
26432#endif
26433
Bram Moolenaar661b1822005-07-28 22:36:45 +000026434/*
26435 * Display script name where an item was last set.
26436 * Should only be invoked when 'verbose' is non-zero.
26437 */
26438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026439last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026440{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026441 char_u *p;
26442
Bram Moolenaar661b1822005-07-28 22:36:45 +000026443 if (scriptID != 0)
26444 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026445 p = home_replace_save(NULL, get_scriptname(scriptID));
26446 if (p != NULL)
26447 {
26448 verbose_enter();
26449 MSG_PUTS(_("\n\tLast set from "));
26450 MSG_PUTS(p);
26451 vim_free(p);
26452 verbose_leave();
26453 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026454 }
26455}
26456
Bram Moolenaard812df62008-11-09 12:46:09 +000026457/*
26458 * List v:oldfiles in a nice way.
26459 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026461ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026462{
26463 list_T *l = vimvars[VV_OLDFILES].vv_list;
26464 listitem_T *li;
26465 int nr = 0;
26466
26467 if (l == NULL)
26468 msg((char_u *)_("No old files"));
26469 else
26470 {
26471 msg_start();
26472 msg_scroll = TRUE;
26473 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26474 {
26475 msg_outnum((long)++nr);
26476 MSG_PUTS(": ");
26477 msg_outtrans(get_tv_string(&li->li_tv));
26478 msg_putchar('\n');
26479 out_flush(); /* output one line at a time */
26480 ui_breakcheck();
26481 }
26482 /* Assume "got_int" was set to truncate the listing. */
26483 got_int = FALSE;
26484
26485#ifdef FEAT_BROWSE_CMD
26486 if (cmdmod.browse)
26487 {
26488 quit_more = FALSE;
26489 nr = prompt_for_number(FALSE);
26490 msg_starthere();
26491 if (nr > 0)
26492 {
26493 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26494 (long)nr);
26495
26496 if (p != NULL)
26497 {
26498 p = expand_env_save(p);
26499 eap->arg = p;
26500 eap->cmdidx = CMD_edit;
26501 cmdmod.browse = FALSE;
26502 do_exedit(eap, NULL);
26503 vim_free(p);
26504 }
26505 }
26506 }
26507#endif
26508 }
26509}
26510
Bram Moolenaar53744302015-07-17 17:38:22 +020026511/* reset v:option_new, v:option_old and v:option_type */
26512 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026513reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026514{
26515 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26516 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26517 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26518}
26519
26520
Bram Moolenaar071d4272004-06-13 20:20:40 +000026521#endif /* FEAT_EVAL */
26522
Bram Moolenaar071d4272004-06-13 20:20:40 +000026523
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026524#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026525
26526#ifdef WIN3264
26527/*
26528 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26529 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026530static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26531static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26532static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026533
26534/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026535 * Get the short path (8.3) for the filename in "fnamep".
26536 * Only works for a valid file name.
26537 * When the path gets longer "fnamep" is changed and the allocated buffer
26538 * is put in "bufp".
26539 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26540 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026541 */
26542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026543get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026545 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546 char_u *newbuf;
26547
26548 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026549 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026550 if (l > len - 1)
26551 {
26552 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026553 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026554 newbuf = vim_strnsave(*fnamep, l);
26555 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026557
26558 vim_free(*bufp);
26559 *fnamep = *bufp = newbuf;
26560
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026561 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026562 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563 }
26564
26565 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026566 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567}
26568
26569/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026570 * Get the short path (8.3) for the filename in "fname". The converted
26571 * path is returned in "bufp".
26572 *
26573 * Some of the directories specified in "fname" may not exist. This function
26574 * will shorten the existing directories at the beginning of the path and then
26575 * append the remaining non-existing path.
26576 *
26577 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026578 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026579 * bufp - Pointer to an allocated buffer for the filename.
26580 * fnamelen - Length of the filename pointed to by fname
26581 *
26582 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583 */
26584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026585shortpath_for_invalid_fname(
26586 char_u **fname,
26587 char_u **bufp,
26588 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026589{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026590 char_u *short_fname, *save_fname, *pbuf_unused;
26591 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026592 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026593 int old_len, len;
26594 int new_len, sfx_len;
26595 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026596
26597 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026598 old_len = *fnamelen;
26599 save_fname = vim_strnsave(*fname, old_len);
26600 pbuf_unused = NULL;
26601 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026602
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026603 endp = save_fname + old_len - 1; /* Find the end of the copy */
26604 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026605
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026606 /*
26607 * Try shortening the supplied path till it succeeds by removing one
26608 * directory at a time from the tail of the path.
26609 */
26610 len = 0;
26611 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026612 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026613 /* go back one path-separator */
26614 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26615 --endp;
26616 if (endp <= save_fname)
26617 break; /* processed the complete path */
26618
26619 /*
26620 * Replace the path separator with a NUL and try to shorten the
26621 * resulting path.
26622 */
26623 ch = *endp;
26624 *endp = 0;
26625 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026626 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026627 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26628 {
26629 retval = FAIL;
26630 goto theend;
26631 }
26632 *endp = ch; /* preserve the string */
26633
26634 if (len > 0)
26635 break; /* successfully shortened the path */
26636
26637 /* failed to shorten the path. Skip the path separator */
26638 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026639 }
26640
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026641 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026642 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026643 /*
26644 * Succeeded in shortening the path. Now concatenate the shortened
26645 * path with the remaining path at the tail.
26646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026647
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026648 /* Compute the length of the new path. */
26649 sfx_len = (int)(save_endp - endp) + 1;
26650 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026651
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026652 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026653 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026654 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026655 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026656 /* There is not enough space in the currently allocated string,
26657 * copy it to a buffer big enough. */
26658 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026660 {
26661 retval = FAIL;
26662 goto theend;
26663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026664 }
26665 else
26666 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026667 /* Transfer short_fname to the main buffer (it's big enough),
26668 * unless get_short_pathname() did its work in-place. */
26669 *fname = *bufp = save_fname;
26670 if (short_fname != save_fname)
26671 vim_strncpy(save_fname, short_fname, len);
26672 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026673 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026674
26675 /* concat the not-shortened part of the path */
26676 vim_strncpy(*fname + len, endp, sfx_len);
26677 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026678 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026679
26680theend:
26681 vim_free(pbuf_unused);
26682 vim_free(save_fname);
26683
26684 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026685}
26686
26687/*
26688 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026689 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026690 */
26691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026692shortpath_for_partial(
26693 char_u **fnamep,
26694 char_u **bufp,
26695 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026696{
26697 int sepcount, len, tflen;
26698 char_u *p;
26699 char_u *pbuf, *tfname;
26700 int hasTilde;
26701
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026702 /* Count up the path separators from the RHS.. so we know which part
26703 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026704 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026705 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026706 if (vim_ispathsep(*p))
26707 ++sepcount;
26708
26709 /* Need full path first (use expand_env() to remove a "~/") */
26710 hasTilde = (**fnamep == '~');
26711 if (hasTilde)
26712 pbuf = tfname = expand_env_save(*fnamep);
26713 else
26714 pbuf = tfname = FullName_save(*fnamep, FALSE);
26715
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026716 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026717
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026718 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26719 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026720
26721 if (len == 0)
26722 {
26723 /* Don't have a valid filename, so shorten the rest of the
26724 * path if we can. This CAN give us invalid 8.3 filenames, but
26725 * there's not a lot of point in guessing what it might be.
26726 */
26727 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026728 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26729 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026730 }
26731
26732 /* Count the paths backward to find the beginning of the desired string. */
26733 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026734 {
26735#ifdef FEAT_MBYTE
26736 if (has_mbyte)
26737 p -= mb_head_off(tfname, p);
26738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026739 if (vim_ispathsep(*p))
26740 {
26741 if (sepcount == 0 || (hasTilde && sepcount == 1))
26742 break;
26743 else
26744 sepcount --;
26745 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026747 if (hasTilde)
26748 {
26749 --p;
26750 if (p >= tfname)
26751 *p = '~';
26752 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026753 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754 }
26755 else
26756 ++p;
26757
26758 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26759 vim_free(*bufp);
26760 *fnamelen = (int)STRLEN(p);
26761 *bufp = pbuf;
26762 *fnamep = p;
26763
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026764 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026765}
26766#endif /* WIN3264 */
26767
26768/*
26769 * Adjust a filename, according to a string of modifiers.
26770 * *fnamep must be NUL terminated when called. When returning, the length is
26771 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026772 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026773 * When there is an error, *fnamep is set to NULL.
26774 */
26775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026776modify_fname(
26777 char_u *src, /* string with modifiers */
26778 int *usedlen, /* characters after src that are used */
26779 char_u **fnamep, /* file name so far */
26780 char_u **bufp, /* buffer for allocated file name or NULL */
26781 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026782{
26783 int valid = 0;
26784 char_u *tail;
26785 char_u *s, *p, *pbuf;
26786 char_u dirname[MAXPATHL];
26787 int c;
26788 int has_fullname = 0;
26789#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026790 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026791 int has_shortname = 0;
26792#endif
26793
26794repeat:
26795 /* ":p" - full path/file_name */
26796 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26797 {
26798 has_fullname = 1;
26799
26800 valid |= VALID_PATH;
26801 *usedlen += 2;
26802
26803 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26804 if ((*fnamep)[0] == '~'
26805#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26806 && ((*fnamep)[1] == '/'
26807# ifdef BACKSLASH_IN_FILENAME
26808 || (*fnamep)[1] == '\\'
26809# endif
26810 || (*fnamep)[1] == NUL)
26811
26812#endif
26813 )
26814 {
26815 *fnamep = expand_env_save(*fnamep);
26816 vim_free(*bufp); /* free any allocated file name */
26817 *bufp = *fnamep;
26818 if (*fnamep == NULL)
26819 return -1;
26820 }
26821
26822 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026823 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026824 {
26825 if (vim_ispathsep(*p)
26826 && p[1] == '.'
26827 && (p[2] == NUL
26828 || vim_ispathsep(p[2])
26829 || (p[2] == '.'
26830 && (p[3] == NUL || vim_ispathsep(p[3])))))
26831 break;
26832 }
26833
26834 /* FullName_save() is slow, don't use it when not needed. */
26835 if (*p != NUL || !vim_isAbsName(*fnamep))
26836 {
26837 *fnamep = FullName_save(*fnamep, *p != NUL);
26838 vim_free(*bufp); /* free any allocated file name */
26839 *bufp = *fnamep;
26840 if (*fnamep == NULL)
26841 return -1;
26842 }
26843
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026844#ifdef WIN3264
26845# if _WIN32_WINNT >= 0x0500
26846 if (vim_strchr(*fnamep, '~') != NULL)
26847 {
26848 /* Expand 8.3 filename to full path. Needed to make sure the same
26849 * file does not have two different names.
26850 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26851 p = alloc(_MAX_PATH + 1);
26852 if (p != NULL)
26853 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026854 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026855 {
26856 vim_free(*bufp);
26857 *bufp = *fnamep = p;
26858 }
26859 else
26860 vim_free(p);
26861 }
26862 }
26863# endif
26864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026865 /* Append a path separator to a directory. */
26866 if (mch_isdir(*fnamep))
26867 {
26868 /* Make room for one or two extra characters. */
26869 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26870 vim_free(*bufp); /* free any allocated file name */
26871 *bufp = *fnamep;
26872 if (*fnamep == NULL)
26873 return -1;
26874 add_pathsep(*fnamep);
26875 }
26876 }
26877
26878 /* ":." - path relative to the current directory */
26879 /* ":~" - path relative to the home directory */
26880 /* ":8" - shortname path - postponed till after */
26881 while (src[*usedlen] == ':'
26882 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26883 {
26884 *usedlen += 2;
26885 if (c == '8')
26886 {
26887#ifdef WIN3264
26888 has_shortname = 1; /* Postpone this. */
26889#endif
26890 continue;
26891 }
26892 pbuf = NULL;
26893 /* Need full path first (use expand_env() to remove a "~/") */
26894 if (!has_fullname)
26895 {
26896 if (c == '.' && **fnamep == '~')
26897 p = pbuf = expand_env_save(*fnamep);
26898 else
26899 p = pbuf = FullName_save(*fnamep, FALSE);
26900 }
26901 else
26902 p = *fnamep;
26903
26904 has_fullname = 0;
26905
26906 if (p != NULL)
26907 {
26908 if (c == '.')
26909 {
26910 mch_dirname(dirname, MAXPATHL);
26911 s = shorten_fname(p, dirname);
26912 if (s != NULL)
26913 {
26914 *fnamep = s;
26915 if (pbuf != NULL)
26916 {
26917 vim_free(*bufp); /* free any allocated file name */
26918 *bufp = pbuf;
26919 pbuf = NULL;
26920 }
26921 }
26922 }
26923 else
26924 {
26925 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26926 /* Only replace it when it starts with '~' */
26927 if (*dirname == '~')
26928 {
26929 s = vim_strsave(dirname);
26930 if (s != NULL)
26931 {
26932 *fnamep = s;
26933 vim_free(*bufp);
26934 *bufp = s;
26935 }
26936 }
26937 }
26938 vim_free(pbuf);
26939 }
26940 }
26941
26942 tail = gettail(*fnamep);
26943 *fnamelen = (int)STRLEN(*fnamep);
26944
26945 /* ":h" - head, remove "/file_name", can be repeated */
26946 /* Don't remove the first "/" or "c:\" */
26947 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26948 {
26949 valid |= VALID_HEAD;
26950 *usedlen += 2;
26951 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026952 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026953 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026954 *fnamelen = (int)(tail - *fnamep);
26955#ifdef VMS
26956 if (*fnamelen > 0)
26957 *fnamelen += 1; /* the path separator is part of the path */
26958#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026959 if (*fnamelen == 0)
26960 {
26961 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26962 p = vim_strsave((char_u *)".");
26963 if (p == NULL)
26964 return -1;
26965 vim_free(*bufp);
26966 *bufp = *fnamep = tail = p;
26967 *fnamelen = 1;
26968 }
26969 else
26970 {
26971 while (tail > s && !after_pathsep(s, tail))
26972 mb_ptr_back(*fnamep, tail);
26973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026974 }
26975
26976 /* ":8" - shortname */
26977 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26978 {
26979 *usedlen += 2;
26980#ifdef WIN3264
26981 has_shortname = 1;
26982#endif
26983 }
26984
26985#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026986 /*
26987 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026988 */
26989 if (has_shortname)
26990 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026991 /* Copy the string if it is shortened by :h and when it wasn't copied
26992 * yet, because we are going to change it in place. Avoids changing
26993 * the buffer name for "%:8". */
26994 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026995 {
26996 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026997 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026998 return -1;
26999 vim_free(*bufp);
27000 *bufp = *fnamep = p;
27001 }
27002
27003 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027004 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027005 if (!has_fullname && !vim_isAbsName(*fnamep))
27006 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027007 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027008 return -1;
27009 }
27010 else
27011 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027012 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027013
Bram Moolenaardc935552011-08-17 15:23:23 +020027014 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027015 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027016 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027017 return -1;
27018
27019 if (l == 0)
27020 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027021 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027022 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027023 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027024 return -1;
27025 }
27026 *fnamelen = l;
27027 }
27028 }
27029#endif /* WIN3264 */
27030
27031 /* ":t" - tail, just the basename */
27032 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27033 {
27034 *usedlen += 2;
27035 *fnamelen -= (int)(tail - *fnamep);
27036 *fnamep = tail;
27037 }
27038
27039 /* ":e" - extension, can be repeated */
27040 /* ":r" - root, without extension, can be repeated */
27041 while (src[*usedlen] == ':'
27042 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27043 {
27044 /* find a '.' in the tail:
27045 * - for second :e: before the current fname
27046 * - otherwise: The last '.'
27047 */
27048 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27049 s = *fnamep - 2;
27050 else
27051 s = *fnamep + *fnamelen - 1;
27052 for ( ; s > tail; --s)
27053 if (s[0] == '.')
27054 break;
27055 if (src[*usedlen + 1] == 'e') /* :e */
27056 {
27057 if (s > tail)
27058 {
27059 *fnamelen += (int)(*fnamep - (s + 1));
27060 *fnamep = s + 1;
27061#ifdef VMS
27062 /* cut version from the extension */
27063 s = *fnamep + *fnamelen - 1;
27064 for ( ; s > *fnamep; --s)
27065 if (s[0] == ';')
27066 break;
27067 if (s > *fnamep)
27068 *fnamelen = s - *fnamep;
27069#endif
27070 }
27071 else if (*fnamep <= tail)
27072 *fnamelen = 0;
27073 }
27074 else /* :r */
27075 {
27076 if (s > tail) /* remove one extension */
27077 *fnamelen = (int)(s - *fnamep);
27078 }
27079 *usedlen += 2;
27080 }
27081
27082 /* ":s?pat?foo?" - substitute */
27083 /* ":gs?pat?foo?" - global substitute */
27084 if (src[*usedlen] == ':'
27085 && (src[*usedlen + 1] == 's'
27086 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27087 {
27088 char_u *str;
27089 char_u *pat;
27090 char_u *sub;
27091 int sep;
27092 char_u *flags;
27093 int didit = FALSE;
27094
27095 flags = (char_u *)"";
27096 s = src + *usedlen + 2;
27097 if (src[*usedlen + 1] == 'g')
27098 {
27099 flags = (char_u *)"g";
27100 ++s;
27101 }
27102
27103 sep = *s++;
27104 if (sep)
27105 {
27106 /* find end of pattern */
27107 p = vim_strchr(s, sep);
27108 if (p != NULL)
27109 {
27110 pat = vim_strnsave(s, (int)(p - s));
27111 if (pat != NULL)
27112 {
27113 s = p + 1;
27114 /* find end of substitution */
27115 p = vim_strchr(s, sep);
27116 if (p != NULL)
27117 {
27118 sub = vim_strnsave(s, (int)(p - s));
27119 str = vim_strnsave(*fnamep, *fnamelen);
27120 if (sub != NULL && str != NULL)
27121 {
27122 *usedlen = (int)(p + 1 - src);
27123 s = do_string_sub(str, pat, sub, flags);
27124 if (s != NULL)
27125 {
27126 *fnamep = s;
27127 *fnamelen = (int)STRLEN(s);
27128 vim_free(*bufp);
27129 *bufp = s;
27130 didit = TRUE;
27131 }
27132 }
27133 vim_free(sub);
27134 vim_free(str);
27135 }
27136 vim_free(pat);
27137 }
27138 }
27139 /* after using ":s", repeat all the modifiers */
27140 if (didit)
27141 goto repeat;
27142 }
27143 }
27144
Bram Moolenaar26df0922014-02-23 23:39:13 +010027145 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27146 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027147 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027148 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027149 if (c != NUL)
27150 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027151 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027152 if (c != NUL)
27153 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027154 if (p == NULL)
27155 return -1;
27156 vim_free(*bufp);
27157 *bufp = *fnamep = p;
27158 *fnamelen = (int)STRLEN(p);
27159 *usedlen += 2;
27160 }
27161
Bram Moolenaar071d4272004-06-13 20:20:40 +000027162 return valid;
27163}
27164
27165/*
27166 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27167 * "flags" can be "g" to do a global substitute.
27168 * Returns an allocated string, NULL for error.
27169 */
27170 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027171do_string_sub(
27172 char_u *str,
27173 char_u *pat,
27174 char_u *sub,
27175 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027176{
27177 int sublen;
27178 regmatch_T regmatch;
27179 int i;
27180 int do_all;
27181 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027182 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027183 garray_T ga;
27184 char_u *ret;
27185 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027186 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027187
27188 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27189 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027190 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027191
27192 ga_init2(&ga, 1, 200);
27193
27194 do_all = (flags[0] == 'g');
27195
27196 regmatch.rm_ic = p_ic;
27197 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27198 if (regmatch.regprog != NULL)
27199 {
27200 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027201 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027202 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27203 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027204 /* Skip empty match except for first match. */
27205 if (regmatch.startp[0] == regmatch.endp[0])
27206 {
27207 if (zero_width == regmatch.startp[0])
27208 {
27209 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027210 i = MB_PTR2LEN(tail);
27211 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27212 (size_t)i);
27213 ga.ga_len += i;
27214 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027215 continue;
27216 }
27217 zero_width = regmatch.startp[0];
27218 }
27219
Bram Moolenaar071d4272004-06-13 20:20:40 +000027220 /*
27221 * Get some space for a temporary buffer to do the substitution
27222 * into. It will contain:
27223 * - The text up to where the match is.
27224 * - The substituted text.
27225 * - The text after the match.
27226 */
27227 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027228 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027229 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27230 {
27231 ga_clear(&ga);
27232 break;
27233 }
27234
27235 /* copy the text up to where the match is */
27236 i = (int)(regmatch.startp[0] - tail);
27237 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27238 /* add the substituted text */
27239 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27240 + ga.ga_len + i, TRUE, TRUE, FALSE);
27241 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027242 tail = regmatch.endp[0];
27243 if (*tail == NUL)
27244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027245 if (!do_all)
27246 break;
27247 }
27248
27249 if (ga.ga_data != NULL)
27250 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27251
Bram Moolenaar473de612013-06-08 18:19:48 +020027252 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027253 }
27254
27255 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27256 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027257 if (p_cpo == empty_option)
27258 p_cpo = save_cpo;
27259 else
27260 /* Darn, evaluating {sub} expression changed the value. */
27261 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027262
27263 return ret;
27264}
27265
27266#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */