blob: 6987485edcc5157cff4ff6e937ce71b51a23f43e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
558static void f_eventhandler(typval_T *argvars, typval_T *rettv);
559static void f_executable(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79815f12016-07-09 17:07:29 +0200560static void f_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exepath(typval_T *argvars, typval_T *rettv);
562static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_expand(typval_T *argvars, typval_T *rettv);
567static void f_extend(typval_T *argvars, typval_T *rettv);
568static void f_feedkeys(typval_T *argvars, typval_T *rettv);
569static void f_filereadable(typval_T *argvars, typval_T *rettv);
570static void f_filewritable(typval_T *argvars, typval_T *rettv);
571static void f_filter(typval_T *argvars, typval_T *rettv);
572static void f_finddir(typval_T *argvars, typval_T *rettv);
573static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_float2nr(typval_T *argvars, typval_T *rettv);
576static void f_floor(typval_T *argvars, typval_T *rettv);
577static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000578#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100579static void f_fnameescape(typval_T *argvars, typval_T *rettv);
580static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
581static void f_foldclosed(typval_T *argvars, typval_T *rettv);
582static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
583static void f_foldlevel(typval_T *argvars, typval_T *rettv);
584static void f_foldtext(typval_T *argvars, typval_T *rettv);
585static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
586static void f_foreground(typval_T *argvars, typval_T *rettv);
587static void f_function(typval_T *argvars, typval_T *rettv);
588static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
589static void f_get(typval_T *argvars, typval_T *rettv);
590static void f_getbufline(typval_T *argvars, typval_T *rettv);
591static void f_getbufvar(typval_T *argvars, typval_T *rettv);
592static void f_getchar(typval_T *argvars, typval_T *rettv);
593static void f_getcharmod(typval_T *argvars, typval_T *rettv);
594static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
595static void f_getcmdline(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +0200596#if defined(FEAT_CMDL_COMPL)
597static void f_getcompletion(typval_T *argvars, typval_T *rettv);
598#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100599static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
600static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
601static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
602static void f_getcwd(typval_T *argvars, typval_T *rettv);
603static void f_getfontname(typval_T *argvars, typval_T *rettv);
604static void f_getfperm(typval_T *argvars, typval_T *rettv);
605static void f_getfsize(typval_T *argvars, typval_T *rettv);
606static void f_getftime(typval_T *argvars, typval_T *rettv);
607static void f_getftype(typval_T *argvars, typval_T *rettv);
608static void f_getline(typval_T *argvars, typval_T *rettv);
609static void f_getmatches(typval_T *argvars, typval_T *rettv);
610static void f_getpid(typval_T *argvars, typval_T *rettv);
611static void f_getcurpos(typval_T *argvars, typval_T *rettv);
612static void f_getpos(typval_T *argvars, typval_T *rettv);
613static void f_getqflist(typval_T *argvars, typval_T *rettv);
614static void f_getreg(typval_T *argvars, typval_T *rettv);
615static void f_getregtype(typval_T *argvars, typval_T *rettv);
616static void f_gettabvar(typval_T *argvars, typval_T *rettv);
617static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
618static void f_getwinposx(typval_T *argvars, typval_T *rettv);
619static void f_getwinposy(typval_T *argvars, typval_T *rettv);
620static void f_getwinvar(typval_T *argvars, typval_T *rettv);
621static void f_glob(typval_T *argvars, typval_T *rettv);
622static void f_globpath(typval_T *argvars, typval_T *rettv);
623static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
624static void f_has(typval_T *argvars, typval_T *rettv);
625static void f_has_key(typval_T *argvars, typval_T *rettv);
626static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
627static void f_hasmapto(typval_T *argvars, typval_T *rettv);
628static void f_histadd(typval_T *argvars, typval_T *rettv);
629static void f_histdel(typval_T *argvars, typval_T *rettv);
630static void f_histget(typval_T *argvars, typval_T *rettv);
631static void f_histnr(typval_T *argvars, typval_T *rettv);
632static void f_hlID(typval_T *argvars, typval_T *rettv);
633static void f_hlexists(typval_T *argvars, typval_T *rettv);
634static void f_hostname(typval_T *argvars, typval_T *rettv);
635static void f_iconv(typval_T *argvars, typval_T *rettv);
636static void f_indent(typval_T *argvars, typval_T *rettv);
637static void f_index(typval_T *argvars, typval_T *rettv);
638static void f_input(typval_T *argvars, typval_T *rettv);
639static void f_inputdialog(typval_T *argvars, typval_T *rettv);
640static void f_inputlist(typval_T *argvars, typval_T *rettv);
641static void f_inputrestore(typval_T *argvars, typval_T *rettv);
642static void f_inputsave(typval_T *argvars, typval_T *rettv);
643static void f_inputsecret(typval_T *argvars, typval_T *rettv);
644static void f_insert(typval_T *argvars, typval_T *rettv);
645static void f_invert(typval_T *argvars, typval_T *rettv);
646static void f_isdirectory(typval_T *argvars, typval_T *rettv);
647static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100648#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
649static void f_isnan(typval_T *argvars, typval_T *rettv);
650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100652#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100653static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100654static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100655static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100656static void f_job_start(typval_T *argvars, typval_T *rettv);
657static void f_job_stop(typval_T *argvars, typval_T *rettv);
658static void f_job_status(typval_T *argvars, typval_T *rettv);
659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100661static void f_js_decode(typval_T *argvars, typval_T *rettv);
662static void f_js_encode(typval_T *argvars, typval_T *rettv);
663static void f_json_decode(typval_T *argvars, typval_T *rettv);
664static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_keys(typval_T *argvars, typval_T *rettv);
666static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
667static void f_len(typval_T *argvars, typval_T *rettv);
668static void f_libcall(typval_T *argvars, typval_T *rettv);
669static void f_libcallnr(typval_T *argvars, typval_T *rettv);
670static void f_line(typval_T *argvars, typval_T *rettv);
671static void f_line2byte(typval_T *argvars, typval_T *rettv);
672static void f_lispindent(typval_T *argvars, typval_T *rettv);
673static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_log(typval_T *argvars, typval_T *rettv);
676static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000677#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200678#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_map(typval_T *argvars, typval_T *rettv);
682static void f_maparg(typval_T *argvars, typval_T *rettv);
683static void f_mapcheck(typval_T *argvars, typval_T *rettv);
684static void f_match(typval_T *argvars, typval_T *rettv);
685static void f_matchadd(typval_T *argvars, typval_T *rettv);
686static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
687static void f_matcharg(typval_T *argvars, typval_T *rettv);
688static void f_matchdelete(typval_T *argvars, typval_T *rettv);
689static void f_matchend(typval_T *argvars, typval_T *rettv);
690static void f_matchlist(typval_T *argvars, typval_T *rettv);
691static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200692static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_max(typval_T *argvars, typval_T *rettv);
694static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100699#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100701#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
703static void f_nr2char(typval_T *argvars, typval_T *rettv);
704static void f_or(typval_T *argvars, typval_T *rettv);
705static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100706#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100708#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000709#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
713static void f_printf(typval_T *argvars, typval_T *rettv);
714static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
718#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_range(typval_T *argvars, typval_T *rettv);
722static void f_readfile(typval_T *argvars, typval_T *rettv);
723static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100724#ifdef FEAT_FLOAT
725static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
726#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_reltimestr(typval_T *argvars, typval_T *rettv);
728static void f_remote_expr(typval_T *argvars, typval_T *rettv);
729static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
730static void f_remote_peek(typval_T *argvars, typval_T *rettv);
731static void f_remote_read(typval_T *argvars, typval_T *rettv);
732static void f_remote_send(typval_T *argvars, typval_T *rettv);
733static void f_remove(typval_T *argvars, typval_T *rettv);
734static void f_rename(typval_T *argvars, typval_T *rettv);
735static void f_repeat(typval_T *argvars, typval_T *rettv);
736static void f_resolve(typval_T *argvars, typval_T *rettv);
737static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000740#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100741static void f_screenattr(typval_T *argvars, typval_T *rettv);
742static void f_screenchar(typval_T *argvars, typval_T *rettv);
743static void f_screencol(typval_T *argvars, typval_T *rettv);
744static void f_screenrow(typval_T *argvars, typval_T *rettv);
745static void f_search(typval_T *argvars, typval_T *rettv);
746static void f_searchdecl(typval_T *argvars, typval_T *rettv);
747static void f_searchpair(typval_T *argvars, typval_T *rettv);
748static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
749static void f_searchpos(typval_T *argvars, typval_T *rettv);
750static void f_server2client(typval_T *argvars, typval_T *rettv);
751static void f_serverlist(typval_T *argvars, typval_T *rettv);
752static void f_setbufvar(typval_T *argvars, typval_T *rettv);
753static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
754static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100755static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_setline(typval_T *argvars, typval_T *rettv);
757static void f_setloclist(typval_T *argvars, typval_T *rettv);
758static void f_setmatches(typval_T *argvars, typval_T *rettv);
759static void f_setpos(typval_T *argvars, typval_T *rettv);
760static void f_setqflist(typval_T *argvars, typval_T *rettv);
761static void f_setreg(typval_T *argvars, typval_T *rettv);
762static void f_settabvar(typval_T *argvars, typval_T *rettv);
763static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
764static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100765#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100767#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_shellescape(typval_T *argvars, typval_T *rettv);
769static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
770static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sin(typval_T *argvars, typval_T *rettv);
773static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sort(typval_T *argvars, typval_T *rettv);
776static void f_soundfold(typval_T *argvars, typval_T *rettv);
777static void f_spellbadword(typval_T *argvars, typval_T *rettv);
778static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
779static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_sqrt(typval_T *argvars, typval_T *rettv);
782static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_str2nr(typval_T *argvars, typval_T *rettv);
785static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000786#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000788#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200789static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_stridx(typval_T *argvars, typval_T *rettv);
791static void f_string(typval_T *argvars, typval_T *rettv);
792static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200793static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_strpart(typval_T *argvars, typval_T *rettv);
795static void f_strridx(typval_T *argvars, typval_T *rettv);
796static void f_strtrans(typval_T *argvars, typval_T *rettv);
797static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
798static void f_strwidth(typval_T *argvars, typval_T *rettv);
799static void f_submatch(typval_T *argvars, typval_T *rettv);
800static void f_substitute(typval_T *argvars, typval_T *rettv);
801static void f_synID(typval_T *argvars, typval_T *rettv);
802static void f_synIDattr(typval_T *argvars, typval_T *rettv);
803static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
804static void f_synstack(typval_T *argvars, typval_T *rettv);
805static void f_synconcealed(typval_T *argvars, typval_T *rettv);
806static void f_system(typval_T *argvars, typval_T *rettv);
807static void f_systemlist(typval_T *argvars, typval_T *rettv);
808static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
809static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
810static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
811static void f_taglist(typval_T *argvars, typval_T *rettv);
812static void f_tagfiles(typval_T *argvars, typval_T *rettv);
813static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200814static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5c719942016-07-09 23:40:45 +0200815static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200816static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200817static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
818#ifdef FEAT_JOB_CHANNEL
819static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
820#endif
821static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
822#ifdef FEAT_JOB_CHANNEL
823static void f_test_null_job(typval_T *argvars, typval_T *rettv);
824#endif
825static void f_test_null_list(typval_T *argvars, typval_T *rettv);
826static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
827static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200828static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200829#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static void f_tan(typval_T *argvars, typval_T *rettv);
831static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200832#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100833#ifdef FEAT_TIMERS
834static void f_timer_start(typval_T *argvars, typval_T *rettv);
835static void f_timer_stop(typval_T *argvars, typval_T *rettv);
836#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_tolower(typval_T *argvars, typval_T *rettv);
838static void f_toupper(typval_T *argvars, typval_T *rettv);
839static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000840#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000842#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static void f_type(typval_T *argvars, typval_T *rettv);
844static void f_undofile(typval_T *argvars, typval_T *rettv);
845static void f_undotree(typval_T *argvars, typval_T *rettv);
846static void f_uniq(typval_T *argvars, typval_T *rettv);
847static void f_values(typval_T *argvars, typval_T *rettv);
848static void f_virtcol(typval_T *argvars, typval_T *rettv);
849static void f_visualmode(typval_T *argvars, typval_T *rettv);
850static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100851static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100852static void f_win_getid(typval_T *argvars, typval_T *rettv);
853static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
854static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
855static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static void f_winbufnr(typval_T *argvars, typval_T *rettv);
857static void f_wincol(typval_T *argvars, typval_T *rettv);
858static void f_winheight(typval_T *argvars, typval_T *rettv);
859static void f_winline(typval_T *argvars, typval_T *rettv);
860static void f_winnr(typval_T *argvars, typval_T *rettv);
861static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
862static void f_winrestview(typval_T *argvars, typval_T *rettv);
863static void f_winsaveview(typval_T *argvars, typval_T *rettv);
864static void f_winwidth(typval_T *argvars, typval_T *rettv);
865static void f_writefile(typval_T *argvars, typval_T *rettv);
866static void f_wordcount(typval_T *argvars, typval_T *rettv);
867static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000868
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
870static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
871static int get_env_len(char_u **arg);
872static int get_id_len(char_u **arg);
873static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
874static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000875#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
876#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
877 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
879static int eval_isnamec(int c);
880static int eval_isnamec1(int c);
881static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
882static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static typval_T *alloc_string_tv(char_u *string);
884static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100885#ifdef FEAT_FLOAT
886static float_T get_tv_float(typval_T *varp);
887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static linenr_T get_tv_lnum(typval_T *argvars);
889static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
891static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
892static hashtab_T *find_var_ht(char_u *name, char_u **varname);
893static funccall_T *get_funccal(void);
894static void vars_clear_ext(hashtab_T *ht, int free_val);
895static void delete_var(hashtab_T *ht, hashitem_T *hi);
896static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
897static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
898static void set_var(char_u *name, typval_T *varp, int copy);
899static int var_check_ro(int flags, char_u *name, int use_gettext);
900static int var_check_fixed(int flags, char_u *name, int use_gettext);
901static int var_check_func_name(char_u *name, int new_var);
902static int valid_varname(char_u *varname);
903static int tv_check_lock(int lock, char_u *name, int use_gettext);
904static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
905static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100906static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int eval_fname_script(char_u *p);
908static int eval_fname_sid(char_u *p);
909static void list_func_head(ufunc_T *fp, int indent);
910static ufunc_T *find_func(char_u *name);
911static int function_exists(char_u *name);
912static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000913#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100914static void func_do_profile(ufunc_T *fp);
915static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
916static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000917static int
918# ifdef __BORLANDC__
919 _RTLENTRYF
920# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100921 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000922static int
923# ifdef __BORLANDC__
924 _RTLENTRYF
925# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100926 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100928static int script_autoload(char_u *name, int reload);
929static char_u *autoload_name(char_u *name);
930static void cat_func_name(char_u *buf, ufunc_T *fp);
931static void func_free(ufunc_T *fp);
932static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
933static int can_free_funccal(funccall_T *fc, int copyID) ;
934static void free_funccal(funccall_T *fc, int free_val);
935static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
936static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
937static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
938static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
939static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
940static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
941static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
942static int write_list(FILE *fd, list_T *list, int binary);
943static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200945
946#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100947static int compare_func_name(const void *s1, const void *s2);
948static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200949#endif
950
Bram Moolenaar33570922005-01-25 22:26:29 +0000951/*
952 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000953 */
954 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000956{
Bram Moolenaar33570922005-01-25 22:26:29 +0000957 int i;
958 struct vimvar *p;
959
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200960 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
961 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200962 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000963 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000964 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000965
966 for (i = 0; i < VV_LEN; ++i)
967 {
968 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200969 if (STRLEN(p->vv_name) > 16)
970 {
971 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
972 getout(1);
973 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000974 STRCPY(p->vv_di.di_key, p->vv_name);
975 if (p->vv_flags & VV_RO)
976 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
977 else if (p->vv_flags & VV_RO_SBX)
978 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
979 else
980 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000981
982 /* add to v: scope dict, unless the value is not always available */
983 if (p->vv_type != VAR_UNKNOWN)
984 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000985 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000986 /* add to compat scope dict */
987 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000988 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100989 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
990
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000991 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100992 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200993 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100994 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100995
996 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
997 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
998 set_vim_var_nr(VV_NONE, VVAL_NONE);
999 set_vim_var_nr(VV_NULL, VVAL_NULL);
1000
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001001 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001002
1003#ifdef EBCDIC
1004 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001005 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001006 */
1007 sortFunctions();
1008#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001009}
1010
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001011#if defined(EXITFREE) || defined(PROTO)
1012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001013eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001014{
1015 int i;
1016 struct vimvar *p;
1017
1018 for (i = 0; i < VV_LEN; ++i)
1019 {
1020 p = &vimvars[i];
1021 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001022 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001023 vim_free(p->vv_str);
1024 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001025 }
1026 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1027 {
1028 list_unref(p->vv_list);
1029 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001030 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001031 }
1032 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001033 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001034 hash_clear(&compat_hashtab);
1035
Bram Moolenaard9fba312005-06-26 22:34:35 +00001036 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001037# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001038 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001039# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001040
1041 /* global variables */
1042 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001043
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001044 /* autoloaded script names */
1045 ga_clear_strings(&ga_loaded);
1046
Bram Moolenaarcca74132013-09-25 21:00:28 +02001047 /* Script-local variables. First clear all the variables and in a second
1048 * loop free the scriptvar_T, because a variable in one script might hold
1049 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001051 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001052 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001053 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001054 ga_clear(&ga_scripts);
1055
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001056 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001057 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001058
1059 /* functions */
1060 free_all_functions();
1061 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001062}
1063#endif
1064
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 * Return the name of the executed function.
1067 */
1068 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001069func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
1074/*
1075 * Return the address holding the next breakpoint line for a funccall cookie.
1076 */
1077 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001078func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082
1083/*
1084 * Return the address holding the debug tick for a funccall cookie.
1085 */
1086 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/*
1093 * Return the nesting level for a funccall cookie.
1094 */
1095 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001096func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
Bram Moolenaar33570922005-01-25 22:26:29 +00001098 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099}
1100
1101/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001102funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001104/* pointer to list of previously used funccal, still around because some
1105 * item in it is still being used. */
1106funccall_T *previous_funccal = NULL;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/*
1109 * Return TRUE when a function was ended by a ":return" command.
1110 */
1111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 return current_funccal->returned;
1115}
1116
1117
1118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 * Set an internal variable to a string value. Creates the variable if it does
1120 * not already exist.
1121 */
1122 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001123set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001126 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127
1128 val = vim_strsave(value);
1129 if (val != NULL)
1130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001132 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001134 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001135 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 }
1138}
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001141#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143static char_u *redir_endp = NULL;
1144static char_u *redir_varname = NULL;
1145
1146/*
1147 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001148 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 * Returns OK if successfully completed the setup. FAIL otherwise.
1150 */
1151 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001152var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153{
1154 int save_emsg;
1155 int err;
1156 typval_T tv;
1157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001159 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 {
1161 EMSG(_(e_invarg));
1162 return FAIL;
1163 }
1164
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 redir_varname = vim_strsave(name);
1167 if (redir_varname == NULL)
1168 return FAIL;
1169
1170 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1171 if (redir_lval == NULL)
1172 {
1173 var_redir_stop();
1174 return FAIL;
1175 }
1176
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 /* The output is stored in growarray "redir_ga" until redirection ends. */
1178 ga_init2(&redir_ga, (int)sizeof(char), 500);
1179
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001180 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001181 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001182 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1184 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001185 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 if (redir_endp != NULL && *redir_endp != NUL)
1187 /* Trailing characters are present after the variable name */
1188 EMSG(_(e_trailing));
1189 else
1190 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
1193 return FAIL;
1194 }
1195
1196 /* check if we can write to the variable: set it to or append an empty
1197 * string */
1198 save_emsg = did_emsg;
1199 did_emsg = FALSE;
1200 tv.v_type = VAR_STRING;
1201 tv.vval.v_string = (char_u *)"";
1202 if (append)
1203 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1204 else
1205 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001206 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001208 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209 if (err)
1210 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001211 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 var_redir_stop();
1213 return FAIL;
1214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215
1216 return OK;
1217}
1218
1219/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001220 * Append "value[value_len]" to the variable set by var_redir_start().
1221 * The actual appending is postponed until redirection ends, because the value
1222 * appended may in fact be the string we write to, changing it may cause freed
1223 * memory to be used:
1224 * :redir => foo
1225 * :let foo
1226 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 */
1228 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001229var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232
1233 if (redir_lval == NULL)
1234 return;
1235
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001239 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001241 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001242 {
1243 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001244 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001245 }
1246 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248}
1249
1250/*
1251 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001252 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001253 */
1254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001255var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001256{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001257 typval_T tv;
1258
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001259 if (EVALCMD_BUSY)
1260 {
1261 redir_lval = NULL;
1262 return;
1263 }
1264
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001265 if (redir_lval != NULL)
1266 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001267 /* If there was no error: assign the text to the variable. */
1268 if (redir_endp != NULL)
1269 {
1270 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1271 tv.v_type = VAR_STRING;
1272 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001273 /* Call get_lval() again, if it's inside a Dict or List it may
1274 * have changed. */
1275 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001276 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001277 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1278 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1279 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001280 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001281
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001282 /* free the collected output */
1283 vim_free(redir_ga.ga_data);
1284 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001285
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001286 vim_free(redir_lval);
1287 redir_lval = NULL;
1288 }
1289 vim_free(redir_varname);
1290 redir_varname = NULL;
1291}
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293# if defined(FEAT_MBYTE) || defined(PROTO)
1294 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001295eval_charconvert(
1296 char_u *enc_from,
1297 char_u *enc_to,
1298 char_u *fname_from,
1299 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int err = FALSE;
1302
1303 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1304 set_vim_var_string(VV_CC_TO, enc_to, -1);
1305 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1306 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1307 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1308 err = TRUE;
1309 set_vim_var_string(VV_CC_FROM, NULL, -1);
1310 set_vim_var_string(VV_CC_TO, NULL, -1);
1311 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1312 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1313
1314 if (err)
1315 return FAIL;
1316 return OK;
1317}
1318# endif
1319
1320# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1321 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
1324 int err = FALSE;
1325
1326 set_vim_var_string(VV_FNAME_IN, fname, -1);
1327 set_vim_var_string(VV_CMDARG, args, -1);
1328 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1329 err = TRUE;
1330 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1331 set_vim_var_string(VV_CMDARG, NULL, -1);
1332
1333 if (err)
1334 {
1335 mch_remove(fname);
1336 return FAIL;
1337 }
1338 return OK;
1339}
1340# endif
1341
1342# if defined(FEAT_DIFF) || defined(PROTO)
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_diff(
1345 char_u *origfile,
1346 char_u *newfile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err = FALSE;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359
1360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361eval_patch(
1362 char_u *origfile,
1363 char_u *difffile,
1364 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 int err;
1367
1368 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1369 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1370 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1371 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1372 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1373 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1374 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1375}
1376# endif
1377
1378/*
1379 * Top level evaluation function, returning a boolean.
1380 * Sets "error" to TRUE if there was an error.
1381 * Return TRUE or FALSE.
1382 */
1383 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001384eval_to_bool(
1385 char_u *arg,
1386 int *error,
1387 char_u **nextcmd,
1388 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
Bram Moolenaar33570922005-01-25 22:26:29 +00001390 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001391 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 if (skip)
1394 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 else
1398 {
1399 *error = FALSE;
1400 if (!skip)
1401 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001402 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 }
1406 if (skip)
1407 --emsg_skip;
1408
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001409 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410}
1411
1412/*
1413 * Top level evaluation function, returning a string. If "skip" is TRUE,
1414 * only parsing to "nextcmd" is done, without reporting errors. Return
1415 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1416 */
1417 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001418eval_to_string_skip(
1419 char_u *arg,
1420 char_u **nextcmd,
1421 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422{
Bram Moolenaar33570922005-01-25 22:26:29 +00001423 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 char_u *retval;
1425
1426 if (skip)
1427 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 retval = NULL;
1430 else
1431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 retval = vim_strsave(get_tv_string(&tv));
1433 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435 if (skip)
1436 --emsg_skip;
1437
1438 return retval;
1439}
1440
1441/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001442 * Skip over an expression at "*pp".
1443 * Return FAIL for an error, OK otherwise.
1444 */
1445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001446skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001447{
Bram Moolenaar33570922005-01-25 22:26:29 +00001448 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001449
1450 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001451 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001452}
1453
1454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001456 * When "convert" is TRUE convert a List into a sequence of lines and convert
1457 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 * Return pointer to allocated memory, or NULL for failure.
1459 */
1460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461eval_to_string(
1462 char_u *arg,
1463 char_u **nextcmd,
1464 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001468 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001469#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001470 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001473 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 retval = NULL;
1475 else
1476 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001477 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001478 {
1479 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001480 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001481 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001482 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001483 if (tv.vval.v_list->lv_len > 0)
1484 ga_append(&ga, NL);
1485 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001486 ga_append(&ga, NUL);
1487 retval = (char_u *)ga.ga_data;
1488 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001489#ifdef FEAT_FLOAT
1490 else if (convert && tv.v_type == VAR_FLOAT)
1491 {
1492 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1493 retval = vim_strsave(numbuf);
1494 }
1495#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001496 else
1497 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500
1501 return retval;
1502}
1503
1504/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 * Call eval_to_string() without using current local variables and using
1506 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
1508 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509eval_to_string_safe(
1510 char_u *arg,
1511 char_u **nextcmd,
1512 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
1514 char_u *retval;
1515 void *save_funccalp;
1516
1517 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001518 if (use_sandbox)
1519 ++sandbox;
1520 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001521 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001522 if (use_sandbox)
1523 --sandbox;
1524 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 restore_funccal(save_funccalp);
1526 return retval;
1527}
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529/*
1530 * Top level evaluation function, returning a number.
1531 * Evaluates "expr" silently.
1532 * Returns -1 for an error.
1533 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536{
Bram Moolenaar33570922005-01-25 22:26:29 +00001537 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001538 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001539 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 ++emsg_off;
1542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001543 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 retval = -1;
1545 else
1546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001547 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 --emsg_off;
1551
1552 return retval;
1553}
1554
Bram Moolenaara40058a2005-07-11 22:42:07 +00001555/*
1556 * Prepare v: variable "idx" to be used.
1557 * Save the current typeval in "save_tv".
1558 * When not used yet add the variable to the v: hashtable.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001562{
1563 *save_tv = vimvars[idx].vv_tv;
1564 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1565 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1566}
1567
1568/*
1569 * Restore v: variable "idx" to typeval "save_tv".
1570 * When no longer defined, remove the variable from the v: hashtable.
1571 */
1572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001573restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001574{
1575 hashitem_T *hi;
1576
Bram Moolenaara40058a2005-07-11 22:42:07 +00001577 vimvars[idx].vv_tv = *save_tv;
1578 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1579 {
1580 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1581 if (HASHITEM_EMPTY(hi))
1582 EMSG2(_(e_intern2), "restore_vimvar()");
1583 else
1584 hash_remove(&vimvarht, hi);
1585 }
1586}
1587
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001588#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589/*
1590 * Evaluate an expression to a list with suggestions.
1591 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001592 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593 */
1594 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001595eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001596{
1597 typval_T save_val;
1598 typval_T rettv;
1599 list_T *list = NULL;
1600 char_u *p = skipwhite(expr);
1601
1602 /* Set "v:val" to the bad word. */
1603 prepare_vimvar(VV_VAL, &save_val);
1604 vimvars[VV_VAL].vv_type = VAR_STRING;
1605 vimvars[VV_VAL].vv_str = badword;
1606 if (p_verbose == 0)
1607 ++emsg_off;
1608
1609 if (eval1(&p, &rettv, TRUE) == OK)
1610 {
1611 if (rettv.v_type != VAR_LIST)
1612 clear_tv(&rettv);
1613 else
1614 list = rettv.vval.v_list;
1615 }
1616
1617 if (p_verbose == 0)
1618 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001619 restore_vimvar(VV_VAL, &save_val);
1620
1621 return list;
1622}
1623
1624/*
1625 * "list" is supposed to contain two items: a word and a number. Return the
1626 * word in "pp" and the number as the return value.
1627 * Return -1 if anything isn't right.
1628 * Used to get the good word and score from the eval_spell_expr() result.
1629 */
1630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001631get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001632{
1633 listitem_T *li;
1634
1635 li = list->lv_first;
1636 if (li == NULL)
1637 return -1;
1638 *pp = get_tv_string(&li->li_tv);
1639
1640 li = li->li_next;
1641 if (li == NULL)
1642 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001643 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001644}
1645#endif
1646
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001648 * Top level evaluation function.
1649 * Returns an allocated typval_T with the result.
1650 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001651 */
1652 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001653eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001654{
1655 typval_T *tv;
1656
1657 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001658 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001659 {
1660 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001661 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001662 }
1663
1664 return tv;
1665}
1666
1667
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001670 * Uses argv[argc] for the function arguments. Only Number and String
1671 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001674 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675call_vim_function(
1676 char_u *func,
1677 int argc,
1678 char_u **argv,
1679 int safe, /* use the sandbox */
1680 int str_arg_only, /* all arguments are strings */
1681 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
Bram Moolenaar33570922005-01-25 22:26:29 +00001683 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001684 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 int len;
1686 int i;
1687 int doesrange;
1688 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001691 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695 for (i = 0; i < argc; i++)
1696 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001697 /* Pass a NULL or empty argument as an empty string */
1698 if (argv[i] == NULL || *argv[i] == NUL)
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001702 continue;
1703 }
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 if (str_arg_only)
1706 len = 0;
1707 else
1708 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001709 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (len != 0 && len == (int)STRLEN(argv[i]))
1711 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001712 argvars[i].v_type = VAR_NUMBER;
1713 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 else
1716 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001717 argvars[i].v_type = VAR_STRING;
1718 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720 }
1721
1722 if (safe)
1723 {
1724 save_funccalp = save_funccal();
1725 ++sandbox;
1726 }
1727
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1729 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001731 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (safe)
1733 {
1734 --sandbox;
1735 restore_funccal(save_funccalp);
1736 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 vim_free(argvars);
1738
1739 if (ret == FAIL)
1740 clear_tv(rettv);
1741
1742 return ret;
1743}
1744
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001745/*
1746 * Call vimL function "func" and return the result as a number.
1747 * Returns -1 when calling the function fails.
1748 * Uses argv[argc] for the function arguments.
1749 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001750 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001751call_func_retnr(
1752 char_u *func,
1753 int argc,
1754 char_u **argv,
1755 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001756{
1757 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001758 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759
1760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1762 return -1;
1763
1764 retval = get_tv_number_chk(&rettv, NULL);
1765 clear_tv(&rettv);
1766 return retval;
1767}
1768
1769#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1770 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1771
Bram Moolenaar4f688582007-07-24 12:34:30 +00001772# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001774 * Call vimL function "func" and return the result as a string.
1775 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 * Uses argv[argc] for the function arguments.
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779call_func_retstr(
1780 char_u *func,
1781 int argc,
1782 char_u **argv,
1783 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784{
1785 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001786 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001787
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001788 /* All arguments are passed as strings, no conversion to number. */
1789 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001790 return NULL;
1791
1792 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return retval;
1795}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001796# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001798/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001799 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001800 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001801 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001802 */
1803 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001804call_func_retlist(
1805 char_u *func,
1806 int argc,
1807 char_u **argv,
1808 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001809{
1810 typval_T rettv;
1811
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001812 /* All arguments are passed as strings, no conversion to number. */
1813 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001814 return NULL;
1815
1816 if (rettv.v_type != VAR_LIST)
1817 {
1818 clear_tv(&rettv);
1819 return NULL;
1820 }
1821
1822 return rettv.vval.v_list;
1823}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#endif
1825
1826/*
1827 * Save the current function call pointer, and set it to NULL.
1828 * Used when executing autocommands and for ":source".
1829 */
1830 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001833 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 current_funccal = NULL;
1836 return (void *)fc;
1837}
1838
1839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001842 funccall_T *fc = (funccall_T *)vfc;
1843
1844 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845}
1846
Bram Moolenaar05159a02005-02-26 23:04:13 +00001847#if defined(FEAT_PROFILE) || defined(PROTO)
1848/*
1849 * Prepare profiling for entering a child or something else that is not
1850 * counted for the script/function itself.
1851 * Should always be called in pair with prof_child_exit().
1852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854prof_child_enter(
1855 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001856{
1857 funccall_T *fc = current_funccal;
1858
1859 if (fc != NULL && fc->func->uf_profiling)
1860 profile_start(&fc->prof_child);
1861 script_prof_save(tm);
1862}
1863
1864/*
1865 * Take care of time spent in a child.
1866 * Should always be called after prof_child_enter().
1867 */
1868 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001869prof_child_exit(
1870 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001871{
1872 funccall_T *fc = current_funccal;
1873
1874 if (fc != NULL && fc->func->uf_profiling)
1875 {
1876 profile_end(&fc->prof_child);
1877 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1878 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1879 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1880 }
1881 script_prof_restore(tm);
1882}
1883#endif
1884
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#ifdef FEAT_FOLDING
1887/*
1888 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1889 * it in "*cp". Doesn't give error messages.
1890 */
1891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001895 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001897 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1898 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001901 if (use_sandbox)
1902 ++sandbox;
1903 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 retval = 0;
1907 else
1908 {
1909 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 if (tv.v_type == VAR_NUMBER)
1911 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001912 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 retval = 0;
1914 else
1915 {
1916 /* If the result is a string, check if there is a non-digit before
1917 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (!VIM_ISDIGIT(*s) && *s != '-')
1920 *cp = *s++;
1921 retval = atol((char *)s);
1922 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001926 if (use_sandbox)
1927 --sandbox;
1928 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001930 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932#endif
1933
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 * ":let" list all variable values
1936 * ":let var1 var2" list variable values
1937 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 * ":let var += expr" assignment command.
1939 * ":let var -= expr" assignment command.
1940 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001944ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001948 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 int var_count = 0;
1951 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001953 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
Bram Moolenaardb552d602006-03-23 22:59:57 +00001956 argend = skip_var_list(arg, &var_count, &semicolon);
1957 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001959 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1960 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001961 expr = skipwhite(argend);
1962 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1963 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001965 /*
1966 * ":let" without "=": list variables
1967 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 if (*arg == '[')
1969 EMSG(_(e_invarg));
1970 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001973 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976 list_glob_vars(&first);
1977 list_buf_vars(&first);
1978 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001979#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001980 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001981#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001982 list_script_vars(&first);
1983 list_func_vars(&first);
1984 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 eap->nextcmd = check_nextcmd(arg);
1987 }
1988 else
1989 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 op[0] = '=';
1991 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001992 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001994 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1995 op[0] = *expr; /* +=, -= or .= */
1996 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001998 else
1999 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (eap->skip)
2002 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (eap->skip)
2005 {
2006 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002007 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 --emsg_skip;
2009 }
2010 else if (i != FAIL)
2011 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
2017}
2018
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019/*
2020 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2021 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 * When "nextchars" is not NULL it points to a string with characters that
2023 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2024 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 * Returns OK or FAIL;
2026 */
2027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028ex_let_vars(
2029 char_u *arg_start,
2030 typval_T *tv,
2031 int copy, /* copy values from "tv", don't move */
2032 int semicolon, /* from skip_var_list() */
2033 int var_count, /* from skip_var_list() */
2034 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035{
2036 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002037 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002039 listitem_T *item;
2040 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041
2042 if (*arg != '[')
2043 {
2044 /*
2045 * ":let var = expr" or ":for var in list"
2046 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 return OK;
2050 }
2051
2052 /*
2053 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2054 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002055 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 {
2057 EMSG(_(e_listreq));
2058 return FAIL;
2059 }
2060
2061 i = list_len(l);
2062 if (semicolon == 0 && var_count < i)
2063 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002064 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 return FAIL;
2066 }
2067 if (var_count - semicolon > i)
2068 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002069 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 return FAIL;
2071 }
2072
2073 item = l->lv_first;
2074 while (*arg != ']')
2075 {
2076 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002077 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078 item = item->li_next;
2079 if (arg == NULL)
2080 return FAIL;
2081
2082 arg = skipwhite(arg);
2083 if (*arg == ';')
2084 {
2085 /* Put the rest of the list (may be empty) in the var after ';'.
2086 * Create a new list for this. */
2087 l = list_alloc();
2088 if (l == NULL)
2089 return FAIL;
2090 while (item != NULL)
2091 {
2092 list_append_tv(l, &item->li_tv);
2093 item = item->li_next;
2094 }
2095
2096 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002097 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002098 ltv.vval.v_list = l;
2099 l->lv_refcount = 1;
2100
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2102 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103 clear_tv(&ltv);
2104 if (arg == NULL)
2105 return FAIL;
2106 break;
2107 }
2108 else if (*arg != ',' && *arg != ']')
2109 {
2110 EMSG2(_(e_intern2), "ex_let_vars()");
2111 return FAIL;
2112 }
2113 }
2114
2115 return OK;
2116}
2117
2118/*
2119 * Skip over assignable variable "var" or list of variables "[var, var]".
2120 * Used for ":let varvar = expr" and ":for varvar in expr".
2121 * For "[var, var]" increment "*var_count" for each variable.
2122 * for "[var, var; var]" set "semicolon".
2123 * Return NULL for an error.
2124 */
2125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126skip_var_list(
2127 char_u *arg,
2128 int *var_count,
2129 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130{
2131 char_u *p, *s;
2132
2133 if (*arg == '[')
2134 {
2135 /* "[var, var]": find the matching ']'. */
2136 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002137 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002138 {
2139 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2140 s = skip_var_one(p);
2141 if (s == p)
2142 {
2143 EMSG2(_(e_invarg2), p);
2144 return NULL;
2145 }
2146 ++*var_count;
2147
2148 p = skipwhite(s);
2149 if (*p == ']')
2150 break;
2151 else if (*p == ';')
2152 {
2153 if (*semicolon == 1)
2154 {
2155 EMSG(_("Double ; in list of variables"));
2156 return NULL;
2157 }
2158 *semicolon = 1;
2159 }
2160 else if (*p != ',')
2161 {
2162 EMSG2(_(e_invarg2), p);
2163 return NULL;
2164 }
2165 }
2166 return p + 1;
2167 }
2168 else
2169 return skip_var_one(arg);
2170}
2171
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002173 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002174 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002176 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002178{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002179 if (*arg == '@' && arg[1] != NUL)
2180 return arg + 2;
2181 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2182 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002183}
2184
Bram Moolenaara7043832005-01-21 11:56:39 +00002185/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002186 * List variables for hashtab "ht" with prefix "prefix".
2187 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_hashtable_vars(
2191 hashtab_T *ht,
2192 char_u *prefix,
2193 int empty,
2194 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002195{
Bram Moolenaar33570922005-01-25 22:26:29 +00002196 hashitem_T *hi;
2197 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002198 int todo;
2199
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002200 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002201 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2202 {
2203 if (!HASHITEM_EMPTY(hi))
2204 {
2205 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002206 di = HI2DI(hi);
2207 if (empty || di->di_tv.v_type != VAR_STRING
2208 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002210 }
2211 }
2212}
2213
2214/*
2215 * List global variables.
2216 */
2217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002218list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002219{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
2223/*
2224 * List buffer variables.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 char_u numbuf[NUMBUFLEN];
2230
Bram Moolenaar429fa852013-04-15 12:27:36 +02002231 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002233
2234 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2236 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002237}
2238
2239/*
2240 * List window variables.
2241 */
2242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002243list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002245 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002246 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002247}
2248
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#ifdef FEAT_WINDOWS
2250/*
2251 * List tab page variables.
2252 */
2253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002254list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002256 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002258}
2259#endif
2260
Bram Moolenaara7043832005-01-21 11:56:39 +00002261/*
2262 * List Vim variables.
2263 */
2264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002265list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002267 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268}
2269
2270/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271 * List script-local variables, if there is a script.
2272 */
2273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002274list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275{
2276 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2278 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002279}
2280
2281/*
2282 * List function variables, if there is a function.
2283 */
2284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002285list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002286{
2287 if (current_funccal != NULL)
2288 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002289 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002290}
2291
2292/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 * List variables in "arg".
2294 */
2295 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002296list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297{
2298 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 char_u *name_start;
2302 char_u *arg_subsc;
2303 char_u *tofree;
2304 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305
2306 while (!ends_excmd(*arg) && !got_int)
2307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002310 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2312 {
2313 emsg_severe = TRUE;
2314 EMSG(_(e_trailing));
2315 break;
2316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 /* get_name_len() takes care of expanding curly braces */
2321 name_start = name = arg;
2322 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2323 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 /* This is mainly to keep test 49 working: when expanding
2326 * curly braces fails overrule the exception error message. */
2327 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 emsg_severe = TRUE;
2330 EMSG2(_(e_invarg2), arg);
2331 break;
2332 }
2333 error = TRUE;
2334 }
2335 else
2336 {
2337 if (tofree != NULL)
2338 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002339 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 else
2342 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002343 /* handle d.key, l[idx], f(expr) */
2344 arg_subsc = arg;
2345 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002349 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002352 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002353 case 'g': list_glob_vars(first); break;
2354 case 'b': list_buf_vars(first); break;
2355 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002356#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002357 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002358#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002359 case 'v': list_vim_vars(first); break;
2360 case 's': list_script_vars(first); break;
2361 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362 default:
2363 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002364 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002365 }
2366 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002367 {
2368 char_u numbuf[NUMBUFLEN];
2369 char_u *tf;
2370 int c;
2371 char_u *s;
2372
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002373 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374 c = *arg;
2375 *arg = NUL;
2376 list_one_var_a((char_u *)"",
2377 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002378 tv.v_type,
2379 s == NULL ? (char_u *)"" : s,
2380 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002381 *arg = c;
2382 vim_free(tf);
2383 }
2384 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002385 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
2387 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002388
2389 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002391
2392 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 }
2394
2395 return arg;
2396}
2397
2398/*
2399 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2400 * Returns a pointer to the char just after the var name.
2401 * Returns NULL if there is an error.
2402 */
2403 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002404ex_let_one(
2405 char_u *arg, /* points to variable name */
2406 typval_T *tv, /* value to assign to variable */
2407 int copy, /* copy value from "tv" */
2408 char_u *endchars, /* valid chars after variable name or NULL */
2409 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410{
2411 int c1;
2412 char_u *name;
2413 char_u *p;
2414 char_u *arg_end = NULL;
2415 int len;
2416 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418
2419 /*
2420 * ":let $VAR = expr": Set environment variable.
2421 */
2422 if (*arg == '$')
2423 {
2424 /* Find the end of the name. */
2425 ++arg;
2426 name = arg;
2427 len = get_env_len(&arg);
2428 if (len == 0)
2429 EMSG2(_(e_invarg2), name - 1);
2430 else
2431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 if (op != NULL && (*op == '+' || *op == '-'))
2433 EMSG2(_(e_letwrong), op);
2434 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002437 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 {
2439 c1 = name[len];
2440 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 p = get_tv_string_chk(tv);
2442 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 int mustfree = FALSE;
2445 char_u *s = vim_getenv(name, &mustfree);
2446
2447 if (s != NULL)
2448 {
2449 p = tofree = concat_str(s, p);
2450 if (mustfree)
2451 vim_free(s);
2452 }
2453 }
2454 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 if (STRICMP(name, "HOME") == 0)
2458 init_homedir();
2459 else if (didset_vim && STRICMP(name, "VIM") == 0)
2460 didset_vim = FALSE;
2461 else if (didset_vimruntime
2462 && STRICMP(name, "VIMRUNTIME") == 0)
2463 didset_vimruntime = FALSE;
2464 arg_end = arg;
2465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002467 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470 }
2471
2472 /*
2473 * ":let &option = expr": Set option value.
2474 * ":let &l:option = expr": Set local option value.
2475 * ":let &g:option = expr": Set global option value.
2476 */
2477 else if (*arg == '&')
2478 {
2479 /* Find the end of the name. */
2480 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002481 if (p == NULL || (endchars != NULL
2482 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 long n;
2487 int opt_type;
2488 long numval;
2489 char_u *stringval = NULL;
2490 char_u *s;
2491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 c1 = *p;
2493 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002495 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 s = get_tv_string_chk(tv); /* != NULL if number or string */
2497 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 {
2499 opt_type = get_option_value(arg, &numval,
2500 &stringval, opt_flags);
2501 if ((opt_type == 1 && *op == '.')
2502 || (opt_type == 0 && *op != '.'))
2503 EMSG2(_(e_letwrong), op);
2504 else
2505 {
2506 if (opt_type == 1) /* number */
2507 {
2508 if (*op == '+')
2509 n = numval + n;
2510 else
2511 n = numval - n;
2512 }
2513 else if (opt_type == 0 && stringval != NULL) /* string */
2514 {
2515 s = concat_str(stringval, s);
2516 vim_free(stringval);
2517 stringval = s;
2518 }
2519 }
2520 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 if (s != NULL)
2522 {
2523 set_option_value(arg, n, s, opt_flags);
2524 arg_end = p;
2525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 }
2529 }
2530
2531 /*
2532 * ":let @r = expr": Set register contents.
2533 */
2534 else if (*arg == '@')
2535 {
2536 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 if (op != NULL && (*op == '+' || *op == '-'))
2538 EMSG2(_(e_letwrong), op);
2539 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002540 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 EMSG(_(e_letunexp));
2542 else
2543 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002544 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 char_u *s;
2546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 p = get_tv_string_chk(tv);
2548 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002550 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002551 if (s != NULL)
2552 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002553 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002554 vim_free(s);
2555 }
2556 }
2557 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002559 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 arg_end = arg + 1;
2561 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002562 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 }
2564 }
2565
2566 /*
2567 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002570 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002574 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2578 EMSG(_(e_letunexp));
2579 else
2580 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002581 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 arg_end = p;
2583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 }
2587
2588 else
2589 EMSG2(_(e_invarg2), arg);
2590
2591 return arg_end;
2592}
2593
2594/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2596 */
2597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002598check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599{
2600 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2601 {
2602 EMSG2(_(e_readonlyvar), arg);
2603 return TRUE;
2604 }
2605 return FALSE;
2606}
2607
2608/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * Get an lval: variable, Dict item or List item that can be assigned a value
2610 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2611 * "name.key", "name.key[expr]" etc.
2612 * Indexing only works if "name" is an existing List or Dictionary.
2613 * "name" points to the start of the name.
2614 * If "rettv" is not NULL it points to the value to be assigned.
2615 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2616 * wrong; must end in space or cmd separator.
2617 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002618 * flags:
2619 * GLV_QUIET: do not give error messages
2620 * GLV_NO_AUTOLOAD: do not use script autoloading
2621 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002623 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002625 */
2626 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002627get_lval(
2628 char_u *name,
2629 typval_T *rettv,
2630 lval_T *lp,
2631 int unlet,
2632 int skip,
2633 int flags, /* GLV_ values */
2634 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002636 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 char_u *expr_start, *expr_end;
2638 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 dictitem_T *v;
2640 typval_T var1;
2641 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002643 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002644 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002647 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651
2652 if (skip)
2653 {
2654 /* When skipping just find the end of the name. */
2655 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002656 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 }
2658
2659 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002660 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (expr_start != NULL)
2662 {
2663 /* Don't expand the name when we already know there is an error. */
2664 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2665 && *p != '[' && *p != '.')
2666 {
2667 EMSG(_(e_trailing));
2668 return NULL;
2669 }
2670
2671 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2672 if (lp->ll_exp_name == NULL)
2673 {
2674 /* Report an invalid expression in braces, unless the
2675 * expression evaluation has been cancelled due to an
2676 * aborting error, an interrupt, or an exception. */
2677 if (!aborting() && !quiet)
2678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002679 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 EMSG2(_(e_invarg2), name);
2681 return NULL;
2682 }
2683 }
2684 lp->ll_name = lp->ll_exp_name;
2685 }
2686 else
2687 lp->ll_name = name;
2688
2689 /* Without [idx] or .key we are done. */
2690 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2691 return p;
2692
2693 cc = *p;
2694 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002695 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (v == NULL && !quiet)
2697 EMSG2(_(e_undefvar), lp->ll_name);
2698 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 if (v == NULL)
2700 return NULL;
2701
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 /*
2703 * Loop until no more [idx] or .key is following.
2704 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002705 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2709 && !(lp->ll_tv->v_type == VAR_DICT
2710 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_("E689: Can only index a List or Dictionary"));
2714 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E708: [:] must come last"));
2720 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002721 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 len = -1;
2724 if (*p == '.')
2725 {
2726 key = p + 1;
2727 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2728 ;
2729 if (len == 0)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
2733 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 }
2735 p = key + len;
2736 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 else
2738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 if (*p == ':')
2742 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743 else
2744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 empty1 = FALSE;
2746 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 if (get_tv_string_chk(&var1) == NULL)
2749 {
2750 /* not a number or string */
2751 clear_tv(&var1);
2752 return NULL;
2753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755
2756 /* Optionally get the second index [ :expr]. */
2757 if (*p == ':')
2758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002762 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002763 if (!empty1)
2764 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002766 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (rettv != NULL && (rettv->v_type != VAR_LIST
2768 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
2771 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (!empty1)
2773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 p = skipwhite(p + 1);
2777 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 else
2780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (!empty1)
2785 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 if (get_tv_string_chk(&var2) == NULL)
2789 {
2790 /* not a number or string */
2791 if (!empty1)
2792 clear_tv(&var1);
2793 clear_tv(&var2);
2794 return NULL;
2795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002798 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002801
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (!quiet)
2805 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 if (!empty1)
2807 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812
2813 /* Skip to past ']'. */
2814 ++p;
2815 }
2816
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
2819 if (len == -1)
2820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002822 key = get_tv_string_chk(&var1); /* is number or string */
2823 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 }
2828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 lp->ll_list = NULL;
2830 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002831 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002832
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002833 /* When assigning to a scope dictionary check that a function and
2834 * variable name is valid (only variable name unless it is l: or
2835 * g: dictionary). Disallow overwriting a builtin function. */
2836 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002838 int prevval;
2839 int wrong;
2840
2841 if (len != -1)
2842 {
2843 prevval = key[len];
2844 key[len] = NUL;
2845 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002846 else
2847 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002848 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2849 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002850 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002851 || !valid_varname(key);
2852 if (len != -1)
2853 key[len] = prevval;
2854 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002855 return NULL;
2856 }
2857
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 /* Can't add "v:" variable. */
2861 if (lp->ll_dict == &vimvardict)
2862 {
2863 EMSG2(_(e_illvar), name);
2864 return NULL;
2865 }
2866
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002867 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 if (len == -1)
2873 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 }
2876 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 if (len == -1)
2881 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 p = NULL;
2884 break;
2885 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002886 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002887 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002888 return NULL;
2889
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 if (len == -1)
2891 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 }
2894 else
2895 {
2896 /*
2897 * Get the number and item for the only or first index of the List.
2898 */
2899 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 else
2902 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002903 lp->ll_n1 = (long)get_tv_number(&var1);
2904 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 clear_tv(&var1);
2906 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_list = lp->ll_tv->vval.v_list;
2909 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2910 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002912 if (lp->ll_n1 < 0)
2913 {
2914 lp->ll_n1 = 0;
2915 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2916 }
2917 }
2918 if (lp->ll_li == NULL)
2919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002922 if (!quiet)
2923 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002925 }
2926
2927 /*
2928 * May need to find the item or absolute index for the second
2929 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 * When no index given: "lp->ll_empty2" is TRUE.
2931 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002934 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002935 lp->ll_n2 = (long)get_tv_number(&var2);
2936 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002941 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 {
2943 if (!quiet)
2944 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002948 }
2949
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2951 if (lp->ll_n1 < 0)
2952 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2953 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002954 {
2955 if (!quiet)
2956 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002958 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002959 }
2960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002962 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 return p;
2966}
2967
2968/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002969 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 */
2971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002972clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973{
2974 vim_free(lp->ll_exp_name);
2975 vim_free(lp->ll_newkey);
2976}
2977
2978/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 */
2983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002984set_var_lval(
2985 lval_T *lp,
2986 char_u *endp,
2987 typval_T *rettv,
2988 int copy,
2989 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990{
2991 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002992 listitem_T *ri;
2993 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994
2995 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 cc = *endp;
3000 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 if (op != NULL && *op != '=')
3002 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003003 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003006 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003007 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003008 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003010 if ((di == NULL
3011 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3012 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3013 FALSE)))
3014 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 set_var(lp->ll_name, &tv, FALSE);
3016 clear_tv(&tv);
3017 }
3018 }
3019 else
3020 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003024 else if (tv_check_lock(lp->ll_newkey == NULL
3025 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003026 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003027 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 else if (lp->ll_range)
3029 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003030 listitem_T *ll_li = lp->ll_li;
3031 int ll_n1 = lp->ll_n1;
3032
3033 /*
3034 * Check whether any of the list items is locked
3035 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003036 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003037 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003038 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003039 return;
3040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3042 break;
3043 ll_li = ll_li->li_next;
3044 ++ll_n1;
3045 }
3046
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 /*
3048 * Assign the List values to the list items.
3049 */
3050 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 if (op != NULL && *op != '=')
3053 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3054 else
3055 {
3056 clear_tv(&lp->ll_li->li_tv);
3057 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 ri = ri->li_next;
3060 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3061 break;
3062 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003063 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003065 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003066 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 ri = NULL;
3068 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003069 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 lp->ll_li = lp->ll_li->li_next;
3072 ++lp->ll_n1;
3073 }
3074 if (ri != NULL)
3075 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 else if (lp->ll_empty2
3077 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 : lp->ll_n1 != lp->ll_n2)
3079 EMSG(_("E711: List value has not enough items"));
3080 }
3081 else
3082 {
3083 /*
3084 * Assign to a List or Dictionary item.
3085 */
3086 if (lp->ll_newkey != NULL)
3087 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 if (op != NULL && *op != '=')
3089 {
3090 EMSG2(_(e_letwrong), op);
3091 return;
3092 }
3093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003095 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 if (di == NULL)
3097 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003098 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3099 {
3100 vim_free(di);
3101 return;
3102 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 else if (op != NULL && *op != '=')
3106 {
3107 tv_op(lp->ll_tv, rettv, op);
3108 return;
3109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003111 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003112
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003113 /*
3114 * Assign the value to the variable or list item.
3115 */
3116 if (copy)
3117 copy_tv(rettv, lp->ll_tv);
3118 else
3119 {
3120 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003121 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003122 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003123 }
3124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003125}
3126
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003128 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3129 * Returns OK or FAIL.
3130 */
3131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003132tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003134 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 char_u numbuf[NUMBUFLEN];
3136 char_u *s;
3137
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003138 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3139 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3140 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 {
3142 switch (tv1->v_type)
3143 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003144 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 case VAR_DICT:
3146 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003147 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003148 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003149 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003150 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 break;
3152
3153 case VAR_LIST:
3154 if (*op != '+' || tv2->v_type != VAR_LIST)
3155 break;
3156 /* List += List */
3157 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3158 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3159 return OK;
3160
3161 case VAR_NUMBER:
3162 case VAR_STRING:
3163 if (tv2->v_type == VAR_LIST)
3164 break;
3165 if (*op == '+' || *op == '-')
3166 {
3167 /* nr += nr or nr -= nr*/
3168 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169#ifdef FEAT_FLOAT
3170 if (tv2->v_type == VAR_FLOAT)
3171 {
3172 float_T f = n;
3173
3174 if (*op == '+')
3175 f += tv2->vval.v_float;
3176 else
3177 f -= tv2->vval.v_float;
3178 clear_tv(tv1);
3179 tv1->v_type = VAR_FLOAT;
3180 tv1->vval.v_float = f;
3181 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003183#endif
3184 {
3185 if (*op == '+')
3186 n += get_tv_number(tv2);
3187 else
3188 n -= get_tv_number(tv2);
3189 clear_tv(tv1);
3190 tv1->v_type = VAR_NUMBER;
3191 tv1->vval.v_number = n;
3192 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003193 }
3194 else
3195 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003196 if (tv2->v_type == VAR_FLOAT)
3197 break;
3198
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003199 /* str .= str */
3200 s = get_tv_string(tv1);
3201 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3202 clear_tv(tv1);
3203 tv1->v_type = VAR_STRING;
3204 tv1->vval.v_string = s;
3205 }
3206 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003209#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003210 {
3211 float_T f;
3212
3213 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3214 && tv2->v_type != VAR_NUMBER
3215 && tv2->v_type != VAR_STRING))
3216 break;
3217 if (tv2->v_type == VAR_FLOAT)
3218 f = tv2->vval.v_float;
3219 else
3220 f = get_tv_number(tv2);
3221 if (*op == '+')
3222 tv1->vval.v_float += f;
3223 else
3224 tv1->vval.v_float -= f;
3225 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003226#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003227 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003228 }
3229 }
3230
3231 EMSG2(_(e_letwrong), op);
3232 return FAIL;
3233}
3234
3235/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 * Add a watcher to a list.
3237 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
3241 lw->lw_next = l->lv_watch;
3242 l->lv_watch = lw;
3243}
3244
3245/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003246 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 * No warning when it isn't found...
3248 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003249 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003250list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251{
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253
3254 lwp = &l->lv_watch;
3255 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3256 {
3257 if (lw == lwrem)
3258 {
3259 *lwp = lw->lw_next;
3260 break;
3261 }
3262 lwp = &lw->lw_next;
3263 }
3264}
3265
3266/*
3267 * Just before removing an item from a list: advance watchers to the next
3268 * item.
3269 */
3270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272{
Bram Moolenaar33570922005-01-25 22:26:29 +00003273 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274
3275 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3276 if (lw->lw_item == item)
3277 lw->lw_item = item->li_next;
3278}
3279
3280/*
3281 * Evaluate the expression used in a ":for var in expr" command.
3282 * "arg" points to "var".
3283 * Set "*errp" to TRUE for an error, FALSE otherwise;
3284 * Return a pointer that holds the info. Null when there is an error.
3285 */
3286 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287eval_for_line(
3288 char_u *arg,
3289 int *errp,
3290 char_u **nextcmdp,
3291 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292{
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003295 typval_T tv;
3296 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297
3298 *errp = TRUE; /* default: there is an error */
3299
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 if (fi == NULL)
3302 return NULL;
3303
3304 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3305 if (expr == NULL)
3306 return fi;
3307
3308 expr = skipwhite(expr);
3309 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3310 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003311 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003312 return fi;
3313 }
3314
3315 if (skip)
3316 ++emsg_skip;
3317 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3318 {
3319 *errp = FALSE;
3320 if (!skip)
3321 {
3322 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003323 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 clear_tv(&tv);
3327 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003328 else if (l == NULL)
3329 {
3330 /* a null list is like an empty list: do nothing */
3331 clear_tv(&tv);
3332 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 else
3334 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003335 /* No need to increment the refcount, it's already set for the
3336 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 fi->fi_list = l;
3338 list_add_watch(l, &fi->fi_lw);
3339 fi->fi_lw.lw_item = l->lv_first;
3340 }
3341 }
3342 }
3343 if (skip)
3344 --emsg_skip;
3345
3346 return fi;
3347}
3348
3349/*
3350 * Use the first item in a ":for" list. Advance to the next.
3351 * Assign the values to the variable (list). "arg" points to the first one.
3352 * Return TRUE when a valid item was found, FALSE when at end of list or
3353 * something wrong.
3354 */
3355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003358 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003360 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361
3362 item = fi->fi_lw.lw_item;
3363 if (item == NULL)
3364 result = FALSE;
3365 else
3366 {
3367 fi->fi_lw.lw_item = item->li_next;
3368 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3369 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3370 }
3371 return result;
3372}
3373
3374/*
3375 * Free the structure used to store info used by ":for".
3376 */
3377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003379{
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003382 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003383 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003385 list_unref(fi->fi_list);
3386 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 vim_free(fi);
3388}
3389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3391
3392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393set_context_for_expression(
3394 expand_T *xp,
3395 char_u *arg,
3396 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
3398 int got_eq = FALSE;
3399 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003400 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 if (cmdidx == CMD_let)
3403 {
3404 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003405 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003406 {
3407 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003408 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003409 {
3410 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003411 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003412 if (vim_iswhite(*p))
3413 break;
3414 }
3415 return;
3416 }
3417 }
3418 else
3419 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3420 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 while ((xp->xp_pattern = vim_strpbrk(arg,
3422 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3423 {
3424 c = *xp->xp_pattern;
3425 if (c == '&')
3426 {
3427 c = xp->xp_pattern[1];
3428 if (c == '&')
3429 {
3430 ++xp->xp_pattern;
3431 xp->xp_context = cmdidx != CMD_let || got_eq
3432 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3433 }
3434 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003435 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003437 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3438 xp->xp_pattern += 2;
3439
3440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442 else if (c == '$')
3443 {
3444 /* environment variable */
3445 xp->xp_context = EXPAND_ENV_VARS;
3446 }
3447 else if (c == '=')
3448 {
3449 got_eq = TRUE;
3450 xp->xp_context = EXPAND_EXPRESSION;
3451 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003452 else if (c == '#'
3453 && xp->xp_context == EXPAND_EXPRESSION)
3454 {
3455 /* Autoload function/variable contains '#'. */
3456 break;
3457 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003458 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 && xp->xp_context == EXPAND_FUNCTIONS
3460 && vim_strchr(xp->xp_pattern, '(') == NULL)
3461 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003462 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 break;
3464 }
3465 else if (cmdidx != CMD_let || got_eq)
3466 {
3467 if (c == '"') /* string */
3468 {
3469 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3470 if (c == '\\' && xp->xp_pattern[1] != NUL)
3471 ++xp->xp_pattern;
3472 xp->xp_context = EXPAND_NOTHING;
3473 }
3474 else if (c == '\'') /* literal string */
3475 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003476 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3478 /* skip */ ;
3479 xp->xp_context = EXPAND_NOTHING;
3480 }
3481 else if (c == '|')
3482 {
3483 if (xp->xp_pattern[1] == '|')
3484 {
3485 ++xp->xp_pattern;
3486 xp->xp_context = EXPAND_EXPRESSION;
3487 }
3488 else
3489 xp->xp_context = EXPAND_COMMANDS;
3490 }
3491 else
3492 xp->xp_context = EXPAND_EXPRESSION;
3493 }
3494 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003495 /* Doesn't look like something valid, expand as an expression
3496 * anyway. */
3497 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 arg = xp->xp_pattern;
3499 if (*arg != NUL)
3500 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3501 /* skip */ ;
3502 }
3503 xp->xp_pattern = arg;
3504}
3505
3506#endif /* FEAT_CMDL_COMPL */
3507
3508/*
3509 * ":1,25call func(arg1, arg2)" function call.
3510 */
3511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003512ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513{
3514 char_u *arg = eap->arg;
3515 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003517 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 linenr_T lnum;
3521 int doesrange;
3522 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003523 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003524 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003526 if (eap->skip)
3527 {
3528 /* trans_function_name() doesn't work well when skipping, use eval0()
3529 * instead to skip to any following command, e.g. for:
3530 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003531 ++emsg_skip;
3532 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3533 clear_tv(&rettv);
3534 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003535 return;
3536 }
3537
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003539 if (fudi.fd_newkey != NULL)
3540 {
3541 /* Still need to give an error message for missing key. */
3542 EMSG2(_(e_dictkey), fudi.fd_newkey);
3543 vim_free(fudi.fd_newkey);
3544 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003545 if (tofree == NULL)
3546 return;
3547
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003548 /* Increase refcount on dictionary, it could get deleted when evaluating
3549 * the arguments. */
3550 if (fudi.fd_dict != NULL)
3551 ++fudi.fd_dict->dv_refcount;
3552
Bram Moolenaar65639032016-03-16 21:40:30 +01003553 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3554 * contents. For VAR_PARTIAL get its partial, unless we already have one
3555 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003557 name = deref_func_name(tofree, &len,
3558 partial != NULL ? NULL : &partial, FALSE);
3559
Bram Moolenaar532c7802005-01-27 14:44:31 +00003560 /* Skip white space to allow ":call func ()". Not good, but required for
3561 * backward compatibility. */
3562 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003563 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 if (*startarg != '(')
3566 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003567 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 goto end;
3569 }
3570
3571 /*
3572 * When skipping, evaluate the function once, to find the end of the
3573 * arguments.
3574 * When the function takes a range, this is discovered after the first
3575 * call, and the loop is broken.
3576 */
3577 if (eap->skip)
3578 {
3579 ++emsg_skip;
3580 lnum = eap->line2; /* do it once, also with an invalid range */
3581 }
3582 else
3583 lnum = eap->line1;
3584 for ( ; lnum <= eap->line2; ++lnum)
3585 {
3586 if (!eap->skip && eap->addr_count > 0)
3587 {
3588 curwin->w_cursor.lnum = lnum;
3589 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003590#ifdef FEAT_VIRTUALEDIT
3591 curwin->w_cursor.coladd = 0;
3592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003595 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003596 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003597 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 failed = TRUE;
3600 break;
3601 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003602
3603 /* Handle a function returning a Funcref, Dictionary or List. */
3604 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3605 {
3606 failed = TRUE;
3607 break;
3608 }
3609
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (doesrange || eap->skip)
3612 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003615 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003617 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 if (aborting())
3619 break;
3620 }
3621 if (eap->skip)
3622 --emsg_skip;
3623
3624 if (!failed)
3625 {
3626 /* Check for trailing illegal characters and a following command. */
3627 if (!ends_excmd(*arg))
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 else
3633 eap->nextcmd = check_nextcmd(arg);
3634 }
3635
3636end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003637 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003638 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639}
3640
3641/*
3642 * ":unlet[!] var1 ... " command.
3643 */
3644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 ex_unletlock(eap, eap->arg, 0);
3648}
3649
3650/*
3651 * ":lockvar" and ":unlockvar" commands
3652 */
3653 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 int deep = 2;
3658
3659 if (eap->forceit)
3660 deep = -1;
3661 else if (vim_isdigit(*arg))
3662 {
3663 deep = getdigits(&arg);
3664 arg = skipwhite(arg);
3665 }
3666
3667 ex_unletlock(eap, arg, deep);
3668}
3669
3670/*
3671 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3672 */
3673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003674ex_unletlock(
3675 exarg_T *eap,
3676 char_u *argstart,
3677 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678{
3679 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 do
3685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003687 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003688 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 if (lv.ll_name == NULL)
3690 error = TRUE; /* error but continue parsing */
3691 if (name_end == NULL || (!vim_iswhite(*name_end)
3692 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 if (name_end != NULL)
3695 {
3696 emsg_severe = TRUE;
3697 EMSG(_(e_trailing));
3698 }
3699 if (!(eap->skip || error))
3700 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 break;
3702 }
3703
3704 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 {
3706 if (eap->cmdidx == CMD_unlet)
3707 {
3708 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3709 error = TRUE;
3710 }
3711 else
3712 {
3713 if (do_lock_var(&lv, name_end, deep,
3714 eap->cmdidx == CMD_lockvar) == FAIL)
3715 error = TRUE;
3716 }
3717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 if (!eap->skip)
3720 clear_lval(&lv);
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 arg = skipwhite(name_end);
3723 } while (!ends_excmd(*arg));
3724
3725 eap->nextcmd = check_nextcmd(arg);
3726}
3727
Bram Moolenaar8c711452005-01-14 21:53:12 +00003728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet_var(
3730 lval_T *lp,
3731 char_u *name_end,
3732 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003733{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003734 int ret = OK;
3735 int cc;
3736
3737 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003738 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 cc = *name_end;
3740 *name_end = NUL;
3741
3742 /* Normal name or expanded name. */
3743 if (check_changedtick(lp->ll_name))
3744 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003746 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003748 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003749 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003750 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003751 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003752 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003753 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003754 else if (lp->ll_range)
3755 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003757 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003758 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003759
3760 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3761 {
3762 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003763 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003764 return FAIL;
3765 ll_li = li;
3766 ++ll_n1;
3767 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768
3769 /* Delete a range of List items. */
3770 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3771 {
3772 li = lp->ll_li->li_next;
3773 listitem_remove(lp->ll_list, lp->ll_li);
3774 lp->ll_li = li;
3775 ++lp->ll_n1;
3776 }
3777 }
3778 else
3779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003781 /* unlet a List item. */
3782 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003783 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003784 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003785 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003786 }
3787
3788 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003789}
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791/*
3792 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003796do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaar33570922005-01-25 22:26:29 +00003798 hashtab_T *ht;
3799 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003800 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003801 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003802 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaar33570922005-01-25 22:26:29 +00003804 ht = find_var_ht(name, &varname);
3805 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003807 if (ht == &globvarht)
3808 d = &globvardict;
3809 else if (current_funccal != NULL
3810 && ht == &current_funccal->l_vars.dv_hashtab)
3811 d = &current_funccal->l_vars;
3812 else if (ht == &compat_hashtab)
3813 d = &vimvardict;
3814 else
3815 {
3816 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3817 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3818 }
3819 if (d == NULL)
3820 {
3821 EMSG2(_(e_intern2), "do_unlet()");
3822 return FAIL;
3823 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003824 hi = hash_find(ht, varname);
3825 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003826 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003827 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003828 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003829 || var_check_ro(di->di_flags, name, FALSE)
3830 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003831 return FAIL;
3832
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 delete_var(ht, hi);
3834 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003837 if (forceit)
3838 return OK;
3839 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return FAIL;
3841}
3842
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843/*
3844 * Lock or unlock variable indicated by "lp".
3845 * "deep" is the levels to go (-1 for unlimited);
3846 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3847 */
3848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849do_lock_var(
3850 lval_T *lp,
3851 char_u *name_end,
3852 int deep,
3853 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854{
3855 int ret = OK;
3856 int cc;
3857 dictitem_T *di;
3858
3859 if (deep == 0) /* nothing to do */
3860 return OK;
3861
3862 if (lp->ll_tv == NULL)
3863 {
3864 cc = *name_end;
3865 *name_end = NUL;
3866
3867 /* Normal name or expanded name. */
3868 if (check_changedtick(lp->ll_name))
3869 ret = FAIL;
3870 else
3871 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003872 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003873 if (di == NULL)
3874 ret = FAIL;
3875 else
3876 {
3877 if (lock)
3878 di->di_flags |= DI_FLAGS_LOCK;
3879 else
3880 di->di_flags &= ~DI_FLAGS_LOCK;
3881 item_lock(&di->di_tv, deep, lock);
3882 }
3883 }
3884 *name_end = cc;
3885 }
3886 else if (lp->ll_range)
3887 {
3888 listitem_T *li = lp->ll_li;
3889
3890 /* (un)lock a range of List items. */
3891 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3892 {
3893 item_lock(&li->li_tv, deep, lock);
3894 li = li->li_next;
3895 ++lp->ll_n1;
3896 }
3897 }
3898 else if (lp->ll_list != NULL)
3899 /* (un)lock a List item. */
3900 item_lock(&lp->ll_li->li_tv, deep, lock);
3901 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003902 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003903 item_lock(&lp->ll_di->di_tv, deep, lock);
3904
3905 return ret;
3906}
3907
3908/*
3909 * Lock or unlock an item. "deep" is nr of levels to go.
3910 */
3911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003912item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003913{
3914 static int recurse = 0;
3915 list_T *l;
3916 listitem_T *li;
3917 dict_T *d;
3918 hashitem_T *hi;
3919 int todo;
3920
3921 if (recurse >= DICT_MAXNEST)
3922 {
3923 EMSG(_("E743: variable nested too deep for (un)lock"));
3924 return;
3925 }
3926 if (deep == 0)
3927 return;
3928 ++recurse;
3929
3930 /* lock/unlock the item itself */
3931 if (lock)
3932 tv->v_lock |= VAR_LOCKED;
3933 else
3934 tv->v_lock &= ~VAR_LOCKED;
3935
3936 switch (tv->v_type)
3937 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003938 case VAR_UNKNOWN:
3939 case VAR_NUMBER:
3940 case VAR_STRING:
3941 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003942 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003943 case VAR_FLOAT:
3944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003946 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003947 break;
3948
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003949 case VAR_LIST:
3950 if ((l = tv->vval.v_list) != NULL)
3951 {
3952 if (lock)
3953 l->lv_lock |= VAR_LOCKED;
3954 else
3955 l->lv_lock &= ~VAR_LOCKED;
3956 if (deep < 0 || deep > 1)
3957 /* recursive: lock/unlock the items the List contains */
3958 for (li = l->lv_first; li != NULL; li = li->li_next)
3959 item_lock(&li->li_tv, deep - 1, lock);
3960 }
3961 break;
3962 case VAR_DICT:
3963 if ((d = tv->vval.v_dict) != NULL)
3964 {
3965 if (lock)
3966 d->dv_lock |= VAR_LOCKED;
3967 else
3968 d->dv_lock &= ~VAR_LOCKED;
3969 if (deep < 0 || deep > 1)
3970 {
3971 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003972 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003973 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3974 {
3975 if (!HASHITEM_EMPTY(hi))
3976 {
3977 --todo;
3978 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3979 }
3980 }
3981 }
3982 }
3983 }
3984 --recurse;
3985}
3986
Bram Moolenaara40058a2005-07-11 22:42:07 +00003987/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003988 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3989 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003990 */
3991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003992tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003993{
3994 return (tv->v_lock & VAR_LOCKED)
3995 || (tv->v_type == VAR_LIST
3996 && tv->vval.v_list != NULL
3997 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3998 || (tv->v_type == VAR_DICT
3999 && tv->vval.v_dict != NULL
4000 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
4001}
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4004/*
4005 * Delete all "menutrans_" variables.
4006 */
4007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004008del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
Bram Moolenaar33570922005-01-25 22:26:29 +00004010 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004011 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar33570922005-01-25 22:26:29 +00004013 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004014 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 {
4017 if (!HASHITEM_EMPTY(hi))
4018 {
4019 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4021 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 }
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025}
4026#endif
4027
4028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4029
4030/*
4031 * Local string buffer for the next two functions to store a variable name
4032 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4033 * get_user_var_name().
4034 */
4035
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004036static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038static char_u *varnamebuf = NULL;
4039static int varnamebuflen = 0;
4040
4041/*
4042 * Function to concatenate a prefix and a variable name.
4043 */
4044 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004045cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
4047 int len;
4048
4049 len = (int)STRLEN(name) + 3;
4050 if (len > varnamebuflen)
4051 {
4052 vim_free(varnamebuf);
4053 len += 10; /* some additional space */
4054 varnamebuf = alloc(len);
4055 if (varnamebuf == NULL)
4056 {
4057 varnamebuflen = 0;
4058 return NULL;
4059 }
4060 varnamebuflen = len;
4061 }
4062 *varnamebuf = prefix;
4063 varnamebuf[1] = ':';
4064 STRCPY(varnamebuf + 2, name);
4065 return varnamebuf;
4066}
4067
4068/*
4069 * Function given to ExpandGeneric() to obtain the list of user defined
4070 * (global/buffer/window/built-in) variable names.
4071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004073get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004075 static long_u gdone;
4076 static long_u bdone;
4077 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078#ifdef FEAT_WINDOWS
4079 static long_u tdone;
4080#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004081 static int vidx;
4082 static hashitem_T *hi;
4083 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004086 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004087 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088#ifdef FEAT_WINDOWS
4089 tdone = 0;
4090#endif
4091 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004092
4093 /* Global variables */
4094 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004098 else
4099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004100 while (HASHITEM_EMPTY(hi))
4101 ++hi;
4102 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4103 return cat_prefix_varname('g', hi->hi_key);
4104 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004106
4107 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004108 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004111 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004113 else
4114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004115 while (HASHITEM_EMPTY(hi))
4116 ++hi;
4117 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004119 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004121 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 return (char_u *)"b:changedtick";
4123 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004124
4125 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004126 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004127 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004129 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004130 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004131 else
4132 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004133 while (HASHITEM_EMPTY(hi))
4134 ++hi;
4135 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004137
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004138#ifdef FEAT_WINDOWS
4139 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004140 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004141 if (tdone < ht->ht_used)
4142 {
4143 if (tdone++ == 0)
4144 hi = ht->ht_array;
4145 else
4146 ++hi;
4147 while (HASHITEM_EMPTY(hi))
4148 ++hi;
4149 return cat_prefix_varname('t', hi->hi_key);
4150 }
4151#endif
4152
Bram Moolenaar33570922005-01-25 22:26:29 +00004153 /* v: variables */
4154 if (vidx < VV_LEN)
4155 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156
4157 vim_free(varnamebuf);
4158 varnamebuf = NULL;
4159 varnamebuflen = 0;
4160 return NULL;
4161}
4162
4163#endif /* FEAT_CMDL_COMPL */
4164
4165/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004166 * Return TRUE if "pat" matches "text".
4167 * Does not use 'cpo' and always uses 'magic'.
4168 */
4169 static int
4170pattern_match(char_u *pat, char_u *text, int ic)
4171{
4172 int matches = FALSE;
4173 char_u *save_cpo;
4174 regmatch_T regmatch;
4175
4176 /* avoid 'l' flag in 'cpoptions' */
4177 save_cpo = p_cpo;
4178 p_cpo = (char_u *)"";
4179 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4180 if (regmatch.regprog != NULL)
4181 {
4182 regmatch.rm_ic = ic;
4183 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4184 vim_regfree(regmatch.regprog);
4185 }
4186 p_cpo = save_cpo;
4187 return matches;
4188}
4189
4190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * types for expressions.
4192 */
4193typedef enum
4194{
4195 TYPE_UNKNOWN = 0
4196 , TYPE_EQUAL /* == */
4197 , TYPE_NEQUAL /* != */
4198 , TYPE_GREATER /* > */
4199 , TYPE_GEQUAL /* >= */
4200 , TYPE_SMALLER /* < */
4201 , TYPE_SEQUAL /* <= */
4202 , TYPE_MATCH /* =~ */
4203 , TYPE_NOMATCH /* !~ */
4204} exptype_T;
4205
4206/*
4207 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4210 */
4211
4212/*
4213 * Handle zero level expression.
4214 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004216 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004220eval0(
4221 char_u *arg,
4222 typval_T *rettv,
4223 char_u **nextcmd,
4224 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 int ret;
4227 char_u *p;
4228
4229 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 if (ret == FAIL || !ends_excmd(*p))
4232 {
4233 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 /*
4236 * Report the invalid expression unless the expression evaluation has
4237 * been cancelled due to an aborting error, an interrupt, or an
4238 * exception.
4239 */
4240 if (!aborting())
4241 EMSG2(_(e_invexpr2), arg);
4242 ret = FAIL;
4243 }
4244 if (nextcmd != NULL)
4245 *nextcmd = check_nextcmd(p);
4246
4247 return ret;
4248}
4249
4250/*
4251 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004252 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 *
4254 * "arg" must point to the first non-white of the expression.
4255 * "arg" is advanced to the next non-white after the recognized expression.
4256 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004257 * Note: "rettv.v_lock" is not set.
4258 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 * Return OK or FAIL.
4260 */
4261 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004262eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263{
4264 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004265 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 /*
4268 * Get the first variable.
4269 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 return FAIL;
4272
4273 if ((*arg)[0] == '?')
4274 {
4275 result = FALSE;
4276 if (evaluate)
4277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 int error = FALSE;
4279
4280 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286
4287 /*
4288 * Get the second variable.
4289 */
4290 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004291 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 return FAIL;
4293
4294 /*
4295 * Check for the ":".
4296 */
4297 if ((*arg)[0] != ':')
4298 {
4299 EMSG(_("E109: Missing ':' after '?'"));
4300 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return FAIL;
4303 }
4304
4305 /*
4306 * Get the third variable.
4307 */
4308 *arg = skipwhite(*arg + 1);
4309 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4310 {
4311 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FAIL;
4314 }
4315 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 }
4318
4319 return OK;
4320}
4321
4322/*
4323 * Handle first level expression:
4324 * expr2 || expr2 || expr2 logical OR
4325 *
4326 * "arg" must point to the first non-white of the expression.
4327 * "arg" is advanced to the next non-white after the recognized expression.
4328 *
4329 * Return OK or FAIL.
4330 */
4331 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004332eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaar33570922005-01-25 22:26:29 +00004334 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 long result;
4336 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 /*
4340 * Get the first variable.
4341 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 return FAIL;
4344
4345 /*
4346 * Repeat until there is no following "||".
4347 */
4348 first = TRUE;
4349 result = FALSE;
4350 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4351 {
4352 if (evaluate && first)
4353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (error)
4358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 first = FALSE;
4360 }
4361
4362 /*
4363 * Get the second variable.
4364 */
4365 *arg = skipwhite(*arg + 2);
4366 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4367 return FAIL;
4368
4369 /*
4370 * Compute the result.
4371 */
4372 if (evaluate && !result)
4373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004374 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 if (error)
4378 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 if (evaluate)
4381 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 rettv->v_type = VAR_NUMBER;
4383 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 }
4386
4387 return OK;
4388}
4389
4390/*
4391 * Handle second level expression:
4392 * expr3 && expr3 && expr3 logical AND
4393 *
4394 * "arg" must point to the first non-white of the expression.
4395 * "arg" is advanced to the next non-white after the recognized expression.
4396 *
4397 * Return OK or FAIL.
4398 */
4399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004400eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
Bram Moolenaar33570922005-01-25 22:26:29 +00004402 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 long result;
4404 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004405 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
4407 /*
4408 * Get the first variable.
4409 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004410 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return FAIL;
4412
4413 /*
4414 * Repeat until there is no following "&&".
4415 */
4416 first = TRUE;
4417 result = TRUE;
4418 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4419 {
4420 if (evaluate && first)
4421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004422 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004424 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 if (error)
4426 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 first = FALSE;
4428 }
4429
4430 /*
4431 * Get the second variable.
4432 */
4433 *arg = skipwhite(*arg + 2);
4434 if (eval4(arg, &var2, evaluate && result) == FAIL)
4435 return FAIL;
4436
4437 /*
4438 * Compute the result.
4439 */
4440 if (evaluate && result)
4441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004442 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004444 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 if (error)
4446 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 if (evaluate)
4449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004450 rettv->v_type = VAR_NUMBER;
4451 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 }
4454
4455 return OK;
4456}
4457
4458/*
4459 * Handle third level expression:
4460 * var1 == var2
4461 * var1 =~ var2
4462 * var1 != var2
4463 * var1 !~ var2
4464 * var1 > var2
4465 * var1 >= var2
4466 * var1 < var2
4467 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 * var1 is var2
4469 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 *
4471 * "arg" must point to the first non-white of the expression.
4472 * "arg" is advanced to the next non-white after the recognized expression.
4473 *
4474 * Return OK or FAIL.
4475 */
4476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004477eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
Bram Moolenaar33570922005-01-25 22:26:29 +00004479 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 char_u *p;
4481 int i;
4482 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004485 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 char_u *s1, *s2;
4487 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 /*
4491 * Get the first variable.
4492 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004493 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return FAIL;
4495
4496 p = *arg;
4497 switch (p[0])
4498 {
4499 case '=': if (p[1] == '=')
4500 type = TYPE_EQUAL;
4501 else if (p[1] == '~')
4502 type = TYPE_MATCH;
4503 break;
4504 case '!': if (p[1] == '=')
4505 type = TYPE_NEQUAL;
4506 else if (p[1] == '~')
4507 type = TYPE_NOMATCH;
4508 break;
4509 case '>': if (p[1] != '=')
4510 {
4511 type = TYPE_GREATER;
4512 len = 1;
4513 }
4514 else
4515 type = TYPE_GEQUAL;
4516 break;
4517 case '<': if (p[1] != '=')
4518 {
4519 type = TYPE_SMALLER;
4520 len = 1;
4521 }
4522 else
4523 type = TYPE_SEQUAL;
4524 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004525 case 'i': if (p[1] == 's')
4526 {
4527 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4528 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004529 i = p[len];
4530 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004531 {
4532 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4533 type_is = TRUE;
4534 }
4535 }
4536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
4539 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
4542 if (type != TYPE_UNKNOWN)
4543 {
4544 /* extra question mark appended: ignore case */
4545 if (p[len] == '?')
4546 {
4547 ic = TRUE;
4548 ++len;
4549 }
4550 /* extra '#' appended: match case */
4551 else if (p[len] == '#')
4552 {
4553 ic = FALSE;
4554 ++len;
4555 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004556 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
4558 ic = p_ic;
4559
4560 /*
4561 * Get the second variable.
4562 */
4563 *arg = skipwhite(p + len);
4564 if (eval5(arg, &var2, evaluate) == FAIL)
4565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004566 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 return FAIL;
4568 }
4569
4570 if (evaluate)
4571 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004572 if (type_is && rettv->v_type != var2.v_type)
4573 {
4574 /* For "is" a different type always means FALSE, for "notis"
4575 * it means TRUE. */
4576 n1 = (type == TYPE_NEQUAL);
4577 }
4578 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4579 {
4580 if (type_is)
4581 {
4582 n1 = (rettv->v_type == var2.v_type
4583 && rettv->vval.v_list == var2.vval.v_list);
4584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 else if (rettv->v_type != var2.v_type
4588 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4589 {
4590 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004591 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004592 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004593 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 clear_tv(rettv);
4595 clear_tv(&var2);
4596 return FAIL;
4597 }
4598 else
4599 {
4600 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004601 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4602 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004603 if (type == TYPE_NEQUAL)
4604 n1 = !n1;
4605 }
4606 }
4607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004608 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4609 {
4610 if (type_is)
4611 {
4612 n1 = (rettv->v_type == var2.v_type
4613 && rettv->vval.v_dict == var2.vval.v_dict);
4614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 else if (rettv->v_type != var2.v_type
4618 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4619 {
4620 if (rettv->v_type != var2.v_type)
4621 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4622 else
4623 EMSG(_("E736: Invalid operation for Dictionary"));
4624 clear_tv(rettv);
4625 clear_tv(&var2);
4626 return FAIL;
4627 }
4628 else
4629 {
4630 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004631 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4632 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004633 if (type == TYPE_NEQUAL)
4634 n1 = !n1;
4635 }
4636 }
4637
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004638 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4639 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004640 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004641 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004642 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004643 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004644 clear_tv(rettv);
4645 clear_tv(&var2);
4646 return FAIL;
4647 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004648 if ((rettv->v_type == VAR_PARTIAL
4649 && rettv->vval.v_partial == NULL)
4650 || (var2.v_type == VAR_PARTIAL
4651 && var2.vval.v_partial == NULL))
4652 /* when a partial is NULL assume not equal */
4653 n1 = FALSE;
4654 else if (type_is)
4655 {
4656 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4657 /* strings are considered the same if their value is
4658 * the same */
4659 n1 = tv_equal(rettv, &var2, ic, FALSE);
4660 else if (rettv->v_type == VAR_PARTIAL
4661 && var2.v_type == VAR_PARTIAL)
4662 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4663 else
4664 n1 = FALSE;
4665 }
4666 else
4667 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004668 if (type == TYPE_NEQUAL)
4669 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004670 }
4671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004672#ifdef FEAT_FLOAT
4673 /*
4674 * If one of the two variables is a float, compare as a float.
4675 * When using "=~" or "!~", always compare as string.
4676 */
4677 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4678 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4679 {
4680 float_T f1, f2;
4681
4682 if (rettv->v_type == VAR_FLOAT)
4683 f1 = rettv->vval.v_float;
4684 else
4685 f1 = get_tv_number(rettv);
4686 if (var2.v_type == VAR_FLOAT)
4687 f2 = var2.vval.v_float;
4688 else
4689 f2 = get_tv_number(&var2);
4690 n1 = FALSE;
4691 switch (type)
4692 {
4693 case TYPE_EQUAL: n1 = (f1 == f2); break;
4694 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4695 case TYPE_GREATER: n1 = (f1 > f2); break;
4696 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4697 case TYPE_SMALLER: n1 = (f1 < f2); break;
4698 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4699 case TYPE_UNKNOWN:
4700 case TYPE_MATCH:
4701 case TYPE_NOMATCH: break; /* avoid gcc warning */
4702 }
4703 }
4704#endif
4705
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 /*
4707 * If one of the two variables is a number, compare as a number.
4708 * When using "=~" or "!~", always compare as string.
4709 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004710 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004713 n1 = get_tv_number(rettv);
4714 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 switch (type)
4716 {
4717 case TYPE_EQUAL: n1 = (n1 == n2); break;
4718 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4719 case TYPE_GREATER: n1 = (n1 > n2); break;
4720 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4721 case TYPE_SMALLER: n1 = (n1 < n2); break;
4722 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4723 case TYPE_UNKNOWN:
4724 case TYPE_MATCH:
4725 case TYPE_NOMATCH: break; /* avoid gcc warning */
4726 }
4727 }
4728 else
4729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 s1 = get_tv_string_buf(rettv, buf1);
4731 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4733 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4734 else
4735 i = 0;
4736 n1 = FALSE;
4737 switch (type)
4738 {
4739 case TYPE_EQUAL: n1 = (i == 0); break;
4740 case TYPE_NEQUAL: n1 = (i != 0); break;
4741 case TYPE_GREATER: n1 = (i > 0); break;
4742 case TYPE_GEQUAL: n1 = (i >= 0); break;
4743 case TYPE_SMALLER: n1 = (i < 0); break;
4744 case TYPE_SEQUAL: n1 = (i <= 0); break;
4745
4746 case TYPE_MATCH:
4747 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004748 n1 = pattern_match(s2, s1, ic);
4749 if (type == TYPE_NOMATCH)
4750 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752
4753 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4754 }
4755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 rettv->v_type = VAR_NUMBER;
4759 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762
4763 return OK;
4764}
4765
4766/*
4767 * Handle fourth level expression:
4768 * + number addition
4769 * - number subtraction
4770 * . string concatenation
4771 *
4772 * "arg" must point to the first non-white of the expression.
4773 * "arg" is advanced to the next non-white after the recognized expression.
4774 *
4775 * Return OK or FAIL.
4776 */
4777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004778eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
Bram Moolenaar33570922005-01-25 22:26:29 +00004780 typval_T var2;
4781 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004783 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784#ifdef FEAT_FLOAT
4785 float_T f1 = 0, f2 = 0;
4786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 char_u *s1, *s2;
4788 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4789 char_u *p;
4790
4791 /*
4792 * Get the first variable.
4793 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796
4797 /*
4798 * Repeat computing, until no '+', '-' or '.' is following.
4799 */
4800 for (;;)
4801 {
4802 op = **arg;
4803 if (op != '+' && op != '-' && op != '.')
4804 break;
4805
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806 if ((op != '+' || rettv->v_type != VAR_LIST)
4807#ifdef FEAT_FLOAT
4808 && (op == '.' || rettv->v_type != VAR_FLOAT)
4809#endif
4810 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 {
4812 /* For "list + ...", an illegal use of the first operand as
4813 * a number cannot be determined before evaluating the 2nd
4814 * operand: if this is also a list, all is ok.
4815 * For "something . ...", "something - ..." or "non-list + ...",
4816 * we know that the first operand needs to be a string or number
4817 * without evaluating the 2nd operand. So check before to avoid
4818 * side effects after an error. */
4819 if (evaluate && get_tv_string_chk(rettv) == NULL)
4820 {
4821 clear_tv(rettv);
4822 return FAIL;
4823 }
4824 }
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 /*
4827 * Get the second variable.
4828 */
4829 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004830 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
4834 }
4835
4836 if (evaluate)
4837 {
4838 /*
4839 * Compute the result.
4840 */
4841 if (op == '.')
4842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4844 s2 = get_tv_string_buf_chk(&var2, buf2);
4845 if (s2 == NULL) /* type error ? */
4846 {
4847 clear_tv(rettv);
4848 clear_tv(&var2);
4849 return FAIL;
4850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004851 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
4853 rettv->v_type = VAR_STRING;
4854 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004856 else if (op == '+' && rettv->v_type == VAR_LIST
4857 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004858 {
4859 /* concatenate Lists */
4860 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4861 &var3) == FAIL)
4862 {
4863 clear_tv(rettv);
4864 clear_tv(&var2);
4865 return FAIL;
4866 }
4867 clear_tv(rettv);
4868 *rettv = var3;
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 else
4871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 int error = FALSE;
4873
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 f1 = rettv->vval.v_float;
4878 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880 else
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 n1 = get_tv_number_chk(rettv, &error);
4884 if (error)
4885 {
4886 /* This can only happen for "list + non-list". For
4887 * "non-list + ..." or "something - ...", we returned
4888 * before evaluating the 2nd operand. */
4889 clear_tv(rettv);
4890 return FAIL;
4891 }
4892#ifdef FEAT_FLOAT
4893 if (var2.v_type == VAR_FLOAT)
4894 f1 = n1;
4895#endif
4896 }
4897#ifdef FEAT_FLOAT
4898 if (var2.v_type == VAR_FLOAT)
4899 {
4900 f2 = var2.vval.v_float;
4901 n2 = 0;
4902 }
4903 else
4904#endif
4905 {
4906 n2 = get_tv_number_chk(&var2, &error);
4907 if (error)
4908 {
4909 clear_tv(rettv);
4910 clear_tv(&var2);
4911 return FAIL;
4912 }
4913#ifdef FEAT_FLOAT
4914 if (rettv->v_type == VAR_FLOAT)
4915 f2 = n2;
4916#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004919
4920#ifdef FEAT_FLOAT
4921 /* If there is a float on either side the result is a float. */
4922 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4923 {
4924 if (op == '+')
4925 f1 = f1 + f2;
4926 else
4927 f1 = f1 - f2;
4928 rettv->v_type = VAR_FLOAT;
4929 rettv->vval.v_float = f1;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932#endif
4933 {
4934 if (op == '+')
4935 n1 = n1 + n2;
4936 else
4937 n1 = n1 - n2;
4938 rettv->v_type = VAR_NUMBER;
4939 rettv->vval.v_number = n1;
4940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004942 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
4944 }
4945 return OK;
4946}
4947
4948/*
4949 * Handle fifth level expression:
4950 * * number multiplication
4951 * / number division
4952 * % number modulo
4953 *
4954 * "arg" must point to the first non-white of the expression.
4955 * "arg" is advanced to the next non-white after the recognized expression.
4956 *
4957 * Return OK or FAIL.
4958 */
4959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004960eval6(
4961 char_u **arg,
4962 typval_T *rettv,
4963 int evaluate,
4964 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar33570922005-01-25 22:26:29 +00004966 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004968 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969#ifdef FEAT_FLOAT
4970 int use_float = FALSE;
4971 float_T f1 = 0, f2;
4972#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004973 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 /*
4976 * Get the first variable.
4977 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004978 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 return FAIL;
4980
4981 /*
4982 * Repeat computing, until no '*', '/' or '%' is following.
4983 */
4984 for (;;)
4985 {
4986 op = **arg;
4987 if (op != '*' && op != '/' && op != '%')
4988 break;
4989
4990 if (evaluate)
4991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992#ifdef FEAT_FLOAT
4993 if (rettv->v_type == VAR_FLOAT)
4994 {
4995 f1 = rettv->vval.v_float;
4996 use_float = TRUE;
4997 n1 = 0;
4998 }
4999 else
5000#endif
5001 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005003 if (error)
5004 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 else
5007 n1 = 0;
5008
5009 /*
5010 * Get the second variable.
5011 */
5012 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005013 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 return FAIL;
5015
5016 if (evaluate)
5017 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018#ifdef FEAT_FLOAT
5019 if (var2.v_type == VAR_FLOAT)
5020 {
5021 if (!use_float)
5022 {
5023 f1 = n1;
5024 use_float = TRUE;
5025 }
5026 f2 = var2.vval.v_float;
5027 n2 = 0;
5028 }
5029 else
5030#endif
5031 {
5032 n2 = get_tv_number_chk(&var2, &error);
5033 clear_tv(&var2);
5034 if (error)
5035 return FAIL;
5036#ifdef FEAT_FLOAT
5037 if (use_float)
5038 f2 = n2;
5039#endif
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 /*
5043 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046#ifdef FEAT_FLOAT
5047 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 if (op == '*')
5050 f1 = f1 * f2;
5051 else if (op == '/')
5052 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005053# ifdef VMS
5054 /* VMS crashes on divide by zero, work around it */
5055 if (f2 == 0.0)
5056 {
5057 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005058 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005059 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005060 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005061 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005062 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005063 }
5064 else
5065 f1 = f1 / f2;
5066# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 /* We rely on the floating point library to handle divide
5068 * by zero to result in "inf" and not a crash. */
5069 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005070# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005074 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005075 return FAIL;
5076 }
5077 rettv->v_type = VAR_FLOAT;
5078 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005083 if (op == '*')
5084 n1 = n1 * n2;
5085 else if (op == '/')
5086 {
5087 if (n2 == 0) /* give an error message? */
5088 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005089#ifdef FEAT_NUM64
5090 if (n1 == 0)
5091 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5092 else if (n1 < 0)
5093 n1 = -0x7fffffffffffffff;
5094 else
5095 n1 = 0x7fffffffffffffff;
5096#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 if (n1 == 0)
5098 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5099 else if (n1 < 0)
5100 n1 = -0x7fffffffL;
5101 else
5102 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005103#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 }
5105 else
5106 n1 = n1 / n2;
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 {
5110 if (n2 == 0) /* give an error message? */
5111 n1 = 0;
5112 else
5113 n1 = n1 % n2;
5114 }
5115 rettv->v_type = VAR_NUMBER;
5116 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120
5121 return OK;
5122}
5123
5124/*
5125 * Handle sixth level expression:
5126 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005127 * "string" string constant
5128 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 * &option-name option value
5130 * @r register contents
5131 * identifier variable value
5132 * function() function call
5133 * $VAR environment variable
5134 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005135 * [expr, expr] List
5136 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 *
5138 * Also handle:
5139 * ! in front logical NOT
5140 * - in front unary minus
5141 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 * trailing [] subscript in String or List
5143 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 *
5145 * "arg" must point to the first non-white of the expression.
5146 * "arg" is advanced to the next non-white after the recognized expression.
5147 *
5148 * Return OK or FAIL.
5149 */
5150 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005151eval7(
5152 char_u **arg,
5153 typval_T *rettv,
5154 int evaluate,
5155 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005157 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 int len;
5159 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 char_u *start_leader, *end_leader;
5161 int ret = OK;
5162 char_u *alias;
5163
5164 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005168 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * Skip '!' and '-' characters. They are handled later.
5172 */
5173 start_leader = *arg;
5174 while (**arg == '!' || **arg == '-' || **arg == '+')
5175 *arg = skipwhite(*arg + 1);
5176 end_leader = *arg;
5177
5178 switch (**arg)
5179 {
5180 /*
5181 * Number constant.
5182 */
5183 case '0':
5184 case '1':
5185 case '2':
5186 case '3':
5187 case '4':
5188 case '5':
5189 case '6':
5190 case '7':
5191 case '8':
5192 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005193 {
5194#ifdef FEAT_FLOAT
5195 char_u *p = skipdigits(*arg + 1);
5196 int get_float = FALSE;
5197
5198 /* We accept a float when the format matches
5199 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005200 * strict to avoid backwards compatibility problems.
5201 * Don't look for a float after the "." operator, so that
5202 * ":let vers = 1.2.3" doesn't fail. */
5203 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005205 get_float = TRUE;
5206 p = skipdigits(p + 2);
5207 if (*p == 'e' || *p == 'E')
5208 {
5209 ++p;
5210 if (*p == '-' || *p == '+')
5211 ++p;
5212 if (!vim_isdigit(*p))
5213 get_float = FALSE;
5214 else
5215 p = skipdigits(p + 1);
5216 }
5217 if (ASCII_ISALPHA(*p) || *p == '.')
5218 get_float = FALSE;
5219 }
5220 if (get_float)
5221 {
5222 float_T f;
5223
5224 *arg += string2float(*arg, &f);
5225 if (evaluate)
5226 {
5227 rettv->v_type = VAR_FLOAT;
5228 rettv->vval.v_float = f;
5229 }
5230 }
5231 else
5232#endif
5233 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005234 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235 *arg += len;
5236 if (evaluate)
5237 {
5238 rettv->v_type = VAR_NUMBER;
5239 rettv->vval.v_number = n;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 /*
5246 * String constant: "string".
5247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005248 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250
5251 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 break;
5256
5257 /*
5258 * List: [expr, expr]
5259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 break;
5262
5263 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 * Dictionary: {key: val, key: val}
5265 */
5266 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5267 break;
5268
5269 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005270 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005272 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 break;
5274
5275 /*
5276 * Environment variable: $VAR.
5277 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 break;
5280
5281 /*
5282 * Register contents: @r.
5283 */
5284 case '@': ++*arg;
5285 if (evaluate)
5286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005287 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005288 rettv->vval.v_string = get_reg_contents(**arg,
5289 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 if (**arg != NUL)
5292 ++*arg;
5293 break;
5294
5295 /*
5296 * nested expression: (expression).
5297 */
5298 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (**arg == ')')
5301 ++*arg;
5302 else if (ret == OK)
5303 {
5304 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005305 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 ret = FAIL;
5307 }
5308 break;
5309
Bram Moolenaar8c711452005-01-14 21:53:12 +00005310 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 break;
5312 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005313
5314 if (ret == NOTDONE)
5315 {
5316 /*
5317 * Must be a variable or function name.
5318 * Can also be a curly-braces kind of name: {expr}.
5319 */
5320 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 if (alias != NULL)
5323 s = alias;
5324
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 ret = FAIL;
5327 else
5328 {
5329 if (**arg == '(') /* recursive! */
5330 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005331 partial_T *partial;
5332
Bram Moolenaar8c711452005-01-14 21:53:12 +00005333 /* If "s" is the name of a variable of type VAR_FUNC
5334 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005335 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005336
5337 /* Invoke the function. */
5338 ret = get_func_tv(s, len, rettv, arg,
5339 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005340 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005341
5342 /* If evaluate is FALSE rettv->v_type was not set in
5343 * get_func_tv, but it's needed in handle_subscript() to parse
5344 * what follows. So set it here. */
5345 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5346 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005347 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005348 rettv->v_type = VAR_FUNC;
5349 }
5350
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 /* Stop the expression evaluation when immediately
5352 * aborting on error, or when an interrupt occurred or
5353 * an exception was thrown but not caught. */
5354 if (aborting())
5355 {
5356 if (ret == OK)
5357 clear_tv(rettv);
5358 ret = FAIL;
5359 }
5360 }
5361 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005362 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005363 else
5364 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005365 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005366 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005367 }
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 *arg = skipwhite(*arg);
5370
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005371 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5372 * expr(expr). */
5373 if (ret == OK)
5374 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 /*
5377 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5378 */
5379 if (ret == OK && evaluate && end_leader > start_leader)
5380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005382 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005383#ifdef FEAT_FLOAT
5384 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005385
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005386 if (rettv->v_type == VAR_FLOAT)
5387 f = rettv->vval.v_float;
5388 else
5389#endif
5390 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005391 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005393 clear_tv(rettv);
5394 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005396 else
5397 {
5398 while (end_leader > start_leader)
5399 {
5400 --end_leader;
5401 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005402 {
5403#ifdef FEAT_FLOAT
5404 if (rettv->v_type == VAR_FLOAT)
5405 f = !f;
5406 else
5407#endif
5408 val = !val;
5409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005411 {
5412#ifdef FEAT_FLOAT
5413 if (rettv->v_type == VAR_FLOAT)
5414 f = -f;
5415 else
5416#endif
5417 val = -val;
5418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005420#ifdef FEAT_FLOAT
5421 if (rettv->v_type == VAR_FLOAT)
5422 {
5423 clear_tv(rettv);
5424 rettv->vval.v_float = f;
5425 }
5426 else
5427#endif
5428 {
5429 clear_tv(rettv);
5430 rettv->v_type = VAR_NUMBER;
5431 rettv->vval.v_number = val;
5432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435
5436 return ret;
5437}
5438
5439/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5441 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5443 */
5444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005445eval_index(
5446 char_u **arg,
5447 typval_T *rettv,
5448 int evaluate,
5449 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005450{
5451 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005452 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 long len = -1;
5455 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005457 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458
Bram Moolenaara03f2332016-02-06 18:09:59 +01005459 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005461 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005462 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005463 if (verbose)
5464 EMSG(_("E695: Cannot index a Funcref"));
5465 return FAIL;
5466 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005467#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005468 if (verbose)
5469 EMSG(_(e_float_as_string));
5470 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005471#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005472 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005473 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005474 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005475 if (verbose)
5476 EMSG(_("E909: Cannot index a special variable"));
5477 return FAIL;
5478 case VAR_UNKNOWN:
5479 if (evaluate)
5480 return FAIL;
5481 /* FALLTHROUGH */
5482
5483 case VAR_STRING:
5484 case VAR_NUMBER:
5485 case VAR_LIST:
5486 case VAR_DICT:
5487 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005488 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005490 init_tv(&var1);
5491 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 /*
5495 * dict.name
5496 */
5497 key = *arg + 1;
5498 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5499 ;
5500 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 /*
5507 * something[idx]
5508 *
5509 * Get the (first) variable from inside the [].
5510 */
5511 *arg = skipwhite(*arg + 1);
5512 if (**arg == ':')
5513 empty1 = TRUE;
5514 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5515 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005516 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5517 {
5518 /* not a number or string */
5519 clear_tv(&var1);
5520 return FAIL;
5521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005522
5523 /*
5524 * Get the second variable from inside the [:].
5525 */
5526 if (**arg == ':')
5527 {
5528 range = TRUE;
5529 *arg = skipwhite(*arg + 1);
5530 if (**arg == ']')
5531 empty2 = TRUE;
5532 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005534 if (!empty1)
5535 clear_tv(&var1);
5536 return FAIL;
5537 }
5538 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5539 {
5540 /* not a number or string */
5541 if (!empty1)
5542 clear_tv(&var1);
5543 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544 return FAIL;
5545 }
5546 }
5547
5548 /* Check for the ']'. */
5549 if (**arg != ']')
5550 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005551 if (verbose)
5552 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005553 clear_tv(&var1);
5554 if (range)
5555 clear_tv(&var2);
5556 return FAIL;
5557 }
5558 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 }
5560
5561 if (evaluate)
5562 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005563 n1 = 0;
5564 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 n1 = get_tv_number(&var1);
5567 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 }
5569 if (range)
5570 {
5571 if (empty2)
5572 n2 = -1;
5573 else
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 n2 = get_tv_number(&var2);
5576 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 }
5578 }
5579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005583 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005584 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005586 case VAR_SPECIAL:
5587 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005588 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005589 break; /* not evaluating, skipping over subscript */
5590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 case VAR_NUMBER:
5592 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005593 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 len = (long)STRLEN(s);
5595 if (range)
5596 {
5597 /* The resulting variable is a substring. If the indexes
5598 * are out of range the result is empty. */
5599 if (n1 < 0)
5600 {
5601 n1 = len + n1;
5602 if (n1 < 0)
5603 n1 = 0;
5604 }
5605 if (n2 < 0)
5606 n2 = len + n2;
5607 else if (n2 >= len)
5608 n2 = len;
5609 if (n1 >= len || n2 < 0 || n1 > n2)
5610 s = NULL;
5611 else
5612 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5613 }
5614 else
5615 {
5616 /* The resulting variable is a string of a single
5617 * character. If the index is too big or negative the
5618 * result is empty. */
5619 if (n1 >= len || n1 < 0)
5620 s = NULL;
5621 else
5622 s = vim_strnsave(s + n1, 1);
5623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 clear_tv(rettv);
5625 rettv->v_type = VAR_STRING;
5626 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 break;
5628
5629 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005631 if (n1 < 0)
5632 n1 = len + n1;
5633 if (!empty1 && (n1 < 0 || n1 >= len))
5634 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005635 /* For a range we allow invalid values and return an empty
5636 * list. A list index out of range is an error. */
5637 if (!range)
5638 {
5639 if (verbose)
5640 EMSGN(_(e_listidx), n1);
5641 return FAIL;
5642 }
5643 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005644 }
5645 if (range)
5646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *l;
5648 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649
5650 if (n2 < 0)
5651 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005652 else if (n2 >= len)
5653 n2 = len - 1;
5654 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005655 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 l = list_alloc();
5657 if (l == NULL)
5658 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005659 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 n1 <= n2; ++n1)
5661 {
5662 if (list_append_tv(l, &item->li_tv) == FAIL)
5663 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005664 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return FAIL;
5666 }
5667 item = item->li_next;
5668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 clear_tv(rettv);
5670 rettv->v_type = VAR_LIST;
5671 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005672 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005673 }
5674 else
5675 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005676 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005677 clear_tv(rettv);
5678 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005679 }
5680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681
5682 case VAR_DICT:
5683 if (range)
5684 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005685 if (verbose)
5686 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 if (len == -1)
5688 clear_tv(&var1);
5689 return FAIL;
5690 }
5691 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005692 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005693
5694 if (len == -1)
5695 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005696 key = get_tv_string_chk(&var1);
5697 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 clear_tv(&var1);
5700 return FAIL;
5701 }
5702 }
5703
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005704 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005706 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005707 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 if (len == -1)
5709 clear_tv(&var1);
5710 if (item == NULL)
5711 return FAIL;
5712
5713 copy_tv(&item->di_tv, &var1);
5714 clear_tv(rettv);
5715 *rettv = var1;
5716 }
5717 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005718 }
5719 }
5720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 return OK;
5722}
5723
5724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * Get an option value.
5726 * "arg" points to the '&' or '+' before the option name.
5727 * "arg" is advanced to character after the option name.
5728 * Return OK or FAIL.
5729 */
5730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005731get_option_tv(
5732 char_u **arg,
5733 typval_T *rettv, /* when NULL, only check if option exists */
5734 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 char_u *option_end;
5737 long numval;
5738 char_u *stringval;
5739 int opt_type;
5740 int c;
5741 int working = (**arg == '+'); /* has("+option") */
5742 int ret = OK;
5743 int opt_flags;
5744
5745 /*
5746 * Isolate the option name and find its value.
5747 */
5748 option_end = find_option_end(arg, &opt_flags);
5749 if (option_end == NULL)
5750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005751 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 EMSG2(_("E112: Option name missing: %s"), *arg);
5753 return FAIL;
5754 }
5755
5756 if (!evaluate)
5757 {
5758 *arg = option_end;
5759 return OK;
5760 }
5761
5762 c = *option_end;
5763 *option_end = NUL;
5764 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
5767 if (opt_type == -3) /* invalid name */
5768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 EMSG2(_("E113: Unknown option: %s"), *arg);
5771 ret = FAIL;
5772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (opt_type == -2) /* hidden string option */
5776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 rettv->v_type = VAR_STRING;
5778 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 else if (opt_type == -1) /* hidden number option */
5781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005782 rettv->v_type = VAR_NUMBER;
5783 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785 else if (opt_type == 1) /* number option */
5786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005787 rettv->v_type = VAR_NUMBER;
5788 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 else /* string option */
5791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005792 rettv->v_type = VAR_STRING;
5793 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
5795 }
5796 else if (working && (opt_type == -2 || opt_type == -1))
5797 ret = FAIL;
5798
5799 *option_end = c; /* put back for error messages */
5800 *arg = option_end;
5801
5802 return ret;
5803}
5804
5805/*
5806 * Allocate a variable for a string constant.
5807 * Return OK or FAIL.
5808 */
5809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 char_u *p;
5813 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 int extra = 0;
5815
5816 /*
5817 * Find the end of the string, skipping backslashed characters.
5818 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005819 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 {
5821 if (*p == '\\' && p[1] != NUL)
5822 {
5823 ++p;
5824 /* A "\<x>" form occupies at least 4 characters, and produces up
5825 * to 6 characters: reserve space for 2 extra */
5826 if (*p == '<')
5827 extra += 2;
5828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830
5831 if (*p != '"')
5832 {
5833 EMSG2(_("E114: Missing quote: %s"), *arg);
5834 return FAIL;
5835 }
5836
5837 /* If only parsing, set *arg and return here */
5838 if (!evaluate)
5839 {
5840 *arg = p + 1;
5841 return OK;
5842 }
5843
5844 /*
5845 * Copy the string into allocated memory, handling backslashed
5846 * characters.
5847 */
5848 name = alloc((unsigned)(p - *arg + extra));
5849 if (name == NULL)
5850 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 rettv->v_type = VAR_STRING;
5852 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
5856 if (*p == '\\')
5857 {
5858 switch (*++p)
5859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 case 'b': *name++ = BS; ++p; break;
5861 case 'e': *name++ = ESC; ++p; break;
5862 case 'f': *name++ = FF; ++p; break;
5863 case 'n': *name++ = NL; ++p; break;
5864 case 'r': *name++ = CAR; ++p; break;
5865 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 case 'X': /* hex: "\x1", "\x12" */
5868 case 'x':
5869 case 'u': /* Unicode: "\u0023" */
5870 case 'U':
5871 if (vim_isxdigit(p[1]))
5872 {
5873 int n, nr;
5874 int c = toupper(*p);
5875
5876 if (c == 'X')
5877 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005878 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005880 else
5881 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 nr = 0;
5883 while (--n >= 0 && vim_isxdigit(p[1]))
5884 {
5885 ++p;
5886 nr = (nr << 4) + hex2nr(*p);
5887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#ifdef FEAT_MBYTE
5890 /* For "\u" store the number according to
5891 * 'encoding'. */
5892 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 else
5895#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 break;
5899
5900 /* octal: "\1", "\12", "\123" */
5901 case '0':
5902 case '1':
5903 case '2':
5904 case '3':
5905 case '4':
5906 case '5':
5907 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 case '7': *name = *p++ - '0';
5909 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 *name = (*name << 3) + *p++ - '0';
5912 if (*p >= '0' && *p <= '7')
5913 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 break;
5917
5918 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (extra != 0)
5921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 break;
5924 }
5925 /* FALLTHROUGH */
5926
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 break;
5929 }
5930 }
5931 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 *arg = p + 1;
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 return OK;
5939}
5940
5941/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 * Return OK or FAIL.
5944 */
5945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005946get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
5948 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 int reduce = 0;
5951
5952 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 */
5955 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960 break;
5961 ++reduce;
5962 ++p;
5963 }
5964 }
5965
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 return FAIL;
5970 }
5971
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 if (!evaluate)
5974 {
5975 *arg = p + 1;
5976 return OK;
5977 }
5978
5979 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005981 */
5982 str = alloc((unsigned)((p - *arg) - reduce));
5983 if (str == NULL)
5984 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005985 rettv->v_type = VAR_STRING;
5986 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005990 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 break;
5994 ++p;
5995 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005996 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005998 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005999 *arg = p + 1;
6000
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006001 return OK;
6002}
6003
Bram Moolenaarddecc252016-04-06 22:59:37 +02006004 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006005partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006006{
6007 int i;
6008
6009 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006010 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006011 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006012 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006013 func_unref(pt->pt_name);
6014 vim_free(pt->pt_name);
6015 vim_free(pt);
6016}
6017
6018/*
6019 * Unreference a closure: decrement the reference count and free it when it
6020 * becomes zero.
6021 */
6022 void
6023partial_unref(partial_T *pt)
6024{
6025 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006026 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006027}
6028
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Allocate a variable for a List and fill it from "*arg".
6031 * Return OK or FAIL.
6032 */
6033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035{
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 list_T *l = NULL;
6037 typval_T tv;
6038 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039
6040 if (evaluate)
6041 {
6042 l = list_alloc();
6043 if (l == NULL)
6044 return FAIL;
6045 }
6046
6047 *arg = skipwhite(*arg + 1);
6048 while (**arg != ']' && **arg != NUL)
6049 {
6050 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6051 goto failret;
6052 if (evaluate)
6053 {
6054 item = listitem_alloc();
6055 if (item != NULL)
6056 {
6057 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006058 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 list_append(l, item);
6060 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006061 else
6062 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 }
6064
6065 if (**arg == ']')
6066 break;
6067 if (**arg != ',')
6068 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006069 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 goto failret;
6071 }
6072 *arg = skipwhite(*arg + 1);
6073 }
6074
6075 if (**arg != ']')
6076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078failret:
6079 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006080 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 return FAIL;
6082 }
6083
6084 *arg = skipwhite(*arg + 1);
6085 if (evaluate)
6086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006087 rettv->v_type = VAR_LIST;
6088 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 ++l->lv_refcount;
6090 }
6091
6092 return OK;
6093}
6094
6095/*
6096 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006097 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006099 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006101{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006102 list_T *l;
6103
6104 l = (list_T *)alloc_clear(sizeof(list_T));
6105 if (l != NULL)
6106 {
6107 /* Prepend the list to the list of lists for garbage collection. */
6108 if (first_list != NULL)
6109 first_list->lv_used_prev = l;
6110 l->lv_used_prev = NULL;
6111 l->lv_used_next = first_list;
6112 first_list = l;
6113 }
6114 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115}
6116
6117/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006118 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006119 * Returns OK or FAIL.
6120 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006121 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006123{
6124 list_T *l = list_alloc();
6125
6126 if (l == NULL)
6127 return FAIL;
6128
6129 rettv->vval.v_list = l;
6130 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006131 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006132 ++l->lv_refcount;
6133 return OK;
6134}
6135
6136/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137 * Unreference a list: decrement the reference count and free it when it
6138 * becomes zero.
6139 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006141list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006143 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006144 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145}
6146
6147/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006148 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 * Ignores the reference count.
6150 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006151 static void
6152list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153{
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006156 for (item = l->lv_first; item != NULL; item = l->lv_first)
6157 {
6158 /* Remove the item before deleting it. */
6159 l->lv_first = item->li_next;
6160 clear_tv(&item->li_tv);
6161 vim_free(item);
6162 }
6163}
6164
6165 static void
6166list_free_list(list_T *l)
6167{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006168 /* Remove the list from the list of lists for garbage collection. */
6169 if (l->lv_used_prev == NULL)
6170 first_list = l->lv_used_next;
6171 else
6172 l->lv_used_prev->lv_used_next = l->lv_used_next;
6173 if (l->lv_used_next != NULL)
6174 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6175
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006176 vim_free(l);
6177}
6178
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006179 void
6180list_free(list_T *l)
6181{
6182 if (!in_free_unref_items)
6183 {
6184 list_free_contents(l);
6185 list_free_list(l);
6186 }
6187}
6188
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006189/*
6190 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006191 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006193 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006194listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006195{
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197}
6198
6199/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006200 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006205 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206 vim_free(item);
6207}
6208
6209/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006210 * Remove a list item from a List and free it. Also clears the value.
6211 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006214{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006215 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006216 listitem_free(item);
6217}
6218
6219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 * Get the number of items in a list.
6221 */
6222 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 if (l == NULL)
6226 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006227 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228}
6229
6230/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006231 * Return TRUE when two lists have exactly the same values.
6232 */
6233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234list_equal(
6235 list_T *l1,
6236 list_T *l2,
6237 int ic, /* ignore case for strings */
6238 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239{
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006242 if (l1 == NULL || l2 == NULL)
6243 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006244 if (l1 == l2)
6245 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006246 if (list_len(l1) != list_len(l2))
6247 return FALSE;
6248
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 for (item1 = l1->lv_first, item2 = l2->lv_first;
6250 item1 != NULL && item2 != NULL;
6251 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006252 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 return FALSE;
6254 return item1 == NULL && item2 == NULL;
6255}
6256
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006257/*
6258 * Return the dictitem that an entry in a hashtable points to.
6259 */
6260 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006262{
6263 return HI2DI(hi);
6264}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006265
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006267 * Return TRUE when two dictionaries have exactly the same key/values.
6268 */
6269 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006270dict_equal(
6271 dict_T *d1,
6272 dict_T *d2,
6273 int ic, /* ignore case for strings */
6274 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275{
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 hashitem_T *hi;
6277 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006278 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006279
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006280 if (d1 == NULL && d2 == NULL)
6281 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006282 if (d1 == NULL || d2 == NULL)
6283 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006284 if (d1 == d2)
6285 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006286 if (dict_len(d1) != dict_len(d2))
6287 return FALSE;
6288
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006289 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006290 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006291 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006292 if (!HASHITEM_EMPTY(hi))
6293 {
6294 item2 = dict_find(d2, hi->hi_key, -1);
6295 if (item2 == NULL)
6296 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006297 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 return FALSE;
6299 --todo;
6300 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006301 }
6302 return TRUE;
6303}
6304
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305static int tv_equal_recurse_limit;
6306
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006307 static int
6308func_equal(
6309 typval_T *tv1,
6310 typval_T *tv2,
6311 int ic) /* ignore case */
6312{
6313 char_u *s1, *s2;
6314 dict_T *d1, *d2;
6315 int a1, a2;
6316 int i;
6317
6318 /* empty and NULL function name considered the same */
6319 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6320 : tv1->vval.v_partial->pt_name;
6321 if (s1 != NULL && *s1 == NUL)
6322 s1 = NULL;
6323 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6324 : tv2->vval.v_partial->pt_name;
6325 if (s2 != NULL && *s2 == NUL)
6326 s2 = NULL;
6327 if (s1 == NULL || s2 == NULL)
6328 {
6329 if (s1 != s2)
6330 return FALSE;
6331 }
6332 else if (STRCMP(s1, s2) != 0)
6333 return FALSE;
6334
6335 /* empty dict and NULL dict is different */
6336 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6337 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6338 if (d1 == NULL || d2 == NULL)
6339 {
6340 if (d1 != d2)
6341 return FALSE;
6342 }
6343 else if (!dict_equal(d1, d2, ic, TRUE))
6344 return FALSE;
6345
6346 /* empty list and no list considered the same */
6347 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6348 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6349 if (a1 != a2)
6350 return FALSE;
6351 for (i = 0; i < a1; ++i)
6352 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6353 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6354 return FALSE;
6355
6356 return TRUE;
6357}
6358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006359/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006361 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006362 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006363 */
6364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006365tv_equal(
6366 typval_T *tv1,
6367 typval_T *tv2,
6368 int ic, /* ignore case */
6369 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370{
6371 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006372 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006373 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006374 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006376 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006377 * recursiveness to a limit. We guess they are equal then.
6378 * A fixed limit has the problem of still taking an awful long time.
6379 * Reduce the limit every time running into it. That should work fine for
6380 * deeply linked structures that are not recursively linked and catch
6381 * recursiveness quickly. */
6382 if (!recursive)
6383 tv_equal_recurse_limit = 1000;
6384 if (recursive_cnt >= tv_equal_recurse_limit)
6385 {
6386 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006387 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006388 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006389
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006390 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6391 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006392 if ((tv1->v_type == VAR_FUNC
6393 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6394 && (tv2->v_type == VAR_FUNC
6395 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6396 {
6397 ++recursive_cnt;
6398 r = func_equal(tv1, tv2, ic);
6399 --recursive_cnt;
6400 return r;
6401 }
6402
6403 if (tv1->v_type != tv2->v_type)
6404 return FALSE;
6405
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006406 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006408 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006409 ++recursive_cnt;
6410 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6411 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006412 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006413
6414 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006415 ++recursive_cnt;
6416 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6417 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006418 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006419
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006420 case VAR_NUMBER:
6421 return tv1->vval.v_number == tv2->vval.v_number;
6422
6423 case VAR_STRING:
6424 s1 = get_tv_string_buf(tv1, buf1);
6425 s2 = get_tv_string_buf(tv2, buf2);
6426 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006427
6428 case VAR_SPECIAL:
6429 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006430
6431 case VAR_FLOAT:
6432#ifdef FEAT_FLOAT
6433 return tv1->vval.v_float == tv2->vval.v_float;
6434#endif
6435 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006437 return tv1->vval.v_job == tv2->vval.v_job;
6438#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006439 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006440#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006441 return tv1->vval.v_channel == tv2->vval.v_channel;
6442#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006443 case VAR_FUNC:
6444 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006445 case VAR_UNKNOWN:
6446 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006448
Bram Moolenaara03f2332016-02-06 18:09:59 +01006449 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6450 * does not equal anything, not even itself. */
6451 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452}
6453
6454/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 * Locate item with index "n" in list "l" and return it.
6456 * A negative index is counted from the end; -1 is the last item.
6457 * Returns NULL when "n" is out of range.
6458 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006459 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
Bram Moolenaar33570922005-01-25 22:26:29 +00006462 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 long idx;
6464
6465 if (l == NULL)
6466 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006467
6468 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006470 n = l->lv_len + n;
6471
6472 /* Check for index out of range. */
6473 if (n < 0 || n >= l->lv_len)
6474 return NULL;
6475
6476 /* When there is a cached index may start search from there. */
6477 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006479 if (n < l->lv_idx / 2)
6480 {
6481 /* closest to the start of the list */
6482 item = l->lv_first;
6483 idx = 0;
6484 }
6485 else if (n > (l->lv_idx + l->lv_len) / 2)
6486 {
6487 /* closest to the end of the list */
6488 item = l->lv_last;
6489 idx = l->lv_len - 1;
6490 }
6491 else
6492 {
6493 /* closest to the cached index */
6494 item = l->lv_idx_item;
6495 idx = l->lv_idx;
6496 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 }
6498 else
6499 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006500 if (n < l->lv_len / 2)
6501 {
6502 /* closest to the start of the list */
6503 item = l->lv_first;
6504 idx = 0;
6505 }
6506 else
6507 {
6508 /* closest to the end of the list */
6509 item = l->lv_last;
6510 idx = l->lv_len - 1;
6511 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513
6514 while (n > idx)
6515 {
6516 /* search forward */
6517 item = item->li_next;
6518 ++idx;
6519 }
6520 while (n < idx)
6521 {
6522 /* search backward */
6523 item = item->li_prev;
6524 --idx;
6525 }
6526
6527 /* cache the used index */
6528 l->lv_idx = idx;
6529 l->lv_idx_item = item;
6530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 return item;
6532}
6533
6534/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006535 * Get list item "l[idx]" as a number.
6536 */
6537 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_find_nr(
6539 list_T *l,
6540 long idx,
6541 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006542{
6543 listitem_T *li;
6544
6545 li = list_find(l, idx);
6546 if (li == NULL)
6547 {
6548 if (errorp != NULL)
6549 *errorp = TRUE;
6550 return -1L;
6551 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006552 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006553}
6554
6555/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006556 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6557 */
6558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006560{
6561 listitem_T *li;
6562
6563 li = list_find(l, idx - 1);
6564 if (li == NULL)
6565 {
6566 EMSGN(_(e_listidx), idx);
6567 return NULL;
6568 }
6569 return get_tv_string(&li->li_tv);
6570}
6571
6572/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006573 * Locate "item" list "l" and return its index.
6574 * Returns -1 when "item" is not in the list.
6575 */
6576 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006578{
6579 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006581
6582 if (l == NULL)
6583 return -1;
6584 idx = 0;
6585 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6586 ++idx;
6587 if (li == NULL)
6588 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006589 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006590}
6591
6592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 * Append item "item" to the end of list "l".
6594 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006596list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597{
6598 if (l->lv_last == NULL)
6599 {
6600 /* empty list */
6601 l->lv_first = item;
6602 l->lv_last = item;
6603 item->li_prev = NULL;
6604 }
6605 else
6606 {
6607 l->lv_last->li_next = item;
6608 item->li_prev = l->lv_last;
6609 l->lv_last = item;
6610 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 item->li_next = NULL;
6613}
6614
6615/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623
Bram Moolenaar05159a02005-02-26 23:04:13 +00006624 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006626 copy_tv(tv, &li->li_tv);
6627 list_append(l, li);
6628 return OK;
6629}
6630
6631/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006632 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633 * Return FAIL when out of memory.
6634 */
6635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006637{
6638 listitem_T *li = listitem_alloc();
6639
6640 if (li == NULL)
6641 return FAIL;
6642 li->li_tv.v_type = VAR_DICT;
6643 li->li_tv.v_lock = 0;
6644 li->li_tv.vval.v_dict = dict;
6645 list_append(list, li);
6646 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 return OK;
6648}
6649
6650/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006651 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006652 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006653 * Returns FAIL when out of memory.
6654 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006656list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006657{
6658 listitem_T *li = listitem_alloc();
6659
6660 if (li == NULL)
6661 return FAIL;
6662 list_append(l, li);
6663 li->li_tv.v_type = VAR_STRING;
6664 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006665 if (str == NULL)
6666 li->li_tv.vval.v_string = NULL;
6667 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006668 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006669 return FAIL;
6670 return OK;
6671}
6672
6673/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006674 * Append "n" to list "l".
6675 * Returns FAIL when out of memory.
6676 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006679{
6680 listitem_T *li;
6681
6682 li = listitem_alloc();
6683 if (li == NULL)
6684 return FAIL;
6685 li->li_tv.v_type = VAR_NUMBER;
6686 li->li_tv.v_lock = 0;
6687 li->li_tv.vval.v_number = n;
6688 list_append(l, li);
6689 return OK;
6690}
6691
6692/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006694 * If "item" is NULL append at the end.
6695 * Return FAIL when out of memory.
6696 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006697 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006699{
Bram Moolenaar33570922005-01-25 22:26:29 +00006700 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006701
6702 if (ni == NULL)
6703 return FAIL;
6704 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006705 list_insert(l, ni, item);
6706 return OK;
6707}
6708
6709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006711{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006712 if (item == NULL)
6713 /* Append new item at end of list. */
6714 list_append(l, ni);
6715 else
6716 {
6717 /* Insert new item before existing item. */
6718 ni->li_prev = item->li_prev;
6719 ni->li_next = item;
6720 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006721 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006722 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006723 ++l->lv_idx;
6724 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006726 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006727 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006728 l->lv_idx_item = NULL;
6729 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006731 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733}
6734
6735/*
6736 * Extend "l1" with "l2".
6737 * If "bef" is NULL append at the end, otherwise insert before this item.
6738 * Returns FAIL when out of memory.
6739 */
6740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742{
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006744 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006746 /* We also quit the loop when we have inserted the original item count of
6747 * the list, avoid a hang when we extend a list with itself. */
6748 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006749 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6750 return FAIL;
6751 return OK;
6752}
6753
6754/*
6755 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6756 * Return FAIL when out of memory.
6757 */
6758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006760{
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006763 if (l1 == NULL || l2 == NULL)
6764 return FAIL;
6765
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006766 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006768 if (l == NULL)
6769 return FAIL;
6770 tv->v_type = VAR_LIST;
6771 tv->vval.v_list = l;
6772
6773 /* append all items from the second list */
6774 return list_extend(l, l2, NULL);
6775}
6776
6777/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006778 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006779 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 * Returns NULL when out of memory.
6782 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006783 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006784list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785{
Bram Moolenaar33570922005-01-25 22:26:29 +00006786 list_T *copy;
6787 listitem_T *item;
6788 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789
6790 if (orig == NULL)
6791 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792
6793 copy = list_alloc();
6794 if (copy != NULL)
6795 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006796 if (copyID != 0)
6797 {
6798 /* Do this before adding the items, because one of the items may
6799 * refer back to this list. */
6800 orig->lv_copyID = copyID;
6801 orig->lv_copylist = copy;
6802 }
6803 for (item = orig->lv_first; item != NULL && !got_int;
6804 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006805 {
6806 ni = listitem_alloc();
6807 if (ni == NULL)
6808 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006809 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006810 {
6811 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6812 {
6813 vim_free(ni);
6814 break;
6815 }
6816 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006818 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006819 list_append(copy, ni);
6820 }
6821 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006822 if (item != NULL)
6823 {
6824 list_unref(copy);
6825 copy = NULL;
6826 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006827 }
6828
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006829 return copy;
6830}
6831
6832/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006833 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006834 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006835 * This used to be called list_remove, but that conflicts with a Sun header
6836 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006840{
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006843 /* notify watchers */
6844 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006845 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006846 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847 list_fix_watch(l, ip);
6848 if (ip == item2)
6849 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006850 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006851
6852 if (item2->li_next == NULL)
6853 l->lv_last = item->li_prev;
6854 else
6855 item2->li_next->li_prev = item->li_prev;
6856 if (item->li_prev == NULL)
6857 l->lv_first = item2->li_next;
6858 else
6859 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006860 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006861}
6862
6863/*
6864 * Return an allocated string with the string representation of a list.
6865 * May return NULL.
6866 */
6867 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006868list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006869{
6870 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006871
6872 if (tv->vval.v_list == NULL)
6873 return NULL;
6874 ga_init2(&ga, (int)sizeof(char), 80);
6875 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006876 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6877 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006878 {
6879 vim_free(ga.ga_data);
6880 return NULL;
6881 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006882 ga_append(&ga, ']');
6883 ga_append(&ga, NUL);
6884 return (char_u *)ga.ga_data;
6885}
6886
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006887typedef struct join_S {
6888 char_u *s;
6889 char_u *tofree;
6890} join_T;
6891
6892 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006893list_join_inner(
6894 garray_T *gap, /* to store the result in */
6895 list_T *l,
6896 char_u *sep,
6897 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006898 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006899 int copyID,
6900 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006901{
6902 int i;
6903 join_T *p;
6904 int len;
6905 int sumlen = 0;
6906 int first = TRUE;
6907 char_u *tofree;
6908 char_u numbuf[NUMBUFLEN];
6909 listitem_T *item;
6910 char_u *s;
6911
6912 /* Stringify each item in the list. */
6913 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6914 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006915 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6916 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006917 if (s == NULL)
6918 return FAIL;
6919
6920 len = (int)STRLEN(s);
6921 sumlen += len;
6922
Bram Moolenaarcde88542015-08-11 19:14:00 +02006923 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006924 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6925 if (tofree != NULL || s != numbuf)
6926 {
6927 p->s = s;
6928 p->tofree = tofree;
6929 }
6930 else
6931 {
6932 p->s = vim_strnsave(s, len);
6933 p->tofree = p->s;
6934 }
6935
6936 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006937 if (did_echo_string_emsg) /* recursion error, bail out */
6938 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006939 }
6940
6941 /* Allocate result buffer with its total size, avoid re-allocation and
6942 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6943 if (join_gap->ga_len >= 2)
6944 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6945 if (ga_grow(gap, sumlen + 2) == FAIL)
6946 return FAIL;
6947
6948 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6949 {
6950 if (first)
6951 first = FALSE;
6952 else
6953 ga_concat(gap, sep);
6954 p = ((join_T *)join_gap->ga_data) + i;
6955
6956 if (p->s != NULL)
6957 ga_concat(gap, p->s);
6958 line_breakcheck();
6959 }
6960
6961 return OK;
6962}
6963
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006964/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006965 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006966 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006967 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006968 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970list_join(
6971 garray_T *gap,
6972 list_T *l,
6973 char_u *sep,
6974 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006975 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006976 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006977{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006978 garray_T join_ga;
6979 int retval;
6980 join_T *p;
6981 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006982
Bram Moolenaard39a7512015-04-16 22:51:22 +02006983 if (l->lv_len < 1)
6984 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006985 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006986 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6987 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988
6989 /* Dispose each item in join_ga. */
6990 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006991 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006992 p = (join_T *)join_ga.ga_data;
6993 for (i = 0; i < join_ga.ga_len; ++i)
6994 {
6995 vim_free(p->tofree);
6996 ++p;
6997 }
6998 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006999 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01007000
7001 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002}
7003
7004/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007005 * Return the next (unique) copy ID.
7006 * Used for serializing nested structures.
7007 */
7008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007009get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007010{
7011 current_copyID += COPYID_INC;
7012 return current_copyID;
7013}
7014
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007015/* Used by get_func_tv() */
7016static garray_T funcargs = GA_EMPTY;
7017
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007018/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 * Garbage collection for lists and dictionaries.
7020 *
7021 * We use reference counts to be able to free most items right away when they
7022 * are no longer used. But for composite items it's possible that it becomes
7023 * unused while the reference count is > 0: When there is a recursive
7024 * reference. Example:
7025 * :let l = [1, 2, 3]
7026 * :let d = {9: l}
7027 * :let l[1] = d
7028 *
7029 * Since this is quite unusual we handle this with garbage collection: every
7030 * once in a while find out which lists and dicts are not referenced from any
7031 * variable.
7032 *
7033 * Here is a good reference text about garbage collection (refers to Python
7034 * but it applies to all reference-counting mechanisms):
7035 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007037
7038/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007040 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007042 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007044garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007045{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007046 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 buf_T *buf;
7049 win_T *wp;
7050 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007051 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007052 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007053 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007054#ifdef FEAT_WINDOWS
7055 tabpage_T *tp;
7056#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007058 if (!testing)
7059 {
7060 /* Only do this once. */
7061 want_garbage_collect = FALSE;
7062 may_garbage_collect = FALSE;
7063 garbage_collect_at_exit = FALSE;
7064 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007065
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007066 /* We advance by two because we add one for items referenced through
7067 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007068 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 /*
7071 * 1. Go through all accessible variables and mark all lists and dicts
7072 * with copyID.
7073 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074
7075 /* Don't free variables in the previous_funccal list unless they are only
7076 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007077 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007078 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7079 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7081 NULL);
7082 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7083 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007084 }
7085
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 /* script-local variables */
7087 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089
7090 /* buffer-local variables */
7091 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7093 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094
7095 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007096 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7098 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007099#ifdef FEAT_AUTOCMD
7100 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7102 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007103#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007105#ifdef FEAT_WINDOWS
7106 /* tabpage-local variables */
7107 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7109 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007110#endif
7111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114
7115 /* function-local variables */
7116 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7117 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7119 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 }
7121
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007122 /* function call arguments, if v:testing is set. */
7123 for (i = 0; i < funcargs.ga_len; ++i)
7124 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7125 copyID, NULL, NULL);
7126
Bram Moolenaard812df62008-11-09 12:46:09 +00007127 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007129
Bram Moolenaar1dced572012-04-05 16:54:08 +02007130#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007132#endif
7133
Bram Moolenaardb913952012-06-29 12:54:53 +02007134#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007136#endif
7137
7138#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007140#endif
7141
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007143 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007144 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007145#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007146#ifdef FEAT_NETBEANS_INTG
7147 abort = abort || set_ref_in_nb_channel(copyID);
7148#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007149
Bram Moolenaare3188e22016-05-31 21:13:04 +02007150#ifdef FEAT_TIMERS
7151 abort = abort || set_ref_in_timer(copyID);
7152#endif
7153
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 /*
7157 * 2. Free lists and dictionaries that are not referenced.
7158 */
7159 did_free = free_unref_items(copyID);
7160
7161 /*
7162 * 3. Check if any funccal can be freed now.
7163 */
7164 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007165 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 if (can_free_funccal(*pfc, copyID))
7167 {
7168 fc = *pfc;
7169 *pfc = fc->caller;
7170 free_funccal(fc, TRUE);
7171 did_free = TRUE;
7172 did_free_funccal = TRUE;
7173 }
7174 else
7175 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007176 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 if (did_free_funccal)
7178 /* When a funccal was freed some more items might be garbage
7179 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007180 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007181 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 else if (p_verbose > 0)
7183 {
7184 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7185 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007186
7187 return did_free;
7188}
7189
7190/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007191 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007192 */
7193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007194free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007195{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007196 dict_T *dd, *dd_next;
7197 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007198 int did_free = FALSE;
7199
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007200 /* Let all "free" functions know that we are here. This means no
7201 * dictionaries, lists, channels or jobs are to be freed, because we will
7202 * do that here. */
7203 in_free_unref_items = TRUE;
7204
7205 /*
7206 * PASS 1: free the contents of the items. We don't free the items
7207 * themselves yet, so that it is possible to decrement refcount counters
7208 */
7209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007210 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007211 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007213 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007214 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007215 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007216 /* Free the Dictionary and ordinary items it contains, but don't
7217 * recurse into Lists and Dictionaries, they will be in the list
7218 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007219 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007220 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
7223 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007224 * Go through the list of lists and free items without the copyID.
7225 * But don't free a list that has a watcher (used in a for loop), these
7226 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007228 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7229 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7230 && ll->lv_watch == NULL)
7231 {
7232 /* Free the List and ordinary items it contains, but don't recurse
7233 * into Lists and Dictionaries, they will be in the list of dicts
7234 * or list of lists. */
7235 list_free_contents(ll);
7236 did_free = TRUE;
7237 }
7238
7239#ifdef FEAT_JOB_CHANNEL
7240 /* Go through the list of jobs and free items without the copyID. This
7241 * must happen before doing channels, because jobs refer to channels, but
7242 * the reference from the channel to the job isn't tracked. */
7243 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7244
7245 /* Go through the list of channels and free items without the copyID. */
7246 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7247#endif
7248
7249 /*
7250 * PASS 2: free the items themselves.
7251 */
7252 for (dd = first_dict; dd != NULL; dd = dd_next)
7253 {
7254 dd_next = dd->dv_used_next;
7255 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7256 dict_free_dict(dd);
7257 }
7258
7259 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007260 {
7261 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007262 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7263 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007264 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265 /* Free the List and ordinary items it contains, but don't recurse
7266 * into Lists and Dictionaries, they will be in the list of dicts
7267 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007270 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007271
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007272#ifdef FEAT_JOB_CHANNEL
7273 /* Go through the list of jobs and free items without the copyID. This
7274 * must happen before doing channels, because jobs refer to channels, but
7275 * the reference from the channel to the job isn't tracked. */
7276 free_unused_jobs(copyID, COPYID_MASK);
7277
7278 /* Go through the list of channels and free items without the copyID. */
7279 free_unused_channels(copyID, COPYID_MASK);
7280#endif
7281
7282 in_free_unref_items = FALSE;
7283
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007284 return did_free;
7285}
7286
7287/*
7288 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 * "list_stack" is used to add lists to be marked. Can be NULL.
7290 *
7291 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007292 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007295{
7296 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007297 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007298 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007299 hashtab_T *cur_ht;
7300 ht_stack_T *ht_stack = NULL;
7301 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007302
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007303 cur_ht = ht;
7304 for (;;)
7305 {
7306 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007307 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308 /* Mark each item in the hashtab. If the item contains a hashtab
7309 * it is added to ht_stack, if it contains a list it is added to
7310 * list_stack. */
7311 todo = (int)cur_ht->ht_used;
7312 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7313 if (!HASHITEM_EMPTY(hi))
7314 {
7315 --todo;
7316 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7317 &ht_stack, list_stack);
7318 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007319 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007320
7321 if (ht_stack == NULL)
7322 break;
7323
7324 /* take an item from the stack */
7325 cur_ht = ht_stack->ht;
7326 tempitem = ht_stack;
7327 ht_stack = ht_stack->prev;
7328 free(tempitem);
7329 }
7330
7331 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007332}
7333
7334/*
7335 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7337 *
7338 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007339 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007341set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007342{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007343 listitem_T *li;
7344 int abort = FALSE;
7345 list_T *cur_l;
7346 list_stack_T *list_stack = NULL;
7347 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007348
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007349 cur_l = l;
7350 for (;;)
7351 {
7352 if (!abort)
7353 /* Mark each item in the list. If the item contains a hashtab
7354 * it is added to ht_stack, if it contains a list it is added to
7355 * list_stack. */
7356 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7357 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7358 ht_stack, &list_stack);
7359 if (list_stack == NULL)
7360 break;
7361
7362 /* take an item from the stack */
7363 cur_l = list_stack->list;
7364 tempitem = list_stack;
7365 list_stack = list_stack->prev;
7366 free(tempitem);
7367 }
7368
7369 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007370}
7371
7372/*
7373 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007374 * "list_stack" is used to add lists to be marked. Can be NULL.
7375 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7376 *
7377 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007378 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007380set_ref_in_item(
7381 typval_T *tv,
7382 int copyID,
7383 ht_stack_T **ht_stack,
7384 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007385{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007386 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007388 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007389 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007390 dict_T *dd = tv->vval.v_dict;
7391
Bram Moolenaara03f2332016-02-06 18:09:59 +01007392 if (dd != NULL && dd->dv_copyID != copyID)
7393 {
7394 /* Didn't see this dict yet. */
7395 dd->dv_copyID = copyID;
7396 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007397 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007398 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7399 }
7400 else
7401 {
7402 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7403 if (newitem == NULL)
7404 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007405 else
7406 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007407 newitem->ht = &dd->dv_hashtab;
7408 newitem->prev = *ht_stack;
7409 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007410 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007411 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007412 }
7413 }
7414 else if (tv->v_type == VAR_LIST)
7415 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007416 list_T *ll = tv->vval.v_list;
7417
Bram Moolenaara03f2332016-02-06 18:09:59 +01007418 if (ll != NULL && ll->lv_copyID != copyID)
7419 {
7420 /* Didn't see this list yet. */
7421 ll->lv_copyID = copyID;
7422 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007423 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007424 abort = set_ref_in_list(ll, copyID, ht_stack);
7425 }
7426 else
7427 {
7428 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007429 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007430 if (newitem == NULL)
7431 abort = TRUE;
7432 else
7433 {
7434 newitem->list = ll;
7435 newitem->prev = *list_stack;
7436 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007437 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007438 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007439 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007440 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007441 else if (tv->v_type == VAR_PARTIAL)
7442 {
7443 partial_T *pt = tv->vval.v_partial;
7444 int i;
7445
7446 /* A partial does not have a copyID, because it cannot contain itself.
7447 */
7448 if (pt != NULL)
7449 {
7450 if (pt->pt_dict != NULL)
7451 {
7452 typval_T dtv;
7453
7454 dtv.v_type = VAR_DICT;
7455 dtv.vval.v_dict = pt->pt_dict;
7456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7457 }
7458
7459 for (i = 0; i < pt->pt_argc; ++i)
7460 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7461 ht_stack, list_stack);
7462 }
7463 }
7464#ifdef FEAT_JOB_CHANNEL
7465 else if (tv->v_type == VAR_JOB)
7466 {
7467 job_T *job = tv->vval.v_job;
7468 typval_T dtv;
7469
7470 if (job != NULL && job->jv_copyID != copyID)
7471 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007472 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 if (job->jv_channel != NULL)
7474 {
7475 dtv.v_type = VAR_CHANNEL;
7476 dtv.vval.v_channel = job->jv_channel;
7477 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7478 }
7479 if (job->jv_exit_partial != NULL)
7480 {
7481 dtv.v_type = VAR_PARTIAL;
7482 dtv.vval.v_partial = job->jv_exit_partial;
7483 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7484 }
7485 }
7486 }
7487 else if (tv->v_type == VAR_CHANNEL)
7488 {
7489 channel_T *ch =tv->vval.v_channel;
7490 int part;
7491 typval_T dtv;
7492 jsonq_T *jq;
7493 cbq_T *cq;
7494
7495 if (ch != NULL && ch->ch_copyID != copyID)
7496 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007497 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007498 for (part = PART_SOCK; part <= PART_IN; ++part)
7499 {
7500 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7501 jq = jq->jq_next)
7502 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7503 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7504 cq = cq->cq_next)
7505 if (cq->cq_partial != NULL)
7506 {
7507 dtv.v_type = VAR_PARTIAL;
7508 dtv.vval.v_partial = cq->cq_partial;
7509 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7510 }
7511 if (ch->ch_part[part].ch_partial != NULL)
7512 {
7513 dtv.v_type = VAR_PARTIAL;
7514 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7515 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7516 }
7517 }
7518 if (ch->ch_partial != NULL)
7519 {
7520 dtv.v_type = VAR_PARTIAL;
7521 dtv.vval.v_partial = ch->ch_partial;
7522 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7523 }
7524 if (ch->ch_close_partial != NULL)
7525 {
7526 dtv.v_type = VAR_PARTIAL;
7527 dtv.vval.v_partial = ch->ch_close_partial;
7528 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7529 }
7530 }
7531 }
7532#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007533 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007534}
7535
7536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 * Allocate an empty header for a dictionary.
7538 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007539 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541{
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007546 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007547 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007548 if (first_dict != NULL)
7549 first_dict->dv_used_prev = d;
7550 d->dv_used_next = first_dict;
7551 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007553
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007556 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007557 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007558 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561}
7562
7563/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007564 * Allocate an empty dict for a return value.
7565 * Returns OK or FAIL.
7566 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007569{
7570 dict_T *d = dict_alloc();
7571
7572 if (d == NULL)
7573 return FAIL;
7574
7575 rettv->vval.v_dict = d;
7576 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007577 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007578 ++d->dv_refcount;
7579 return OK;
7580}
7581
7582
7583/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 * Unreference a Dictionary: decrement the reference count and free it when it
7585 * becomes zero.
7586 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007588dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007590 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007591 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592}
7593
7594/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007595 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 * Ignores the reference count.
7597 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007598 static void
7599dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007602 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007603 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007605 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007606 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007607 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007608 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007610 if (!HASHITEM_EMPTY(hi))
7611 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007612 /* Remove the item before deleting it, just in case there is
7613 * something recursive causing trouble. */
7614 di = HI2DI(hi);
7615 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007616 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007617 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 --todo;
7619 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007622}
7623
7624 static void
7625dict_free_dict(dict_T *d)
7626{
7627 /* Remove the dict from the list of dicts for garbage collection. */
7628 if (d->dv_used_prev == NULL)
7629 first_dict = d->dv_used_next;
7630 else
7631 d->dv_used_prev->dv_used_next = d->dv_used_next;
7632 if (d->dv_used_next != NULL)
7633 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 vim_free(d);
7635}
7636
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007637 void
7638dict_free(dict_T *d)
7639{
7640 if (!in_free_unref_items)
7641 {
7642 dict_free_contents(d);
7643 dict_free_dict(d);
7644 }
7645}
7646
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647/*
7648 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 * The "key" is copied to the new item.
7650 * Note that the value of the item "di_tv" still needs to be initialized!
7651 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007653 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007658 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007662 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007663 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665}
7666
7667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 * Make a copy of a Dictionary item.
7669 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007671dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007672{
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007675 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7676 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 if (di != NULL)
7678 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007679 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007680 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007681 copy_tv(&org->di_tv, &di->di_tv);
7682 }
7683 return di;
7684}
7685
7686/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687 * Remove item "item" from Dictionary "dict" and free it.
7688 */
7689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007690dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691{
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 if (HASHITEM_EMPTY(hi))
7696 EMSG2(_(e_intern2), "dictitem_remove()");
7697 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007698 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 dictitem_free(item);
7700}
7701
7702/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 * Free a dict item. Also clears the value.
7704 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007706dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007709 if (item->di_flags & DI_FLAGS_ALLOC)
7710 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711}
7712
7713/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7715 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007716 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717 * Returns NULL when out of memory.
7718 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007721{
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 dict_T *copy;
7723 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007725 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726
7727 if (orig == NULL)
7728 return NULL;
7729
7730 copy = dict_alloc();
7731 if (copy != NULL)
7732 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007733 if (copyID != 0)
7734 {
7735 orig->dv_copyID = copyID;
7736 orig->dv_copydict = copy;
7737 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007738 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007739 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007741 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 --todo;
7744
7745 di = dictitem_alloc(hi->hi_key);
7746 if (di == NULL)
7747 break;
7748 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007749 {
7750 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7751 copyID) == FAIL)
7752 {
7753 vim_free(di);
7754 break;
7755 }
7756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 else
7758 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7759 if (dict_add(copy, di) == FAIL)
7760 {
7761 dictitem_free(di);
7762 break;
7763 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766
Bram Moolenaare9a41262005-01-15 22:18:47 +00007767 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007768 if (todo > 0)
7769 {
7770 dict_unref(copy);
7771 copy = NULL;
7772 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007773 }
7774
7775 return copy;
7776}
7777
7778/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007780 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007783dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784{
Bram Moolenaar33570922005-01-25 22:26:29 +00007785 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786}
7787
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007789 * Add a number or string entry to dictionary "d".
7790 * When "str" is NULL use number "nr", otherwise use "str".
7791 * Returns FAIL when out of memory and when key already exists.
7792 */
7793 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794dict_add_nr_str(
7795 dict_T *d,
7796 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007797 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007798 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799{
7800 dictitem_T *item;
7801
7802 item = dictitem_alloc((char_u *)key);
7803 if (item == NULL)
7804 return FAIL;
7805 item->di_tv.v_lock = 0;
7806 if (str == NULL)
7807 {
7808 item->di_tv.v_type = VAR_NUMBER;
7809 item->di_tv.vval.v_number = nr;
7810 }
7811 else
7812 {
7813 item->di_tv.v_type = VAR_STRING;
7814 item->di_tv.vval.v_string = vim_strsave(str);
7815 }
7816 if (dict_add(d, item) == FAIL)
7817 {
7818 dictitem_free(item);
7819 return FAIL;
7820 }
7821 return OK;
7822}
7823
7824/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007825 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007826 * Returns FAIL when out of memory and when key already exists.
7827 */
7828 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007829dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007830{
7831 dictitem_T *item;
7832
7833 item = dictitem_alloc((char_u *)key);
7834 if (item == NULL)
7835 return FAIL;
7836 item->di_tv.v_lock = 0;
7837 item->di_tv.v_type = VAR_LIST;
7838 item->di_tv.vval.v_list = list;
7839 if (dict_add(d, item) == FAIL)
7840 {
7841 dictitem_free(item);
7842 return FAIL;
7843 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007844 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007845 return OK;
7846}
7847
7848/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 * Get the number of items in a Dictionary.
7850 */
7851 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007852dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 if (d == NULL)
7855 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007856 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857}
7858
7859/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 * Find item "key[len]" in Dictionary "d".
7861 * If "len" is negative use strlen(key).
7862 * Returns NULL when not found.
7863 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007864 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007865dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007867#define AKEYLEN 200
7868 char_u buf[AKEYLEN];
7869 char_u *akey;
7870 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007871 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007873 if (d == NULL)
7874 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 if (len < 0)
7876 akey = key;
7877 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007878 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 tofree = akey = vim_strnsave(key, len);
7880 if (akey == NULL)
7881 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007882 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 else
7884 {
7885 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007886 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007887 akey = buf;
7888 }
7889
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007891 vim_free(tofree);
7892 if (HASHITEM_EMPTY(hi))
7893 return NULL;
7894 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895}
7896
7897/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007898 * Get a string item from a dictionary.
7899 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007900 * Returns NULL if the entry doesn't exist or out of memory.
7901 */
7902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007903get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007904{
7905 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007906 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007907
7908 di = dict_find(d, key, -1);
7909 if (di == NULL)
7910 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007911 s = get_tv_string(&di->di_tv);
7912 if (save && s != NULL)
7913 s = vim_strsave(s);
7914 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007915}
7916
7917/*
7918 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007919 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007920 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007921 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007922get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007923{
7924 dictitem_T *di;
7925
7926 di = dict_find(d, key, -1);
7927 if (di == NULL)
7928 return 0;
7929 return get_tv_number(&di->di_tv);
7930}
7931
7932/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 * Return an allocated string with the string representation of a Dictionary.
7934 * May return NULL.
7935 */
7936 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007937dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007938{
7939 garray_T ga;
7940 int first = TRUE;
7941 char_u *tofree;
7942 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007943 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007945 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007946 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007948 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007949 return NULL;
7950 ga_init2(&ga, (int)sizeof(char), 80);
7951 ga_append(&ga, '{');
7952
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007954 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007956 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 --todo;
7959
7960 if (first)
7961 first = FALSE;
7962 else
7963 ga_concat(&ga, (char_u *)", ");
7964
7965 tofree = string_quote(hi->hi_key, FALSE);
7966 if (tofree != NULL)
7967 {
7968 ga_concat(&ga, tofree);
7969 vim_free(tofree);
7970 }
7971 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007972 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7973 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 if (s != NULL)
7975 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007976 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007977 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007978 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007979 line_breakcheck();
7980
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007983 if (todo > 0)
7984 {
7985 vim_free(ga.ga_data);
7986 return NULL;
7987 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007988
7989 ga_append(&ga, '}');
7990 ga_append(&ga, NUL);
7991 return (char_u *)ga.ga_data;
7992}
7993
7994/*
7995 * Allocate a variable for a Dictionary and fill it from "*arg".
7996 * Return OK or FAIL. Returns NOTDONE for {expr}.
7997 */
7998 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007999get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008000{
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dict_T *d = NULL;
8002 typval_T tvkey;
8003 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008004 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008007 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008
8009 /*
8010 * First check if it's not a curly-braces thing: {expr}.
8011 * Must do this without evaluating, otherwise a function may be called
8012 * twice. Unfortunately this means we need to call eval1() twice for the
8013 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008015 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 if (*start != '}')
8017 {
8018 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8019 return FAIL;
8020 if (*start == '}')
8021 return NOTDONE;
8022 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023
8024 if (evaluate)
8025 {
8026 d = dict_alloc();
8027 if (d == NULL)
8028 return FAIL;
8029 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008030 tvkey.v_type = VAR_UNKNOWN;
8031 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032
8033 *arg = skipwhite(*arg + 1);
8034 while (**arg != '}' && **arg != NUL)
8035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008036 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 goto failret;
8038 if (**arg != ':')
8039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008040 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008041 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008042 goto failret;
8043 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008044 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008047 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008048 {
8049 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008050 clear_tv(&tvkey);
8051 goto failret;
8052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054
8055 *arg = skipwhite(*arg + 1);
8056 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8057 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008058 if (evaluate)
8059 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 goto failret;
8061 }
8062 if (evaluate)
8063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008064 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 if (item != NULL)
8066 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008067 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008068 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 clear_tv(&tv);
8070 goto failret;
8071 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008072 item = dictitem_alloc(key);
8073 clear_tv(&tvkey);
8074 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008077 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008078 if (dict_add(d, item) == FAIL)
8079 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008080 }
8081 }
8082
8083 if (**arg == '}')
8084 break;
8085 if (**arg != ',')
8086 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008087 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008088 goto failret;
8089 }
8090 *arg = skipwhite(*arg + 1);
8091 }
8092
8093 if (**arg != '}')
8094 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008095 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008096failret:
8097 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008098 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008099 return FAIL;
8100 }
8101
8102 *arg = skipwhite(*arg + 1);
8103 if (evaluate)
8104 {
8105 rettv->v_type = VAR_DICT;
8106 rettv->vval.v_dict = d;
8107 ++d->dv_refcount;
8108 }
8109
8110 return OK;
8111}
8112
Bram Moolenaar17a13432016-01-24 14:22:10 +01008113 static char *
8114get_var_special_name(int nr)
8115{
8116 switch (nr)
8117 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008118 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008119 case VVAL_TRUE: return "v:true";
8120 case VVAL_NONE: return "v:none";
8121 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008122 }
8123 EMSG2(_(e_intern2), "get_var_special_name()");
8124 return "42";
8125}
8126
Bram Moolenaar8c711452005-01-14 21:53:12 +00008127/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008128 * Return a string with the string representation of a variable.
8129 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008130 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008132 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8133 * "string()", otherwise does not put quotes around strings, as ":echo"
8134 * displays values.
8135 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8136 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008137 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138 */
8139 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008140echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008141 typval_T *tv,
8142 char_u **tofree,
8143 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008144 int copyID,
8145 int echo_style,
8146 int restore_copyID,
8147 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 static int recurse = 0;
8150 char_u *r = NULL;
8151
Bram Moolenaar33570922005-01-25 22:26:29 +00008152 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008154 if (!did_echo_string_emsg)
8155 {
8156 /* Only give this message once for a recursive call to avoid
8157 * flooding the user with errors. And stop iterating over lists
8158 * and dicts. */
8159 did_echo_string_emsg = TRUE;
8160 EMSG(_("E724: variable nested too deep for displaying"));
8161 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008162 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008163 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008164 }
8165 ++recurse;
8166
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167 switch (tv->v_type)
8168 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008169 case VAR_STRING:
8170 if (echo_style && !dict_val)
8171 {
8172 *tofree = NULL;
8173 r = get_tv_string_buf(tv, numbuf);
8174 }
8175 else
8176 {
8177 *tofree = string_quote(tv->vval.v_string, FALSE);
8178 r = *tofree;
8179 }
8180 break;
8181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008183 if (echo_style)
8184 {
8185 *tofree = NULL;
8186 r = tv->vval.v_string;
8187 }
8188 else
8189 {
8190 *tofree = string_quote(tv->vval.v_string, TRUE);
8191 r = *tofree;
8192 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008193 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008194
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008195 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008196 {
8197 partial_T *pt = tv->vval.v_partial;
8198 char_u *fname = string_quote(pt == NULL ? NULL
8199 : pt->pt_name, FALSE);
8200 garray_T ga;
8201 int i;
8202 char_u *tf;
8203
8204 ga_init2(&ga, 1, 100);
8205 ga_concat(&ga, (char_u *)"function(");
8206 if (fname != NULL)
8207 {
8208 ga_concat(&ga, fname);
8209 vim_free(fname);
8210 }
8211 if (pt != NULL && pt->pt_argc > 0)
8212 {
8213 ga_concat(&ga, (char_u *)", [");
8214 for (i = 0; i < pt->pt_argc; ++i)
8215 {
8216 if (i > 0)
8217 ga_concat(&ga, (char_u *)", ");
8218 ga_concat(&ga,
8219 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8220 vim_free(tf);
8221 }
8222 ga_concat(&ga, (char_u *)"]");
8223 }
8224 if (pt != NULL && pt->pt_dict != NULL)
8225 {
8226 typval_T dtv;
8227
8228 ga_concat(&ga, (char_u *)", ");
8229 dtv.v_type = VAR_DICT;
8230 dtv.vval.v_dict = pt->pt_dict;
8231 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8232 vim_free(tf);
8233 }
8234 ga_concat(&ga, (char_u *)")");
8235
8236 *tofree = ga.ga_data;
8237 r = *tofree;
8238 break;
8239 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008240
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008241 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008242 if (tv->vval.v_list == NULL)
8243 {
8244 *tofree = NULL;
8245 r = NULL;
8246 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008247 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8248 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008249 {
8250 *tofree = NULL;
8251 r = (char_u *)"[...]";
8252 }
8253 else
8254 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008255 int old_copyID = tv->vval.v_list->lv_copyID;
8256
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008258 *tofree = list2string(tv, copyID, restore_copyID);
8259 if (restore_copyID)
8260 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008261 r = *tofree;
8262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008263 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008264
Bram Moolenaar8c711452005-01-14 21:53:12 +00008265 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008266 if (tv->vval.v_dict == NULL)
8267 {
8268 *tofree = NULL;
8269 r = NULL;
8270 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008271 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8272 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008273 {
8274 *tofree = NULL;
8275 r = (char_u *)"{...}";
8276 }
8277 else
8278 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008279 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008280 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008281 *tofree = dict2string(tv, copyID, restore_copyID);
8282 if (restore_copyID)
8283 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008284 r = *tofree;
8285 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008287
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008288 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008289 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008290 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008291 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 *tofree = NULL;
8293 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008294 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008296 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008297#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298 *tofree = NULL;
8299 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8300 r = numbuf;
8301 break;
8302#endif
8303
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008304 case VAR_SPECIAL:
8305 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008306 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008307 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008308 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008309
Bram Moolenaar8502c702014-06-17 12:51:16 +02008310 if (--recurse == 0)
8311 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008312 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008313}
8314
8315/*
8316 * Return a string with the string representation of a variable.
8317 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8318 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008319 * Does not put quotes around strings, as ":echo" displays values.
8320 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8321 * May return NULL.
8322 */
8323 static char_u *
8324echo_string(
8325 typval_T *tv,
8326 char_u **tofree,
8327 char_u *numbuf,
8328 int copyID)
8329{
8330 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8331}
8332
8333/*
8334 * Return a string with the string representation of a variable.
8335 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8336 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008337 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008338 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008339 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008341tv2string(
8342 typval_T *tv,
8343 char_u **tofree,
8344 char_u *numbuf,
8345 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008346{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008347 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008348}
8349
8350/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 * Return string "str" in ' quotes, doubling ' characters.
8352 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008354 */
8355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008356string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008357{
Bram Moolenaar33570922005-01-25 22:26:29 +00008358 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008359 char_u *p, *r, *s;
8360
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 len = (function ? 13 : 3);
8362 if (str != NULL)
8363 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008364 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008365 for (p = str; *p != NUL; mb_ptr_adv(p))
8366 if (*p == '\'')
8367 ++len;
8368 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008369 s = r = alloc(len);
8370 if (r != NULL)
8371 {
8372 if (function)
8373 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008375 r += 10;
8376 }
8377 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008378 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 if (str != NULL)
8380 for (p = str; *p != NUL; )
8381 {
8382 if (*p == '\'')
8383 *r++ = '\'';
8384 MB_COPY_CHAR(p, r);
8385 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008386 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008387 if (function)
8388 *r++ = ')';
8389 *r++ = NUL;
8390 }
8391 return s;
8392}
8393
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008394#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395/*
8396 * Convert the string "text" to a floating point number.
8397 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8398 * this always uses a decimal point.
8399 * Returns the length of the text that was consumed.
8400 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008402string2float(
8403 char_u *text,
8404 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008405{
8406 char *s = (char *)text;
8407 float_T f;
8408
8409 f = strtod(s, &s);
8410 *value = f;
8411 return (int)((char_u *)s - text);
8412}
8413#endif
8414
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 * Get the value of an environment variable.
8417 * "arg" is pointing to the '$'. It is advanced to after the name.
8418 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008419 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 */
8421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008422get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 char_u *string = NULL;
8425 int len;
8426 int cc;
8427 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008428 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429
8430 ++*arg;
8431 name = *arg;
8432 len = get_env_len(arg);
8433 if (evaluate)
8434 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008435 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008436 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008437
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008438 cc = name[len];
8439 name[len] = NUL;
8440 /* first try vim_getenv(), fast for normal environment vars */
8441 string = vim_getenv(name, &mustfree);
8442 if (string != NULL && *string != NUL)
8443 {
8444 if (!mustfree)
8445 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008447 else
8448 {
8449 if (mustfree)
8450 vim_free(string);
8451
8452 /* next try expanding things like $VIM and ${HOME} */
8453 string = expand_env_save(name - 1);
8454 if (string != NULL && *string == '$')
8455 {
8456 vim_free(string);
8457 string = NULL;
8458 }
8459 }
8460 name[len] = cc;
8461
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->v_type = VAR_STRING;
8463 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 }
8465
8466 return OK;
8467}
8468
8469/*
8470 * Array with names and number of arguments of all internal functions
8471 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8472 */
8473static struct fst
8474{
8475 char *f_name; /* function name */
8476 char f_min_argc; /* minimal number of arguments */
8477 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008478 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008479 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480} functions[] =
8481{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008482#ifdef FEAT_FLOAT
8483 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008484 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008485#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008486 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008487 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"append", 2, 2, f_append},
8489 {"argc", 0, 0, f_argc},
8490 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008491 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008492 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008493#ifdef FEAT_FLOAT
8494 {"asin", 1, 1, f_asin}, /* WJMc */
8495#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008496 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008497 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008498 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008499 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008500 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008501 {"assert_notequal", 2, 3, f_assert_notequal},
8502 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008503 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008504#ifdef FEAT_FLOAT
8505 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008509 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"bufexists", 1, 1, f_bufexists},
8511 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8512 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8513 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8514 {"buflisted", 1, 1, f_buflisted},
8515 {"bufloaded", 1, 1, f_bufloaded},
8516 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008517 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008518 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"bufwinnr", 1, 1, f_bufwinnr},
8520 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008521 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008522 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008523 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008524#ifdef FEAT_FLOAT
8525 {"ceil", 1, 1, f_ceil},
8526#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008527#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008528 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008529 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8530 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008531 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008532 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008533 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008534 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008535 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008536 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008537 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008538 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008539 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8540 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008541 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008542 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008543#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008544 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008545 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008547 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008549#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008550 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008551 {"complete_add", 1, 1, f_complete_add},
8552 {"complete_check", 0, 0, f_complete_check},
8553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008555 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008556#ifdef FEAT_FLOAT
8557 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008558 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008559#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008560 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008562 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008564 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008566 {"diff_filler", 1, 1, f_diff_filler},
8567 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008568 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008570 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"eventhandler", 0, 0, f_eventhandler},
8572 {"executable", 1, 1, f_executable},
Bram Moolenaar79815f12016-07-09 17:07:29 +02008573 {"execute", 1, 2, f_execute},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008574 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008576#ifdef FEAT_FLOAT
8577 {"exp", 1, 1, f_exp},
8578#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008579 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008580 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008581 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8583 {"filereadable", 1, 1, f_filereadable},
8584 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008585 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008586 {"finddir", 1, 3, f_finddir},
8587 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#ifdef FEAT_FLOAT
8589 {"float2nr", 1, 1, f_float2nr},
8590 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008591 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008592#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008593 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"fnamemodify", 2, 2, f_fnamemodify},
8595 {"foldclosed", 1, 1, f_foldclosed},
8596 {"foldclosedend", 1, 1, f_foldclosedend},
8597 {"foldlevel", 1, 1, f_foldlevel},
8598 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008599 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008601 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008602 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008603 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008604 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008605 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {"getchar", 0, 1, f_getchar},
8607 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008608 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {"getcmdline", 0, 0, f_getcmdline},
8610 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008611 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008612 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaaraa4d7322016-07-09 18:50:29 +02008613#if defined(FEAT_CMDL_COMPL)
8614 {"getcompletion", 2, 2, f_getcompletion},
8615#endif
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008616 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008617 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008618 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008619 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {"getfsize", 1, 1, f_getfsize},
8621 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008622 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008623 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008624 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008625 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008626 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008627 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008628 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008629 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008631 {"gettabvar", 2, 3, f_gettabvar},
8632 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {"getwinposx", 0, 0, f_getwinposx},
8634 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008635 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008636 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008637 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008638 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008641 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008642 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8644 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8645 {"histadd", 2, 2, f_histadd},
8646 {"histdel", 1, 2, f_histdel},
8647 {"histget", 1, 2, f_histget},
8648 {"histnr", 1, 1, f_histnr},
8649 {"hlID", 1, 1, f_hlID},
8650 {"hlexists", 1, 1, f_hlexists},
8651 {"hostname", 0, 0, f_hostname},
8652 {"iconv", 3, 3, f_iconv},
8653 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008654 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008655 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008657 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 {"inputrestore", 0, 0, f_inputrestore},
8659 {"inputsave", 0, 0, f_inputsave},
8660 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008662 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008664 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8666 {"isnan", 1, 1, f_isnan},
8667#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008668 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008669#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008670 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008671 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008672 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008673 {"job_start", 1, 2, f_job_start},
8674 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008675 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008676#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008677 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008678 {"js_decode", 1, 1, f_js_decode},
8679 {"js_encode", 1, 1, f_js_encode},
8680 {"json_decode", 1, 1, f_json_decode},
8681 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008682 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {"libcall", 3, 3, f_libcall},
8686 {"libcallnr", 3, 3, f_libcallnr},
8687 {"line", 1, 1, f_line},
8688 {"line2byte", 1, 1, f_line2byte},
8689 {"lispindent", 1, 1, f_lispindent},
8690 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008691#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008692 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693 {"log10", 1, 1, f_log10},
8694#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008695#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008696 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008697#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008698 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008699 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008700 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008702 {"matchadd", 2, 5, f_matchadd},
8703 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008704 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008705 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008706 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008707 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008708 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008709 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008710 {"max", 1, 1, f_max},
8711 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008712#ifdef vim_mkdir
8713 {"mkdir", 1, 3, f_mkdir},
8714#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008715 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008716#ifdef FEAT_MZSCHEME
8717 {"mzeval", 1, 1, f_mzeval},
8718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008720 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008721 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008722 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008723#ifdef FEAT_PERL
8724 {"perleval", 1, 1, f_perleval},
8725#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008726#ifdef FEAT_FLOAT
8727 {"pow", 2, 2, f_pow},
8728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008730 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008731 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008732#ifdef FEAT_PYTHON3
8733 {"py3eval", 1, 1, f_py3eval},
8734#endif
8735#ifdef FEAT_PYTHON
8736 {"pyeval", 1, 1, f_pyeval},
8737#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008738 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008739 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008740 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008741#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008742 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008743#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008744 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {"remote_expr", 2, 3, f_remote_expr},
8746 {"remote_foreground", 1, 1, f_remote_foreground},
8747 {"remote_peek", 1, 2, f_remote_peek},
8748 {"remote_read", 1, 1, f_remote_read},
8749 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008750 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008752 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008755#ifdef FEAT_FLOAT
8756 {"round", 1, 1, f_round},
8757#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008758 {"screenattr", 2, 2, f_screenattr},
8759 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008760 {"screencol", 0, 0, f_screencol},
8761 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008762 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008763 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008764 {"searchpair", 3, 7, f_searchpair},
8765 {"searchpairpos", 3, 7, f_searchpairpos},
8766 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 {"server2client", 2, 2, f_server2client},
8768 {"serverlist", 0, 0, f_serverlist},
8769 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008770 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008772 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008774 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008775 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008776 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008777 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008779 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008780 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008782#ifdef FEAT_CRYPT
8783 {"sha256", 1, 1, f_sha256},
8784#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008785 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008786 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008788#ifdef FEAT_FLOAT
8789 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008790 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008792 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008793 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008794 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008795 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008796 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008797#ifdef FEAT_FLOAT
8798 {"sqrt", 1, 1, f_sqrt},
8799 {"str2float", 1, 1, f_str2float},
8800#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008801 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008802 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008803 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008804 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805#ifdef HAVE_STRFTIME
8806 {"strftime", 1, 2, f_strftime},
8807#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008808 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008810 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 {"strlen", 1, 1, f_strlen},
8812 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008813 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008815 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008816 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {"substitute", 4, 4, f_substitute},
8818 {"synID", 3, 3, f_synID},
8819 {"synIDattr", 2, 3, f_synIDattr},
8820 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008821 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008822 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008823 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008824 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008825 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008826 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008827 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008828 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008829 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008830#ifdef FEAT_FLOAT
8831 {"tan", 1, 1, f_tan},
8832 {"tanh", 1, 1, f_tanh},
8833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008835 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
Bram Moolenaar5c719942016-07-09 23:40:45 +02008836 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008837 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008838 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8839#ifdef FEAT_JOB_CHANNEL
8840 {"test_null_channel", 0, 0, f_test_null_channel},
8841#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008842 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008843#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008844 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008845#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008846 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008847 {"test_null_partial", 0, 0, f_test_null_partial},
8848 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008849 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008850#ifdef FEAT_TIMERS
8851 {"timer_start", 2, 3, f_timer_start},
8852 {"timer_stop", 1, 1, f_timer_stop},
8853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {"tolower", 1, 1, f_tolower},
8855 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008856 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008857#ifdef FEAT_FLOAT
8858 {"trunc", 1, 1, f_trunc},
8859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008861 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008862 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008863 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008864 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {"virtcol", 1, 1, f_virtcol},
8866 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008867 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008868 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008869 {"win_getid", 0, 2, f_win_getid},
8870 {"win_gotoid", 1, 1, f_win_gotoid},
8871 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8872 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {"winbufnr", 1, 1, f_winbufnr},
8874 {"wincol", 0, 0, f_wincol},
8875 {"winheight", 1, 1, f_winheight},
8876 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008877 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008879 {"winrestview", 1, 1, f_winrestview},
8880 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008882 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008883 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008884 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885};
8886
8887#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8888
8889/*
8890 * Function given to ExpandGeneric() to obtain the list of internal
8891 * or user defined function names.
8892 */
8893 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 static int intidx = -1;
8897 char_u *name;
8898
8899 if (idx == 0)
8900 intidx = -1;
8901 if (intidx < 0)
8902 {
8903 name = get_user_func_name(xp, idx);
8904 if (name != NULL)
8905 return name;
8906 }
8907 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8908 {
8909 STRCPY(IObuff, functions[intidx].f_name);
8910 STRCAT(IObuff, "(");
8911 if (functions[intidx].f_max_argc == 0)
8912 STRCAT(IObuff, ")");
8913 return IObuff;
8914 }
8915
8916 return NULL;
8917}
8918
8919/*
8920 * Function given to ExpandGeneric() to obtain the list of internal or
8921 * user defined variable or function names.
8922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008924get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
8926 static int intidx = -1;
8927 char_u *name;
8928
8929 if (idx == 0)
8930 intidx = -1;
8931 if (intidx < 0)
8932 {
8933 name = get_function_name(xp, idx);
8934 if (name != NULL)
8935 return name;
8936 }
8937 return get_user_var_name(xp, ++intidx);
8938}
8939
8940#endif /* FEAT_CMDL_COMPL */
8941
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008942#if defined(EBCDIC) || defined(PROTO)
8943/*
8944 * Compare struct fst by function name.
8945 */
8946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008947compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008948{
8949 struct fst *p1 = (struct fst *)s1;
8950 struct fst *p2 = (struct fst *)s2;
8951
8952 return STRCMP(p1->f_name, p2->f_name);
8953}
8954
8955/*
8956 * Sort the function table by function name.
8957 * The sorting of the table above is ASCII dependant.
8958 * On machines using EBCDIC we have to sort it.
8959 */
8960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008961sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008962{
8963 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8964
8965 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8966}
8967#endif
8968
8969
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970/*
8971 * Find internal function in table above.
8972 * Return index, or -1 if not found
8973 */
8974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975find_internal_func(
8976 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 int first = 0;
8979 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8980 int cmp;
8981 int x;
8982
8983 /*
8984 * Find the function name in the table. Binary search.
8985 */
8986 while (first <= last)
8987 {
8988 x = first + ((unsigned)(last - first) >> 1);
8989 cmp = STRCMP(name, functions[x].f_name);
8990 if (cmp < 0)
8991 last = x - 1;
8992 else if (cmp > 0)
8993 first = x + 1;
8994 else
8995 return x;
8996 }
8997 return -1;
8998}
8999
9000/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009001 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
9002 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01009003 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
9004 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 */
9006 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01009007deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009008{
Bram Moolenaar33570922005-01-25 22:26:29 +00009009 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 int cc;
9011
Bram Moolenaar65639032016-03-16 21:40:30 +01009012 if (partialp != NULL)
9013 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009014
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009015 cc = name[*lenp];
9016 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009017 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009018 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009022 {
9023 *lenp = 0;
9024 return (char_u *)""; /* just in case */
9025 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009026 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009027 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009028 }
9029
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009030 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9031 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009032 partial_T *pt = v->di_tv.vval.v_partial;
9033
9034 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 {
9036 *lenp = 0;
9037 return (char_u *)""; /* just in case */
9038 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009039 if (partialp != NULL)
9040 *partialp = pt;
9041 *lenp = (int)STRLEN(pt->pt_name);
9042 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 }
9044
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009045 return name;
9046}
9047
9048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 * Allocate a variable for the result of a function.
9050 * Return OK or FAIL.
9051 */
9052 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009053get_func_tv(
9054 char_u *name, /* name of the function */
9055 int len, /* length of "name" */
9056 typval_T *rettv,
9057 char_u **arg, /* argument, pointing to the '(' */
9058 linenr_T firstline, /* first line of range */
9059 linenr_T lastline, /* last line of range */
9060 int *doesrange, /* return: function handled range */
9061 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009062 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064{
9065 char_u *argp;
9066 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009067 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int argcount = 0; /* number of arguments found */
9069
9070 /*
9071 * Get the arguments.
9072 */
9073 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009074 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 {
9076 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9077 if (*argp == ')' || *argp == ',' || *argp == NUL)
9078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9080 {
9081 ret = FAIL;
9082 break;
9083 }
9084 ++argcount;
9085 if (*argp != ',')
9086 break;
9087 }
9088 if (*argp == ')')
9089 ++argp;
9090 else
9091 ret = FAIL;
9092
9093 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009094 {
9095 int i = 0;
9096
9097 if (get_vim_var_nr(VV_TESTING))
9098 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009099 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009100 * what variables are used on the call stack. */
9101 if (funcargs.ga_itemsize == 0)
9102 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9103 for (i = 0; i < argcount; ++i)
9104 if (ga_grow(&funcargs, 1) == OK)
9105 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9106 &argvars[i];
9107 }
9108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009110 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009111
9112 funcargs.ga_len -= i;
9113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009115 {
9116 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009117 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009118 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009119 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
9122 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
9125 *arg = skipwhite(argp);
9126 return ret;
9127}
9128
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009129#define ERROR_UNKNOWN 0
9130#define ERROR_TOOMANY 1
9131#define ERROR_TOOFEW 2
9132#define ERROR_SCRIPT 3
9133#define ERROR_DICT 4
9134#define ERROR_NONE 5
9135#define ERROR_OTHER 6
9136#define FLEN_FIXED 40
9137
9138/*
9139 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9140 * Change <SNR>123_name() to K_SNR 123_name().
9141 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9142 * (slow).
9143 */
9144 static char_u *
9145fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9146{
9147 int llen;
9148 char_u *fname;
9149 int i;
9150
9151 llen = eval_fname_script(name);
9152 if (llen > 0)
9153 {
9154 fname_buf[0] = K_SPECIAL;
9155 fname_buf[1] = KS_EXTRA;
9156 fname_buf[2] = (int)KE_SNR;
9157 i = 3;
9158 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9159 {
9160 if (current_SID <= 0)
9161 *error = ERROR_SCRIPT;
9162 else
9163 {
9164 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9165 i = (int)STRLEN(fname_buf);
9166 }
9167 }
9168 if (i + STRLEN(name + llen) < FLEN_FIXED)
9169 {
9170 STRCPY(fname_buf + i, name + llen);
9171 fname = fname_buf;
9172 }
9173 else
9174 {
9175 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9176 if (fname == NULL)
9177 *error = ERROR_OTHER;
9178 else
9179 {
9180 *tofree = fname;
9181 mch_memmove(fname, fname_buf, (size_t)i);
9182 STRCPY(fname + i, name + llen);
9183 }
9184 }
9185 }
9186 else
9187 fname = name;
9188 return fname;
9189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190
9191/*
9192 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009193 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009194 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009196 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009197call_func(
9198 char_u *funcname, /* name of the function */
9199 int len, /* length of "name" */
9200 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009201 int argcount_in, /* number of "argvars" */
9202 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009203 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204 linenr_T firstline, /* first line of range */
9205 linenr_T lastline, /* last line of range */
9206 int *doesrange, /* return: function handled range */
9207 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009208 partial_T *partial, /* optional, can be NULL */
9209 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 int error = ERROR_NONE;
9213 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009216 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009218 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009219 int argcount = argcount_in;
9220 typval_T *argvars = argvars_in;
9221 dict_T *selfdict = selfdict_in;
9222 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9223 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009224
9225 /* Make a copy of the name, if it comes from a funcref variable it could
9226 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009227 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009228 if (name == NULL)
9229 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009231 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 *doesrange = FALSE;
9234
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 if (partial != NULL)
9236 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009237 /* When the function has a partial with a dict and there is a dict
9238 * argument, use the dict argument. That is backwards compatible.
9239 * When the dict was bound explicitly use the one from the partial. */
9240 if (partial->pt_dict != NULL
9241 && (selfdict_in == NULL || !partial->pt_auto))
9242 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009243 if (error == ERROR_NONE && partial->pt_argc > 0)
9244 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009245 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9246 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9247 for (i = 0; i < argcount_in; ++i)
9248 argv[i + argv_clear] = argvars_in[i];
9249 argvars = argv;
9250 argcount = partial->pt_argc + argcount_in;
9251 }
9252 }
9253
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254
9255 /* execute the function if no errors detected and executing */
9256 if (evaluate && error == ERROR_NONE)
9257 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009258 char_u *rfname = fname;
9259
9260 /* Ignore "g:" before a function name. */
9261 if (fname[0] == 'g' && fname[1] == ':')
9262 rfname = fname + 2;
9263
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009264 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9265 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 error = ERROR_UNKNOWN;
9267
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009268 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 {
9270 /*
9271 * User defined function.
9272 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009273 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009276 /* Trigger FuncUndefined event, may load the function. */
9277 if (fp == NULL
9278 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009279 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009282 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009283 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 }
9285#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009286 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009287 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009288 {
9289 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009290 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009291 }
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 if (fp != NULL)
9294 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009295 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009297 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009299 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009301 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009302 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 else
9304 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009305 int did_save_redo = FALSE;
9306
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 /*
9308 * Call the user function.
9309 * Save and restore search patterns, script variables and
9310 * redo buffer.
9311 */
9312 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009313#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009314 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009315#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009316 {
9317 saveRedobuff();
9318 did_save_redo = TRUE;
9319 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009320 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009322 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009323 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9324 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9325 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009326 /* Function was unreferenced while being used, free it
9327 * now. */
9328 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009329 if (did_save_redo)
9330 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 restore_search_patterns();
9332 error = ERROR_NONE;
9333 }
9334 }
9335 }
9336 else
9337 {
9338 /*
9339 * Find the function name in the table, call its implementation.
9340 */
9341 i = find_internal_func(fname);
9342 if (i >= 0)
9343 {
9344 if (argcount < functions[i].f_min_argc)
9345 error = ERROR_TOOFEW;
9346 else if (argcount > functions[i].f_max_argc)
9347 error = ERROR_TOOMANY;
9348 else
9349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009350 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 error = ERROR_NONE;
9353 }
9354 }
9355 }
9356 /*
9357 * The function call (or "FuncUndefined" autocommand sequence) might
9358 * have been aborted by an error, an interrupt, or an explicitly thrown
9359 * exception that has not been caught so far. This situation can be
9360 * tested for by calling aborting(). For an error in an internal
9361 * function or for the "E132" error in call_user_func(), however, the
9362 * throw point at which the "force_abort" flag (temporarily reset by
9363 * emsg()) is normally updated has not been reached yet. We need to
9364 * update that flag first to make aborting() reliable.
9365 */
9366 update_force_abort();
9367 }
9368 if (error == ERROR_NONE)
9369 ret = OK;
9370
9371 /*
9372 * Report an error unless the argument evaluation or function call has been
9373 * cancelled due to an aborting error, an interrupt, or an exception.
9374 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009375 if (!aborting())
9376 {
9377 switch (error)
9378 {
9379 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009380 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009381 break;
9382 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009383 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009384 break;
9385 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009387 name);
9388 break;
9389 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009390 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009391 name);
9392 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009393 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009394 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009395 name);
9396 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009397 }
9398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009400 while (argv_clear > 0)
9401 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009402 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009403 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
9405 return ret;
9406}
9407
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009408/*
9409 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009410 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009411 */
9412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009413emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009414{
9415 char_u *p;
9416
9417 if (*name == K_SPECIAL)
9418 p = concat_str((char_u *)"<SNR>", name + 3);
9419 else
9420 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009421 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009422 if (p != name)
9423 vim_free(p);
9424}
9425
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009426/*
9427 * Return TRUE for a non-zero Number and a non-empty String.
9428 */
9429 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009431{
9432 return ((argvars[0].v_type == VAR_NUMBER
9433 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009434 || (argvars[0].v_type == VAR_SPECIAL
9435 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009436 || (argvars[0].v_type == VAR_STRING
9437 && argvars[0].vval.v_string != NULL
9438 && *argvars[0].vval.v_string != NUL));
9439}
9440
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441/*********************************************
9442 * Implementation of the built-in functions
9443 */
9444
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009445#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009446static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009447
9448/*
9449 * Get the float value of "argvars[0]" into "f".
9450 * Returns FAIL when the argument is not a Number or Float.
9451 */
9452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009454{
9455 if (argvars[0].v_type == VAR_FLOAT)
9456 {
9457 *f = argvars[0].vval.v_float;
9458 return OK;
9459 }
9460 if (argvars[0].v_type == VAR_NUMBER)
9461 {
9462 *f = (float_T)argvars[0].vval.v_number;
9463 return OK;
9464 }
9465 EMSG(_("E808: Number or Float required"));
9466 return FAIL;
9467}
9468
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009469/*
9470 * "abs(expr)" function
9471 */
9472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009473f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009474{
9475 if (argvars[0].v_type == VAR_FLOAT)
9476 {
9477 rettv->v_type = VAR_FLOAT;
9478 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9479 }
9480 else
9481 {
9482 varnumber_T n;
9483 int error = FALSE;
9484
9485 n = get_tv_number_chk(&argvars[0], &error);
9486 if (error)
9487 rettv->vval.v_number = -1;
9488 else if (n > 0)
9489 rettv->vval.v_number = n;
9490 else
9491 rettv->vval.v_number = -n;
9492 }
9493}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494
9495/*
9496 * "acos()" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009501 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009502
9503 rettv->v_type = VAR_FLOAT;
9504 if (get_float_arg(argvars, &f) == OK)
9505 rettv->vval.v_float = acos(f);
9506 else
9507 rettv->vval.v_float = 0.0;
9508}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009509#endif
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009512 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 */
9514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009515f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516{
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009522 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009523 && !tv_check_lock(l->lv_lock,
9524 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009525 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009527 }
9528 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009529 EMSG(_(e_listreq));
9530}
9531
9532/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009533 * "and(expr, expr)" function
9534 */
9535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009537{
9538 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9539 & get_tv_number_chk(&argvars[1], NULL);
9540}
9541
9542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009543 * "append(lnum, string/list)" function
9544 */
9545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009546f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009547{
9548 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 list_T *l = NULL;
9551 listitem_T *li = NULL;
9552 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553 long added = 0;
9554
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009555 /* When coming here from Insert mode, sync undo, so that this can be
9556 * undone separately from what was previously inserted. */
9557 if (u_sync_once == 2)
9558 {
9559 u_sync_once = 1; /* notify that u_sync() was called */
9560 u_sync(TRUE);
9561 }
9562
Bram Moolenaar0d660222005-01-07 21:51:51 +00009563 lnum = get_tv_lnum(argvars);
9564 if (lnum >= 0
9565 && lnum <= curbuf->b_ml.ml_line_count
9566 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009568 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009570 l = argvars[1].vval.v_list;
9571 if (l == NULL)
9572 return;
9573 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009575 for (;;)
9576 {
9577 if (l == NULL)
9578 tv = &argvars[1]; /* append a string */
9579 else if (li == NULL)
9580 break; /* end of list */
9581 else
9582 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 line = get_tv_string_chk(tv);
9584 if (line == NULL) /* type error */
9585 {
9586 rettv->vval.v_number = 1; /* Failed */
9587 break;
9588 }
9589 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009590 ++added;
9591 if (l == NULL)
9592 break;
9593 li = li->li_next;
9594 }
9595
9596 appended_lines_mark(lnum, added);
9597 if (curwin->w_cursor.lnum > lnum)
9598 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 else
9601 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602}
9603
9604/*
9605 * "argc()" function
9606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009608f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009610 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
9613/*
9614 * "argidx()" function
9615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009623 * "arglistid()" function
9624 */
9625 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009626f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009627{
9628 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009629
9630 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009631 wp = find_tabwin(&argvars[0], &argvars[1]);
9632 if (wp != NULL)
9633 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009634}
9635
9636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 * "argv(nr)" function
9638 */
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 int idx;
9643
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009644 if (argvars[0].v_type != VAR_UNKNOWN)
9645 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009646 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009647 if (idx >= 0 && idx < ARGCOUNT)
9648 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9649 else
9650 rettv->vval.v_string = NULL;
9651 rettv->v_type = VAR_STRING;
9652 }
9653 else if (rettv_list_alloc(rettv) == OK)
9654 for (idx = 0; idx < ARGCOUNT; ++idx)
9655 list_append_string(rettv->vval.v_list,
9656 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657}
9658
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009659typedef enum
9660{
9661 ASSERT_EQUAL,
9662 ASSERT_NOTEQUAL,
9663 ASSERT_MATCH,
9664 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009665 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009666} assert_type_T;
9667
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009668static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009670static void assert_error(garray_T *gap);
9671static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009672
9673/*
9674 * Prepare "gap" for an assert error and add the sourcing position.
9675 */
9676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009677prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009678{
9679 char buf[NUMBUFLEN];
9680
9681 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009682 if (sourcing_name != NULL)
9683 {
9684 ga_concat(gap, sourcing_name);
9685 if (sourcing_lnum > 0)
9686 ga_concat(gap, (char_u *)" ");
9687 }
9688 if (sourcing_lnum > 0)
9689 {
9690 sprintf(buf, "line %ld", (long)sourcing_lnum);
9691 ga_concat(gap, (char_u *)buf);
9692 }
9693 if (sourcing_name != NULL || sourcing_lnum > 0)
9694 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009695}
9696
9697/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009698 * Append "str" to "gap", escaping unprintable characters.
9699 * Changes NL to \n, CR to \r, etc.
9700 */
9701 static void
9702ga_concat_esc(garray_T *gap, char_u *str)
9703{
9704 char_u *p;
9705 char_u buf[NUMBUFLEN];
9706
Bram Moolenaarf1551962016-03-15 12:55:58 +01009707 if (str == NULL)
9708 {
9709 ga_concat(gap, (char_u *)"NULL");
9710 return;
9711 }
9712
Bram Moolenaar23689172016-02-15 22:37:37 +01009713 for (p = str; *p != NUL; ++p)
9714 switch (*p)
9715 {
9716 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9717 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9718 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9719 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9720 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9721 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9722 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9723 default:
9724 if (*p < ' ')
9725 {
9726 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9727 ga_concat(gap, buf);
9728 }
9729 else
9730 ga_append(gap, *p);
9731 break;
9732 }
9733}
9734
9735/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009736 * Fill "gap" with information about an assert error.
9737 */
9738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009739fill_assert_error(
9740 garray_T *gap,
9741 typval_T *opt_msg_tv,
9742 char_u *exp_str,
9743 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009744 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009745 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009746{
9747 char_u numbuf[NUMBUFLEN];
9748 char_u *tofree;
9749
9750 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9751 {
9752 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9753 vim_free(tofree);
9754 }
9755 else
9756 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009757 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009758 ga_concat(gap, (char_u *)"Pattern ");
9759 else
9760 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009761 if (exp_str == NULL)
9762 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009763 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009764 vim_free(tofree);
9765 }
9766 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009767 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009768 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009769 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009770 else if (atype == ASSERT_NOTMATCH)
9771 ga_concat(gap, (char_u *)" does match ");
9772 else if (atype == ASSERT_NOTEQUAL)
9773 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009774 else
9775 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009776 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009777 vim_free(tofree);
9778 }
9779}
Bram Moolenaar43345542015-11-29 17:35:35 +01009780
9781/*
9782 * Add an assert error to v:errors.
9783 */
9784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009785assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009786{
9787 struct vimvar *vp = &vimvars[VV_ERRORS];
9788
9789 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9790 /* Make sure v:errors is a list. */
9791 set_vim_var_list(VV_ERRORS, list_alloc());
9792 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9793}
9794
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009795 static void
9796assert_equal_common(typval_T *argvars, assert_type_T atype)
9797{
9798 garray_T ga;
9799
9800 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9801 != (atype == ASSERT_EQUAL))
9802 {
9803 prepare_assert_error(&ga);
9804 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9805 atype);
9806 assert_error(&ga);
9807 ga_clear(&ga);
9808 }
9809}
9810
Bram Moolenaar43345542015-11-29 17:35:35 +01009811/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009812 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009813 */
9814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009816{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009817 assert_equal_common(argvars, ASSERT_EQUAL);
9818}
Bram Moolenaar43345542015-11-29 17:35:35 +01009819
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009820/*
9821 * "assert_notequal(expected, actual[, msg])" function
9822 */
9823 static void
9824f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9825{
9826 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009827}
9828
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009829/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009830 * "assert_exception(string[, msg])" function
9831 */
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009834{
9835 garray_T ga;
9836 char *error;
9837
9838 error = (char *)get_tv_string_chk(&argvars[0]);
9839 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9840 {
9841 prepare_assert_error(&ga);
9842 ga_concat(&ga, (char_u *)"v:exception is not set");
9843 assert_error(&ga);
9844 ga_clear(&ga);
9845 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009846 else if (error != NULL
9847 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009848 {
9849 prepare_assert_error(&ga);
9850 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009851 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009852 assert_error(&ga);
9853 ga_clear(&ga);
9854 }
9855}
9856
9857/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009858 * "assert_fails(cmd [, error])" function
9859 */
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009862{
9863 char_u *cmd = get_tv_string_chk(&argvars[0]);
9864 garray_T ga;
9865
9866 called_emsg = FALSE;
9867 suppress_errthrow = TRUE;
9868 emsg_silent = TRUE;
9869 do_cmdline_cmd(cmd);
9870 if (!called_emsg)
9871 {
9872 prepare_assert_error(&ga);
9873 ga_concat(&ga, (char_u *)"command did not fail: ");
9874 ga_concat(&ga, cmd);
9875 assert_error(&ga);
9876 ga_clear(&ga);
9877 }
9878 else if (argvars[1].v_type != VAR_UNKNOWN)
9879 {
9880 char_u buf[NUMBUFLEN];
9881 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9882
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009883 if (error == NULL
9884 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009885 {
9886 prepare_assert_error(&ga);
9887 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009888 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009889 assert_error(&ga);
9890 ga_clear(&ga);
9891 }
9892 }
9893
9894 called_emsg = FALSE;
9895 suppress_errthrow = FALSE;
9896 emsg_silent = FALSE;
9897 emsg_on_display = FALSE;
9898 set_vim_var_string(VV_ERRMSG, NULL, 0);
9899}
9900
9901/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009902 * Common for assert_true() and assert_false().
9903 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009905assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009906{
9907 int error = FALSE;
9908 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009909
Bram Moolenaar37127922016-02-06 20:29:28 +01009910 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009911 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009912 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009913 if (argvars[0].v_type != VAR_NUMBER
9914 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9915 || error)
9916 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009917 prepare_assert_error(&ga);
9918 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009919 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009920 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009921 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009922 ga_clear(&ga);
9923 }
9924}
9925
9926/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009927 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009928 */
9929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009930f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009931{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009932 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009933}
9934
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009935 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009936assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009937{
9938 garray_T ga;
9939 char_u buf1[NUMBUFLEN];
9940 char_u buf2[NUMBUFLEN];
9941 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9942 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9943
Bram Moolenaar72188e92016-03-28 22:48:29 +02009944 if (pat == NULL || text == NULL)
9945 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009946 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009947 {
9948 prepare_assert_error(&ga);
9949 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009950 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009951 assert_error(&ga);
9952 ga_clear(&ga);
9953 }
9954}
9955
9956/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009957 * "assert_match(pattern, actual[, msg])" function
9958 */
9959 static void
9960f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9961{
9962 assert_match_common(argvars, ASSERT_MATCH);
9963}
9964
9965/*
9966 * "assert_notmatch(pattern, actual[, msg])" function
9967 */
9968 static void
9969f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9970{
9971 assert_match_common(argvars, ASSERT_NOTMATCH);
9972}
9973
9974/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009975 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009976 */
9977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009978f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009979{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009980 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009981}
9982
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009983#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009984/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009985 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009986 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009988f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009990 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009991
9992 rettv->v_type = VAR_FLOAT;
9993 if (get_float_arg(argvars, &f) == OK)
9994 rettv->vval.v_float = asin(f);
9995 else
9996 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009997}
9998
9999/*
10000 * "atan()" function
10001 */
10002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +010010005 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010006
10007 rettv->v_type = VAR_FLOAT;
10008 if (get_float_arg(argvars, &f) == OK)
10009 rettv->vval.v_float = atan(f);
10010 else
10011 rettv->vval.v_float = 0.0;
10012}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010013
10014/*
10015 * "atan2()" function
10016 */
10017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010019{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010020 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010021
10022 rettv->v_type = VAR_FLOAT;
10023 if (get_float_arg(argvars, &fx) == OK
10024 && get_float_arg(&argvars[1], &fy) == OK)
10025 rettv->vval.v_float = atan2(fx, fy);
10026 else
10027 rettv->vval.v_float = 0.0;
10028}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010029#endif
10030
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031/*
10032 * "browse(save, title, initdir, default)" function
10033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010035f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037#ifdef FEAT_BROWSE
10038 int save;
10039 char_u *title;
10040 char_u *initdir;
10041 char_u *defname;
10042 char_u buf[NUMBUFLEN];
10043 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010046 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010047 title = get_tv_string_chk(&argvars[1]);
10048 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10049 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010051 if (error || title == NULL || initdir == NULL || defname == NULL)
10052 rettv->vval.v_string = NULL;
10053 else
10054 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010055 do_browse(save ? BROWSE_SAVE : 0,
10056 title, defname, NULL, initdir, NULL, curbuf);
10057#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010059#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010061}
10062
10063/*
10064 * "browsedir(title, initdir)" function
10065 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010067f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010068{
10069#ifdef FEAT_BROWSE
10070 char_u *title;
10071 char_u *initdir;
10072 char_u buf[NUMBUFLEN];
10073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 title = get_tv_string_chk(&argvars[0]);
10075 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 if (title == NULL || initdir == NULL)
10078 rettv->vval.v_string = NULL;
10079 else
10080 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010081 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010088static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010089
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090/*
10091 * Find a buffer by number or exact name.
10092 */
10093 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010094find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
10096 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010098 if (avar->v_type == VAR_NUMBER)
10099 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010100 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010102 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010103 if (buf == NULL)
10104 {
10105 /* No full path name match, try a match with a URL or a "nofile"
10106 * buffer, these don't use the full path. */
10107 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10108 if (buf->b_fname != NULL
10109 && (path_with_url(buf->b_fname)
10110#ifdef FEAT_QUICKFIX
10111 || bt_nofile(buf)
10112#endif
10113 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010114 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010115 break;
10116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 }
10118 return buf;
10119}
10120
10121/*
10122 * "bufexists(expr)" function
10123 */
10124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010125f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010127 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128}
10129
10130/*
10131 * "buflisted(expr)" function
10132 */
10133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010134f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136 buf_T *buf;
10137
10138 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010139 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140}
10141
10142/*
10143 * "bufloaded(expr)" function
10144 */
10145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010146f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
10148 buf_T *buf;
10149
10150 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010151 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152}
10153
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010154 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010155buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 int save_magic;
10158 char_u *save_cpo;
10159 buf_T *buf;
10160
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10162 save_magic = p_magic;
10163 p_magic = TRUE;
10164 save_cpo = p_cpo;
10165 p_cpo = (char_u *)"";
10166
10167 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010168 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169
10170 p_magic = save_magic;
10171 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010172 return buf;
10173}
10174
10175/*
10176 * Get buffer by number or pattern.
10177 */
10178 static buf_T *
10179get_buf_tv(typval_T *tv, int curtab_only)
10180{
10181 char_u *name = tv->vval.v_string;
10182 buf_T *buf;
10183
10184 if (tv->v_type == VAR_NUMBER)
10185 return buflist_findnr((int)tv->vval.v_number);
10186 if (tv->v_type != VAR_STRING)
10187 return NULL;
10188 if (name == NULL || *name == NUL)
10189 return curbuf;
10190 if (name[0] == '$' && name[1] == NUL)
10191 return lastbuf;
10192
10193 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
10195 /* If not found, try expanding the name, like done for bufexists(). */
10196 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010197 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198
10199 return buf;
10200}
10201
10202/*
10203 * "bufname(expr)" function
10204 */
10205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207{
10208 buf_T *buf;
10209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010210 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010212 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010215 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 --emsg_off;
10219}
10220
10221/*
10222 * "bufnr(expr)" function
10223 */
10224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010225f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226{
10227 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010228 int error = FALSE;
10229 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010233 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010234 --emsg_off;
10235
10236 /* If the buffer isn't found and the second argument is not zero create a
10237 * new buffer. */
10238 if (buf == NULL
10239 && argvars[1].v_type != VAR_UNKNOWN
10240 && get_tv_number_chk(&argvars[1], &error) != 0
10241 && !error
10242 && (name = get_tv_string_chk(&argvars[0])) != NULL
10243 && !error)
10244 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010247 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250}
10251
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010253buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254{
10255#ifdef FEAT_WINDOWS
10256 win_T *wp;
10257 int winnr = 0;
10258#endif
10259 buf_T *buf;
10260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010263 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#ifdef FEAT_WINDOWS
10265 for (wp = firstwin; wp; wp = wp->w_next)
10266 {
10267 ++winnr;
10268 if (wp->w_buffer == buf)
10269 break;
10270 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010271 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010273 rettv->vval.v_number = (curwin->w_buffer == buf
10274 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275#endif
10276 --emsg_off;
10277}
10278
10279/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010280 * "bufwinid(nr)" function
10281 */
10282 static void
10283f_bufwinid(typval_T *argvars, typval_T *rettv)
10284{
10285 buf_win_common(argvars, rettv, FALSE);
10286}
10287
10288/*
10289 * "bufwinnr(nr)" function
10290 */
10291 static void
10292f_bufwinnr(typval_T *argvars, typval_T *rettv)
10293{
10294 buf_win_common(argvars, rettv, TRUE);
10295}
10296
10297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 * "byte2line(byte)" function
10299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302{
10303#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305#else
10306 long boff = 0;
10307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 (linenr_T)0, &boff);
10314#endif
10315}
10316
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010318byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010319{
10320#ifdef FEAT_MBYTE
10321 char_u *t;
10322#endif
10323 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010324 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010325
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 str = get_tv_string_chk(&argvars[0]);
10327 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010330 return;
10331
10332#ifdef FEAT_MBYTE
10333 t = str;
10334 for ( ; idx > 0; idx--)
10335 {
10336 if (*t == NUL) /* EOL reached */
10337 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010338 if (enc_utf8 && comp)
10339 t += utf_ptr2len(t);
10340 else
10341 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010342 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010343 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010344#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010345 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010346 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010347#endif
10348}
10349
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010350/*
10351 * "byteidx()" function
10352 */
10353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010354f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010355{
10356 byteidx(argvars, rettv, FALSE);
10357}
10358
10359/*
10360 * "byteidxcomp()" function
10361 */
10362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010364{
10365 byteidx(argvars, rettv, TRUE);
10366}
10367
Bram Moolenaardb913952012-06-29 12:54:53 +020010368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010369func_call(
10370 char_u *name,
10371 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010372 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010373 dict_T *selfdict,
10374 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010375{
10376 listitem_T *item;
10377 typval_T argv[MAX_FUNC_ARGS + 1];
10378 int argc = 0;
10379 int dummy;
10380 int r = 0;
10381
10382 for (item = args->vval.v_list->lv_first; item != NULL;
10383 item = item->li_next)
10384 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010385 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010386 {
10387 EMSG(_("E699: Too many arguments"));
10388 break;
10389 }
10390 /* Make a copy of each argument. This is needed to be able to set
10391 * v_lock to VAR_FIXED in the copy without changing the original list.
10392 */
10393 copy_tv(&item->li_tv, &argv[argc++]);
10394 }
10395
10396 if (item == NULL)
10397 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10398 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010399 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010400
10401 /* Free the arguments. */
10402 while (argc > 0)
10403 clear_tv(&argv[--argc]);
10404
10405 return r;
10406}
10407
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010408/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010409 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 */
10411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010412f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010413{
10414 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010415 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010416 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010417
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418 if (argvars[1].v_type != VAR_LIST)
10419 {
10420 EMSG(_(e_listreq));
10421 return;
10422 }
10423 if (argvars[1].vval.v_list == NULL)
10424 return;
10425
10426 if (argvars[0].v_type == VAR_FUNC)
10427 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010428 else if (argvars[0].v_type == VAR_PARTIAL)
10429 {
10430 partial = argvars[0].vval.v_partial;
10431 func = partial->pt_name;
10432 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010433 else
10434 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010435 if (*func == NUL)
10436 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010437
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 if (argvars[2].v_type != VAR_UNKNOWN)
10439 {
10440 if (argvars[2].v_type != VAR_DICT)
10441 {
10442 EMSG(_(e_dictreq));
10443 return;
10444 }
10445 selfdict = argvars[2].vval.v_dict;
10446 }
10447
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010448 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010449}
10450
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451#ifdef FEAT_FLOAT
10452/*
10453 * "ceil({float})" function
10454 */
10455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010456f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010457{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010458 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010459
10460 rettv->v_type = VAR_FLOAT;
10461 if (get_float_arg(argvars, &f) == OK)
10462 rettv->vval.v_float = ceil(f);
10463 else
10464 rettv->vval.v_float = 0.0;
10465}
10466#endif
10467
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010468#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010469/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010470 * "ch_close()" function
10471 */
10472 static void
10473f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10474{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010475 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010476
10477 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010478 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010479 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010480 channel_clear(channel);
10481 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010482}
10483
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010484/*
10485 * "ch_getbufnr()" function
10486 */
10487 static void
10488f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10489{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010490 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010491
10492 rettv->vval.v_number = -1;
10493 if (channel != NULL)
10494 {
10495 char_u *what = get_tv_string(&argvars[1]);
10496 int part;
10497
10498 if (STRCMP(what, "err") == 0)
10499 part = PART_ERR;
10500 else if (STRCMP(what, "out") == 0)
10501 part = PART_OUT;
10502 else if (STRCMP(what, "in") == 0)
10503 part = PART_IN;
10504 else
10505 part = PART_SOCK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020010506 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
10507 rettv->vval.v_number =
10508 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010509 }
10510}
10511
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010512/*
10513 * "ch_getjob()" function
10514 */
10515 static void
10516f_ch_getjob(typval_T *argvars, typval_T *rettv)
10517{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010518 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010519
10520 if (channel != NULL)
10521 {
10522 rettv->v_type = VAR_JOB;
10523 rettv->vval.v_job = channel->ch_job;
10524 if (channel->ch_job != NULL)
10525 ++channel->ch_job->jv_refcount;
10526 }
10527}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010528
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010529/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010530 * "ch_info()" function
10531 */
10532 static void
10533f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10534{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010535 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010536
10537 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10538 channel_info(channel, rettv->vval.v_dict);
10539}
10540
10541/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010542 * "ch_log()" function
10543 */
10544 static void
10545f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10546{
10547 char_u *msg = get_tv_string(&argvars[0]);
10548 channel_T *channel = NULL;
10549
10550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010551 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010552
10553 ch_log(channel, (char *)msg);
10554}
10555
10556/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010557 * "ch_logfile()" function
10558 */
10559 static void
10560f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10561{
10562 char_u *fname;
10563 char_u *opt = (char_u *)"";
10564 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010565
10566 fname = get_tv_string(&argvars[0]);
10567 if (argvars[1].v_type == VAR_STRING)
10568 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010569 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010570}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010571
10572/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010573 * "ch_open()" function
10574 */
10575 static void
10576f_ch_open(typval_T *argvars, typval_T *rettv)
10577{
Bram Moolenaar77073442016-02-13 23:23:53 +010010578 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010579 if (check_restricted() || check_secure())
10580 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010581 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010582}
10583
10584/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010585 * "ch_read()" function
10586 */
10587 static void
10588f_ch_read(typval_T *argvars, typval_T *rettv)
10589{
10590 common_channel_read(argvars, rettv, FALSE);
10591}
10592
10593/*
10594 * "ch_readraw()" function
10595 */
10596 static void
10597f_ch_readraw(typval_T *argvars, typval_T *rettv)
10598{
10599 common_channel_read(argvars, rettv, TRUE);
10600}
10601
10602/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010603 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010604 */
10605 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010606f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10607{
10608 ch_expr_common(argvars, rettv, TRUE);
10609}
10610
10611/*
10612 * "ch_sendexpr()" function
10613 */
10614 static void
10615f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10616{
10617 ch_expr_common(argvars, rettv, FALSE);
10618}
10619
10620/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010621 * "ch_evalraw()" function
10622 */
10623 static void
10624f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10625{
10626 ch_raw_common(argvars, rettv, TRUE);
10627}
10628
10629/*
10630 * "ch_sendraw()" function
10631 */
10632 static void
10633f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10634{
10635 ch_raw_common(argvars, rettv, FALSE);
10636}
10637
10638/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010639 * "ch_setoptions()" function
10640 */
10641 static void
10642f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10643{
10644 channel_T *channel;
10645 jobopt_T opt;
10646
Bram Moolenaar437905c2016-04-26 19:01:05 +020010647 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010648 if (channel == NULL)
10649 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010650 clear_job_options(&opt);
10651 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010652 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10653 channel_set_options(channel, &opt);
10654 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010655}
10656
10657/*
10658 * "ch_status()" function
10659 */
10660 static void
10661f_ch_status(typval_T *argvars, typval_T *rettv)
10662{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010663 channel_T *channel;
10664
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010665 /* return an empty string by default */
10666 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010667 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010668
Bram Moolenaar437905c2016-04-26 19:01:05 +020010669 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010670 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010671}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010672#endif
10673
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010674/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010675 * "changenr()" function
10676 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010679{
10680 rettv->vval.v_number = curbuf->b_u_seq_cur;
10681}
10682
10683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 * "char2nr(string)" function
10685 */
10686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010687f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689#ifdef FEAT_MBYTE
10690 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010691 {
10692 int utf8 = 0;
10693
10694 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010695 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010696
10697 if (utf8)
10698 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10699 else
10700 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 else
10703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707/*
10708 * "cindent(lnum)" function
10709 */
10710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010711f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
10713#ifdef FEAT_CINDENT
10714 pos_T pos;
10715 linenr_T lnum;
10716
10717 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10720 {
10721 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010722 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 curwin->w_cursor = pos;
10724 }
10725 else
10726#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010731 * "clearmatches()" function
10732 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010735{
10736#ifdef FEAT_SEARCH_EXTRA
10737 clear_matches(curwin);
10738#endif
10739}
10740
10741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 * "col(string)" function
10743 */
10744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010745f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746{
10747 colnr_T col = 0;
10748 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010749 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010751 fp = var2fpos(&argvars[0], FALSE, &fnum);
10752 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 {
10754 if (fp->col == MAXCOL)
10755 {
10756 /* '> can be MAXCOL, get the length of the line then */
10757 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010758 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
10760 col = MAXCOL;
10761 }
10762 else
10763 {
10764 col = fp->col + 1;
10765#ifdef FEAT_VIRTUALEDIT
10766 /* col(".") when the cursor is on the NUL at the end of the line
10767 * because of "coladd" can be seen as an extra column. */
10768 if (virtual_active() && fp == &curwin->w_cursor)
10769 {
10770 char_u *p = ml_get_cursor();
10771
10772 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10773 curwin->w_virtcol - curwin->w_cursor.coladd))
10774 {
10775# ifdef FEAT_MBYTE
10776 int l;
10777
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010778 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 col += l;
10780# else
10781 if (*p != NUL && p[1] == NUL)
10782 ++col;
10783# endif
10784 }
10785 }
10786#endif
10787 }
10788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010789 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790}
10791
Bram Moolenaar572cb562005-08-05 21:35:02 +000010792#if defined(FEAT_INS_EXPAND)
10793/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010794 * "complete()" function
10795 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010797f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010798{
10799 int startcol;
10800
10801 if ((State & INSERT) == 0)
10802 {
10803 EMSG(_("E785: complete() can only be used in Insert mode"));
10804 return;
10805 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010806
10807 /* Check for undo allowed here, because if something was already inserted
10808 * the line was already saved for undo and this check isn't done. */
10809 if (!undo_allowed())
10810 return;
10811
Bram Moolenaarade00832006-03-10 21:46:58 +000010812 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10813 {
10814 EMSG(_(e_invarg));
10815 return;
10816 }
10817
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010818 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010819 if (startcol <= 0)
10820 return;
10821
10822 set_completion(startcol - 1, argvars[1].vval.v_list);
10823}
10824
10825/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010826 * "complete_add()" function
10827 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010830{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010831 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010832}
10833
10834/*
10835 * "complete_check()" function
10836 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010838f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010839{
10840 int saved = RedrawingDisabled;
10841
10842 RedrawingDisabled = 0;
10843 ins_compl_check_keys(0);
10844 rettv->vval.v_number = compl_interrupted;
10845 RedrawingDisabled = saved;
10846}
10847#endif
10848
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849/*
10850 * "confirm(message, buttons[, default [, type]])" function
10851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010853f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
10855#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10856 char_u *message;
10857 char_u *buttons = NULL;
10858 char_u buf[NUMBUFLEN];
10859 char_u buf2[NUMBUFLEN];
10860 int def = 1;
10861 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010862 char_u *typestr;
10863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 message = get_tv_string_chk(&argvars[0]);
10866 if (message == NULL)
10867 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10871 if (buttons == NULL)
10872 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010875 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010876 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10879 if (typestr == NULL)
10880 error = TRUE;
10881 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010883 switch (TOUPPER_ASC(*typestr))
10884 {
10885 case 'E': type = VIM_ERROR; break;
10886 case 'Q': type = VIM_QUESTION; break;
10887 case 'I': type = VIM_INFO; break;
10888 case 'W': type = VIM_WARNING; break;
10889 case 'G': type = VIM_GENERIC; break;
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 }
10892 }
10893 }
10894 }
10895
10896 if (buttons == NULL || *buttons == NUL)
10897 buttons = (char_u *)_("&Ok");
10898
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010899 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010901 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902#endif
10903}
10904
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010905/*
10906 * "copy()" function
10907 */
10908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010909f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010910{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010911 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010912}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010914#ifdef FEAT_FLOAT
10915/*
10916 * "cos()" function
10917 */
10918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010919f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010920{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010921 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010922
10923 rettv->v_type = VAR_FLOAT;
10924 if (get_float_arg(argvars, &f) == OK)
10925 rettv->vval.v_float = cos(f);
10926 else
10927 rettv->vval.v_float = 0.0;
10928}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010929
10930/*
10931 * "cosh()" function
10932 */
10933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010934f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010935{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010936 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010937
10938 rettv->v_type = VAR_FLOAT;
10939 if (get_float_arg(argvars, &f) == OK)
10940 rettv->vval.v_float = cosh(f);
10941 else
10942 rettv->vval.v_float = 0.0;
10943}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010944#endif
10945
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010947 * "count()" function
10948 */
10949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010950f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010951{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010952 long n = 0;
10953 int ic = FALSE;
10954
Bram Moolenaare9a41262005-01-15 22:18:47 +000010955 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010956 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010957 listitem_T *li;
10958 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010960
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 if ((l = argvars[0].vval.v_list) != NULL)
10962 {
10963 li = l->lv_first;
10964 if (argvars[2].v_type != VAR_UNKNOWN)
10965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 int error = FALSE;
10967
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010968 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 if (argvars[3].v_type != VAR_UNKNOWN)
10970 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010971 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 if (!error)
10973 {
10974 li = list_find(l, idx);
10975 if (li == NULL)
10976 EMSGN(_(e_listidx), idx);
10977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 if (error)
10980 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 }
10982
10983 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010984 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010985 ++n;
10986 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010988 else if (argvars[0].v_type == VAR_DICT)
10989 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 int todo;
10991 dict_T *d;
10992 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010993
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010994 if ((d = argvars[0].vval.v_dict) != NULL)
10995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 int error = FALSE;
10997
Bram Moolenaare9a41262005-01-15 22:18:47 +000010998 if (argvars[2].v_type != VAR_UNKNOWN)
10999 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011000 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011001 if (argvars[3].v_type != VAR_UNKNOWN)
11002 EMSG(_(e_invarg));
11003 }
11004
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011005 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011007 {
11008 if (!HASHITEM_EMPTY(hi))
11009 {
11010 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011011 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011012 ++n;
11013 }
11014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011015 }
11016 }
11017 else
11018 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011019 rettv->vval.v_number = n;
11020}
11021
11022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11024 *
11025 * Checks the existence of a cscope connection.
11026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011028f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029{
11030#ifdef FEAT_CSCOPE
11031 int num = 0;
11032 char_u *dbpath = NULL;
11033 char_u *prepend = NULL;
11034 char_u buf[NUMBUFLEN];
11035
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011036 if (argvars[0].v_type != VAR_UNKNOWN
11037 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 num = (int)get_tv_number(&argvars[0]);
11040 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 }
11044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046#endif
11047}
11048
11049/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011050 * "cursor(lnum, col)" function, or
11051 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011053 * Moves the cursor to the specified line and column.
11054 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011057f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058{
11059 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011060#ifdef FEAT_VIRTUALEDIT
11061 long coladd = 0;
11062#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011063 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011065 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011066 if (argvars[1].v_type == VAR_UNKNOWN)
11067 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011068 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011069 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011070
Bram Moolenaar493c1782014-05-28 14:34:46 +020011071 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011072 {
11073 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011074 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011075 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011076 line = pos.lnum;
11077 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011078#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011079 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011080#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011081 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011082 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011083 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011084 set_curswant = FALSE;
11085 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011086 }
11087 else
11088 {
11089 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011090 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011091#ifdef FEAT_VIRTUALEDIT
11092 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011093 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011094#endif
11095 }
11096 if (line < 0 || col < 0
11097#ifdef FEAT_VIRTUALEDIT
11098 || coladd < 0
11099#endif
11100 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 if (line > 0)
11103 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011104 if (col > 0)
11105 curwin->w_cursor.col = col - 1;
11106#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011107 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108#endif
11109
11110 /* Make sure the cursor is in a valid position. */
11111 check_cursor();
11112#ifdef FEAT_MBYTE
11113 /* Correct cursor for multi-byte character. */
11114 if (has_mbyte)
11115 mb_adjust_cursor();
11116#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011117
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011118 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011119 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120}
11121
11122/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011123 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124 */
11125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011126f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011128 int noref = 0;
11129
11130 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011131 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011132 if (noref < 0 || noref > 1)
11133 EMSG(_(e_invarg));
11134 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011135 {
11136 current_copyID += COPYID_INC;
11137 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140
11141/*
11142 * "delete()" function
11143 */
11144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011145f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011147 char_u nbuf[NUMBUFLEN];
11148 char_u *name;
11149 char_u *flags;
11150
11151 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011153 return;
11154
11155 name = get_tv_string(&argvars[0]);
11156 if (name == NULL || *name == NUL)
11157 {
11158 EMSG(_(e_invarg));
11159 return;
11160 }
11161
11162 if (argvars[1].v_type != VAR_UNKNOWN)
11163 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011165 flags = (char_u *)"";
11166
11167 if (*flags == NUL)
11168 /* delete a file */
11169 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11170 else if (STRCMP(flags, "d") == 0)
11171 /* delete an empty directory */
11172 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11173 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011174 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011175 rettv->vval.v_number = delete_recursive(name);
11176 else
11177 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178}
11179
11180/*
11181 * "did_filetype()" function
11182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011184f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011185{
11186#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188#endif
11189}
11190
11191/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011192 * "diff_filler()" function
11193 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011195f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011196{
11197#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011199#endif
11200}
11201
11202/*
11203 * "diff_hlID()" function
11204 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011206f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011207{
11208#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011209 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011210 static linenr_T prev_lnum = 0;
11211 static int changedtick = 0;
11212 static int fnum = 0;
11213 static int change_start = 0;
11214 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011215 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011216 int filler_lines;
11217 int col;
11218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011219 if (lnum < 0) /* ignore type error in {lnum} arg */
11220 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011221 if (lnum != prev_lnum
11222 || changedtick != curbuf->b_changedtick
11223 || fnum != curbuf->b_fnum)
11224 {
11225 /* New line, buffer, change: need to get the values. */
11226 filler_lines = diff_check(curwin, lnum);
11227 if (filler_lines < 0)
11228 {
11229 if (filler_lines == -1)
11230 {
11231 change_start = MAXCOL;
11232 change_end = -1;
11233 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11234 hlID = HLF_ADD; /* added line */
11235 else
11236 hlID = HLF_CHD; /* changed line */
11237 }
11238 else
11239 hlID = HLF_ADD; /* added line */
11240 }
11241 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011242 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011243 prev_lnum = lnum;
11244 changedtick = curbuf->b_changedtick;
11245 fnum = curbuf->b_fnum;
11246 }
11247
11248 if (hlID == HLF_CHD || hlID == HLF_TXD)
11249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011251 if (col >= change_start && col <= change_end)
11252 hlID = HLF_TXD; /* changed text */
11253 else
11254 hlID = HLF_CHD; /* changed line */
11255 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011256 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011257#endif
11258}
11259
11260/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011261 * "empty({expr})" function
11262 */
11263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011264f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011265{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011266 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011267
11268 switch (argvars[0].v_type)
11269 {
11270 case VAR_STRING:
11271 case VAR_FUNC:
11272 n = argvars[0].vval.v_string == NULL
11273 || *argvars[0].vval.v_string == NUL;
11274 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011275 case VAR_PARTIAL:
11276 n = FALSE;
11277 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011278 case VAR_NUMBER:
11279 n = argvars[0].vval.v_number == 0;
11280 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011281 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011282#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011283 n = argvars[0].vval.v_float == 0.0;
11284 break;
11285#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011286 case VAR_LIST:
11287 n = argvars[0].vval.v_list == NULL
11288 || argvars[0].vval.v_list->lv_first == NULL;
11289 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290 case VAR_DICT:
11291 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011292 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011293 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011294 case VAR_SPECIAL:
11295 n = argvars[0].vval.v_number != VVAL_TRUE;
11296 break;
11297
Bram Moolenaar835dc632016-02-07 14:27:38 +010011298 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011299#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011300 n = argvars[0].vval.v_job == NULL
11301 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11302 break;
11303#endif
11304 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011305#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011306 n = argvars[0].vval.v_channel == NULL
11307 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011308 break;
11309#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011310 case VAR_UNKNOWN:
11311 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11312 n = TRUE;
11313 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011314 }
11315
11316 rettv->vval.v_number = n;
11317}
11318
11319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011320 * "escape({string}, {chars})" function
11321 */
11322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011323f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324{
11325 char_u buf[NUMBUFLEN];
11326
Bram Moolenaar758711c2005-02-02 23:11:38 +000011327 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11328 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330}
11331
11332/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011333 * "eval()" function
11334 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011336f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011337{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011338 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 s = get_tv_string_chk(&argvars[0]);
11341 if (s != NULL)
11342 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011343
Bram Moolenaar615b9972015-01-14 17:15:05 +010011344 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11346 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011347 if (p != NULL && !aborting())
11348 EMSG2(_(e_invexpr2), p);
11349 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011351 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011352 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011353 else if (*s != NUL)
11354 EMSG(_(e_trailing));
11355}
11356
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358 * "eventhandler()" function
11359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364}
11365
11366/*
11367 * "executable()" function
11368 */
11369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011370f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011372 char_u *name = get_tv_string(&argvars[0]);
11373
11374 /* Check in $PATH and also check directly if there is a directory name. */
11375 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11376 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011377}
11378
Bram Moolenaar79815f12016-07-09 17:07:29 +020011379static garray_T redir_execute_ga;
11380
11381/*
11382 * Append "value[value_len]" to the execute() output.
11383 */
11384 void
11385execute_redir_str(char_u *value, int value_len)
11386{
11387 int len;
11388
11389 if (value_len == -1)
11390 len = (int)STRLEN(value); /* Append the entire string */
11391 else
11392 len = value_len; /* Append only "value_len" characters */
11393 if (ga_grow(&redir_execute_ga, len) == OK)
11394 {
11395 mch_memmove((char *)redir_execute_ga.ga_data
11396 + redir_execute_ga.ga_len, value, len);
11397 redir_execute_ga.ga_len += len;
11398 }
11399}
11400
11401/*
11402 * Get next line from a list.
11403 * Called by do_cmdline() to get the next line.
11404 * Returns allocated string, or NULL for end of function.
11405 */
11406
11407 static char_u *
11408get_list_line(
11409 int c UNUSED,
11410 void *cookie,
11411 int indent UNUSED)
11412{
11413 listitem_T **p = (listitem_T **)cookie;
11414 listitem_T *item = *p;
11415 char_u buf[NUMBUFLEN];
11416 char_u *s;
11417
11418 if (item == NULL)
11419 return NULL;
11420 s = get_tv_string_buf_chk(&item->li_tv, buf);
11421 *p = item->li_next;
11422 return s == NULL ? NULL : vim_strsave(s);
11423}
11424
11425/*
11426 * "execute()" function
11427 */
11428 static void
11429f_execute(typval_T *argvars, typval_T *rettv)
11430{
11431 char_u *cmd = NULL;
11432 list_T *list = NULL;
11433 int save_msg_silent = msg_silent;
11434 int save_emsg_silent = emsg_silent;
11435 int save_emsg_noredir = emsg_noredir;
11436 int save_redir_execute = redir_execute;
11437 garray_T save_ga;
11438
11439 rettv->vval.v_string = NULL;
11440 rettv->v_type = VAR_STRING;
11441
11442 if (argvars[0].v_type == VAR_LIST)
11443 {
11444 list = argvars[0].vval.v_list;
11445 if (list == NULL || list->lv_first == NULL)
11446 /* empty list, no commands, empty output */
11447 return;
11448 ++list->lv_refcount;
11449 }
11450 else
11451 {
11452 cmd = get_tv_string_chk(&argvars[0]);
11453 if (cmd == NULL)
11454 return;
11455 }
11456
Bram Moolenaar79815f12016-07-09 17:07:29 +020011457 if (argvars[1].v_type != VAR_UNKNOWN)
11458 {
11459 char_u buf[NUMBUFLEN];
11460 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
11461
11462 if (s == NULL)
11463 return;
11464 if (STRNCMP(s, "silent", 6) == 0)
11465 ++msg_silent;
11466 if (STRCMP(s, "silent!") == 0)
11467 {
11468 emsg_silent = TRUE;
11469 emsg_noredir = TRUE;
11470 }
11471 }
11472 else
11473 ++msg_silent;
11474
Bram Moolenaared59aa62016-07-09 17:41:12 +020011475 if (redir_execute)
11476 save_ga = redir_execute_ga;
11477 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
11478 redir_execute = TRUE;
11479
Bram Moolenaar79815f12016-07-09 17:07:29 +020011480 if (cmd != NULL)
11481 do_cmdline_cmd(cmd);
11482 else
11483 {
11484 listitem_T *item = list->lv_first;
11485
11486 do_cmdline(NULL, get_list_line, (void *)&item,
11487 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
11488 --list->lv_refcount;
11489 }
11490
11491 rettv->vval.v_string = redir_execute_ga.ga_data;
11492 msg_silent = save_msg_silent;
11493 emsg_silent = save_emsg_silent;
11494 emsg_noredir = save_emsg_noredir;
11495
11496 redir_execute = save_redir_execute;
11497 if (redir_execute)
11498 redir_execute_ga = save_ga;
11499
11500 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
11501 * line. Put it back in the first column. */
11502 msg_col = 0;
11503}
11504
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011505/*
11506 * "exepath()" function
11507 */
11508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011509f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011510{
11511 char_u *p = NULL;
11512
Bram Moolenaarb5971142015-03-21 17:32:19 +010011513 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011514 rettv->v_type = VAR_STRING;
11515 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516}
11517
11518/*
11519 * "exists()" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523{
11524 char_u *p;
11525 char_u *name;
11526 int n = FALSE;
11527 int len = 0;
11528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 if (*p == '$') /* environment variable */
11531 {
11532 /* first try "normal" environment variables (fast) */
11533 if (mch_getenv(p + 1) != NULL)
11534 n = TRUE;
11535 else
11536 {
11537 /* try expanding things like $VIM and ${HOME} */
11538 p = expand_env_save(p);
11539 if (p != NULL && *p != '$')
11540 n = TRUE;
11541 vim_free(p);
11542 }
11543 }
11544 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011547 if (*skipwhite(p) != NUL)
11548 n = FALSE; /* trailing garbage */
11549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550 else if (*p == '*') /* internal or user defined function */
11551 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011552 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 }
11554 else if (*p == ':')
11555 {
11556 n = cmd_exists(p + 1);
11557 }
11558 else if (*p == '#')
11559 {
11560#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011561 if (p[1] == '#')
11562 n = autocmd_supported(p + 2);
11563 else
11564 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565#endif
11566 }
11567 else /* internal variable */
11568 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011569 char_u *tofree;
11570 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011572 /* get_name_len() takes care of expanding curly braces */
11573 name = p;
11574 len = get_name_len(&p, &tofree, TRUE, FALSE);
11575 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011577 if (tofree != NULL)
11578 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011579 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011580 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011582 /* handle d.key, l[idx], f(expr) */
11583 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11584 if (n)
11585 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 }
11587 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011588 if (*p != NUL)
11589 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011591 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 }
11593
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595}
11596
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011597#ifdef FEAT_FLOAT
11598/*
11599 * "exp()" function
11600 */
11601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011602f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011603{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011604 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011605
11606 rettv->v_type = VAR_FLOAT;
11607 if (get_float_arg(argvars, &f) == OK)
11608 rettv->vval.v_float = exp(f);
11609 else
11610 rettv->vval.v_float = 0.0;
11611}
11612#endif
11613
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614/*
11615 * "expand()" function
11616 */
11617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011618f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011619{
11620 char_u *s;
11621 int len;
11622 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011623 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011626 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011629 if (argvars[1].v_type != VAR_UNKNOWN
11630 && argvars[2].v_type != VAR_UNKNOWN
11631 && get_tv_number_chk(&argvars[2], &error)
11632 && !error)
11633 {
11634 rettv->v_type = VAR_LIST;
11635 rettv->vval.v_list = NULL;
11636 }
11637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011638 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 if (*s == '%' || *s == '#' || *s == '<')
11640 {
11641 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011642 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011644 if (rettv->v_type == VAR_LIST)
11645 {
11646 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11647 list_append_string(rettv->vval.v_list, result, -1);
11648 else
11649 vim_free(result);
11650 }
11651 else
11652 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 }
11654 else
11655 {
11656 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011657 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 if (argvars[1].v_type != VAR_UNKNOWN
11659 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011660 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 if (!error)
11662 {
11663 ExpandInit(&xpc);
11664 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011665 if (p_wic)
11666 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011667 if (rettv->v_type == VAR_STRING)
11668 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11669 options, WILD_ALL);
11670 else if (rettv_list_alloc(rettv) != FAIL)
11671 {
11672 int i;
11673
11674 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11675 for (i = 0; i < xpc.xp_numfiles; i++)
11676 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11677 ExpandCleanup(&xpc);
11678 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011679 }
11680 else
11681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 }
11683}
11684
11685/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011686 * Go over all entries in "d2" and add them to "d1".
11687 * When "action" is "error" then a duplicate key is an error.
11688 * When "action" is "force" then a duplicate key is overwritten.
11689 * Otherwise duplicate keys are ignored ("action" is "keep").
11690 */
11691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011692dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011693{
11694 dictitem_T *di1;
11695 hashitem_T *hi2;
11696 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011697 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011698
11699 todo = (int)d2->dv_hashtab.ht_used;
11700 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11701 {
11702 if (!HASHITEM_EMPTY(hi2))
11703 {
11704 --todo;
11705 di1 = dict_find(d1, hi2->hi_key, -1);
11706 if (d1->dv_scope != 0)
11707 {
11708 /* Disallow replacing a builtin function in l: and g:.
11709 * Check the key to be valid when adding to any
11710 * scope. */
11711 if (d1->dv_scope == VAR_DEF_SCOPE
11712 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11713 && var_check_func_name(hi2->hi_key,
11714 di1 == NULL))
11715 break;
11716 if (!valid_varname(hi2->hi_key))
11717 break;
11718 }
11719 if (di1 == NULL)
11720 {
11721 di1 = dictitem_copy(HI2DI(hi2));
11722 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11723 dictitem_free(di1);
11724 }
11725 else if (*action == 'e')
11726 {
11727 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11728 break;
11729 }
11730 else if (*action == 'f' && HI2DI(hi2) != di1)
11731 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011732 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11733 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011734 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011735 clear_tv(&di1->di_tv);
11736 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11737 }
11738 }
11739 }
11740}
11741
11742/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011743 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011745 */
11746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011747f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011748{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011749 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011750
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011752 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011753 list_T *l1, *l2;
11754 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011755 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011756 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011757
Bram Moolenaare9a41262005-01-15 22:18:47 +000011758 l1 = argvars[0].vval.v_list;
11759 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011760 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011761 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011762 {
11763 if (argvars[2].v_type != VAR_UNKNOWN)
11764 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011765 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 if (error)
11767 return; /* type error; errmsg already given */
11768
Bram Moolenaar758711c2005-02-02 23:11:38 +000011769 if (before == l1->lv_len)
11770 item = NULL;
11771 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011773 item = list_find(l1, before);
11774 if (item == NULL)
11775 {
11776 EMSGN(_(e_listidx), before);
11777 return;
11778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011779 }
11780 }
11781 else
11782 item = NULL;
11783 list_extend(l1, l2, item);
11784
Bram Moolenaare9a41262005-01-15 22:18:47 +000011785 copy_tv(&argvars[0], rettv);
11786 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011787 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11789 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011790 dict_T *d1, *d2;
11791 char_u *action;
11792 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793
11794 d1 = argvars[0].vval.v_dict;
11795 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011796 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011797 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011798 {
11799 /* Check the third argument. */
11800 if (argvars[2].v_type != VAR_UNKNOWN)
11801 {
11802 static char *(av[]) = {"keep", "force", "error"};
11803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 action = get_tv_string_chk(&argvars[2]);
11805 if (action == NULL)
11806 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011807 for (i = 0; i < 3; ++i)
11808 if (STRCMP(action, av[i]) == 0)
11809 break;
11810 if (i == 3)
11811 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011812 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011813 return;
11814 }
11815 }
11816 else
11817 action = (char_u *)"force";
11818
Bram Moolenaara9922d62013-05-30 13:01:18 +020011819 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011820
Bram Moolenaare9a41262005-01-15 22:18:47 +000011821 copy_tv(&argvars[0], rettv);
11822 }
11823 }
11824 else
11825 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011826}
11827
11828/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011829 * "feedkeys()" function
11830 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011832f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011833{
11834 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011835 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011836 char_u *keys, *flags;
11837 char_u nbuf[NUMBUFLEN];
11838 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011839 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011840 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011841 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011842
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011843 /* This is not allowed in the sandbox. If the commands would still be
11844 * executed in the sandbox it would be OK, but it probably happens later,
11845 * when "sandbox" is no longer set. */
11846 if (check_secure())
11847 return;
11848
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011849 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011850
11851 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011852 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011853 flags = get_tv_string_buf(&argvars[1], nbuf);
11854 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011855 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011856 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011857 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011858 case 'n': remap = FALSE; break;
11859 case 'm': remap = TRUE; break;
11860 case 't': typed = TRUE; break;
11861 case 'i': insert = TRUE; break;
11862 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011863 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011864 }
11865 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011866 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011867
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011868 if (*keys != NUL || execute)
11869 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011870 /* Need to escape K_SPECIAL and CSI before putting the string in the
11871 * typeahead buffer. */
11872 keys_esc = vim_strsave_escape_csi(keys);
11873 if (keys_esc != NULL)
11874 {
11875 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011876 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011877 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011878 if (vgetc_busy)
11879 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011880 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011881 {
11882 int save_msg_scroll = msg_scroll;
11883
11884 /* Avoid a 1 second delay when the keys start Insert mode. */
11885 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011886
Bram Moolenaar245c4102016-04-20 17:37:41 +020011887 if (!dangerous)
11888 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011889 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011890 if (!dangerous)
11891 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011892 msg_scroll |= save_msg_scroll;
11893 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011894 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011895 }
11896}
11897
11898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 * "filereadable()" function
11900 */
11901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011902f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011904 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905 char_u *p;
11906 int n;
11907
Bram Moolenaarc236c162008-07-13 17:41:49 +000011908#ifndef O_NONBLOCK
11909# define O_NONBLOCK 0
11910#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011912 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11913 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914 {
11915 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011916 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917 }
11918 else
11919 n = FALSE;
11920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011921 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922}
11923
11924/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011925 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 * rights to write into.
11927 */
11928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011929f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011931 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932}
11933
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011934 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011935findfilendir(
11936 typval_T *argvars UNUSED,
11937 typval_T *rettv,
11938 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011939{
11940#ifdef FEAT_SEARCHPATH
11941 char_u *fname;
11942 char_u *fresult = NULL;
11943 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11944 char_u *p;
11945 char_u pathbuf[NUMBUFLEN];
11946 int count = 1;
11947 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011948 int error = FALSE;
11949#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011950
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011951 rettv->vval.v_string = NULL;
11952 rettv->v_type = VAR_STRING;
11953
11954#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011956
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011957 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11960 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011961 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011962 else
11963 {
11964 if (*p != NUL)
11965 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011968 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011969 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011970 }
11971
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011972 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11973 error = TRUE;
11974
11975 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977 do
11978 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011979 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011980 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 fresult = find_file_in_path_option(first ? fname : NULL,
11982 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011983 0, first, path,
11984 find_what,
11985 curbuf->b_ffname,
11986 find_what == FINDFILE_DIR
11987 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011989
11990 if (fresult != NULL && rettv->v_type == VAR_LIST)
11991 list_append_string(rettv->vval.v_list, fresult, -1);
11992
11993 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011995
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011996 if (rettv->v_type == VAR_STRING)
11997 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011998#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011999}
12000
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012001static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012002static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012003
12004/*
12005 * Implementation of map() and filter().
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012009{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012010 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 listitem_T *li, *nli;
12012 list_T *l = NULL;
12013 dictitem_T *di;
12014 hashtab_T *ht;
12015 hashitem_T *hi;
12016 dict_T *d = NULL;
12017 typval_T save_val;
12018 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012019 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012020 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012021 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020012022 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020012023 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012024 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012025 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012026
Bram Moolenaare9a41262005-01-15 22:18:47 +000012027 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012028 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012029 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012030 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 return;
12032 }
12033 else if (argvars[0].v_type == VAR_DICT)
12034 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012035 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020012036 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000012037 return;
12038 }
12039 else
12040 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000012041 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012042 return;
12043 }
12044
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012045 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012046 /* On type errors, the preceding call has already displayed an error
12047 * message. Avoid a misleading error message for an empty string that
12048 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012049 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012051 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012052
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012053 /* We reset "did_emsg" to be able to detect whether an error
12054 * occurred during evaluation of the expression. */
12055 save_did_emsg = did_emsg;
12056 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012057
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012058 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012060 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061 vimvars[VV_KEY].vv_type = VAR_STRING;
12062
12063 ht = &d->dv_hashtab;
12064 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012065 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 if (!HASHITEM_EMPTY(hi))
12069 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012070 int r;
12071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 --todo;
12073 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012074 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012075 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12076 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 break;
12078 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012079 r = filter_map_one(&di->di_tv, expr, map, &rem);
12080 clear_tv(&vimvars[VV_KEY].vv_tv);
12081 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 break;
12083 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012084 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012085 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12086 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012087 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012090 }
12091 }
12092 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012093 }
12094 else
12095 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012096 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098 for (li = l->lv_first; li != NULL; li = nli)
12099 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012100 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012101 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012102 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012103 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012104 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012105 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012106 break;
12107 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012109 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012110 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012111 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012112
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012113 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012115
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012116 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012117 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012118
12119 copy_tv(&argvars[0], rettv);
12120}
12121
12122 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012123filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124{
Bram Moolenaar33570922005-01-25 22:26:29 +000012125 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012126 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012127 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012129 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012130 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012131
Bram Moolenaar33570922005-01-25 22:26:29 +000012132 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012133 argv[0] = vimvars[VV_KEY].vv_tv;
12134 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012135 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012136 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012137 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012138 if (call_func(s, (int)STRLEN(s),
12139 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12140 goto theend;
12141 }
12142 else if (expr->v_type == VAR_PARTIAL)
12143 {
12144 partial_T *partial = expr->vval.v_partial;
12145
12146 s = partial->pt_name;
12147 if (call_func(s, (int)STRLEN(s),
12148 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12149 == FAIL)
12150 goto theend;
12151 }
12152 else
12153 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012154 s = get_tv_string_buf_chk(expr, buf);
12155 if (s == NULL)
12156 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012157 s = skipwhite(s);
12158 if (eval1(&s, &rettv, TRUE) == FAIL)
12159 goto theend;
12160 if (*s != NUL) /* check for trailing chars after expr */
12161 {
12162 EMSG2(_(e_invexpr2), s);
12163 goto theend;
12164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012165 }
12166 if (map)
12167 {
12168 /* map(): replace the list item value */
12169 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012170 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012171 *tv = rettv;
12172 }
12173 else
12174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 int error = FALSE;
12176
Bram Moolenaare9a41262005-01-15 22:18:47 +000012177 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012178 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012179 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012180 /* On type error, nothing has been removed; return FAIL to stop the
12181 * loop. The error message was given by get_tv_number_chk(). */
12182 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012183 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012184 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012185 retval = OK;
12186theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012188 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012189}
12190
12191/*
12192 * "filter()" function
12193 */
12194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012196{
12197 filter_map(argvars, rettv, FALSE);
12198}
12199
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201 * "finddir({fname}[, {path}[, {count}]])" function
12202 */
12203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012204f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012205{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012206 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207}
12208
12209/*
12210 * "findfile({fname}[, {path}[, {count}]])" function
12211 */
12212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012213f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012215 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216}
12217
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012218#ifdef FEAT_FLOAT
12219/*
12220 * "float2nr({float})" function
12221 */
12222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012223f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012224{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012225 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012226
12227 if (get_float_arg(argvars, &f) == OK)
12228 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012229# ifdef FEAT_NUM64
12230 if (f < -0x7fffffffffffffff)
12231 rettv->vval.v_number = -0x7fffffffffffffff;
12232 else if (f > 0x7fffffffffffffff)
12233 rettv->vval.v_number = 0x7fffffffffffffff;
12234 else
12235 rettv->vval.v_number = (varnumber_T)f;
12236# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012237 if (f < -0x7fffffff)
12238 rettv->vval.v_number = -0x7fffffff;
12239 else if (f > 0x7fffffff)
12240 rettv->vval.v_number = 0x7fffffff;
12241 else
12242 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012243# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012244 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012245}
12246
12247/*
12248 * "floor({float})" function
12249 */
12250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012251f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012252{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012253 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012254
12255 rettv->v_type = VAR_FLOAT;
12256 if (get_float_arg(argvars, &f) == OK)
12257 rettv->vval.v_float = floor(f);
12258 else
12259 rettv->vval.v_float = 0.0;
12260}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012261
12262/*
12263 * "fmod()" function
12264 */
12265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012266f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012267{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012268 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012269
12270 rettv->v_type = VAR_FLOAT;
12271 if (get_float_arg(argvars, &fx) == OK
12272 && get_float_arg(&argvars[1], &fy) == OK)
12273 rettv->vval.v_float = fmod(fx, fy);
12274 else
12275 rettv->vval.v_float = 0.0;
12276}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012277#endif
12278
Bram Moolenaar0d660222005-01-07 21:51:51 +000012279/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012280 * "fnameescape({string})" function
12281 */
12282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012283f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012284{
12285 rettv->vval.v_string = vim_strsave_fnameescape(
12286 get_tv_string(&argvars[0]), FALSE);
12287 rettv->v_type = VAR_STRING;
12288}
12289
12290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 * "fnamemodify({fname}, {mods})" function
12292 */
12293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012294f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295{
12296 char_u *fname;
12297 char_u *mods;
12298 int usedlen = 0;
12299 int len;
12300 char_u *fbuf = NULL;
12301 char_u buf[NUMBUFLEN];
12302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012303 fname = get_tv_string_chk(&argvars[0]);
12304 mods = get_tv_string_buf_chk(&argvars[1], buf);
12305 if (fname == NULL || mods == NULL)
12306 fname = NULL;
12307 else
12308 {
12309 len = (int)STRLEN(fname);
12310 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012315 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 vim_free(fbuf);
12319}
12320
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012321static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322
12323/*
12324 * "foldclosed()" function
12325 */
12326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012327foldclosed_both(
12328 typval_T *argvars UNUSED,
12329 typval_T *rettv,
12330 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331{
12332#ifdef FEAT_FOLDING
12333 linenr_T lnum;
12334 linenr_T first, last;
12335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012336 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12338 {
12339 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12340 {
12341 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 return;
12346 }
12347 }
12348#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350}
12351
12352/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012353 * "foldclosed()" function
12354 */
12355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012356f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357{
12358 foldclosed_both(argvars, rettv, FALSE);
12359}
12360
12361/*
12362 * "foldclosedend()" function
12363 */
12364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012365f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366{
12367 foldclosed_both(argvars, rettv, TRUE);
12368}
12369
12370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371 * "foldlevel()" function
12372 */
12373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012374f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375{
12376#ifdef FEAT_FOLDING
12377 linenr_T lnum;
12378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012379 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012381 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383}
12384
12385/*
12386 * "foldtext()" function
12387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012389f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390{
12391#ifdef FEAT_FOLDING
12392 linenr_T lnum;
12393 char_u *s;
12394 char_u *r;
12395 int len;
12396 char *txt;
12397#endif
12398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 rettv->v_type = VAR_STRING;
12400 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012402 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12403 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12404 <= curbuf->b_ml.ml_line_count
12405 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 {
12407 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12409 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 {
12411 if (!linewhite(lnum))
12412 break;
12413 ++lnum;
12414 }
12415
12416 /* Find interesting text in this line. */
12417 s = skipwhite(ml_get(lnum));
12418 /* skip C comment-start */
12419 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012422 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012424 {
12425 s = skipwhite(ml_get(lnum + 1));
12426 if (*s == '*')
12427 s = skipwhite(s + 1);
12428 }
12429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 txt = _("+-%s%3ld lines: ");
12431 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012432 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 + 20 /* for %3ld */
12434 + STRLEN(s))); /* concatenated */
12435 if (r != NULL)
12436 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012437 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12438 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12439 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 len = (int)STRLEN(r);
12441 STRCAT(r, s);
12442 /* remove 'foldmarker' and 'commentstring' */
12443 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445 }
12446 }
12447#endif
12448}
12449
12450/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012451 * "foldtextresult(lnum)" function
12452 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012454f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012455{
12456#ifdef FEAT_FOLDING
12457 linenr_T lnum;
12458 char_u *text;
12459 char_u buf[51];
12460 foldinfo_T foldinfo;
12461 int fold_count;
12462#endif
12463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012464 rettv->v_type = VAR_STRING;
12465 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012466#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012467 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012468 /* treat illegal types and illegal string values for {lnum} the same */
12469 if (lnum < 0)
12470 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012471 fold_count = foldedCount(curwin, lnum, &foldinfo);
12472 if (fold_count > 0)
12473 {
12474 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12475 &foldinfo, buf);
12476 if (text == buf)
12477 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012479 }
12480#endif
12481}
12482
12483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484 * "foreground()" function
12485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012487f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_GUI
12490 if (gui.in_use)
12491 gui_mch_set_foreground();
12492#else
12493# ifdef WIN32
12494 win32_set_foreground();
12495# endif
12496#endif
12497}
12498
12499/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012500 * "function()" function
12501 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012503f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012504{
12505 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012506 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012507 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012508 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012509
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012510 if (argvars[0].v_type == VAR_FUNC)
12511 {
12512 /* function(MyFunc, [arg], dict) */
12513 s = argvars[0].vval.v_string;
12514 }
12515 else if (argvars[0].v_type == VAR_PARTIAL
12516 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012517 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012518 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012519 arg_pt = argvars[0].vval.v_partial;
12520 s = arg_pt->pt_name;
12521 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012522 else
12523 {
12524 /* function('MyFunc', [arg], dict) */
12525 s = get_tv_string(&argvars[0]);
12526 use_string = TRUE;
12527 }
12528
12529 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012530 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012531 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012532 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12533 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012534 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012535 else
12536 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012537 int dict_idx = 0;
12538 int arg_idx = 0;
12539 list_T *list = NULL;
12540
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012541 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012542 {
12543 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012544 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012545
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012546 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12547 * also be called from another script. Using trans_function_name()
12548 * would also work, but some plugins depend on the name being
12549 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012550 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012551 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12552 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012553 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012554 STRCPY(name, sid_buf);
12555 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012556 }
12557 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012558 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012559 name = vim_strsave(s);
12560
12561 if (argvars[1].v_type != VAR_UNKNOWN)
12562 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012563 if (argvars[2].v_type != VAR_UNKNOWN)
12564 {
12565 /* function(name, [args], dict) */
12566 arg_idx = 1;
12567 dict_idx = 2;
12568 }
12569 else if (argvars[1].v_type == VAR_DICT)
12570 /* function(name, dict) */
12571 dict_idx = 1;
12572 else
12573 /* function(name, [args]) */
12574 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012575 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012576 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012577 if (argvars[dict_idx].v_type != VAR_DICT)
12578 {
12579 EMSG(_("E922: expected a dict"));
12580 vim_free(name);
12581 return;
12582 }
12583 if (argvars[dict_idx].vval.v_dict == NULL)
12584 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012585 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012586 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012587 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012588 if (argvars[arg_idx].v_type != VAR_LIST)
12589 {
12590 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12591 vim_free(name);
12592 return;
12593 }
12594 list = argvars[arg_idx].vval.v_list;
12595 if (list == NULL || list->lv_len == 0)
12596 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012597 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012598 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012599 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012600 {
12601 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012602
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012603 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012604 if (pt == NULL)
12605 vim_free(name);
12606 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012607 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012608 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012609 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012610 listitem_T *li;
12611 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012612 int arg_len = 0;
12613 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012614
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012615 if (arg_pt != NULL)
12616 arg_len = arg_pt->pt_argc;
12617 if (list != NULL)
12618 lv_len = list->lv_len;
12619 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012620 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012621 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012622 if (pt->pt_argv == NULL)
12623 {
12624 vim_free(pt);
12625 vim_free(name);
12626 return;
12627 }
12628 else
12629 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012630 for (i = 0; i < arg_len; i++)
12631 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12632 if (lv_len > 0)
12633 for (li = list->lv_first; li != NULL;
12634 li = li->li_next)
12635 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012636 }
12637 }
12638
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012639 /* For "function(dict.func, [], dict)" and "func" is a partial
12640 * use "dict". That is backwards compatible. */
12641 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012642 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012643 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012644 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12645 ++pt->pt_dict->dv_refcount;
12646 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012647 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012648 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012649 /* If the dict was bound automatically the result is also
12650 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012651 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012652 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012653 if (pt->pt_dict != NULL)
12654 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012655 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012656
12657 pt->pt_refcount = 1;
12658 pt->pt_name = name;
12659 func_ref(pt->pt_name);
12660 }
12661 rettv->v_type = VAR_PARTIAL;
12662 rettv->vval.v_partial = pt;
12663 }
12664 else
12665 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012666 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012667 rettv->v_type = VAR_FUNC;
12668 rettv->vval.v_string = name;
12669 func_ref(name);
12670 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012671 }
12672}
12673
12674/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012675 * "garbagecollect()" function
12676 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012678f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012679{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012680 /* This is postponed until we are back at the toplevel, because we may be
12681 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12682 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012683
12684 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12685 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012686}
12687
12688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689 * "get()" function
12690 */
12691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012692f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012693{
Bram Moolenaar33570922005-01-25 22:26:29 +000012694 listitem_T *li;
12695 list_T *l;
12696 dictitem_T *di;
12697 dict_T *d;
12698 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699
Bram Moolenaare9a41262005-01-15 22:18:47 +000012700 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012701 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012702 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012704 int error = FALSE;
12705
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012706 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012708 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012710 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012711 else if (argvars[0].v_type == VAR_DICT)
12712 {
12713 if ((d = argvars[0].vval.v_dict) != NULL)
12714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012715 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012716 if (di != NULL)
12717 tv = &di->di_tv;
12718 }
12719 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012720 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012721 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012722 partial_T *pt;
12723 partial_T fref_pt;
12724
12725 if (argvars[0].v_type == VAR_PARTIAL)
12726 pt = argvars[0].vval.v_partial;
12727 else
12728 {
12729 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12730 fref_pt.pt_name = argvars[0].vval.v_string;
12731 pt = &fref_pt;
12732 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012733
12734 if (pt != NULL)
12735 {
12736 char_u *what = get_tv_string(&argvars[1]);
12737
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012738 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012739 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012740 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012741 if (pt->pt_name == NULL)
12742 rettv->vval.v_string = NULL;
12743 else
12744 rettv->vval.v_string = vim_strsave(pt->pt_name);
12745 }
12746 else if (STRCMP(what, "dict") == 0)
12747 {
12748 rettv->v_type = VAR_DICT;
12749 rettv->vval.v_dict = pt->pt_dict;
12750 if (pt->pt_dict != NULL)
12751 ++pt->pt_dict->dv_refcount;
12752 }
12753 else if (STRCMP(what, "args") == 0)
12754 {
12755 rettv->v_type = VAR_LIST;
12756 if (rettv_list_alloc(rettv) == OK)
12757 {
12758 int i;
12759
12760 for (i = 0; i < pt->pt_argc; ++i)
12761 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12762 }
12763 }
12764 else
12765 EMSG2(_(e_invarg2), what);
12766 return;
12767 }
12768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012769 else
12770 EMSG2(_(e_listdictarg), "get()");
12771
12772 if (tv == NULL)
12773 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012775 copy_tv(&argvars[2], rettv);
12776 }
12777 else
12778 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012779}
12780
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012781static 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 +000012782
12783/*
12784 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012785 * Return a range (from start to end) of lines in rettv from the specified
12786 * buffer.
12787 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012788 */
12789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012790get_buffer_lines(
12791 buf_T *buf,
12792 linenr_T start,
12793 linenr_T end,
12794 int retlist,
12795 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012796{
12797 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012798
Bram Moolenaar959a1432013-12-14 12:17:38 +010012799 rettv->v_type = VAR_STRING;
12800 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012801 if (retlist && rettv_list_alloc(rettv) == FAIL)
12802 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012803
12804 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12805 return;
12806
12807 if (!retlist)
12808 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012809 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12810 p = ml_get_buf(buf, start, FALSE);
12811 else
12812 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012813 rettv->vval.v_string = vim_strsave(p);
12814 }
12815 else
12816 {
12817 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012818 return;
12819
12820 if (start < 1)
12821 start = 1;
12822 if (end > buf->b_ml.ml_line_count)
12823 end = buf->b_ml.ml_line_count;
12824 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012825 if (list_append_string(rettv->vval.v_list,
12826 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012827 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012828 }
12829}
12830
12831/*
12832 * "getbufline()" function
12833 */
12834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012835f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012836{
12837 linenr_T lnum;
12838 linenr_T end;
12839 buf_T *buf;
12840
12841 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12842 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012843 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012844 --emsg_off;
12845
Bram Moolenaar661b1822005-07-28 22:36:45 +000012846 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012847 if (argvars[2].v_type == VAR_UNKNOWN)
12848 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012849 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012850 end = get_tv_lnum_buf(&argvars[2], buf);
12851
Bram Moolenaar342337a2005-07-21 21:11:17 +000012852 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012853}
12854
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855/*
12856 * "getbufvar()" function
12857 */
12858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012859f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012860{
12861 buf_T *buf;
12862 buf_T *save_curbuf;
12863 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012865 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12868 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012869 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012870 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012871
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012872 rettv->v_type = VAR_STRING;
12873 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012874
12875 if (buf != NULL && varname != NULL)
12876 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012877 /* set curbuf to be our buf, temporarily */
12878 save_curbuf = curbuf;
12879 curbuf = buf;
12880
Bram Moolenaar0d660222005-01-07 21:51:51 +000012881 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012882 {
12883 if (get_option_tv(&varname, rettv, TRUE) == OK)
12884 done = TRUE;
12885 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012886 else if (STRCMP(varname, "changedtick") == 0)
12887 {
12888 rettv->v_type = VAR_NUMBER;
12889 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012890 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012891 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012892 else
12893 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012894 /* Look up the variable. */
12895 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12896 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12897 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012899 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012900 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012901 done = TRUE;
12902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012903 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012904
12905 /* restore previous notion of curbuf */
12906 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012907 }
12908
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012909 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12910 /* use the default value */
12911 copy_tv(&argvars[2], rettv);
12912
Bram Moolenaar0d660222005-01-07 21:51:51 +000012913 --emsg_off;
12914}
12915
12916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 * "getchar()" function
12918 */
12919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012920f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921{
12922 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012923 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012925 /* Position the cursor. Needed after a message that ends in a space. */
12926 windgoto(msg_row, msg_col);
12927
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 ++no_mapping;
12929 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012930 for (;;)
12931 {
12932 if (argvars[0].v_type == VAR_UNKNOWN)
12933 /* getchar(): blocking wait. */
12934 n = safe_vgetc();
12935 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12936 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012937 n = vpeekc_any();
12938 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012939 /* illegal argument or getchar(0) and no char avail: return zero */
12940 n = 0;
12941 else
12942 /* getchar(0) and char avail: return char */
12943 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012944
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012945 if (n == K_IGNORE)
12946 continue;
12947 break;
12948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 --no_mapping;
12950 --allow_keys;
12951
Bram Moolenaar219b8702006-11-01 14:32:36 +000012952 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012953 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012954 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12955 vimvars[VV_MOUSE_COL].vv_nr = 0;
12956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 if (IS_SPECIAL(n) || mod_mask != 0)
12959 {
12960 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12961 int i = 0;
12962
12963 /* Turn a special key into three bytes, plus modifier. */
12964 if (mod_mask != 0)
12965 {
12966 temp[i++] = K_SPECIAL;
12967 temp[i++] = KS_MODIFIER;
12968 temp[i++] = mod_mask;
12969 }
12970 if (IS_SPECIAL(n))
12971 {
12972 temp[i++] = K_SPECIAL;
12973 temp[i++] = K_SECOND(n);
12974 temp[i++] = K_THIRD(n);
12975 }
12976#ifdef FEAT_MBYTE
12977 else if (has_mbyte)
12978 i += (*mb_char2bytes)(n, temp + i);
12979#endif
12980 else
12981 temp[i++] = n;
12982 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->v_type = VAR_STRING;
12984 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012985
12986#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012987 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012988 {
12989 int row = mouse_row;
12990 int col = mouse_col;
12991 win_T *win;
12992 linenr_T lnum;
12993# ifdef FEAT_WINDOWS
12994 win_T *wp;
12995# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012996 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012997
12998 if (row >= 0 && col >= 0)
12999 {
13000 /* Find the window at the mouse coordinates and compute the
13001 * text position. */
13002 win = mouse_find_win(&row, &col);
13003 (void)mouse_comp_pos(win, &row, &col, &lnum);
13004# ifdef FEAT_WINDOWS
13005 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013006 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013007# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000013008 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020013009 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000013010 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
13011 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
13012 }
13013 }
13014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 }
13016}
13017
13018/*
13019 * "getcharmod()" function
13020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013022f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025}
13026
13027/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013028 * "getcharsearch()" function
13029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020013032{
13033 if (rettv_dict_alloc(rettv) != FAIL)
13034 {
13035 dict_T *dict = rettv->vval.v_dict;
13036
13037 dict_add_nr_str(dict, "char", 0L, last_csearch());
13038 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
13039 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
13040 }
13041}
13042
13043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 * "getcmdline()" function
13045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013047f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->v_type = VAR_STRING;
13050 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051}
13052
13053/*
13054 * "getcmdpos()" function
13055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060}
13061
13062/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013063 * "getcmdtype()" function
13064 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013066f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013067{
13068 rettv->v_type = VAR_STRING;
13069 rettv->vval.v_string = alloc(2);
13070 if (rettv->vval.v_string != NULL)
13071 {
13072 rettv->vval.v_string[0] = get_cmdline_type();
13073 rettv->vval.v_string[1] = NUL;
13074 }
13075}
13076
13077/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013078 * "getcmdwintype()" function
13079 */
13080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013081f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013082{
13083 rettv->v_type = VAR_STRING;
13084 rettv->vval.v_string = NULL;
13085#ifdef FEAT_CMDWIN
13086 rettv->vval.v_string = alloc(2);
13087 if (rettv->vval.v_string != NULL)
13088 {
13089 rettv->vval.v_string[0] = cmdwin_type;
13090 rettv->vval.v_string[1] = NUL;
13091 }
13092#endif
13093}
13094
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013095#if defined(FEAT_CMDL_COMPL)
13096/*
13097 * "getcompletion()" function
13098 */
13099 static void
13100f_getcompletion(typval_T *argvars, typval_T *rettv)
13101{
13102 char_u *pat;
13103 expand_T xpc;
13104 int options = WILD_KEEP_ALL | WILD_SILENT | WILD_USE_NL
13105 | WILD_LIST_NOTFOUND | WILD_NO_BEEP;
13106
13107 if (p_wic)
13108 options |= WILD_ICASE;
13109
13110 ExpandInit(&xpc);
13111 xpc.xp_pattern = get_tv_string(&argvars[0]);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020013112 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013113 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
13114 if (xpc.xp_context == EXPAND_NOTHING)
13115 {
13116 if (argvars[1].v_type == VAR_STRING)
13117 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
13118 else
13119 EMSG(_(e_invarg));
13120 return;
13121 }
13122
Bram Moolenaar4c068152016-07-11 23:15:25 +020013123# if defined(FEAT_MENU)
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013124 if (xpc.xp_context == EXPAND_MENUS)
13125 {
13126 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
Bram Moolenaar25065ec2016-07-10 19:22:53 +020013127 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013128 }
Bram Moolenaar4c068152016-07-11 23:15:25 +020013129# endif
Bram Moolenaaraa4d7322016-07-09 18:50:29 +020013130
13131 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
13132 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
13133 {
13134 int i;
13135
13136 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
13137
13138 for (i = 0; i < xpc.xp_numfiles; i++)
13139 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13140 }
13141 vim_free(pat);
13142 ExpandCleanup(&xpc);
13143}
13144#endif
13145
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013147 * "getcwd()" function
13148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013150f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013152 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013153 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013156 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013157
13158 wp = find_tabwin(&argvars[0], &argvars[1]);
13159 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013161 if (wp->w_localdir != NULL)
13162 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar5c719942016-07-09 23:40:45 +020013163 else if (globaldir != NULL)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013164 rettv->vval.v_string = vim_strsave(globaldir);
13165 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013166 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013167 cwd = alloc(MAXPATHL);
13168 if (cwd != NULL)
13169 {
13170 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13171 rettv->vval.v_string = vim_strsave(cwd);
13172 vim_free(cwd);
13173 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013174 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013175#ifdef BACKSLASH_IN_FILENAME
13176 if (rettv->vval.v_string != NULL)
13177 slash_adjust(rettv->vval.v_string);
13178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 }
13180}
13181
13182/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013183 * "getfontname()" function
13184 */
13185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013186f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013187{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013188 rettv->v_type = VAR_STRING;
13189 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013190#ifdef FEAT_GUI
13191 if (gui.in_use)
13192 {
13193 GuiFont font;
13194 char_u *name = NULL;
13195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013196 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013197 {
13198 /* Get the "Normal" font. Either the name saved by
13199 * hl_set_font_name() or from the font ID. */
13200 font = gui.norm_font;
13201 name = hl_get_font_name();
13202 }
13203 else
13204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013206 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13207 return;
13208 font = gui_mch_get_font(name, FALSE);
13209 if (font == NOFONT)
13210 return; /* Invalid font name, return empty string. */
13211 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013212 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013214 gui_mch_free_font(font);
13215 }
13216#endif
13217}
13218
13219/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013220 * "getfperm({fname})" function
13221 */
13222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013223f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013224{
13225 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013226 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013227 char_u *perm = NULL;
13228 char_u flags[] = "rwx";
13229 int i;
13230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013234 if (mch_stat((char *)fname, &st) >= 0)
13235 {
13236 perm = vim_strsave((char_u *)"---------");
13237 if (perm != NULL)
13238 {
13239 for (i = 0; i < 9; i++)
13240 {
13241 if (st.st_mode & (1 << (8 - i)))
13242 perm[i] = flags[i % 3];
13243 }
13244 }
13245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013247}
13248
13249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 * "getfsize({fname})" function
13251 */
13252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013253f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254{
13255 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013256 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013260 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261
13262 if (mch_stat((char *)fname, &st) >= 0)
13263 {
13264 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013267 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013268 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013269
13270 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013271 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013272 rettv->vval.v_number = -2;
13273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 }
13275 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277}
13278
13279/*
13280 * "getftime({fname})" function
13281 */
13282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013283f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284{
13285 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013286 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289
13290 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294}
13295
13296/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013297 * "getftype({fname})" function
13298 */
13299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013300f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013301{
13302 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013303 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013304 char_u *type = NULL;
13305 char *t;
13306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013307 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013310 if (mch_lstat((char *)fname, &st) >= 0)
13311 {
13312#ifdef S_ISREG
13313 if (S_ISREG(st.st_mode))
13314 t = "file";
13315 else if (S_ISDIR(st.st_mode))
13316 t = "dir";
13317# ifdef S_ISLNK
13318 else if (S_ISLNK(st.st_mode))
13319 t = "link";
13320# endif
13321# ifdef S_ISBLK
13322 else if (S_ISBLK(st.st_mode))
13323 t = "bdev";
13324# endif
13325# ifdef S_ISCHR
13326 else if (S_ISCHR(st.st_mode))
13327 t = "cdev";
13328# endif
13329# ifdef S_ISFIFO
13330 else if (S_ISFIFO(st.st_mode))
13331 t = "fifo";
13332# endif
13333# ifdef S_ISSOCK
13334 else if (S_ISSOCK(st.st_mode))
13335 t = "fifo";
13336# endif
13337 else
13338 t = "other";
13339#else
13340# ifdef S_IFMT
13341 switch (st.st_mode & S_IFMT)
13342 {
13343 case S_IFREG: t = "file"; break;
13344 case S_IFDIR: t = "dir"; break;
13345# ifdef S_IFLNK
13346 case S_IFLNK: t = "link"; break;
13347# endif
13348# ifdef S_IFBLK
13349 case S_IFBLK: t = "bdev"; break;
13350# endif
13351# ifdef S_IFCHR
13352 case S_IFCHR: t = "cdev"; break;
13353# endif
13354# ifdef S_IFIFO
13355 case S_IFIFO: t = "fifo"; break;
13356# endif
13357# ifdef S_IFSOCK
13358 case S_IFSOCK: t = "socket"; break;
13359# endif
13360 default: t = "other";
13361 }
13362# else
13363 if (mch_isdir(fname))
13364 t = "dir";
13365 else
13366 t = "file";
13367# endif
13368#endif
13369 type = vim_strsave((char_u *)t);
13370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013372}
13373
13374/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013375 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 */
13377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013378f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013379{
13380 linenr_T lnum;
13381 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013382 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013383
13384 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013385 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013386 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013387 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013388 retlist = FALSE;
13389 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013391 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013393 retlist = TRUE;
13394 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013395
Bram Moolenaar342337a2005-07-21 21:11:17 +000013396 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013397}
13398
13399/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013400 * "getmatches()" function
13401 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013403f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013404{
13405#ifdef FEAT_SEARCH_EXTRA
13406 dict_T *dict;
13407 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013408 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013409
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013410 if (rettv_list_alloc(rettv) == OK)
13411 {
13412 while (cur != NULL)
13413 {
13414 dict = dict_alloc();
13415 if (dict == NULL)
13416 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013417 if (cur->match.regprog == NULL)
13418 {
13419 /* match added with matchaddpos() */
13420 for (i = 0; i < MAXPOSMATCH; ++i)
13421 {
13422 llpos_T *llpos;
13423 char buf[6];
13424 list_T *l;
13425
13426 llpos = &cur->pos.pos[i];
13427 if (llpos->lnum == 0)
13428 break;
13429 l = list_alloc();
13430 if (l == NULL)
13431 break;
13432 list_append_number(l, (varnumber_T)llpos->lnum);
13433 if (llpos->col > 0)
13434 {
13435 list_append_number(l, (varnumber_T)llpos->col);
13436 list_append_number(l, (varnumber_T)llpos->len);
13437 }
13438 sprintf(buf, "pos%d", i + 1);
13439 dict_add_list(dict, buf, l);
13440 }
13441 }
13442 else
13443 {
13444 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13445 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013446 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013447 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13448 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013449# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013450 if (cur->conceal_char)
13451 {
13452 char_u buf[MB_MAXBYTES + 1];
13453
13454 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13455 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13456 }
13457# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013458 list_append_dict(rettv->vval.v_list, dict);
13459 cur = cur->next;
13460 }
13461 }
13462#endif
13463}
13464
13465/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013466 * "getpid()" function
13467 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013469f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013470{
13471 rettv->vval.v_number = mch_get_pid();
13472}
13473
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013475getpos_both(
13476 typval_T *argvars,
13477 typval_T *rettv,
13478 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013479{
Bram Moolenaara5525202006-03-02 22:52:09 +000013480 pos_T *fp;
13481 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013482 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013483
13484 if (rettv_list_alloc(rettv) == OK)
13485 {
13486 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013487 if (getcurpos)
13488 fp = &curwin->w_cursor;
13489 else
13490 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013491 if (fnum != -1)
13492 list_append_number(l, (varnumber_T)fnum);
13493 else
13494 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013495 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13496 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013497 list_append_number(l, (fp != NULL)
13498 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013499 : (varnumber_T)0);
13500 list_append_number(l,
13501#ifdef FEAT_VIRTUALEDIT
13502 (fp != NULL) ? (varnumber_T)fp->coladd :
13503#endif
13504 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013505 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013506 {
13507 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013508 list_append_number(l, curwin->w_curswant == MAXCOL ?
13509 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013510 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013511 }
13512 else
13513 rettv->vval.v_number = FALSE;
13514}
13515
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013516
13517/*
13518 * "getcurpos()" function
13519 */
13520 static void
13521f_getcurpos(typval_T *argvars, typval_T *rettv)
13522{
13523 getpos_both(argvars, rettv, TRUE);
13524}
13525
13526/*
13527 * "getpos(string)" function
13528 */
13529 static void
13530f_getpos(typval_T *argvars, typval_T *rettv)
13531{
13532 getpos_both(argvars, rettv, FALSE);
13533}
13534
Bram Moolenaara5525202006-03-02 22:52:09 +000013535/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013536 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013537 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013539f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013540{
13541#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013542 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013543#endif
13544
Bram Moolenaar2641f772005-03-25 21:58:17 +000013545#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013546 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013547 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013548 wp = NULL;
13549 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13550 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013551 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013552 if (wp == NULL)
13553 return;
13554 }
13555
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013556 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013557 }
13558#endif
13559}
13560
13561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 * "getreg()" function
13563 */
13564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013565f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566{
13567 char_u *strregname;
13568 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013569 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013570 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013573 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 strregname = get_tv_string_chk(&argvars[0]);
13576 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013578 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013579 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013580 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013581 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013582 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013585 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013586
13587 if (error)
13588 return;
13589
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 regname = (strregname == NULL ? '"' : *strregname);
13591 if (regname == 0)
13592 regname = '"';
13593
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013594 if (return_list)
13595 {
13596 rettv->v_type = VAR_LIST;
13597 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13598 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013599 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013600 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013601 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013602 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013603 }
13604 else
13605 {
13606 rettv->v_type = VAR_STRING;
13607 rettv->vval.v_string = get_reg_contents(regname,
13608 arg2 ? GREG_EXPR_SRC : 0);
13609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610}
13611
13612/*
13613 * "getregtype()" function
13614 */
13615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013616f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618 char_u *strregname;
13619 int regname;
13620 char_u buf[NUMBUFLEN + 2];
13621 long reglen = 0;
13622
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013623 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 {
13625 strregname = get_tv_string_chk(&argvars[0]);
13626 if (strregname == NULL) /* type error; errmsg already given */
13627 {
13628 rettv->v_type = VAR_STRING;
13629 rettv->vval.v_string = NULL;
13630 return;
13631 }
13632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633 else
13634 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013635 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636
13637 regname = (strregname == NULL ? '"' : *strregname);
13638 if (regname == 0)
13639 regname = '"';
13640
13641 buf[0] = NUL;
13642 buf[1] = NUL;
13643 switch (get_reg_type(regname, &reglen))
13644 {
13645 case MLINE: buf[0] = 'V'; break;
13646 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 case MBLOCK:
13648 buf[0] = Ctrl_V;
13649 sprintf((char *)buf + 1, "%ld", reglen + 1);
13650 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013652 rettv->v_type = VAR_STRING;
13653 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654}
13655
13656/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013657 * "gettabvar()" function
13658 */
13659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013660f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013661{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013662 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013663 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013664 dictitem_T *v;
13665 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013666 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013667
13668 rettv->v_type = VAR_STRING;
13669 rettv->vval.v_string = NULL;
13670
13671 varname = get_tv_string_chk(&argvars[1]);
13672 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13673 if (tp != NULL && varname != NULL)
13674 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013675 /* Set tp to be our tabpage, temporarily. Also set the window to the
13676 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013677 if (switch_win(&oldcurwin, &oldtabpage,
13678 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013679 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013680 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013681 /* look up the variable */
13682 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13683 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13684 if (v != NULL)
13685 {
13686 copy_tv(&v->di_tv, rettv);
13687 done = TRUE;
13688 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013689 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013690
13691 /* restore previous notion of curwin */
13692 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013693 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013694
13695 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013696 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013697}
13698
13699/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013700 * "gettabwinvar()" function
13701 */
13702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013703f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013704{
13705 getwinvar(argvars, rettv, 1);
13706}
13707
13708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 * "getwinposx()" function
13710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013712f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715#ifdef FEAT_GUI
13716 if (gui.in_use)
13717 {
13718 int x, y;
13719
13720 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 }
13723#endif
13724}
13725
13726/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013727 * "win_findbuf()" function
13728 */
13729 static void
13730f_win_findbuf(typval_T *argvars, typval_T *rettv)
13731{
13732 if (rettv_list_alloc(rettv) != FAIL)
13733 win_findbuf(argvars, rettv->vval.v_list);
13734}
13735
13736/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013737 * "win_getid()" function
13738 */
13739 static void
13740f_win_getid(typval_T *argvars, typval_T *rettv)
13741{
13742 rettv->vval.v_number = win_getid(argvars);
13743}
13744
13745/*
13746 * "win_gotoid()" function
13747 */
13748 static void
13749f_win_gotoid(typval_T *argvars, typval_T *rettv)
13750{
13751 rettv->vval.v_number = win_gotoid(argvars);
13752}
13753
13754/*
13755 * "win_id2tabwin()" function
13756 */
13757 static void
13758f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13759{
13760 if (rettv_list_alloc(rettv) != FAIL)
13761 win_id2tabwin(argvars, rettv->vval.v_list);
13762}
13763
13764/*
13765 * "win_id2win()" function
13766 */
13767 static void
13768f_win_id2win(typval_T *argvars, typval_T *rettv)
13769{
13770 rettv->vval.v_number = win_id2win(argvars);
13771}
13772
13773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774 * "getwinposy()" function
13775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013777f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780#ifdef FEAT_GUI
13781 if (gui.in_use)
13782 {
13783 int x, y;
13784
13785 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013786 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787 }
13788#endif
13789}
13790
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013791/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013792 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013793 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013794 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013795find_win_by_nr(
13796 typval_T *vp,
13797 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013798{
13799#ifdef FEAT_WINDOWS
13800 win_T *wp;
13801#endif
13802 int nr;
13803
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013804 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013805
13806#ifdef FEAT_WINDOWS
13807 if (nr < 0)
13808 return NULL;
13809 if (nr == 0)
13810 return curwin;
13811
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013812 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13813 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013814 if (nr >= LOWEST_WIN_ID)
13815 {
13816 if (wp->w_id == nr)
13817 return wp;
13818 }
13819 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013820 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013821 if (nr >= LOWEST_WIN_ID)
13822 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013823 return wp;
13824#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013825 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013826 return curwin;
13827 return NULL;
13828#endif
13829}
13830
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013832 * Find window specified by "wvp" in tabpage "tvp".
13833 */
13834 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013835find_tabwin(
13836 typval_T *wvp, /* VAR_UNKNOWN for current window */
13837 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013838{
13839 win_T *wp = NULL;
13840 tabpage_T *tp = NULL;
13841 long n;
13842
13843 if (wvp->v_type != VAR_UNKNOWN)
13844 {
13845 if (tvp->v_type != VAR_UNKNOWN)
13846 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013847 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013848 if (n >= 0)
13849 tp = find_tabpage(n);
13850 }
13851 else
13852 tp = curtab;
13853
13854 if (tp != NULL)
13855 wp = find_win_by_nr(wvp, tp);
13856 }
13857 else
13858 wp = curwin;
13859
13860 return wp;
13861}
13862
13863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 * "getwinvar()" function
13865 */
13866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013867f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013869 getwinvar(argvars, rettv, 0);
13870}
13871
13872/*
13873 * getwinvar() and gettabwinvar()
13874 */
13875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013876getwinvar(
13877 typval_T *argvars,
13878 typval_T *rettv,
13879 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013880{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013881 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013883 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013884 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013885 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013886#ifdef FEAT_WINDOWS
13887 win_T *oldcurwin;
13888 tabpage_T *oldtabpage;
13889 int need_switch_win;
13890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013892#ifdef FEAT_WINDOWS
13893 if (off == 1)
13894 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13895 else
13896 tp = curtab;
13897#endif
13898 win = find_win_by_nr(&argvars[off], tp);
13899 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013900 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013902 rettv->v_type = VAR_STRING;
13903 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904
13905 if (win != NULL && varname != NULL)
13906 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013907#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013908 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013909 * otherwise the window is not valid. Only do this when needed,
13910 * autocommands get blocked. */
13911 need_switch_win = !(tp == curtab && win == curwin);
13912 if (!need_switch_win
13913 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13914#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013915 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013916 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013917 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013918 if (get_option_tv(&varname, rettv, 1) == OK)
13919 done = TRUE;
13920 }
13921 else
13922 {
13923 /* Look up the variable. */
13924 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13925 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13926 varname, FALSE);
13927 if (v != NULL)
13928 {
13929 copy_tv(&v->di_tv, rettv);
13930 done = TRUE;
13931 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013934
Bram Moolenaarba117c22015-09-29 16:53:22 +020013935#ifdef FEAT_WINDOWS
13936 if (need_switch_win)
13937 /* restore previous notion of curwin */
13938 restore_win(oldcurwin, oldtabpage, TRUE);
13939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 }
13941
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013942 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13943 /* use the default return value */
13944 copy_tv(&argvars[off + 2], rettv);
13945
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 --emsg_off;
13947}
13948
13949/*
13950 * "glob()" function
13951 */
13952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013953f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013955 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013957 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013959 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013960 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013962 if (argvars[1].v_type != VAR_UNKNOWN)
13963 {
13964 if (get_tv_number_chk(&argvars[1], &error))
13965 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013967 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013968 if (get_tv_number_chk(&argvars[2], &error))
13969 {
13970 rettv->v_type = VAR_LIST;
13971 rettv->vval.v_list = NULL;
13972 }
13973 if (argvars[3].v_type != VAR_UNKNOWN
13974 && get_tv_number_chk(&argvars[3], &error))
13975 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013976 }
13977 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013978 if (!error)
13979 {
13980 ExpandInit(&xpc);
13981 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013982 if (p_wic)
13983 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013984 if (rettv->v_type == VAR_STRING)
13985 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013986 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013987 else if (rettv_list_alloc(rettv) != FAIL)
13988 {
13989 int i;
13990
13991 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13992 NULL, options, WILD_ALL_KEEP);
13993 for (i = 0; i < xpc.xp_numfiles; i++)
13994 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13995
13996 ExpandCleanup(&xpc);
13997 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013998 }
13999 else
14000 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001}
14002
14003/*
14004 * "globpath()" function
14005 */
14006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014007f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014009 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014012 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014013 garray_T ga;
14014 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000014016 /* When the optional second argument is non-zero, don't remove matches
14017 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014018 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014019 if (argvars[2].v_type != VAR_UNKNOWN)
14020 {
14021 if (get_tv_number_chk(&argvars[2], &error))
14022 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010014023 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014024 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010014025 if (get_tv_number_chk(&argvars[3], &error))
14026 {
14027 rettv->v_type = VAR_LIST;
14028 rettv->vval.v_list = NULL;
14029 }
14030 if (argvars[4].v_type != VAR_UNKNOWN
14031 && get_tv_number_chk(&argvars[4], &error))
14032 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014033 }
14034 }
14035 if (file != NULL && !error)
14036 {
14037 ga_init2(&ga, (int)sizeof(char_u *), 10);
14038 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
14039 if (rettv->v_type == VAR_STRING)
14040 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
14041 else if (rettv_list_alloc(rettv) != FAIL)
14042 for (i = 0; i < ga.ga_len; ++i)
14043 list_append_string(rettv->vval.v_list,
14044 ((char_u **)(ga.ga_data))[i], -1);
14045 ga_clear_strings(&ga);
14046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020014048 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049}
14050
14051/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014052 * "glob2regpat()" function
14053 */
14054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014055f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014056{
14057 char_u *pat = get_tv_string_chk(&argvars[0]);
14058
14059 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010014060 rettv->vval.v_string = (pat == NULL)
14061 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010014062}
14063
14064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 * "has()" function
14066 */
14067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014068f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069{
14070 int i;
14071 char_u *name;
14072 int n = FALSE;
14073 static char *(has_list[]) =
14074 {
14075#ifdef AMIGA
14076 "amiga",
14077# ifdef FEAT_ARP
14078 "arp",
14079# endif
14080#endif
14081#ifdef __BEOS__
14082 "beos",
14083#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000014084#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085 "mac",
14086#endif
14087#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010014088 "macunix", /* built with 'darwin' enabled */
14089#endif
14090#if defined(__APPLE__) && __APPLE__ == 1
14091 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093#ifdef __QNX__
14094 "qnx",
14095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096#ifdef UNIX
14097 "unix",
14098#endif
14099#ifdef VMS
14100 "vms",
14101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102#ifdef WIN32
14103 "win32",
14104#endif
14105#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
14106 "win32unix",
14107#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010014108#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 "win64",
14110#endif
14111#ifdef EBCDIC
14112 "ebcdic",
14113#endif
14114#ifndef CASE_INSENSITIVE_FILENAME
14115 "fname_case",
14116#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014117#ifdef HAVE_ACL
14118 "acl",
14119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120#ifdef FEAT_ARABIC
14121 "arabic",
14122#endif
14123#ifdef FEAT_AUTOCMD
14124 "autocmd",
14125#endif
14126#ifdef FEAT_BEVAL
14127 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000014128# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
14129 "balloon_multiline",
14130# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131#endif
14132#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14133 "builtin_terms",
14134# ifdef ALL_BUILTIN_TCAPS
14135 "all_builtin_terms",
14136# endif
14137#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014138#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14139 || defined(FEAT_GUI_W32) \
14140 || defined(FEAT_GUI_MOTIF))
14141 "browsefilter",
14142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143#ifdef FEAT_BYTEOFF
14144 "byte_offset",
14145#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014146#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014147 "channel",
14148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149#ifdef FEAT_CINDENT
14150 "cindent",
14151#endif
14152#ifdef FEAT_CLIENTSERVER
14153 "clientserver",
14154#endif
14155#ifdef FEAT_CLIPBOARD
14156 "clipboard",
14157#endif
14158#ifdef FEAT_CMDL_COMPL
14159 "cmdline_compl",
14160#endif
14161#ifdef FEAT_CMDHIST
14162 "cmdline_hist",
14163#endif
14164#ifdef FEAT_COMMENTS
14165 "comments",
14166#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014167#ifdef FEAT_CONCEAL
14168 "conceal",
14169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170#ifdef FEAT_CRYPT
14171 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014172 "crypt-blowfish",
14173 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174#endif
14175#ifdef FEAT_CSCOPE
14176 "cscope",
14177#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014178#ifdef FEAT_CURSORBIND
14179 "cursorbind",
14180#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014181#ifdef CURSOR_SHAPE
14182 "cursorshape",
14183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184#ifdef DEBUG
14185 "debug",
14186#endif
14187#ifdef FEAT_CON_DIALOG
14188 "dialog_con",
14189#endif
14190#ifdef FEAT_GUI_DIALOG
14191 "dialog_gui",
14192#endif
14193#ifdef FEAT_DIFF
14194 "diff",
14195#endif
14196#ifdef FEAT_DIGRAPHS
14197 "digraphs",
14198#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014199#ifdef FEAT_DIRECTX
14200 "directx",
14201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202#ifdef FEAT_DND
14203 "dnd",
14204#endif
14205#ifdef FEAT_EMACS_TAGS
14206 "emacs_tags",
14207#endif
14208 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014209 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210#ifdef FEAT_SEARCH_EXTRA
14211 "extra_search",
14212#endif
14213#ifdef FEAT_FKMAP
14214 "farsi",
14215#endif
14216#ifdef FEAT_SEARCHPATH
14217 "file_in_path",
14218#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014219#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014220 "filterpipe",
14221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222#ifdef FEAT_FIND_ID
14223 "find_in_path",
14224#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014225#ifdef FEAT_FLOAT
14226 "float",
14227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228#ifdef FEAT_FOLDING
14229 "folding",
14230#endif
14231#ifdef FEAT_FOOTER
14232 "footer",
14233#endif
14234#if !defined(USE_SYSTEM) && defined(UNIX)
14235 "fork",
14236#endif
14237#ifdef FEAT_GETTEXT
14238 "gettext",
14239#endif
14240#ifdef FEAT_GUI
14241 "gui",
14242#endif
14243#ifdef FEAT_GUI_ATHENA
14244# ifdef FEAT_GUI_NEXTAW
14245 "gui_neXtaw",
14246# else
14247 "gui_athena",
14248# endif
14249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250#ifdef FEAT_GUI_GTK
14251 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014252# ifdef USE_GTK3
14253 "gui_gtk3",
14254# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014258#ifdef FEAT_GUI_GNOME
14259 "gui_gnome",
14260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261#ifdef FEAT_GUI_MAC
14262 "gui_mac",
14263#endif
14264#ifdef FEAT_GUI_MOTIF
14265 "gui_motif",
14266#endif
14267#ifdef FEAT_GUI_PHOTON
14268 "gui_photon",
14269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270#ifdef FEAT_GUI_W32
14271 "gui_win32",
14272#endif
14273#ifdef FEAT_HANGULIN
14274 "hangul_input",
14275#endif
14276#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14277 "iconv",
14278#endif
14279#ifdef FEAT_INS_EXPAND
14280 "insert_expand",
14281#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014282#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014283 "job",
14284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285#ifdef FEAT_JUMPLIST
14286 "jumplist",
14287#endif
14288#ifdef FEAT_KEYMAP
14289 "keymap",
14290#endif
14291#ifdef FEAT_LANGMAP
14292 "langmap",
14293#endif
14294#ifdef FEAT_LIBCALL
14295 "libcall",
14296#endif
14297#ifdef FEAT_LINEBREAK
14298 "linebreak",
14299#endif
14300#ifdef FEAT_LISP
14301 "lispindent",
14302#endif
14303#ifdef FEAT_LISTCMDS
14304 "listcmds",
14305#endif
14306#ifdef FEAT_LOCALMAP
14307 "localmap",
14308#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014309#ifdef FEAT_LUA
14310# ifndef DYNAMIC_LUA
14311 "lua",
14312# endif
14313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314#ifdef FEAT_MENU
14315 "menu",
14316#endif
14317#ifdef FEAT_SESSION
14318 "mksession",
14319#endif
14320#ifdef FEAT_MODIFY_FNAME
14321 "modify_fname",
14322#endif
14323#ifdef FEAT_MOUSE
14324 "mouse",
14325#endif
14326#ifdef FEAT_MOUSESHAPE
14327 "mouseshape",
14328#endif
14329#if defined(UNIX) || defined(VMS)
14330# ifdef FEAT_MOUSE_DEC
14331 "mouse_dec",
14332# endif
14333# ifdef FEAT_MOUSE_GPM
14334 "mouse_gpm",
14335# endif
14336# ifdef FEAT_MOUSE_JSB
14337 "mouse_jsbterm",
14338# endif
14339# ifdef FEAT_MOUSE_NET
14340 "mouse_netterm",
14341# endif
14342# ifdef FEAT_MOUSE_PTERM
14343 "mouse_pterm",
14344# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014345# ifdef FEAT_MOUSE_SGR
14346 "mouse_sgr",
14347# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014348# ifdef FEAT_SYSMOUSE
14349 "mouse_sysmouse",
14350# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014351# ifdef FEAT_MOUSE_URXVT
14352 "mouse_urxvt",
14353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354# ifdef FEAT_MOUSE_XTERM
14355 "mouse_xterm",
14356# endif
14357#endif
14358#ifdef FEAT_MBYTE
14359 "multi_byte",
14360#endif
14361#ifdef FEAT_MBYTE_IME
14362 "multi_byte_ime",
14363#endif
14364#ifdef FEAT_MULTI_LANG
14365 "multi_lang",
14366#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014367#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014368#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014369 "mzscheme",
14370#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014371#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014372#ifdef FEAT_NUM64
14373 "num64",
14374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375#ifdef FEAT_OLE
14376 "ole",
14377#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014378 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#ifdef FEAT_PATH_EXTRA
14380 "path_extra",
14381#endif
14382#ifdef FEAT_PERL
14383#ifndef DYNAMIC_PERL
14384 "perl",
14385#endif
14386#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014387#ifdef FEAT_PERSISTENT_UNDO
14388 "persistent_undo",
14389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390#ifdef FEAT_PYTHON
14391#ifndef DYNAMIC_PYTHON
14392 "python",
14393#endif
14394#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014395#ifdef FEAT_PYTHON3
14396#ifndef DYNAMIC_PYTHON3
14397 "python3",
14398#endif
14399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400#ifdef FEAT_POSTSCRIPT
14401 "postscript",
14402#endif
14403#ifdef FEAT_PRINTER
14404 "printer",
14405#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014406#ifdef FEAT_PROFILE
14407 "profile",
14408#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014409#ifdef FEAT_RELTIME
14410 "reltime",
14411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412#ifdef FEAT_QUICKFIX
14413 "quickfix",
14414#endif
14415#ifdef FEAT_RIGHTLEFT
14416 "rightleft",
14417#endif
14418#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14419 "ruby",
14420#endif
14421#ifdef FEAT_SCROLLBIND
14422 "scrollbind",
14423#endif
14424#ifdef FEAT_CMDL_INFO
14425 "showcmd",
14426 "cmdline_info",
14427#endif
14428#ifdef FEAT_SIGNS
14429 "signs",
14430#endif
14431#ifdef FEAT_SMARTINDENT
14432 "smartindent",
14433#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014434#ifdef STARTUPTIME
14435 "startuptime",
14436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437#ifdef FEAT_STL_OPT
14438 "statusline",
14439#endif
14440#ifdef FEAT_SUN_WORKSHOP
14441 "sun_workshop",
14442#endif
14443#ifdef FEAT_NETBEANS_INTG
14444 "netbeans_intg",
14445#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014446#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014447 "spell",
14448#endif
14449#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 "syntax",
14451#endif
14452#if defined(USE_SYSTEM) || !defined(UNIX)
14453 "system",
14454#endif
14455#ifdef FEAT_TAG_BINS
14456 "tag_binary",
14457#endif
14458#ifdef FEAT_TAG_OLDSTATIC
14459 "tag_old_static",
14460#endif
14461#ifdef FEAT_TAG_ANYWHITE
14462 "tag_any_white",
14463#endif
14464#ifdef FEAT_TCL
14465# ifndef DYNAMIC_TCL
14466 "tcl",
14467# endif
14468#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014469#ifdef FEAT_TERMGUICOLORS
14470 "termguicolors",
14471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472#ifdef TERMINFO
14473 "terminfo",
14474#endif
14475#ifdef FEAT_TERMRESPONSE
14476 "termresponse",
14477#endif
14478#ifdef FEAT_TEXTOBJ
14479 "textobjects",
14480#endif
14481#ifdef HAVE_TGETENT
14482 "tgetent",
14483#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014484#ifdef FEAT_TIMERS
14485 "timers",
14486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487#ifdef FEAT_TITLE
14488 "title",
14489#endif
14490#ifdef FEAT_TOOLBAR
14491 "toolbar",
14492#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014493#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14494 "unnamedplus",
14495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496#ifdef FEAT_USR_CMDS
14497 "user-commands", /* was accidentally included in 5.4 */
14498 "user_commands",
14499#endif
14500#ifdef FEAT_VIMINFO
14501 "viminfo",
14502#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014503#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 "vertsplit",
14505#endif
14506#ifdef FEAT_VIRTUALEDIT
14507 "virtualedit",
14508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510#ifdef FEAT_VISUALEXTRA
14511 "visualextra",
14512#endif
14513#ifdef FEAT_VREPLACE
14514 "vreplace",
14515#endif
14516#ifdef FEAT_WILDIGN
14517 "wildignore",
14518#endif
14519#ifdef FEAT_WILDMENU
14520 "wildmenu",
14521#endif
14522#ifdef FEAT_WINDOWS
14523 "windows",
14524#endif
14525#ifdef FEAT_WAK
14526 "winaltkeys",
14527#endif
14528#ifdef FEAT_WRITEBACKUP
14529 "writebackup",
14530#endif
14531#ifdef FEAT_XIM
14532 "xim",
14533#endif
14534#ifdef FEAT_XFONTSET
14535 "xfontset",
14536#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014537#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014538 "xpm",
14539 "xpm_w32", /* for backward compatibility */
14540#else
14541# if defined(HAVE_XPM)
14542 "xpm",
14543# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545#ifdef USE_XSMP
14546 "xsmp",
14547#endif
14548#ifdef USE_XSMP_INTERACT
14549 "xsmp_interact",
14550#endif
14551#ifdef FEAT_XCLIPBOARD
14552 "xterm_clipboard",
14553#endif
14554#ifdef FEAT_XTERM_SAVE
14555 "xterm_save",
14556#endif
14557#if defined(UNIX) && defined(FEAT_X11)
14558 "X11",
14559#endif
14560 NULL
14561 };
14562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 for (i = 0; has_list[i] != NULL; ++i)
14565 if (STRICMP(name, has_list[i]) == 0)
14566 {
14567 n = TRUE;
14568 break;
14569 }
14570
14571 if (n == FALSE)
14572 {
14573 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014574 {
14575 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014576 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014577 && vim_isdigit(name[6])
14578 && vim_isdigit(name[8])
14579 && vim_isdigit(name[10]))
14580 {
14581 int major = atoi((char *)name + 6);
14582 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014583
14584 /* Expect "patch-9.9.01234". */
14585 n = (major < VIM_VERSION_MAJOR
14586 || (major == VIM_VERSION_MAJOR
14587 && (minor < VIM_VERSION_MINOR
14588 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014589 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014590 }
14591 else
14592 n = has_patch(atoi((char *)name + 5));
14593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 else if (STRICMP(name, "vim_starting") == 0)
14595 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014596#ifdef FEAT_MBYTE
14597 else if (STRICMP(name, "multi_byte_encoding") == 0)
14598 n = has_mbyte;
14599#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014600#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14601 else if (STRICMP(name, "balloon_multiline") == 0)
14602 n = multiline_balloon_available();
14603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604#ifdef DYNAMIC_TCL
14605 else if (STRICMP(name, "tcl") == 0)
14606 n = tcl_enabled(FALSE);
14607#endif
14608#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14609 else if (STRICMP(name, "iconv") == 0)
14610 n = iconv_enabled(FALSE);
14611#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014612#ifdef DYNAMIC_LUA
14613 else if (STRICMP(name, "lua") == 0)
14614 n = lua_enabled(FALSE);
14615#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014616#ifdef DYNAMIC_MZSCHEME
14617 else if (STRICMP(name, "mzscheme") == 0)
14618 n = mzscheme_enabled(FALSE);
14619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620#ifdef DYNAMIC_RUBY
14621 else if (STRICMP(name, "ruby") == 0)
14622 n = ruby_enabled(FALSE);
14623#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014624#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625#ifdef DYNAMIC_PYTHON
14626 else if (STRICMP(name, "python") == 0)
14627 n = python_enabled(FALSE);
14628#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014629#endif
14630#ifdef FEAT_PYTHON3
14631#ifdef DYNAMIC_PYTHON3
14632 else if (STRICMP(name, "python3") == 0)
14633 n = python3_enabled(FALSE);
14634#endif
14635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636#ifdef DYNAMIC_PERL
14637 else if (STRICMP(name, "perl") == 0)
14638 n = perl_enabled(FALSE);
14639#endif
14640#ifdef FEAT_GUI
14641 else if (STRICMP(name, "gui_running") == 0)
14642 n = (gui.in_use || gui.starting);
14643# ifdef FEAT_GUI_W32
14644 else if (STRICMP(name, "gui_win32s") == 0)
14645 n = gui_is_win32s();
14646# endif
14647# ifdef FEAT_BROWSE
14648 else if (STRICMP(name, "browse") == 0)
14649 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14650# endif
14651#endif
14652#ifdef FEAT_SYN_HL
14653 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014654 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655#endif
14656#if defined(WIN3264)
14657 else if (STRICMP(name, "win95") == 0)
14658 n = mch_windows95();
14659#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014660#ifdef FEAT_NETBEANS_INTG
14661 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014662 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 }
14665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014666 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667}
14668
14669/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014670 * "has_key()" function
14671 */
14672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014673f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014674{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014675 if (argvars[0].v_type != VAR_DICT)
14676 {
14677 EMSG(_(e_dictreq));
14678 return;
14679 }
14680 if (argvars[0].vval.v_dict == NULL)
14681 return;
14682
14683 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014684 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014685}
14686
14687/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014688 * "haslocaldir()" function
14689 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014691f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014692{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014693 win_T *wp = NULL;
14694
14695 wp = find_tabwin(&argvars[0], &argvars[1]);
14696 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014697}
14698
14699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700 * "hasmapto()" function
14701 */
14702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014703f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704{
14705 char_u *name;
14706 char_u *mode;
14707 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014708 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014710 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014711 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712 mode = (char_u *)"nvo";
14713 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014716 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014717 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719
Bram Moolenaar2c932302006-03-18 21:42:09 +000014720 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014721 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014723 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724}
14725
14726/*
14727 * "histadd()" function
14728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014730f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731{
14732#ifdef FEAT_CMDHIST
14733 int histype;
14734 char_u *str;
14735 char_u buf[NUMBUFLEN];
14736#endif
14737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014738 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739 if (check_restricted() || check_secure())
14740 return;
14741#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14743 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744 if (histype >= 0)
14745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014746 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 if (*str != NUL)
14748 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014749 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 return;
14753 }
14754 }
14755#endif
14756}
14757
14758/*
14759 * "histdel()" function
14760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014762f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763{
14764#ifdef FEAT_CMDHIST
14765 int n;
14766 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014767 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014769 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14770 if (str == NULL)
14771 n = 0;
14772 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014773 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014774 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014775 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 else
14780 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782 get_tv_string_buf(&argvars[1], buf));
14783 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784#endif
14785}
14786
14787/*
14788 * "histget()" function
14789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014791f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792{
14793#ifdef FEAT_CMDHIST
14794 int type;
14795 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014798 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14799 if (str == NULL)
14800 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014802 {
14803 type = get_histtype(str);
14804 if (argvars[1].v_type == VAR_UNKNOWN)
14805 idx = get_history_idx(type);
14806 else
14807 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14808 /* -1 on type error */
14809 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014812 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014814 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815}
14816
14817/*
14818 * "histnr()" function
14819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
14823 int i;
14824
14825#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014826 char_u *history = get_tv_string_chk(&argvars[0]);
14827
14828 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 if (i >= HIST_CMD && i < HIST_COUNT)
14830 i = get_history_idx(i);
14831 else
14832#endif
14833 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835}
14836
14837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 * "highlightID(name)" function
14839 */
14840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844}
14845
14846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014847 * "highlight_exists()" function
14848 */
14849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014850f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014851{
14852 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14853}
14854
14855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 * "hostname()" function
14857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014859f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860{
14861 char_u hostname[256];
14862
14863 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 rettv->v_type = VAR_STRING;
14865 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866}
14867
14868/*
14869 * iconv() function
14870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014872f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873{
14874#ifdef FEAT_MBYTE
14875 char_u buf1[NUMBUFLEN];
14876 char_u buf2[NUMBUFLEN];
14877 char_u *from, *to, *str;
14878 vimconv_T vimconv;
14879#endif
14880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014881 rettv->v_type = VAR_STRING;
14882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883
14884#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885 str = get_tv_string(&argvars[0]);
14886 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14887 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 vimconv.vc_type = CONV_NONE;
14889 convert_setup(&vimconv, from, to);
14890
14891 /* If the encodings are equal, no conversion needed. */
14892 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014895 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896
14897 convert_setup(&vimconv, NULL, NULL);
14898 vim_free(from);
14899 vim_free(to);
14900#endif
14901}
14902
14903/*
14904 * "indent()" function
14905 */
14906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014907f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908{
14909 linenr_T lnum;
14910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916}
14917
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014918/*
14919 * "index()" function
14920 */
14921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014922f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014923{
Bram Moolenaar33570922005-01-25 22:26:29 +000014924 list_T *l;
14925 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014926 long idx = 0;
14927 int ic = FALSE;
14928
14929 rettv->vval.v_number = -1;
14930 if (argvars[0].v_type != VAR_LIST)
14931 {
14932 EMSG(_(e_listreq));
14933 return;
14934 }
14935 l = argvars[0].vval.v_list;
14936 if (l != NULL)
14937 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014938 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014939 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014941 int error = FALSE;
14942
Bram Moolenaar758711c2005-02-02 23:11:38 +000014943 /* Start at specified item. Use the cached index that list_find()
14944 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014945 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014946 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014947 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014948 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 if (error)
14950 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014951 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014952
Bram Moolenaar758711c2005-02-02 23:11:38 +000014953 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014954 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014955 {
14956 rettv->vval.v_number = idx;
14957 break;
14958 }
14959 }
14960}
14961
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962static int inputsecret_flag = 0;
14963
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014964static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014965
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014967 * This function is used by f_input() and f_inputdialog() functions. The third
14968 * argument to f_input() specifies the type of completion to use at the
14969 * prompt. The third argument to f_inputdialog() specifies the value to return
14970 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 */
14972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014973get_user_input(
14974 typval_T *argvars,
14975 typval_T *rettv,
14976 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 char_u *p = NULL;
14980 int c;
14981 char_u buf[NUMBUFLEN];
14982 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014983 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014984 int xp_type = EXPAND_NOTHING;
14985 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014988 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989
14990#ifdef NO_CONSOLE_INPUT
14991 /* While starting up, there is no place to enter text. */
14992 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014993 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994#endif
14995
14996 cmd_silent = FALSE; /* Want to see the prompt. */
14997 if (prompt != NULL)
14998 {
14999 /* Only the part of the message after the last NL is considered as
15000 * prompt for the command line */
15001 p = vim_strrchr(prompt, '\n');
15002 if (p == NULL)
15003 p = prompt;
15004 else
15005 {
15006 ++p;
15007 c = *p;
15008 *p = NUL;
15009 msg_start();
15010 msg_clr_eos();
15011 msg_puts_attr(prompt, echo_attr);
15012 msg_didout = FALSE;
15013 msg_starthere();
15014 *p = c;
15015 }
15016 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015018 if (argvars[1].v_type != VAR_UNKNOWN)
15019 {
15020 defstr = get_tv_string_buf_chk(&argvars[1], buf);
15021 if (defstr != NULL)
15022 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015024 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000015025 {
15026 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000015027 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000015028 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015029
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015030 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000015031 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015032
Bram Moolenaar4463f292005-09-25 22:20:24 +000015033 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
15034 if (xp_name == NULL)
15035 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015036
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015037 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015038
Bram Moolenaar4463f292005-09-25 22:20:24 +000015039 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
15040 &xp_arg) == FAIL)
15041 return;
15042 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015043 }
15044
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015046 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015047 int save_ex_normal_busy = ex_normal_busy;
15048 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015049 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015050 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
15051 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015052 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020015053 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020015054 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020015055 && argvars[1].v_type != VAR_UNKNOWN
15056 && argvars[2].v_type != VAR_UNKNOWN)
15057 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
15058 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000015059
15060 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015062 /* since the user typed this, no need to wait for return */
15063 need_wait_return = FALSE;
15064 msg_didout = FALSE;
15065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 cmd_silent = cmd_silent_save;
15067}
15068
15069/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015070 * "input()" function
15071 * Also handles inputsecret() when inputsecret is set.
15072 */
15073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015074f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015075{
15076 get_user_input(argvars, rettv, FALSE);
15077}
15078
15079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 * "inputdialog()" function
15081 */
15082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015083f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084{
15085#if defined(FEAT_GUI_TEXTDIALOG)
15086 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
15087 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
15088 {
15089 char_u *message;
15090 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000015092
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 message = get_tv_string_chk(&argvars[0]);
15094 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000015095 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000015096 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097 else
15098 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 if (message != NULL && defstr != NULL
15100 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010015101 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 else
15104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015105 if (message != NULL && defstr != NULL
15106 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015107 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015108 rettv->vval.v_string = vim_strsave(
15109 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015113 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 }
15115 else
15116#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000015117 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015118}
15119
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015120/*
15121 * "inputlist()" function
15122 */
15123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015124f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015125{
15126 listitem_T *li;
15127 int selected;
15128 int mouse_used;
15129
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015130#ifdef NO_CONSOLE_INPUT
15131 /* While starting up, there is no place to enter text. */
15132 if (no_console_input())
15133 return;
15134#endif
15135 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15136 {
15137 EMSG2(_(e_listarg), "inputlist()");
15138 return;
15139 }
15140
15141 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015142 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015143 lines_left = Rows; /* avoid more prompt */
15144 msg_scroll = TRUE;
15145 msg_clr_eos();
15146
15147 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15148 {
15149 msg_puts(get_tv_string(&li->li_tv));
15150 msg_putchar('\n');
15151 }
15152
15153 /* Ask for choice. */
15154 selected = prompt_for_number(&mouse_used);
15155 if (mouse_used)
15156 selected -= lines_left;
15157
15158 rettv->vval.v_number = selected;
15159}
15160
15161
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15163
15164/*
15165 * "inputrestore()" function
15166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015168f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169{
15170 if (ga_userinput.ga_len > 0)
15171 {
15172 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15174 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015175 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176 }
15177 else if (p_verbose > 1)
15178 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015179 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 }
15182}
15183
15184/*
15185 * "inputsave()" function
15186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015188f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015189{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015190 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015191 if (ga_grow(&ga_userinput, 1) == OK)
15192 {
15193 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15194 + ga_userinput.ga_len);
15195 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015196 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015197 }
15198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015199 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200}
15201
15202/*
15203 * "inputsecret()" function
15204 */
15205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015206f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207{
15208 ++cmdline_star;
15209 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015210 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 --cmdline_star;
15212 --inputsecret_flag;
15213}
15214
15215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015216 * "insert()" function
15217 */
15218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015219f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015220{
15221 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015222 listitem_T *item;
15223 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015224 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015225
15226 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015228 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015229 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015230 {
15231 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015232 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015233 if (error)
15234 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015235
Bram Moolenaar758711c2005-02-02 23:11:38 +000015236 if (before == l->lv_len)
15237 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015238 else
15239 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015240 item = list_find(l, before);
15241 if (item == NULL)
15242 {
15243 EMSGN(_(e_listidx), before);
15244 l = NULL;
15245 }
15246 }
15247 if (l != NULL)
15248 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015249 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015250 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015251 }
15252 }
15253}
15254
15255/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015256 * "invert(expr)" function
15257 */
15258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015259f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015260{
15261 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15262}
15263
15264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265 * "isdirectory()" function
15266 */
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015270 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271}
15272
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015273/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015274 * "islocked()" function
15275 */
15276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015277f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015278{
15279 lval_T lv;
15280 char_u *end;
15281 dictitem_T *di;
15282
15283 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015284 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15285 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015286 if (end != NULL && lv.ll_name != NULL)
15287 {
15288 if (*end != NUL)
15289 EMSG(_(e_trailing));
15290 else
15291 {
15292 if (lv.ll_tv == NULL)
15293 {
15294 if (check_changedtick(lv.ll_name))
15295 rettv->vval.v_number = 1; /* always locked */
15296 else
15297 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015298 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015299 if (di != NULL)
15300 {
15301 /* Consider a variable locked when:
15302 * 1. the variable itself is locked
15303 * 2. the value of the variable is locked.
15304 * 3. the List or Dict value is locked.
15305 */
15306 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15307 || tv_islocked(&di->di_tv));
15308 }
15309 }
15310 }
15311 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015312 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015313 else if (lv.ll_newkey != NULL)
15314 EMSG2(_(e_dictkey), lv.ll_newkey);
15315 else if (lv.ll_list != NULL)
15316 /* List item. */
15317 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15318 else
15319 /* Dictionary item. */
15320 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15321 }
15322 }
15323
15324 clear_lval(&lv);
15325}
15326
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015327#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15328/*
15329 * "isnan()" function
15330 */
15331 static void
15332f_isnan(typval_T *argvars, typval_T *rettv)
15333{
15334 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15335 && isnan(argvars[0].vval.v_float);
15336}
15337#endif
15338
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015339static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015340
15341/*
15342 * Turn a dict into a list:
15343 * "what" == 0: list of keys
15344 * "what" == 1: list of values
15345 * "what" == 2: list of items
15346 */
15347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015348dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015349{
Bram Moolenaar33570922005-01-25 22:26:29 +000015350 list_T *l2;
15351 dictitem_T *di;
15352 hashitem_T *hi;
15353 listitem_T *li;
15354 listitem_T *li2;
15355 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015356 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015357
Bram Moolenaar8c711452005-01-14 21:53:12 +000015358 if (argvars[0].v_type != VAR_DICT)
15359 {
15360 EMSG(_(e_dictreq));
15361 return;
15362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015363 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015364 return;
15365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015366 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015367 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015369 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015370 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015374 --todo;
15375 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015376
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015377 li = listitem_alloc();
15378 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015379 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015380 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015382 if (what == 0)
15383 {
15384 /* keys() */
15385 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015386 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015387 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15388 }
15389 else if (what == 1)
15390 {
15391 /* values() */
15392 copy_tv(&di->di_tv, &li->li_tv);
15393 }
15394 else
15395 {
15396 /* items() */
15397 l2 = list_alloc();
15398 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015399 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015400 li->li_tv.vval.v_list = l2;
15401 if (l2 == NULL)
15402 break;
15403 ++l2->lv_refcount;
15404
15405 li2 = listitem_alloc();
15406 if (li2 == NULL)
15407 break;
15408 list_append(l2, li2);
15409 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015410 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015411 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15412
15413 li2 = listitem_alloc();
15414 if (li2 == NULL)
15415 break;
15416 list_append(l2, li2);
15417 copy_tv(&di->di_tv, &li2->li_tv);
15418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015419 }
15420 }
15421}
15422
15423/*
15424 * "items(dict)" function
15425 */
15426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015427f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015428{
15429 dict_list(argvars, rettv, 2);
15430}
15431
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015432#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015433/*
15434 * Get the job from the argument.
15435 * Returns NULL if the job is invalid.
15436 */
15437 static job_T *
15438get_job_arg(typval_T *tv)
15439{
15440 job_T *job;
15441
15442 if (tv->v_type != VAR_JOB)
15443 {
15444 EMSG2(_(e_invarg2), get_tv_string(tv));
15445 return NULL;
15446 }
15447 job = tv->vval.v_job;
15448
15449 if (job == NULL)
15450 EMSG(_("E916: not a valid job"));
15451 return job;
15452}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015453
Bram Moolenaar835dc632016-02-07 14:27:38 +010015454/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015455 * "job_getchannel()" function
15456 */
15457 static void
15458f_job_getchannel(typval_T *argvars, typval_T *rettv)
15459{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015460 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015461
Bram Moolenaar65edff82016-02-21 16:40:11 +010015462 if (job != NULL)
15463 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015464 rettv->v_type = VAR_CHANNEL;
15465 rettv->vval.v_channel = job->jv_channel;
15466 if (job->jv_channel != NULL)
15467 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015468 }
15469}
15470
15471/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015472 * "job_info()" function
15473 */
15474 static void
15475f_job_info(typval_T *argvars, typval_T *rettv)
15476{
15477 job_T *job = get_job_arg(&argvars[0]);
15478
15479 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15480 job_info(job, rettv->vval.v_dict);
15481}
15482
15483/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015484 * "job_setoptions()" function
15485 */
15486 static void
15487f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15488{
15489 job_T *job = get_job_arg(&argvars[0]);
15490 jobopt_T opt;
15491
15492 if (job == NULL)
15493 return;
15494 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015495 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15496 job_set_options(job, &opt);
15497 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015498}
15499
15500/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015501 * "job_start()" function
15502 */
15503 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015504f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015505{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015506 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015507 if (check_restricted() || check_secure())
15508 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015509 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015510}
15511
15512/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015513 * "job_status()" function
15514 */
15515 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015516f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015517{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015518 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015519
Bram Moolenaar65edff82016-02-21 16:40:11 +010015520 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015521 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015522 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015523 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015524 }
15525}
15526
15527/*
15528 * "job_stop()" function
15529 */
15530 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015531f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015532{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015533 job_T *job = get_job_arg(&argvars[0]);
15534
15535 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015536 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015537}
15538#endif
15539
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015541 * "join()" function
15542 */
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015545{
15546 garray_T ga;
15547 char_u *sep;
15548
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015549 if (argvars[0].v_type != VAR_LIST)
15550 {
15551 EMSG(_(e_listreq));
15552 return;
15553 }
15554 if (argvars[0].vval.v_list == NULL)
15555 return;
15556 if (argvars[1].v_type == VAR_UNKNOWN)
15557 sep = (char_u *)" ";
15558 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015559 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015560
15561 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562
15563 if (sep != NULL)
15564 {
15565 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015566 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 ga_append(&ga, NUL);
15568 rettv->vval.v_string = (char_u *)ga.ga_data;
15569 }
15570 else
15571 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015572}
15573
15574/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015575 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015576 */
15577 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015578f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015579{
15580 js_read_T reader;
15581
15582 reader.js_buf = get_tv_string(&argvars[0]);
15583 reader.js_fill = NULL;
15584 reader.js_used = 0;
15585 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15586 EMSG(_(e_invarg));
15587}
15588
15589/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015590 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015591 */
15592 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015593f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015594{
15595 rettv->v_type = VAR_STRING;
15596 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15597}
15598
15599/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015600 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015601 */
15602 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015603f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015604{
15605 js_read_T reader;
15606
15607 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015608 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015609 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015610 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015611 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015612}
15613
15614/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015615 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015616 */
15617 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015618f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015619{
15620 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015621 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015622}
15623
15624/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015625 * "keys()" function
15626 */
15627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015628f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015629{
15630 dict_list(argvars, rettv, 0);
15631}
15632
15633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 * "last_buffer_nr()" function.
15635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015637f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638{
15639 int n = 0;
15640 buf_T *buf;
15641
15642 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15643 if (n < buf->b_fnum)
15644 n = buf->b_fnum;
15645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015646 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015647}
15648
15649/*
15650 * "len()" function
15651 */
15652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015653f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015654{
15655 switch (argvars[0].v_type)
15656 {
15657 case VAR_STRING:
15658 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015659 rettv->vval.v_number = (varnumber_T)STRLEN(
15660 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015661 break;
15662 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015663 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015664 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015665 case VAR_DICT:
15666 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15667 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015668 case VAR_UNKNOWN:
15669 case VAR_SPECIAL:
15670 case VAR_FLOAT:
15671 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015672 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015673 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015674 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015675 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015676 break;
15677 }
15678}
15679
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015680static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015681
15682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015683libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015684{
15685#ifdef FEAT_LIBCALL
15686 char_u *string_in;
15687 char_u **string_result;
15688 int nr_result;
15689#endif
15690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015692 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015693 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015694
15695 if (check_restricted() || check_secure())
15696 return;
15697
15698#ifdef FEAT_LIBCALL
15699 /* The first two args must be strings, otherwise its meaningless */
15700 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15701 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015702 string_in = NULL;
15703 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015704 string_in = argvars[2].vval.v_string;
15705 if (type == VAR_NUMBER)
15706 string_result = NULL;
15707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015708 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015709 if (mch_libcall(argvars[0].vval.v_string,
15710 argvars[1].vval.v_string,
15711 string_in,
15712 argvars[2].vval.v_number,
15713 string_result,
15714 &nr_result) == OK
15715 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015717 }
15718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719}
15720
15721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015722 * "libcall()" function
15723 */
15724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015725f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015726{
15727 libcall_common(argvars, rettv, VAR_STRING);
15728}
15729
15730/*
15731 * "libcallnr()" function
15732 */
15733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015734f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735{
15736 libcall_common(argvars, rettv, VAR_NUMBER);
15737}
15738
15739/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 * "line(string)" function
15741 */
15742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015743f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744{
15745 linenr_T lnum = 0;
15746 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015747 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015749 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 if (fp != NULL)
15751 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015752 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753}
15754
15755/*
15756 * "line2byte(lnum)" function
15757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015759f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760{
15761#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015762 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763#else
15764 linenr_T lnum;
15765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015766 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015770 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15771 if (rettv->vval.v_number >= 0)
15772 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773#endif
15774}
15775
15776/*
15777 * "lispindent(lnum)" function
15778 */
15779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015780f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781{
15782#ifdef FEAT_LISP
15783 pos_T pos;
15784 linenr_T lnum;
15785
15786 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15789 {
15790 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015791 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 curwin->w_cursor = pos;
15793 }
15794 else
15795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797}
15798
15799/*
15800 * "localtime()" function
15801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015803f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015805 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806}
15807
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015808static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809
15810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015811get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812{
15813 char_u *keys;
15814 char_u *which;
15815 char_u buf[NUMBUFLEN];
15816 char_u *keys_buf = NULL;
15817 char_u *rhs;
15818 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015819 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015820 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015821 mapblock_T *mp;
15822 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823
15824 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015825 rettv->v_type = VAR_STRING;
15826 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015828 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829 if (*keys == NUL)
15830 return;
15831
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015832 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015834 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015836 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015837 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015838 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015839 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015840 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 else
15843 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015844 if (which == NULL)
15845 return;
15846
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 mode = get_map_mode(&which, 0);
15848
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015849 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015850 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015852
15853 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015854 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015855 /* Return a string. */
15856 if (rhs != NULL)
15857 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858
Bram Moolenaarbd743252010-10-20 21:23:33 +020015859 }
15860 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15861 {
15862 /* Return a dictionary. */
15863 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15864 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15865 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015866
Bram Moolenaarbd743252010-10-20 21:23:33 +020015867 dict_add_nr_str(dict, "lhs", 0L, lhs);
15868 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15869 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15870 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15871 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15872 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15873 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015874 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015875 dict_add_nr_str(dict, "mode", 0L, mapmode);
15876
15877 vim_free(lhs);
15878 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879 }
15880}
15881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015882#ifdef FEAT_FLOAT
15883/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015884 * "log()" function
15885 */
15886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015887f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015888{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015889 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015890
15891 rettv->v_type = VAR_FLOAT;
15892 if (get_float_arg(argvars, &f) == OK)
15893 rettv->vval.v_float = log(f);
15894 else
15895 rettv->vval.v_float = 0.0;
15896}
15897
15898/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015899 * "log10()" function
15900 */
15901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015902f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015903{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015904 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015905
15906 rettv->v_type = VAR_FLOAT;
15907 if (get_float_arg(argvars, &f) == OK)
15908 rettv->vval.v_float = log10(f);
15909 else
15910 rettv->vval.v_float = 0.0;
15911}
15912#endif
15913
Bram Moolenaar1dced572012-04-05 16:54:08 +020015914#ifdef FEAT_LUA
15915/*
15916 * "luaeval()" function
15917 */
15918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015919f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015920{
15921 char_u *str;
15922 char_u buf[NUMBUFLEN];
15923
15924 str = get_tv_string_buf(&argvars[0], buf);
15925 do_luaeval(str, argvars + 1, rettv);
15926}
15927#endif
15928
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015930 * "map()" function
15931 */
15932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015933f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015934{
15935 filter_map(argvars, rettv, TRUE);
15936}
15937
15938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015939 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 */
15941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015942f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945}
15946
15947/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015948 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 */
15950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015951f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015953 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954}
15955
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015956static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957
15958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015959find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015960{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015961 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015962 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015963 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 char_u *pat;
15965 regmatch_T regmatch;
15966 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015967 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 char_u *save_cpo;
15969 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015970 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015971 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015972 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015973 list_T *l = NULL;
15974 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015975 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015976 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977
15978 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15979 save_cpo = p_cpo;
15980 p_cpo = (char_u *)"";
15981
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015982 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015983 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015984 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015985 /* type 3: return empty list when there are no matches.
15986 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015987 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015988 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015989 if (type == 4
15990 && (list_append_string(rettv->vval.v_list,
15991 (char_u *)"", 0) == FAIL
15992 || list_append_number(rettv->vval.v_list,
15993 (varnumber_T)-1) == FAIL
15994 || list_append_number(rettv->vval.v_list,
15995 (varnumber_T)-1) == FAIL
15996 || list_append_number(rettv->vval.v_list,
15997 (varnumber_T)-1) == FAIL))
15998 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015999 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016000 rettv->vval.v_list = NULL;
16001 goto theend;
16002 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016003 }
16004 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016006 rettv->v_type = VAR_STRING;
16007 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016010 if (argvars[0].v_type == VAR_LIST)
16011 {
16012 if ((l = argvars[0].vval.v_list) == NULL)
16013 goto theend;
16014 li = l->lv_first;
16015 }
16016 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016017 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016018 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016019 len = (long)STRLEN(str);
16020 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16023 if (pat == NULL)
16024 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016025
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 int error = FALSE;
16029
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016030 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 if (error)
16032 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016033 if (l != NULL)
16034 {
16035 li = list_find(l, start);
16036 if (li == NULL)
16037 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016038 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016039 }
16040 else
16041 {
16042 if (start < 0)
16043 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016044 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016045 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016046 /* When "count" argument is there ignore matches before "start",
16047 * otherwise skip part of the string. Differs when pattern is "^"
16048 * or "\<". */
16049 if (argvars[3].v_type != VAR_UNKNOWN)
16050 startcol = start;
16051 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016052 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016053 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016054 len -= start;
16055 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016056 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016057
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016058 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016059 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016060 if (error)
16061 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062 }
16063
16064 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16065 if (regmatch.regprog != NULL)
16066 {
16067 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016068
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016069 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016070 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016071 if (l != NULL)
16072 {
16073 if (li == NULL)
16074 {
16075 match = FALSE;
16076 break;
16077 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016078 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016079 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016080 if (str == NULL)
16081 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016082 }
16083
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000016084 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016085
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016086 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016087 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016088 if (l == NULL && !match)
16089 break;
16090
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016091 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016092 if (l != NULL)
16093 {
16094 li = li->li_next;
16095 ++idx;
16096 }
16097 else
16098 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016099#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016100 startcol = (colnr_T)(regmatch.startp[0]
16101 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016102#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020016103 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016104#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010016105 if (startcol > (colnr_T)len
16106 || str + startcol <= regmatch.startp[0])
16107 {
16108 match = FALSE;
16109 break;
16110 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016111 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000016112 }
16113
16114 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016116 if (type == 4)
16117 {
16118 listitem_T *li1 = rettv->vval.v_list->lv_first;
16119 listitem_T *li2 = li1->li_next;
16120 listitem_T *li3 = li2->li_next;
16121 listitem_T *li4 = li3->li_next;
16122
Bram Moolenaar3c809342016-06-01 22:34:48 +020016123 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016124 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
16125 (int)(regmatch.endp[0] - regmatch.startp[0]));
16126 li3->li_tv.vval.v_number =
16127 (varnumber_T)(regmatch.startp[0] - expr);
16128 li4->li_tv.vval.v_number =
16129 (varnumber_T)(regmatch.endp[0] - expr);
16130 if (l != NULL)
16131 li2->li_tv.vval.v_number = (varnumber_T)idx;
16132 }
16133 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016134 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016135 int i;
16136
16137 /* return list with matched string and submatches */
16138 for (i = 0; i < NSUBEXP; ++i)
16139 {
16140 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016141 {
16142 if (list_append_string(rettv->vval.v_list,
16143 (char_u *)"", 0) == FAIL)
16144 break;
16145 }
16146 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016147 regmatch.startp[i],
16148 (int)(regmatch.endp[i] - regmatch.startp[i]))
16149 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016150 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016151 }
16152 }
16153 else if (type == 2)
16154 {
16155 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016156 if (l != NULL)
16157 copy_tv(&li->li_tv, rettv);
16158 else
16159 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016161 }
16162 else if (l != NULL)
16163 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 else
16165 {
16166 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016167 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 (varnumber_T)(regmatch.startp[0] - str);
16169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016170 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016172 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 }
16174 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016175 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 }
16177
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016178 if (type == 4 && l == NULL)
16179 /* matchstrpos() without a list: drop the second item. */
16180 listitem_remove(rettv->vval.v_list,
16181 rettv->vval.v_list->lv_first->li_next);
16182
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016184 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 p_cpo = save_cpo;
16186}
16187
16188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 * "match()" function
16190 */
16191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016192f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193{
16194 find_some_match(argvars, rettv, 1);
16195}
16196
16197/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016198 * "matchadd()" function
16199 */
16200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016201f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016202{
16203#ifdef FEAT_SEARCH_EXTRA
16204 char_u buf[NUMBUFLEN];
16205 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16206 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16207 int prio = 10; /* default priority */
16208 int id = -1;
16209 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016210 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016211
16212 rettv->vval.v_number = -1;
16213
16214 if (grp == NULL || pat == NULL)
16215 return;
16216 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016217 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016218 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016219 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016220 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016221 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016222 if (argvars[4].v_type != VAR_UNKNOWN)
16223 {
16224 if (argvars[4].v_type != VAR_DICT)
16225 {
16226 EMSG(_(e_dictreq));
16227 return;
16228 }
16229 if (dict_find(argvars[4].vval.v_dict,
16230 (char_u *)"conceal", -1) != NULL)
16231 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16232 (char_u *)"conceal", FALSE);
16233 }
16234 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016235 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016236 if (error == TRUE)
16237 return;
16238 if (id >= 1 && id <= 3)
16239 {
16240 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16241 return;
16242 }
16243
Bram Moolenaar6561d522015-07-21 15:48:27 +020016244 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16245 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016246#endif
16247}
16248
16249/*
16250 * "matchaddpos()" function
16251 */
16252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016253f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016254{
16255#ifdef FEAT_SEARCH_EXTRA
16256 char_u buf[NUMBUFLEN];
16257 char_u *group;
16258 int prio = 10;
16259 int id = -1;
16260 int error = FALSE;
16261 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016262 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016263
16264 rettv->vval.v_number = -1;
16265
16266 group = get_tv_string_buf_chk(&argvars[0], buf);
16267 if (group == NULL)
16268 return;
16269
16270 if (argvars[1].v_type != VAR_LIST)
16271 {
16272 EMSG2(_(e_listarg), "matchaddpos()");
16273 return;
16274 }
16275 l = argvars[1].vval.v_list;
16276 if (l == NULL)
16277 return;
16278
16279 if (argvars[2].v_type != VAR_UNKNOWN)
16280 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016281 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016282 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016283 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016284 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016285 if (argvars[4].v_type != VAR_UNKNOWN)
16286 {
16287 if (argvars[4].v_type != VAR_DICT)
16288 {
16289 EMSG(_(e_dictreq));
16290 return;
16291 }
16292 if (dict_find(argvars[4].vval.v_dict,
16293 (char_u *)"conceal", -1) != NULL)
16294 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16295 (char_u *)"conceal", FALSE);
16296 }
16297 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016298 }
16299 if (error == TRUE)
16300 return;
16301
16302 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16303 if (id == 1 || id == 2)
16304 {
16305 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16306 return;
16307 }
16308
Bram Moolenaar6561d522015-07-21 15:48:27 +020016309 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16310 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016311#endif
16312}
16313
16314/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016315 * "matcharg()" function
16316 */
16317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016318f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016319{
16320 if (rettv_list_alloc(rettv) == OK)
16321 {
16322#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016323 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016324 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016325
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016326 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016327 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016328 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16329 {
16330 list_append_string(rettv->vval.v_list,
16331 syn_id2name(m->hlg_id), -1);
16332 list_append_string(rettv->vval.v_list, m->pattern, -1);
16333 }
16334 else
16335 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016336 list_append_string(rettv->vval.v_list, NULL, -1);
16337 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016338 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016339 }
16340#endif
16341 }
16342}
16343
16344/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016345 * "matchdelete()" function
16346 */
16347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016348f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016349{
16350#ifdef FEAT_SEARCH_EXTRA
16351 rettv->vval.v_number = match_delete(curwin,
16352 (int)get_tv_number(&argvars[0]), TRUE);
16353#endif
16354}
16355
16356/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 * "matchend()" function
16358 */
16359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016360f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361{
16362 find_some_match(argvars, rettv, 0);
16363}
16364
16365/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016366 * "matchlist()" function
16367 */
16368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016370{
16371 find_some_match(argvars, rettv, 3);
16372}
16373
16374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 * "matchstr()" function
16376 */
16377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016378f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379{
16380 find_some_match(argvars, rettv, 2);
16381}
16382
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016383/*
16384 * "matchstrpos()" function
16385 */
16386 static void
16387f_matchstrpos(typval_T *argvars, typval_T *rettv)
16388{
16389 find_some_match(argvars, rettv, 4);
16390}
16391
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016392static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016393
16394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016395max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016396{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016397 varnumber_T n = 0;
16398 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016400
16401 if (argvars[0].v_type == VAR_LIST)
16402 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 list_T *l;
16404 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016405
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016406 l = argvars[0].vval.v_list;
16407 if (l != NULL)
16408 {
16409 li = l->lv_first;
16410 if (li != NULL)
16411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016413 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016414 {
16415 li = li->li_next;
16416 if (li == NULL)
16417 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016418 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016419 if (domax ? i > n : i < n)
16420 n = i;
16421 }
16422 }
16423 }
16424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016425 else if (argvars[0].v_type == VAR_DICT)
16426 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016427 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016428 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016430 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016431
16432 d = argvars[0].vval.v_dict;
16433 if (d != NULL)
16434 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016435 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016436 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016438 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016439 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016440 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016442 if (first)
16443 {
16444 n = i;
16445 first = FALSE;
16446 }
16447 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016448 n = i;
16449 }
16450 }
16451 }
16452 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016453 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016454 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016455 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016456}
16457
16458/*
16459 * "max()" function
16460 */
16461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016462f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016463{
16464 max_min(argvars, rettv, TRUE);
16465}
16466
16467/*
16468 * "min()" function
16469 */
16470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016471f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016472{
16473 max_min(argvars, rettv, FALSE);
16474}
16475
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016476static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016477
16478/*
16479 * Create the directory in which "dir" is located, and higher levels when
16480 * needed.
16481 */
16482 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016484{
16485 char_u *p;
16486 char_u *updir;
16487 int r = FAIL;
16488
16489 /* Get end of directory name in "dir".
16490 * We're done when it's "/" or "c:/". */
16491 p = gettail_sep(dir);
16492 if (p <= get_past_head(dir))
16493 return OK;
16494
16495 /* If the directory exists we're done. Otherwise: create it.*/
16496 updir = vim_strnsave(dir, (int)(p - dir));
16497 if (updir == NULL)
16498 return FAIL;
16499 if (mch_isdir(updir))
16500 r = OK;
16501 else if (mkdir_recurse(updir, prot) == OK)
16502 r = vim_mkdir_emsg(updir, prot);
16503 vim_free(updir);
16504 return r;
16505}
16506
16507#ifdef vim_mkdir
16508/*
16509 * "mkdir()" function
16510 */
16511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016512f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016513{
16514 char_u *dir;
16515 char_u buf[NUMBUFLEN];
16516 int prot = 0755;
16517
16518 rettv->vval.v_number = FAIL;
16519 if (check_restricted() || check_secure())
16520 return;
16521
16522 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016523 if (*dir == NUL)
16524 rettv->vval.v_number = FAIL;
16525 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016526 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016527 if (*gettail(dir) == NUL)
16528 /* remove trailing slashes */
16529 *gettail_sep(dir) = NUL;
16530
16531 if (argvars[1].v_type != VAR_UNKNOWN)
16532 {
16533 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016534 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016535 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16536 mkdir_recurse(dir, prot);
16537 }
16538 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016539 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016540}
16541#endif
16542
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 * "mode()" function
16545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016547f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016548{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016549 char_u buf[3];
16550
16551 buf[1] = NUL;
16552 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016553
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016554 if (time_for_testing == 93784)
16555 {
16556 /* Testing the two-character code. */
16557 buf[0] = 'x';
16558 buf[1] = '!';
16559 }
16560 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016561 {
16562 if (VIsual_select)
16563 buf[0] = VIsual_mode + 's' - 'v';
16564 else
16565 buf[0] = VIsual_mode;
16566 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016567 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016568 || State == CONFIRM)
16569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016571 if (State == ASKMORE)
16572 buf[1] = 'm';
16573 else if (State == CONFIRM)
16574 buf[1] = '?';
16575 }
16576 else if (State == EXTERNCMD)
16577 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016578 else if (State & INSERT)
16579 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016580#ifdef FEAT_VREPLACE
16581 if (State & VREPLACE_FLAG)
16582 {
16583 buf[0] = 'R';
16584 buf[1] = 'v';
16585 }
16586 else
16587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588 if (State & REPLACE_FLAG)
16589 buf[0] = 'R';
16590 else
16591 buf[0] = 'i';
16592 }
16593 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016596 if (exmode_active)
16597 buf[1] = 'v';
16598 }
16599 else if (exmode_active)
16600 {
16601 buf[0] = 'c';
16602 buf[1] = 'e';
16603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016604 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016605 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016606 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016607 if (finish_op)
16608 buf[1] = 'o';
16609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016610
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016611 /* Clear out the minor mode when the argument is not a non-zero number or
16612 * non-empty string. */
16613 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016614 buf[1] = NUL;
16615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016616 rettv->vval.v_string = vim_strsave(buf);
16617 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016618}
16619
Bram Moolenaar429fa852013-04-15 12:27:36 +020016620#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016621/*
16622 * "mzeval()" function
16623 */
16624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016625f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016626{
16627 char_u *str;
16628 char_u buf[NUMBUFLEN];
16629
16630 str = get_tv_string_buf(&argvars[0], buf);
16631 do_mzeval(str, rettv);
16632}
Bram Moolenaar75676462013-01-30 14:55:42 +010016633
16634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016635mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016636{
16637 typval_T argvars[3];
16638
16639 argvars[0].v_type = VAR_STRING;
16640 argvars[0].vval.v_string = name;
16641 copy_tv(args, &argvars[1]);
16642 argvars[2].v_type = VAR_UNKNOWN;
16643 f_call(argvars, rettv);
16644 clear_tv(&argvars[1]);
16645}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016646#endif
16647
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016649 * "nextnonblank()" function
16650 */
16651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016652f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653{
16654 linenr_T lnum;
16655
16656 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 {
16660 lnum = 0;
16661 break;
16662 }
16663 if (*skipwhite(ml_get(lnum)) != NUL)
16664 break;
16665 }
16666 rettv->vval.v_number = lnum;
16667}
16668
16669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 * "nr2char()" function
16671 */
16672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016673f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674{
16675 char_u buf[NUMBUFLEN];
16676
16677#ifdef FEAT_MBYTE
16678 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016679 {
16680 int utf8 = 0;
16681
16682 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016683 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016684 if (utf8)
16685 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16686 else
16687 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016689 else
16690#endif
16691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016692 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 buf[1] = NUL;
16694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016695 rettv->v_type = VAR_STRING;
16696 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016697}
16698
16699/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016700 * "or(expr, expr)" function
16701 */
16702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016703f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016704{
16705 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16706 | get_tv_number_chk(&argvars[1], NULL);
16707}
16708
16709/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016710 * "pathshorten()" function
16711 */
16712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016713f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016714{
16715 char_u *p;
16716
16717 rettv->v_type = VAR_STRING;
16718 p = get_tv_string_chk(&argvars[0]);
16719 if (p == NULL)
16720 rettv->vval.v_string = NULL;
16721 else
16722 {
16723 p = vim_strsave(p);
16724 rettv->vval.v_string = p;
16725 if (p != NULL)
16726 shorten_dir(p);
16727 }
16728}
16729
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016730#ifdef FEAT_PERL
16731/*
16732 * "perleval()" function
16733 */
16734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016735f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016736{
16737 char_u *str;
16738 char_u buf[NUMBUFLEN];
16739
16740 str = get_tv_string_buf(&argvars[0], buf);
16741 do_perleval(str, rettv);
16742}
16743#endif
16744
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016745#ifdef FEAT_FLOAT
16746/*
16747 * "pow()" function
16748 */
16749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016750f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016751{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016752 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016753
16754 rettv->v_type = VAR_FLOAT;
16755 if (get_float_arg(argvars, &fx) == OK
16756 && get_float_arg(&argvars[1], &fy) == OK)
16757 rettv->vval.v_float = pow(fx, fy);
16758 else
16759 rettv->vval.v_float = 0.0;
16760}
16761#endif
16762
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016764 * "prevnonblank()" function
16765 */
16766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016767f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016768{
16769 linenr_T lnum;
16770
16771 lnum = get_tv_lnum(argvars);
16772 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16773 lnum = 0;
16774 else
16775 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16776 --lnum;
16777 rettv->vval.v_number = lnum;
16778}
16779
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016780/* This dummy va_list is here because:
16781 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16782 * - locally in the function results in a "used before set" warning
16783 * - using va_start() to initialize it gives "function with fixed args" error */
16784static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016785
Bram Moolenaar8c711452005-01-14 21:53:12 +000016786/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016787 * "printf()" function
16788 */
16789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016790f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016791{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016792 char_u buf[NUMBUFLEN];
16793 int len;
16794 char_u *s;
16795 int saved_did_emsg = did_emsg;
16796 char *fmt;
16797
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016798 rettv->v_type = VAR_STRING;
16799 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016800
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016801 /* Get the required length, allocate the buffer and do it for real. */
16802 did_emsg = FALSE;
16803 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16804 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16805 if (!did_emsg)
16806 {
16807 s = alloc(len + 1);
16808 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016809 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016810 rettv->vval.v_string = s;
16811 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016812 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016813 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016814 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016815}
16816
16817/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016818 * "pumvisible()" function
16819 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016821f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016822{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016823#ifdef FEAT_INS_EXPAND
16824 if (pum_visible())
16825 rettv->vval.v_number = 1;
16826#endif
16827}
16828
Bram Moolenaardb913952012-06-29 12:54:53 +020016829#ifdef FEAT_PYTHON3
16830/*
16831 * "py3eval()" function
16832 */
16833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016834f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016835{
16836 char_u *str;
16837 char_u buf[NUMBUFLEN];
16838
16839 str = get_tv_string_buf(&argvars[0], buf);
16840 do_py3eval(str, rettv);
16841}
16842#endif
16843
16844#ifdef FEAT_PYTHON
16845/*
16846 * "pyeval()" function
16847 */
16848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016849f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016850{
16851 char_u *str;
16852 char_u buf[NUMBUFLEN];
16853
16854 str = get_tv_string_buf(&argvars[0], buf);
16855 do_pyeval(str, rettv);
16856}
16857#endif
16858
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016859/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016860 * "range()" function
16861 */
16862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016863f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016864{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016865 varnumber_T start;
16866 varnumber_T end;
16867 varnumber_T stride = 1;
16868 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016869 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016871 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016872 if (argvars[1].v_type == VAR_UNKNOWN)
16873 {
16874 end = start - 1;
16875 start = 0;
16876 }
16877 else
16878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016879 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016882 }
16883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 if (error)
16885 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016886 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016887 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016888 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016889 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016890 else
16891 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016892 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016893 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016894 if (list_append_number(rettv->vval.v_list,
16895 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016896 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016897 }
16898}
16899
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016900/*
16901 * "readfile()" function
16902 */
16903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016904f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016905{
16906 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016907 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016908 char_u *fname;
16909 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016910 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16911 int io_size = sizeof(buf);
16912 int readlen; /* size of last fread() */
16913 char_u *prev = NULL; /* previously read bytes, if any */
16914 long prevlen = 0; /* length of data in prev */
16915 long prevsize = 0; /* size of prev buffer */
16916 long maxline = MAXLNUM;
16917 long cnt = 0;
16918 char_u *p; /* position in buf */
16919 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016920
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016921 if (argvars[1].v_type != VAR_UNKNOWN)
16922 {
16923 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16924 binary = TRUE;
16925 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016926 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016927 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016928
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016929 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016930 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016931
16932 /* Always open the file in binary mode, library functions have a mind of
16933 * their own about CR-LF conversion. */
16934 fname = get_tv_string(&argvars[0]);
16935 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16936 {
16937 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16938 return;
16939 }
16940
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016941 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016942 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016943 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016944
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016945 /* This for loop processes what was read, but is also entered at end
16946 * of file so that either:
16947 * - an incomplete line gets written
16948 * - a "binary" file gets an empty line at the end if it ends in a
16949 * newline. */
16950 for (p = buf, start = buf;
16951 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16952 ++p)
16953 {
16954 if (*p == '\n' || readlen <= 0)
16955 {
16956 listitem_T *li;
16957 char_u *s = NULL;
16958 long_u len = p - start;
16959
16960 /* Finished a line. Remove CRs before NL. */
16961 if (readlen > 0 && !binary)
16962 {
16963 while (len > 0 && start[len - 1] == '\r')
16964 --len;
16965 /* removal may cross back to the "prev" string */
16966 if (len == 0)
16967 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16968 --prevlen;
16969 }
16970 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016971 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016972 else
16973 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016974 /* Change "prev" buffer to be the right size. This way
16975 * the bytes are only copied once, and very long lines are
16976 * allocated only once. */
16977 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016978 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016979 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016980 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016981 prev = NULL; /* the list will own the string */
16982 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016983 }
16984 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016985 if (s == NULL)
16986 {
16987 do_outofmem_msg((long_u) prevlen + len + 1);
16988 failed = TRUE;
16989 break;
16990 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016991
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016992 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016993 {
16994 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016995 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016996 break;
16997 }
16998 li->li_tv.v_type = VAR_STRING;
16999 li->li_tv.v_lock = 0;
17000 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017001 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017002
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017003 start = p + 1; /* step over newline */
17004 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017005 break;
17006 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017007 else if (*p == NUL)
17008 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020017009#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017010 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
17011 * when finding the BF and check the previous two bytes. */
17012 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020017013 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017014 /* Find the two bytes before the 0xbf. If p is at buf, or buf
17015 * + 1, these may be in the "prev" string. */
17016 char_u back1 = p >= buf + 1 ? p[-1]
17017 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
17018 char_u back2 = p >= buf + 2 ? p[-2]
17019 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
17020 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017021
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017022 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017023 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017024 char_u *dest = p - 2;
17025
17026 /* Usually a BOM is at the beginning of a file, and so at
17027 * the beginning of a line; then we can just step over it.
17028 */
17029 if (start == dest)
17030 start = p + 1;
17031 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020017032 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017033 /* have to shuffle buf to close gap */
17034 int adjust_prevlen = 0;
17035
17036 if (dest < buf)
17037 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017038 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017039 dest = buf;
17040 }
17041 if (readlen > p - buf + 1)
17042 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
17043 readlen -= 3 - adjust_prevlen;
17044 prevlen -= adjust_prevlen;
17045 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020017046 }
17047 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017048 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017049#endif
17050 } /* for */
17051
17052 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
17053 break;
17054 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017055 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017056 /* There's part of a line in buf, store it in "prev". */
17057 if (p - start + prevlen >= prevsize)
17058 {
17059 /* need bigger "prev" buffer */
17060 char_u *newprev;
17061
17062 /* A common use case is ordinary text files and "prev" gets a
17063 * fragment of a line, so the first allocation is made
17064 * small, to avoid repeatedly 'allocing' large and
17065 * 'reallocing' small. */
17066 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017067 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017068 else
17069 {
17070 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017071 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017072 prevsize = grow50pc > growmin ? grow50pc : growmin;
17073 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020017074 newprev = prev == NULL ? alloc(prevsize)
17075 : vim_realloc(prev, prevsize);
17076 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017077 {
17078 do_outofmem_msg((long_u)prevsize);
17079 failed = TRUE;
17080 break;
17081 }
17082 prev = newprev;
17083 }
17084 /* Add the line part to end of "prev". */
17085 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010017086 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017087 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017088 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017089
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017090 /*
17091 * For a negative line count use only the lines at the end of the file,
17092 * free the rest.
17093 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017094 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017095 while (cnt > -maxline)
17096 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017097 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000017098 --cnt;
17099 }
17100
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017101 if (failed)
17102 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020017103 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010017104 /* readfile doc says an empty list is returned on error */
17105 rettv->vval.v_list = list_alloc();
17106 }
17107
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017108 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017109 fclose(fd);
17110}
17111
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017112#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017113static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017114
17115/*
17116 * Convert a List to proftime_T.
17117 * Return FAIL when there is something wrong.
17118 */
17119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017120list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017121{
17122 long n1, n2;
17123 int error = FALSE;
17124
17125 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
17126 || arg->vval.v_list->lv_len != 2)
17127 return FAIL;
17128 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
17129 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
17130# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017131 tm->HighPart = n1;
17132 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017133# else
17134 tm->tv_sec = n1;
17135 tm->tv_usec = n2;
17136# endif
17137 return error ? FAIL : OK;
17138}
17139#endif /* FEAT_RELTIME */
17140
17141/*
17142 * "reltime()" function
17143 */
17144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017145f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017146{
17147#ifdef FEAT_RELTIME
17148 proftime_T res;
17149 proftime_T start;
17150
17151 if (argvars[0].v_type == VAR_UNKNOWN)
17152 {
17153 /* No arguments: get current time. */
17154 profile_start(&res);
17155 }
17156 else if (argvars[1].v_type == VAR_UNKNOWN)
17157 {
17158 if (list2proftime(&argvars[0], &res) == FAIL)
17159 return;
17160 profile_end(&res);
17161 }
17162 else
17163 {
17164 /* Two arguments: compute the difference. */
17165 if (list2proftime(&argvars[0], &start) == FAIL
17166 || list2proftime(&argvars[1], &res) == FAIL)
17167 return;
17168 profile_sub(&res, &start);
17169 }
17170
17171 if (rettv_list_alloc(rettv) == OK)
17172 {
17173 long n1, n2;
17174
17175# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017176 n1 = res.HighPart;
17177 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017178# else
17179 n1 = res.tv_sec;
17180 n2 = res.tv_usec;
17181# endif
17182 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17183 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17184 }
17185#endif
17186}
17187
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017188#ifdef FEAT_FLOAT
17189/*
17190 * "reltimefloat()" function
17191 */
17192 static void
17193f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17194{
17195# ifdef FEAT_RELTIME
17196 proftime_T tm;
17197# endif
17198
17199 rettv->v_type = VAR_FLOAT;
17200 rettv->vval.v_float = 0;
17201# ifdef FEAT_RELTIME
17202 if (list2proftime(&argvars[0], &tm) == OK)
17203 rettv->vval.v_float = profile_float(&tm);
17204# endif
17205}
17206#endif
17207
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017208/*
17209 * "reltimestr()" function
17210 */
17211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017212f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017213{
17214#ifdef FEAT_RELTIME
17215 proftime_T tm;
17216#endif
17217
17218 rettv->v_type = VAR_STRING;
17219 rettv->vval.v_string = NULL;
17220#ifdef FEAT_RELTIME
17221 if (list2proftime(&argvars[0], &tm) == OK)
17222 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17223#endif
17224}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017225
Bram Moolenaar0d660222005-01-07 21:51:51 +000017226#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017227static void make_connection(void);
17228static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229
17230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017231make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017232{
17233 if (X_DISPLAY == NULL
17234# ifdef FEAT_GUI
17235 && !gui.in_use
17236# endif
17237 )
17238 {
17239 x_force_connect = TRUE;
17240 setup_term_clip();
17241 x_force_connect = FALSE;
17242 }
17243}
17244
17245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017246check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017247{
17248 make_connection();
17249 if (X_DISPLAY == NULL)
17250 {
17251 EMSG(_("E240: No connection to Vim server"));
17252 return FAIL;
17253 }
17254 return OK;
17255}
17256#endif
17257
17258#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017260remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017261{
17262 char_u *server_name;
17263 char_u *keys;
17264 char_u *r = NULL;
17265 char_u buf[NUMBUFLEN];
17266# ifdef WIN32
17267 HWND w;
17268# else
17269 Window w;
17270# endif
17271
17272 if (check_restricted() || check_secure())
17273 return;
17274
17275# ifdef FEAT_X11
17276 if (check_connection() == FAIL)
17277 return;
17278# endif
17279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280 server_name = get_tv_string_chk(&argvars[0]);
17281 if (server_name == NULL)
17282 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 keys = get_tv_string_buf(&argvars[1], buf);
17284# ifdef WIN32
17285 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17286# else
17287 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17288 < 0)
17289# endif
17290 {
17291 if (r != NULL)
17292 EMSG(r); /* sending worked but evaluation failed */
17293 else
17294 EMSG2(_("E241: Unable to send to %s"), server_name);
17295 return;
17296 }
17297
17298 rettv->vval.v_string = r;
17299
17300 if (argvars[2].v_type != VAR_UNKNOWN)
17301 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017302 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017303 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017305
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017306 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017307 v.di_tv.v_type = VAR_STRING;
17308 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017309 idvar = get_tv_string_chk(&argvars[2]);
17310 if (idvar != NULL)
17311 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017312 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 }
17314}
17315#endif
17316
17317/*
17318 * "remote_expr()" function
17319 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017321f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017322{
17323 rettv->v_type = VAR_STRING;
17324 rettv->vval.v_string = NULL;
17325#ifdef FEAT_CLIENTSERVER
17326 remote_common(argvars, rettv, TRUE);
17327#endif
17328}
17329
17330/*
17331 * "remote_foreground()" function
17332 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017334f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017335{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017336#ifdef FEAT_CLIENTSERVER
17337# ifdef WIN32
17338 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017339 {
17340 char_u *server_name = get_tv_string_chk(&argvars[0]);
17341
17342 if (server_name != NULL)
17343 serverForeground(server_name);
17344 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017345# else
17346 /* Send a foreground() expression to the server. */
17347 argvars[1].v_type = VAR_STRING;
17348 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17349 argvars[2].v_type = VAR_UNKNOWN;
17350 remote_common(argvars, rettv, TRUE);
17351 vim_free(argvars[1].vval.v_string);
17352# endif
17353#endif
17354}
17355
Bram Moolenaar0d660222005-01-07 21:51:51 +000017356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017357f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017358{
17359#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017360 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017361 char_u *s = NULL;
17362# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017363 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017364# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017365 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017366
17367 if (check_restricted() || check_secure())
17368 {
17369 rettv->vval.v_number = -1;
17370 return;
17371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017372 serverid = get_tv_string_chk(&argvars[0]);
17373 if (serverid == NULL)
17374 {
17375 rettv->vval.v_number = -1;
17376 return; /* type error; errmsg already given */
17377 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017378# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017379 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017380 if (n == 0)
17381 rettv->vval.v_number = -1;
17382 else
17383 {
17384 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17385 rettv->vval.v_number = (s != NULL);
17386 }
17387# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017388 if (check_connection() == FAIL)
17389 return;
17390
17391 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017392 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017393# endif
17394
17395 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017397 char_u *retvar;
17398
Bram Moolenaar33570922005-01-25 22:26:29 +000017399 v.di_tv.v_type = VAR_STRING;
17400 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017401 retvar = get_tv_string_chk(&argvars[1]);
17402 if (retvar != NULL)
17403 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017404 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405 }
17406#else
17407 rettv->vval.v_number = -1;
17408#endif
17409}
17410
Bram Moolenaar0d660222005-01-07 21:51:51 +000017411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017412f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017413{
17414 char_u *r = NULL;
17415
17416#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017417 char_u *serverid = get_tv_string_chk(&argvars[0]);
17418
17419 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017420 {
17421# ifdef WIN32
17422 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017423 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017425 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017426 if (n != 0)
17427 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17428 if (r == NULL)
17429# else
17430 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017431 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432# endif
17433 EMSG(_("E277: Unable to read a server reply"));
17434 }
17435#endif
17436 rettv->v_type = VAR_STRING;
17437 rettv->vval.v_string = r;
17438}
17439
17440/*
17441 * "remote_send()" function
17442 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017444f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017445{
17446 rettv->v_type = VAR_STRING;
17447 rettv->vval.v_string = NULL;
17448#ifdef FEAT_CLIENTSERVER
17449 remote_common(argvars, rettv, FALSE);
17450#endif
17451}
17452
17453/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017454 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017455 */
17456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017457f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017458{
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 list_T *l;
17460 listitem_T *item, *item2;
17461 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017462 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017463 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017464 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 dict_T *d;
17466 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017467 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017468
Bram Moolenaar8c711452005-01-14 21:53:12 +000017469 if (argvars[0].v_type == VAR_DICT)
17470 {
17471 if (argvars[2].v_type != VAR_UNKNOWN)
17472 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017473 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017474 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017476 key = get_tv_string_chk(&argvars[1]);
17477 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017479 di = dict_find(d, key, -1);
17480 if (di == NULL)
17481 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017482 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17483 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017484 {
17485 *rettv = di->di_tv;
17486 init_tv(&di->di_tv);
17487 dictitem_remove(d, di);
17488 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017489 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017490 }
17491 }
17492 else if (argvars[0].v_type != VAR_LIST)
17493 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017494 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017495 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017497 int error = FALSE;
17498
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017499 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017500 if (error)
17501 ; /* type error: do nothing, errmsg already given */
17502 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017503 EMSGN(_(e_listidx), idx);
17504 else
17505 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017506 if (argvars[2].v_type == VAR_UNKNOWN)
17507 {
17508 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017509 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017510 *rettv = item->li_tv;
17511 vim_free(item);
17512 }
17513 else
17514 {
17515 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017516 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017517 if (error)
17518 ; /* type error: do nothing */
17519 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017520 EMSGN(_(e_listidx), end);
17521 else
17522 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017523 int cnt = 0;
17524
17525 for (li = item; li != NULL; li = li->li_next)
17526 {
17527 ++cnt;
17528 if (li == item2)
17529 break;
17530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017531 if (li == NULL) /* didn't find "item2" after "item" */
17532 EMSG(_(e_invrange));
17533 else
17534 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017535 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017536 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017537 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017538 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017539 l->lv_first = item;
17540 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017541 item->li_prev = NULL;
17542 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017543 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017544 }
17545 }
17546 }
17547 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017548 }
17549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550}
17551
17552/*
17553 * "rename({from}, {to})" function
17554 */
17555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017556f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557{
17558 char_u buf[NUMBUFLEN];
17559
17560 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017561 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017563 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17564 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565}
17566
17567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017568 * "repeat()" function
17569 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017571f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017572{
17573 char_u *p;
17574 int n;
17575 int slen;
17576 int len;
17577 char_u *r;
17578 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017579
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017580 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017581 if (argvars[0].v_type == VAR_LIST)
17582 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017583 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017584 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017585 if (list_extend(rettv->vval.v_list,
17586 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017587 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017588 }
17589 else
17590 {
17591 p = get_tv_string(&argvars[0]);
17592 rettv->v_type = VAR_STRING;
17593 rettv->vval.v_string = NULL;
17594
17595 slen = (int)STRLEN(p);
17596 len = slen * n;
17597 if (len <= 0)
17598 return;
17599
17600 r = alloc(len + 1);
17601 if (r != NULL)
17602 {
17603 for (i = 0; i < n; i++)
17604 mch_memmove(r + i * slen, p, (size_t)slen);
17605 r[len] = NUL;
17606 }
17607
17608 rettv->vval.v_string = r;
17609 }
17610}
17611
17612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 * "resolve()" function
17614 */
17615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017616f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617{
17618 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017619#ifdef HAVE_READLINK
17620 char_u *buf = NULL;
17621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624#ifdef FEAT_SHORTCUT
17625 {
17626 char_u *v = NULL;
17627
17628 v = mch_resolve_shortcut(p);
17629 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017632 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 }
17634#else
17635# ifdef HAVE_READLINK
17636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 char_u *cpy;
17638 int len;
17639 char_u *remain = NULL;
17640 char_u *q;
17641 int is_relative_to_current = FALSE;
17642 int has_trailing_pathsep = FALSE;
17643 int limit = 100;
17644
17645 p = vim_strsave(p);
17646
17647 if (p[0] == '.' && (vim_ispathsep(p[1])
17648 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17649 is_relative_to_current = TRUE;
17650
17651 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017652 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017655 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657
17658 q = getnextcomp(p);
17659 if (*q != NUL)
17660 {
17661 /* Separate the first path component in "p", and keep the
17662 * remainder (beginning with the path separator). */
17663 remain = vim_strsave(q - 1);
17664 q[-1] = NUL;
17665 }
17666
Bram Moolenaard9462e32011-04-11 21:35:11 +020017667 buf = alloc(MAXPATHL + 1);
17668 if (buf == NULL)
17669 goto fail;
17670
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 for (;;)
17672 {
17673 for (;;)
17674 {
17675 len = readlink((char *)p, (char *)buf, MAXPATHL);
17676 if (len <= 0)
17677 break;
17678 buf[len] = NUL;
17679
17680 if (limit-- == 0)
17681 {
17682 vim_free(p);
17683 vim_free(remain);
17684 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 goto fail;
17687 }
17688
17689 /* Ensure that the result will have a trailing path separator
17690 * if the argument has one. */
17691 if (remain == NULL && has_trailing_pathsep)
17692 add_pathsep(buf);
17693
17694 /* Separate the first path component in the link value and
17695 * concatenate the remainders. */
17696 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17697 if (*q != NUL)
17698 {
17699 if (remain == NULL)
17700 remain = vim_strsave(q - 1);
17701 else
17702 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017703 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 if (cpy != NULL)
17705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 vim_free(remain);
17707 remain = cpy;
17708 }
17709 }
17710 q[-1] = NUL;
17711 }
17712
17713 q = gettail(p);
17714 if (q > p && *q == NUL)
17715 {
17716 /* Ignore trailing path separator. */
17717 q[-1] = NUL;
17718 q = gettail(p);
17719 }
17720 if (q > p && !mch_isFullName(buf))
17721 {
17722 /* symlink is relative to directory of argument */
17723 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17724 if (cpy != NULL)
17725 {
17726 STRCPY(cpy, p);
17727 STRCPY(gettail(cpy), buf);
17728 vim_free(p);
17729 p = cpy;
17730 }
17731 }
17732 else
17733 {
17734 vim_free(p);
17735 p = vim_strsave(buf);
17736 }
17737 }
17738
17739 if (remain == NULL)
17740 break;
17741
17742 /* Append the first path component of "remain" to "p". */
17743 q = getnextcomp(remain + 1);
17744 len = q - remain - (*q != NUL);
17745 cpy = vim_strnsave(p, STRLEN(p) + len);
17746 if (cpy != NULL)
17747 {
17748 STRNCAT(cpy, remain, len);
17749 vim_free(p);
17750 p = cpy;
17751 }
17752 /* Shorten "remain". */
17753 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017754 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755 else
17756 {
17757 vim_free(remain);
17758 remain = NULL;
17759 }
17760 }
17761
17762 /* If the result is a relative path name, make it explicitly relative to
17763 * the current directory if and only if the argument had this form. */
17764 if (!vim_ispathsep(*p))
17765 {
17766 if (is_relative_to_current
17767 && *p != NUL
17768 && !(p[0] == '.'
17769 && (p[1] == NUL
17770 || vim_ispathsep(p[1])
17771 || (p[1] == '.'
17772 && (p[2] == NUL
17773 || vim_ispathsep(p[2]))))))
17774 {
17775 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017776 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777 if (cpy != NULL)
17778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779 vim_free(p);
17780 p = cpy;
17781 }
17782 }
17783 else if (!is_relative_to_current)
17784 {
17785 /* Strip leading "./". */
17786 q = p;
17787 while (q[0] == '.' && vim_ispathsep(q[1]))
17788 q += 2;
17789 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017790 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 }
17792 }
17793
17794 /* Ensure that the result will have no trailing path separator
17795 * if the argument had none. But keep "/" or "//". */
17796 if (!has_trailing_pathsep)
17797 {
17798 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017799 if (after_pathsep(p, q))
17800 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801 }
17802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017803 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 }
17805# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807# endif
17808#endif
17809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017810 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811
17812#ifdef HAVE_READLINK
17813fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017814 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017816 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817}
17818
17819/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017820 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821 */
17822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017823f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824{
Bram Moolenaar33570922005-01-25 22:26:29 +000017825 list_T *l;
17826 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827
Bram Moolenaar0d660222005-01-07 21:51:51 +000017828 if (argvars[0].v_type != VAR_LIST)
17829 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017830 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017831 && !tv_check_lock(l->lv_lock,
17832 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017833 {
17834 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017835 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017836 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837 while (li != NULL)
17838 {
17839 ni = li->li_prev;
17840 list_append(l, li);
17841 li = ni;
17842 }
17843 rettv->vval.v_list = l;
17844 rettv->v_type = VAR_LIST;
17845 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017846 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848}
17849
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017850#define SP_NOMOVE 0x01 /* don't move cursor */
17851#define SP_REPEAT 0x02 /* repeat to find outer pair */
17852#define SP_RETCOUNT 0x04 /* return matchcount */
17853#define SP_SETPCMARK 0x08 /* set previous context mark */
17854#define SP_START 0x10 /* accept match at start position */
17855#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17856#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017857#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017858
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017859static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017860
17861/*
17862 * Get flags for a search function.
17863 * Possibly sets "p_ws".
17864 * Returns BACKWARD, FORWARD or zero (for an error).
17865 */
17866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017867get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017868{
17869 int dir = FORWARD;
17870 char_u *flags;
17871 char_u nbuf[NUMBUFLEN];
17872 int mask;
17873
17874 if (varp->v_type != VAR_UNKNOWN)
17875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017876 flags = get_tv_string_buf_chk(varp, nbuf);
17877 if (flags == NULL)
17878 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017879 while (*flags != NUL)
17880 {
17881 switch (*flags)
17882 {
17883 case 'b': dir = BACKWARD; break;
17884 case 'w': p_ws = TRUE; break;
17885 case 'W': p_ws = FALSE; break;
17886 default: mask = 0;
17887 if (flagsp != NULL)
17888 switch (*flags)
17889 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017890 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017891 case 'e': mask = SP_END; break;
17892 case 'm': mask = SP_RETCOUNT; break;
17893 case 'n': mask = SP_NOMOVE; break;
17894 case 'p': mask = SP_SUBPAT; break;
17895 case 'r': mask = SP_REPEAT; break;
17896 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017897 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017898 }
17899 if (mask == 0)
17900 {
17901 EMSG2(_(e_invarg2), flags);
17902 dir = 0;
17903 }
17904 else
17905 *flagsp |= mask;
17906 }
17907 if (dir == 0)
17908 break;
17909 ++flags;
17910 }
17911 }
17912 return dir;
17913}
17914
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017916 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017917 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017919search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017920{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017921 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 char_u *pat;
17923 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017924 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 int save_p_ws = p_ws;
17926 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017927 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017928 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017929 proftime_T tm;
17930#ifdef FEAT_RELTIME
17931 long time_limit = 0;
17932#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017933 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017934 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017936 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017937 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017938 if (dir == 0)
17939 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017940 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017941 if (flags & SP_START)
17942 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017943 if (flags & SP_END)
17944 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017945 if (flags & SP_COLUMN)
17946 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017947
Bram Moolenaar76929292008-01-06 19:07:36 +000017948 /* Optional arguments: line number to stop searching and timeout. */
17949 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017950 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017951 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017952 if (lnum_stop < 0)
17953 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017954#ifdef FEAT_RELTIME
17955 if (argvars[3].v_type != VAR_UNKNOWN)
17956 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017957 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017958 if (time_limit < 0)
17959 goto theend;
17960 }
17961#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017962 }
17963
Bram Moolenaar76929292008-01-06 19:07:36 +000017964#ifdef FEAT_RELTIME
17965 /* Set the time limit, if there is one. */
17966 profile_setlimit(time_limit, &tm);
17967#endif
17968
Bram Moolenaar231334e2005-07-25 20:46:57 +000017969 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017970 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017971 * Check to make sure only those flags are set.
17972 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17973 * flags cannot be set. Check for that condition also.
17974 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017975 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017976 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017978 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017979 goto theend;
17980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017982 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017983 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017984 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017985 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017987 if (flags & SP_SUBPAT)
17988 retval = subpatnum;
17989 else
17990 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017991 if (flags & SP_SETPCMARK)
17992 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994 if (match_pos != NULL)
17995 {
17996 /* Store the match cursor position */
17997 match_pos->lnum = pos.lnum;
17998 match_pos->col = pos.col + 1;
17999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 /* "/$" will put the cursor after the end of the line, may need to
18001 * correct that here */
18002 check_cursor();
18003 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018004
18005 /* If 'n' flag is used: restore cursor position. */
18006 if (flags & SP_NOMOVE)
18007 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000018008 else
18009 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018010theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018012
18013 return retval;
18014}
18015
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018016#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018017
18018/*
18019 * round() is not in C90, use ceil() or floor() instead.
18020 */
18021 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010018022vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018023{
18024 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
18025}
18026
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018027/*
18028 * "round({float})" function
18029 */
18030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018031f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018032{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018033 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018034
18035 rettv->v_type = VAR_FLOAT;
18036 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020018037 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018038 else
18039 rettv->vval.v_float = 0.0;
18040}
18041#endif
18042
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018043/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020018044 * "screenattr()" function
18045 */
18046 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018047f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018048{
18049 int row;
18050 int col;
18051 int c;
18052
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018053 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18054 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018055 if (row < 0 || row >= screen_Rows
18056 || col < 0 || col >= screen_Columns)
18057 c = -1;
18058 else
18059 c = ScreenAttrs[LineOffset[row] + col];
18060 rettv->vval.v_number = c;
18061}
18062
18063/*
18064 * "screenchar()" function
18065 */
18066 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010018067f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020018068{
18069 int row;
18070 int col;
18071 int off;
18072 int c;
18073
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018074 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
18075 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020018076 if (row < 0 || row >= screen_Rows
18077 || col < 0 || col >= screen_Columns)
18078 c = -1;
18079 else
18080 {
18081 off = LineOffset[row] + col;
18082#ifdef FEAT_MBYTE
18083 if (enc_utf8 && ScreenLinesUC[off] != 0)
18084 c = ScreenLinesUC[off];
18085 else
18086#endif
18087 c = ScreenLines[off];
18088 }
18089 rettv->vval.v_number = c;
18090}
18091
18092/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018093 * "screencol()" function
18094 *
18095 * First column is 1 to be consistent with virtcol().
18096 */
18097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018098f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018099{
18100 rettv->vval.v_number = screen_screencol() + 1;
18101}
18102
18103/*
18104 * "screenrow()" function
18105 */
18106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018107f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010018108{
18109 rettv->vval.v_number = screen_screenrow() + 1;
18110}
18111
18112/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018113 * "search()" function
18114 */
18115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018116f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018117{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018118 int flags = 0;
18119
18120 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121}
18122
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018124 * "searchdecl()" function
18125 */
18126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018127f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018128{
18129 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018130 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018131 int error = FALSE;
18132 char_u *name;
18133
18134 rettv->vval.v_number = 1; /* default: FAIL */
18135
18136 name = get_tv_string_chk(&argvars[0]);
18137 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018138 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018139 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018140 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018141 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018142 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018143 if (!error && name != NULL)
18144 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018145 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018146}
18147
18148/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018149 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018152searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153{
18154 char_u *spat, *mpat, *epat;
18155 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 int dir;
18158 int flags = 0;
18159 char_u nbuf1[NUMBUFLEN];
18160 char_u nbuf2[NUMBUFLEN];
18161 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018162 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018163 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018164 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 spat = get_tv_string_chk(&argvars[0]);
18168 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18169 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18170 if (spat == NULL || mpat == NULL || epat == NULL)
18171 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 /* Handle the optional fourth argument: flags */
18174 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018175 if (dir == 0)
18176 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018177
18178 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018179 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18180 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018181 if ((flags & (SP_END | SP_SUBPAT)) != 0
18182 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018183 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018184 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018185 goto theend;
18186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018188 /* Using 'r' implies 'W', otherwise it doesn't work. */
18189 if (flags & SP_REPEAT)
18190 p_ws = FALSE;
18191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018192 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018193 if (argvars[3].v_type == VAR_UNKNOWN
18194 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195 skip = (char_u *)"";
18196 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018198 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018199 if (argvars[5].v_type != VAR_UNKNOWN)
18200 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018201 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018202 if (lnum_stop < 0)
18203 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018204#ifdef FEAT_RELTIME
18205 if (argvars[6].v_type != VAR_UNKNOWN)
18206 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018207 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018208 if (time_limit < 0)
18209 goto theend;
18210 }
18211#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018212 }
18213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018214 if (skip == NULL)
18215 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018217 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018218 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018219
18220theend:
18221 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018222
18223 return retval;
18224}
18225
18226/*
18227 * "searchpair()" function
18228 */
18229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018230f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018231{
18232 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18233}
18234
18235/*
18236 * "searchpairpos()" function
18237 */
18238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018239f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018240{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018241 pos_T match_pos;
18242 int lnum = 0;
18243 int col = 0;
18244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018245 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018246 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018247
18248 if (searchpair_cmn(argvars, &match_pos) > 0)
18249 {
18250 lnum = match_pos.lnum;
18251 col = match_pos.col;
18252 }
18253
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018254 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18255 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018256}
18257
18258/*
18259 * Search for a start/middle/end thing.
18260 * Used by searchpair(), see its documentation for the details.
18261 * Returns 0 or -1 for no match,
18262 */
18263 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018264do_searchpair(
18265 char_u *spat, /* start pattern */
18266 char_u *mpat, /* middle pattern */
18267 char_u *epat, /* end pattern */
18268 int dir, /* BACKWARD or FORWARD */
18269 char_u *skip, /* skip expression */
18270 int flags, /* SP_SETPCMARK and other SP_ values */
18271 pos_T *match_pos,
18272 linenr_T lnum_stop, /* stop at this line if not zero */
18273 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018274{
18275 char_u *save_cpo;
18276 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18277 long retval = 0;
18278 pos_T pos;
18279 pos_T firstpos;
18280 pos_T foundpos;
18281 pos_T save_cursor;
18282 pos_T save_pos;
18283 int n;
18284 int r;
18285 int nest = 1;
18286 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018287 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018288 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018289
18290 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18291 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018292 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018293
Bram Moolenaar76929292008-01-06 19:07:36 +000018294#ifdef FEAT_RELTIME
18295 /* Set the time limit, if there is one. */
18296 profile_setlimit(time_limit, &tm);
18297#endif
18298
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018299 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18300 * start/middle/end (pat3, for the top pair). */
18301 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18302 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18303 if (pat2 == NULL || pat3 == NULL)
18304 goto theend;
18305 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18306 if (*mpat == NUL)
18307 STRCPY(pat3, pat2);
18308 else
18309 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18310 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018311 if (flags & SP_START)
18312 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018313
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 save_cursor = curwin->w_cursor;
18315 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018316 clearpos(&firstpos);
18317 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 pat = pat3;
18319 for (;;)
18320 {
18321 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018322 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18324 /* didn't find it or found the first match again: FAIL */
18325 break;
18326
18327 if (firstpos.lnum == 0)
18328 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018329 if (equalpos(pos, foundpos))
18330 {
18331 /* Found the same position again. Can happen with a pattern that
18332 * has "\zs" at the end and searching backwards. Advance one
18333 * character and try again. */
18334 if (dir == BACKWARD)
18335 decl(&pos);
18336 else
18337 incl(&pos);
18338 }
18339 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018341 /* clear the start flag to avoid getting stuck here */
18342 options &= ~SEARCH_START;
18343
Bram Moolenaar071d4272004-06-13 20:20:40 +000018344 /* If the skip pattern matches, ignore this match. */
18345 if (*skip != NUL)
18346 {
18347 save_pos = curwin->w_cursor;
18348 curwin->w_cursor = pos;
18349 r = eval_to_bool(skip, &err, NULL, FALSE);
18350 curwin->w_cursor = save_pos;
18351 if (err)
18352 {
18353 /* Evaluating {skip} caused an error, break here. */
18354 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018355 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018356 break;
18357 }
18358 if (r)
18359 continue;
18360 }
18361
18362 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18363 {
18364 /* Found end when searching backwards or start when searching
18365 * forward: nested pair. */
18366 ++nest;
18367 pat = pat2; /* nested, don't search for middle */
18368 }
18369 else
18370 {
18371 /* Found end when searching forward or start when searching
18372 * backward: end of (nested) pair; or found middle in outer pair. */
18373 if (--nest == 1)
18374 pat = pat3; /* outer level, search for middle */
18375 }
18376
18377 if (nest == 0)
18378 {
18379 /* Found the match: return matchcount or line number. */
18380 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018381 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018383 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018384 if (flags & SP_SETPCMARK)
18385 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 curwin->w_cursor = pos;
18387 if (!(flags & SP_REPEAT))
18388 break;
18389 nest = 1; /* search for next unmatched */
18390 }
18391 }
18392
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018393 if (match_pos != NULL)
18394 {
18395 /* Store the match cursor position */
18396 match_pos->lnum = curwin->w_cursor.lnum;
18397 match_pos->col = curwin->w_cursor.col + 1;
18398 }
18399
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018401 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402 curwin->w_cursor = save_cursor;
18403
18404theend:
18405 vim_free(pat2);
18406 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018407 if (p_cpo == empty_option)
18408 p_cpo = save_cpo;
18409 else
18410 /* Darn, evaluating the {skip} expression changed the value. */
18411 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018412
18413 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414}
18415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018416/*
18417 * "searchpos()" function
18418 */
18419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018420f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018421{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018422 pos_T match_pos;
18423 int lnum = 0;
18424 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018425 int n;
18426 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018427
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018428 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018429 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018430
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018431 n = search_cmn(argvars, &match_pos, &flags);
18432 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018433 {
18434 lnum = match_pos.lnum;
18435 col = match_pos.col;
18436 }
18437
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018438 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18439 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018440 if (flags & SP_SUBPAT)
18441 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018442}
18443
Bram Moolenaar0d660222005-01-07 21:51:51 +000018444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018445f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018447#ifdef FEAT_CLIENTSERVER
18448 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018449 char_u *server = get_tv_string_chk(&argvars[0]);
18450 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018453 if (server == NULL || reply == NULL)
18454 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018455 if (check_restricted() || check_secure())
18456 return;
18457# ifdef FEAT_X11
18458 if (check_connection() == FAIL)
18459 return;
18460# endif
18461
18462 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018464 EMSG(_("E258: Unable to send to client"));
18465 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018467 rettv->vval.v_number = 0;
18468#else
18469 rettv->vval.v_number = -1;
18470#endif
18471}
18472
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018474f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018475{
18476 char_u *r = NULL;
18477
18478#ifdef FEAT_CLIENTSERVER
18479# ifdef WIN32
18480 r = serverGetVimNames();
18481# else
18482 make_connection();
18483 if (X_DISPLAY != NULL)
18484 r = serverGetVimNames(X_DISPLAY);
18485# endif
18486#endif
18487 rettv->v_type = VAR_STRING;
18488 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489}
18490
18491/*
18492 * "setbufvar()" function
18493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018495f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496{
18497 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018500 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 char_u nbuf[NUMBUFLEN];
18502
18503 if (check_restricted() || check_secure())
18504 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018505 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18506 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018507 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508 varp = &argvars[2];
18509
18510 if (buf != NULL && varname != NULL && varp != NULL)
18511 {
18512 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018513 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514
18515 if (*varname == '&')
18516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018517 long numval;
18518 char_u *strval;
18519 int error = FALSE;
18520
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018522 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018523 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018524 if (!error && strval != NULL)
18525 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 }
18527 else
18528 {
18529 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18530 if (bufvarname != NULL)
18531 {
18532 STRCPY(bufvarname, "b:");
18533 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018534 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535 vim_free(bufvarname);
18536 }
18537 }
18538
18539 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542}
18543
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018545f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018546{
18547 dict_T *d;
18548 dictitem_T *di;
18549 char_u *csearch;
18550
18551 if (argvars[0].v_type != VAR_DICT)
18552 {
18553 EMSG(_(e_dictreq));
18554 return;
18555 }
18556
18557 if ((d = argvars[0].vval.v_dict) != NULL)
18558 {
18559 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18560 if (csearch != NULL)
18561 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018562#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018563 if (enc_utf8)
18564 {
18565 int pcc[MAX_MCO];
18566 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018567
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018568 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18569 }
18570 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018571#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018572 set_last_csearch(PTR2CHAR(csearch),
18573 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018574 }
18575
18576 di = dict_find(d, (char_u *)"forward", -1);
18577 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018578 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018579 ? FORWARD : BACKWARD);
18580
18581 di = dict_find(d, (char_u *)"until", -1);
18582 if (di != NULL)
18583 set_csearch_until(!!get_tv_number(&di->di_tv));
18584 }
18585}
18586
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587/*
18588 * "setcmdpos()" function
18589 */
18590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018591f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018593 int pos = (int)get_tv_number(&argvars[0]) - 1;
18594
18595 if (pos >= 0)
18596 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597}
18598
18599/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018600 * "setfperm({fname}, {mode})" function
18601 */
18602 static void
18603f_setfperm(typval_T *argvars, typval_T *rettv)
18604{
18605 char_u *fname;
18606 char_u modebuf[NUMBUFLEN];
18607 char_u *mode_str;
18608 int i;
18609 int mask;
18610 int mode = 0;
18611
18612 rettv->vval.v_number = 0;
18613 fname = get_tv_string_chk(&argvars[0]);
18614 if (fname == NULL)
18615 return;
18616 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18617 if (mode_str == NULL)
18618 return;
18619 if (STRLEN(mode_str) != 9)
18620 {
18621 EMSG2(_(e_invarg2), mode_str);
18622 return;
18623 }
18624
18625 mask = 1;
18626 for (i = 8; i >= 0; --i)
18627 {
18628 if (mode_str[i] != '-')
18629 mode |= mask;
18630 mask = mask << 1;
18631 }
18632 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18633}
18634
18635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636 * "setline()" function
18637 */
18638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018639f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640{
18641 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018642 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018643 list_T *l = NULL;
18644 listitem_T *li = NULL;
18645 long added = 0;
18646 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018648 lnum = get_tv_lnum(&argvars[0]);
18649 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018651 l = argvars[1].vval.v_list;
18652 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018654 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018655 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018656
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018657 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018658 for (;;)
18659 {
18660 if (l != NULL)
18661 {
18662 /* list argument, get next string */
18663 if (li == NULL)
18664 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018665 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018666 li = li->li_next;
18667 }
18668
18669 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018670 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018671 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018672
18673 /* When coming here from Insert mode, sync undo, so that this can be
18674 * undone separately from what was previously inserted. */
18675 if (u_sync_once == 2)
18676 {
18677 u_sync_once = 1; /* notify that u_sync() was called */
18678 u_sync(TRUE);
18679 }
18680
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018681 if (lnum <= curbuf->b_ml.ml_line_count)
18682 {
18683 /* existing line, replace it */
18684 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18685 {
18686 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018687 if (lnum == curwin->w_cursor.lnum)
18688 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018689 rettv->vval.v_number = 0; /* OK */
18690 }
18691 }
18692 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18693 {
18694 /* lnum is one past the last line, append the line */
18695 ++added;
18696 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18697 rettv->vval.v_number = 0; /* OK */
18698 }
18699
18700 if (l == NULL) /* only one string argument */
18701 break;
18702 ++lnum;
18703 }
18704
18705 if (added > 0)
18706 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707}
18708
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018709static 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 +000018710
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018712 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018713 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018715set_qf_ll_list(
18716 win_T *wp UNUSED,
18717 typval_T *list_arg UNUSED,
18718 typval_T *action_arg UNUSED,
18719 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018720{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018721#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018722 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018723 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018724 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018725#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018726
Bram Moolenaar2641f772005-03-25 21:58:17 +000018727 rettv->vval.v_number = -1;
18728
18729#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018730 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018731 EMSG(_(e_listreq));
18732 else
18733 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018734 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018735
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018736 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018737 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018738 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018739 if (act == NULL)
18740 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018741 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018742 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018743 else
18744 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018745 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018746 else if (action_arg->v_type == VAR_UNKNOWN)
18747 action = ' ';
18748 else
18749 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018750
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018751 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018752 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018753 rettv->vval.v_number = 0;
18754 }
18755#endif
18756}
18757
18758/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018759 * "setloclist()" function
18760 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018762f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018763{
18764 win_T *win;
18765
18766 rettv->vval.v_number = -1;
18767
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018768 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018769 if (win != NULL)
18770 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18771}
18772
18773/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018774 * "setmatches()" function
18775 */
18776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018777f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018778{
18779#ifdef FEAT_SEARCH_EXTRA
18780 list_T *l;
18781 listitem_T *li;
18782 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018783 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018784
18785 rettv->vval.v_number = -1;
18786 if (argvars[0].v_type != VAR_LIST)
18787 {
18788 EMSG(_(e_listreq));
18789 return;
18790 }
18791 if ((l = argvars[0].vval.v_list) != NULL)
18792 {
18793
18794 /* To some extent make sure that we are dealing with a list from
18795 * "getmatches()". */
18796 li = l->lv_first;
18797 while (li != NULL)
18798 {
18799 if (li->li_tv.v_type != VAR_DICT
18800 || (d = li->li_tv.vval.v_dict) == NULL)
18801 {
18802 EMSG(_(e_invarg));
18803 return;
18804 }
18805 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018806 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18807 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018808 && dict_find(d, (char_u *)"priority", -1) != NULL
18809 && dict_find(d, (char_u *)"id", -1) != NULL))
18810 {
18811 EMSG(_(e_invarg));
18812 return;
18813 }
18814 li = li->li_next;
18815 }
18816
18817 clear_matches(curwin);
18818 li = l->lv_first;
18819 while (li != NULL)
18820 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018821 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018822 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018823 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018824 char_u *group;
18825 int priority;
18826 int id;
18827 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018828
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018829 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018830 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18831 {
18832 if (s == NULL)
18833 {
18834 s = list_alloc();
18835 if (s == NULL)
18836 return;
18837 }
18838
18839 /* match from matchaddpos() */
18840 for (i = 1; i < 9; i++)
18841 {
18842 sprintf((char *)buf, (char *)"pos%d", i);
18843 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18844 {
18845 if (di->di_tv.v_type != VAR_LIST)
18846 return;
18847
18848 list_append_tv(s, &di->di_tv);
18849 s->lv_refcount++;
18850 }
18851 else
18852 break;
18853 }
18854 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018855
18856 group = get_dict_string(d, (char_u *)"group", FALSE);
18857 priority = (int)get_dict_number(d, (char_u *)"priority");
18858 id = (int)get_dict_number(d, (char_u *)"id");
18859 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18860 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18861 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018862 if (i == 0)
18863 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018864 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018865 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018866 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018867 }
18868 else
18869 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018870 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018871 list_unref(s);
18872 s = NULL;
18873 }
18874
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018875 li = li->li_next;
18876 }
18877 rettv->vval.v_number = 0;
18878 }
18879#endif
18880}
18881
18882/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018883 * "setpos()" function
18884 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018886f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018887{
18888 pos_T pos;
18889 int fnum;
18890 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018891 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018892
Bram Moolenaar08250432008-02-13 11:42:46 +000018893 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018894 name = get_tv_string_chk(argvars);
18895 if (name != NULL)
18896 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018897 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018898 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018899 if (--pos.col < 0)
18900 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018901 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018902 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018903 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018904 if (fnum == curbuf->b_fnum)
18905 {
18906 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018907 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018908 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018909 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018910 curwin->w_set_curswant = FALSE;
18911 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018912 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018913 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018914 }
18915 else
18916 EMSG(_(e_invarg));
18917 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018918 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18919 {
18920 /* set mark */
18921 if (setmark_pos(name[1], &pos, fnum) == OK)
18922 rettv->vval.v_number = 0;
18923 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018924 else
18925 EMSG(_(e_invarg));
18926 }
18927 }
18928}
18929
18930/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018931 * "setqflist()" function
18932 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018934f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018935{
18936 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18937}
18938
18939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940 * "setreg()" function
18941 */
18942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018943f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018944{
18945 int regname;
18946 char_u *strregname;
18947 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018948 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 int append;
18950 char_u yank_type;
18951 long block_len;
18952
18953 block_len = -1;
18954 yank_type = MAUTO;
18955 append = FALSE;
18956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018957 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018958 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018960 if (strregname == NULL)
18961 return; /* type error; errmsg already given */
18962 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963 if (regname == 0 || regname == '@')
18964 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018966 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018968 stropt = get_tv_string_chk(&argvars[2]);
18969 if (stropt == NULL)
18970 return; /* type error */
18971 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 switch (*stropt)
18973 {
18974 case 'a': case 'A': /* append */
18975 append = TRUE;
18976 break;
18977 case 'v': case 'c': /* character-wise selection */
18978 yank_type = MCHAR;
18979 break;
18980 case 'V': case 'l': /* line-wise selection */
18981 yank_type = MLINE;
18982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018983 case 'b': case Ctrl_V: /* block-wise selection */
18984 yank_type = MBLOCK;
18985 if (VIM_ISDIGIT(stropt[1]))
18986 {
18987 ++stropt;
18988 block_len = getdigits(&stropt) - 1;
18989 --stropt;
18990 }
18991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 }
18993 }
18994
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018995 if (argvars[1].v_type == VAR_LIST)
18996 {
18997 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018998 char_u **allocval;
18999 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019000 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020019001 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019002 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019003 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019004 int len;
19005
19006 /* If the list is NULL handle like an empty list. */
19007 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019008
Bram Moolenaar7d647822014-04-05 21:28:56 +020019009 /* First half: use for pointers to result lines; second half: use for
19010 * pointers to allocated copies. */
19011 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019012 if (lstval == NULL)
19013 return;
19014 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020019015 allocval = lstval + len + 2;
19016 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019017
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020019018 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019019 li = li->li_next)
19020 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019021 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019022 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020019023 goto free_lstval;
19024 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019025 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020019026 /* Need to make a copy, next get_tv_string_buf_chk() will
19027 * overwrite the string. */
19028 strval = vim_strsave(buf);
19029 if (strval == NULL)
19030 goto free_lstval;
19031 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019032 }
19033 *curval++ = strval;
19034 }
19035 *curval++ = NULL;
19036
19037 write_reg_contents_lst(regname, lstval, -1,
19038 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020019039free_lstval:
19040 while (curallocval > allocval)
19041 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019042 vim_free(lstval);
19043 }
19044 else
19045 {
19046 strval = get_tv_string_chk(&argvars[1]);
19047 if (strval == NULL)
19048 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019049 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020019051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019052 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053}
19054
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019055/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019056 * "settabvar()" function
19057 */
19058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019059f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019060{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019061#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019062 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019063 tabpage_T *tp;
19064#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019065 char_u *varname, *tabvarname;
19066 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019067
19068 rettv->vval.v_number = 0;
19069
19070 if (check_restricted() || check_secure())
19071 return;
19072
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019073#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019074 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019075#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019076 varname = get_tv_string_chk(&argvars[1]);
19077 varp = &argvars[2];
19078
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019079 if (varname != NULL && varp != NULL
19080#ifdef FEAT_WINDOWS
19081 && tp != NULL
19082#endif
19083 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019084 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019085#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019086 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019087 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019088#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019089
19090 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
19091 if (tabvarname != NULL)
19092 {
19093 STRCPY(tabvarname, "t:");
19094 STRCPY(tabvarname + 2, varname);
19095 set_var(tabvarname, varp, TRUE);
19096 vim_free(tabvarname);
19097 }
19098
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019099#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019100 /* Restore current tabpage */
19101 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020019102 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019103#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020019104 }
19105}
19106
19107/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019108 * "settabwinvar()" function
19109 */
19110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019111f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019112{
19113 setwinvar(argvars, rettv, 1);
19114}
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115
19116/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019117 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019120f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019122 setwinvar(argvars, rettv, 0);
19123}
19124
19125/*
19126 * "setwinvar()" and "settabwinvar()" functions
19127 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020019128
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019130setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019131{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 win_T *win;
19133#ifdef FEAT_WINDOWS
19134 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019135 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019136 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137#endif
19138 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019139 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019141 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142
19143 if (check_restricted() || check_secure())
19144 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019145
19146#ifdef FEAT_WINDOWS
19147 if (off == 1)
19148 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19149 else
19150 tp = curtab;
19151#endif
19152 win = find_win_by_nr(&argvars[off], tp);
19153 varname = get_tv_string_chk(&argvars[off + 1]);
19154 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155
19156 if (win != NULL && varname != NULL && varp != NULL)
19157 {
19158#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019159 need_switch_win = !(tp == curtab && win == curwin);
19160 if (!need_switch_win
19161 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019164 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019166 long numval;
19167 char_u *strval;
19168 int error = FALSE;
19169
19170 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019171 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019172 strval = get_tv_string_buf_chk(varp, nbuf);
19173 if (!error && strval != NULL)
19174 set_option_value(varname, numval, strval, OPT_LOCAL);
19175 }
19176 else
19177 {
19178 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19179 if (winvarname != NULL)
19180 {
19181 STRCPY(winvarname, "w:");
19182 STRCPY(winvarname + 2, varname);
19183 set_var(winvarname, varp, TRUE);
19184 vim_free(winvarname);
19185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 }
19187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019189 if (need_switch_win)
19190 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191#endif
19192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193}
19194
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019195#ifdef FEAT_CRYPT
19196/*
19197 * "sha256({string})" function
19198 */
19199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019200f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019201{
19202 char_u *p;
19203
19204 p = get_tv_string(&argvars[0]);
19205 rettv->vval.v_string = vim_strsave(
19206 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19207 rettv->v_type = VAR_STRING;
19208}
19209#endif /* FEAT_CRYPT */
19210
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019212 * "shellescape({string})" function
19213 */
19214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019215f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019216{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019217 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019218 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019219 rettv->v_type = VAR_STRING;
19220}
19221
19222/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019223 * shiftwidth() function
19224 */
19225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019226f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019227{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019228 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019229}
19230
19231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019232 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 */
19234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019235f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019237 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 p = get_tv_string(&argvars[0]);
19240 rettv->vval.v_string = vim_strsave(p);
19241 simplify_filename(rettv->vval.v_string); /* simplify in place */
19242 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243}
19244
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019245#ifdef FEAT_FLOAT
19246/*
19247 * "sin()" function
19248 */
19249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019250f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019251{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019252 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019253
19254 rettv->v_type = VAR_FLOAT;
19255 if (get_float_arg(argvars, &f) == OK)
19256 rettv->vval.v_float = sin(f);
19257 else
19258 rettv->vval.v_float = 0.0;
19259}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019260
19261/*
19262 * "sinh()" function
19263 */
19264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019265f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019266{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019267 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019268
19269 rettv->v_type = VAR_FLOAT;
19270 if (get_float_arg(argvars, &f) == OK)
19271 rettv->vval.v_float = sinh(f);
19272 else
19273 rettv->vval.v_float = 0.0;
19274}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019275#endif
19276
Bram Moolenaar0d660222005-01-07 21:51:51 +000019277static int
19278#ifdef __BORLANDC__
19279 _RTLENTRYF
19280#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019281 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019282static int
19283#ifdef __BORLANDC__
19284 _RTLENTRYF
19285#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019286 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019287
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019288/* struct used in the array that's given to qsort() */
19289typedef struct
19290{
19291 listitem_T *item;
19292 int idx;
19293} sortItem_T;
19294
Bram Moolenaar0b962472016-02-22 22:51:33 +010019295/* struct storing information about current sort */
19296typedef struct
19297{
19298 int item_compare_ic;
19299 int item_compare_numeric;
19300 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019301#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019302 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019303#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019304 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019305 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019306 dict_T *item_compare_selfdict;
19307 int item_compare_func_err;
19308 int item_compare_keep_zero;
19309} sortinfo_T;
19310static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019311static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019312#define ITEM_COMPARE_FAIL 999
19313
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019315 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317 static int
19318#ifdef __BORLANDC__
19319_RTLENTRYF
19320#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019321item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019323 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019324 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019325 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019326 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019327 int res;
19328 char_u numbuf1[NUMBUFLEN];
19329 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019331 si1 = (sortItem_T *)s1;
19332 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019333 tv1 = &si1->item->li_tv;
19334 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019335
Bram Moolenaar0b962472016-02-22 22:51:33 +010019336 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019337 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019338 varnumber_T v1 = get_tv_number(tv1);
19339 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019340
19341 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19342 }
19343
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019344#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019345 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019346 {
19347 float_T v1 = get_tv_float(tv1);
19348 float_T v2 = get_tv_float(tv2);
19349
19350 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19351 }
19352#endif
19353
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019354 /* tv2string() puts quotes around a string and allocates memory. Don't do
19355 * that for string variables. Use a single quote when comparing with a
19356 * non-string to do what the docs promise. */
19357 if (tv1->v_type == VAR_STRING)
19358 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019359 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019360 p1 = (char_u *)"'";
19361 else
19362 p1 = tv1->vval.v_string;
19363 }
19364 else
19365 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19366 if (tv2->v_type == VAR_STRING)
19367 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019368 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019369 p2 = (char_u *)"'";
19370 else
19371 p2 = tv2->vval.v_string;
19372 }
19373 else
19374 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019375 if (p1 == NULL)
19376 p1 = (char_u *)"";
19377 if (p2 == NULL)
19378 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019379 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019380 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019381 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019382 res = STRICMP(p1, p2);
19383 else
19384 res = STRCMP(p1, p2);
19385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019387 {
19388 double n1, n2;
19389 n1 = strtod((char *)p1, (char **)&p1);
19390 n2 = strtod((char *)p2, (char **)&p2);
19391 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19392 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019393
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019394 /* When the result would be zero, compare the item indexes. Makes the
19395 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019396 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019397 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019398
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 vim_free(tofree1);
19400 vim_free(tofree2);
19401 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402}
19403
19404 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019405#ifdef __BORLANDC__
19406_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019408item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019410 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019411 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019412 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019413 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019415 char_u *func_name;
19416 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019418 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019419 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019420 return 0;
19421
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019422 si1 = (sortItem_T *)s1;
19423 si2 = (sortItem_T *)s2;
19424
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019425 if (partial == NULL)
19426 func_name = sortinfo->item_compare_func;
19427 else
19428 func_name = partial->pt_name;
19429
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019430 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019431 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019432 copy_tv(&si1->item->li_tv, &argv[0]);
19433 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434
19435 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019436 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019437 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019438 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019439 clear_tv(&argv[0]);
19440 clear_tv(&argv[1]);
19441
19442 if (res == FAIL)
19443 res = ITEM_COMPARE_FAIL;
19444 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019445 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019446 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019447 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019448 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019449
19450 /* When the result would be zero, compare the pointers themselves. Makes
19451 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019452 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019453 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019454
Bram Moolenaar0d660222005-01-07 21:51:51 +000019455 return res;
19456}
19457
19458/*
19459 * "sort({list})" function
19460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019462do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463{
Bram Moolenaar33570922005-01-25 22:26:29 +000019464 list_T *l;
19465 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019466 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019467 sortinfo_T *old_sortinfo;
19468 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019469 long len;
19470 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471
Bram Moolenaar0b962472016-02-22 22:51:33 +010019472 /* Pointer to current info struct used in compare function. Save and
19473 * restore the current one for nested calls. */
19474 old_sortinfo = sortinfo;
19475 sortinfo = &info;
19476
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019478 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 else
19480 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019481 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019482 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019483 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19484 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019485 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019486 rettv->vval.v_list = l;
19487 rettv->v_type = VAR_LIST;
19488 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489
Bram Moolenaar0d660222005-01-07 21:51:51 +000019490 len = list_len(l);
19491 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019492 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493
Bram Moolenaar0b962472016-02-22 22:51:33 +010019494 info.item_compare_ic = FALSE;
19495 info.item_compare_numeric = FALSE;
19496 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019497#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019498 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019499#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019500 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019501 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019502 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019503 if (argvars[1].v_type != VAR_UNKNOWN)
19504 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019505 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019506 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019507 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019508 else if (argvars[1].v_type == VAR_PARTIAL)
19509 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019510 else
19511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019512 int error = FALSE;
19513
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019514 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019515 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019516 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019517 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019518 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019519 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019520 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019521 else if (i != 0)
19522 {
19523 EMSG(_(e_invarg));
19524 goto theend;
19525 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019526 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019527 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019528 if (*info.item_compare_func == NUL)
19529 {
19530 /* empty string means default sort */
19531 info.item_compare_func = NULL;
19532 }
19533 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019534 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019535 info.item_compare_func = NULL;
19536 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019537 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019538 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019539 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019540 info.item_compare_func = NULL;
19541 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019542 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019543#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019544 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019545 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019546 info.item_compare_func = NULL;
19547 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019548 }
19549#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019550 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019551 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019552 info.item_compare_func = NULL;
19553 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019554 }
19555 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019556 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019557
19558 if (argvars[2].v_type != VAR_UNKNOWN)
19559 {
19560 /* optional third argument: {dict} */
19561 if (argvars[2].v_type != VAR_DICT)
19562 {
19563 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019564 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019565 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019566 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019567 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569
Bram Moolenaar0d660222005-01-07 21:51:51 +000019570 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019571 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019572 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019573 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574
Bram Moolenaar327aa022014-03-25 18:24:23 +010019575 i = 0;
19576 if (sort)
19577 {
19578 /* sort(): ptrs will be the list to sort */
19579 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019580 {
19581 ptrs[i].item = li;
19582 ptrs[i].idx = i;
19583 ++i;
19584 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019585
Bram Moolenaar0b962472016-02-22 22:51:33 +010019586 info.item_compare_func_err = FALSE;
19587 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019588 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019589 if ((info.item_compare_func != NULL
19590 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019591 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019592 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019593 EMSG(_("E702: Sort compare function failed"));
19594 else
19595 {
19596 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019597 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019598 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019599 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019600 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019601
Bram Moolenaar0b962472016-02-22 22:51:33 +010019602 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019603 {
19604 /* Clear the List and append the items in sorted order. */
19605 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19606 l->lv_len = 0;
19607 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019608 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019609 }
19610 }
19611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019613 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019614 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019615
19616 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019617 info.item_compare_func_err = FALSE;
19618 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019619 item_compare_func_ptr = info.item_compare_func != NULL
19620 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019621 ? item_compare2 : item_compare;
19622
19623 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19624 li = li->li_next)
19625 {
19626 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19627 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019628 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019629 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019630 {
19631 EMSG(_("E882: Uniq compare function failed"));
19632 break;
19633 }
19634 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019635
Bram Moolenaar0b962472016-02-22 22:51:33 +010019636 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019637 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019638 while (--i >= 0)
19639 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019640 li = ptrs[i].item->li_next;
19641 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019642 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019643 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019644 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019645 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019646 list_fix_watch(l, li);
19647 listitem_free(li);
19648 l->lv_len--;
19649 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019650 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019651 }
19652
19653 vim_free(ptrs);
19654 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019655theend:
19656 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019657}
19658
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019659/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019660 * "sort({list})" function
19661 */
19662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019663f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019664{
19665 do_sort_uniq(argvars, rettv, TRUE);
19666}
19667
19668/*
19669 * "uniq({list})" function
19670 */
19671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019672f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019673{
19674 do_sort_uniq(argvars, rettv, FALSE);
19675}
19676
19677/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019678 * "soundfold({word})" function
19679 */
19680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019681f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019682{
19683 char_u *s;
19684
19685 rettv->v_type = VAR_STRING;
19686 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019687#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019688 rettv->vval.v_string = eval_soundfold(s);
19689#else
19690 rettv->vval.v_string = vim_strsave(s);
19691#endif
19692}
19693
19694/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019695 * "spellbadword()" function
19696 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019698f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019699{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019700 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019701 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019702 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019703
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019704 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019705 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019706
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019707#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019708 if (argvars[0].v_type == VAR_UNKNOWN)
19709 {
19710 /* Find the start and length of the badly spelled word. */
19711 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19712 if (len != 0)
19713 word = ml_get_cursor();
19714 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019715 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019716 {
19717 char_u *str = get_tv_string_chk(&argvars[0]);
19718 int capcol = -1;
19719
19720 if (str != NULL)
19721 {
19722 /* Check the argument for spelling. */
19723 while (*str != NUL)
19724 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019725 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019726 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019727 {
19728 word = str;
19729 break;
19730 }
19731 str += len;
19732 }
19733 }
19734 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019735#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019737 list_append_string(rettv->vval.v_list, word, len);
19738 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019739 attr == HLF_SPB ? "bad" :
19740 attr == HLF_SPR ? "rare" :
19741 attr == HLF_SPL ? "local" :
19742 attr == HLF_SPC ? "caps" :
19743 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019744}
19745
19746/*
19747 * "spellsuggest()" function
19748 */
19749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019750f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019751{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019752#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019753 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019754 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019755 int maxcount;
19756 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019757 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019758 listitem_T *li;
19759 int need_capital = FALSE;
19760#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019761
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019762 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019763 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019764
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019765#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019766 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019767 {
19768 str = get_tv_string(&argvars[0]);
19769 if (argvars[1].v_type != VAR_UNKNOWN)
19770 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019771 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019772 if (maxcount <= 0)
19773 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019774 if (argvars[2].v_type != VAR_UNKNOWN)
19775 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019776 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019777 if (typeerr)
19778 return;
19779 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019780 }
19781 else
19782 maxcount = 25;
19783
Bram Moolenaar4770d092006-01-12 23:22:24 +000019784 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019785
19786 for (i = 0; i < ga.ga_len; ++i)
19787 {
19788 str = ((char_u **)ga.ga_data)[i];
19789
19790 li = listitem_alloc();
19791 if (li == NULL)
19792 vim_free(str);
19793 else
19794 {
19795 li->li_tv.v_type = VAR_STRING;
19796 li->li_tv.v_lock = 0;
19797 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019798 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019799 }
19800 }
19801 ga_clear(&ga);
19802 }
19803#endif
19804}
19805
Bram Moolenaar0d660222005-01-07 21:51:51 +000019806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019807f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019808{
19809 char_u *str;
19810 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019811 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019812 regmatch_T regmatch;
19813 char_u patbuf[NUMBUFLEN];
19814 char_u *save_cpo;
19815 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019816 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019817 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019818 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019819
19820 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19821 save_cpo = p_cpo;
19822 p_cpo = (char_u *)"";
19823
19824 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019825 if (argvars[1].v_type != VAR_UNKNOWN)
19826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019827 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19828 if (pat == NULL)
19829 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019830 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019831 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019832 }
19833 if (pat == NULL || *pat == NUL)
19834 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019835
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019836 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019838 if (typeerr)
19839 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840
Bram Moolenaar0d660222005-01-07 21:51:51 +000019841 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19842 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019844 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019845 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019846 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019847 if (*str == NUL)
19848 match = FALSE; /* empty item at the end */
19849 else
19850 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019851 if (match)
19852 end = regmatch.startp[0];
19853 else
19854 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019855 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19856 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019857 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019858 if (list_append_string(rettv->vval.v_list, str,
19859 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019860 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019861 }
19862 if (!match)
19863 break;
19864 /* Advance to just after the match. */
19865 if (regmatch.endp[0] > str)
19866 col = 0;
19867 else
19868 {
19869 /* Don't get stuck at the same match. */
19870#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019871 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019872#else
19873 col = 1;
19874#endif
19875 }
19876 str = regmatch.endp[0];
19877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878
Bram Moolenaar473de612013-06-08 18:19:48 +020019879 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881
Bram Moolenaar0d660222005-01-07 21:51:51 +000019882 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883}
19884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019885#ifdef FEAT_FLOAT
19886/*
19887 * "sqrt()" function
19888 */
19889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019890f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019891{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019892 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019893
19894 rettv->v_type = VAR_FLOAT;
19895 if (get_float_arg(argvars, &f) == OK)
19896 rettv->vval.v_float = sqrt(f);
19897 else
19898 rettv->vval.v_float = 0.0;
19899}
19900
19901/*
19902 * "str2float()" function
19903 */
19904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019905f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019906{
19907 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19908
19909 if (*p == '+')
19910 p = skipwhite(p + 1);
19911 (void)string2float(p, &rettv->vval.v_float);
19912 rettv->v_type = VAR_FLOAT;
19913}
19914#endif
19915
Bram Moolenaar2c932302006-03-18 21:42:09 +000019916/*
19917 * "str2nr()" function
19918 */
19919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019920f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019921{
19922 int base = 10;
19923 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019924 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019925 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019926
19927 if (argvars[1].v_type != VAR_UNKNOWN)
19928 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019929 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019930 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019931 {
19932 EMSG(_(e_invarg));
19933 return;
19934 }
19935 }
19936
19937 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019938 if (*p == '+')
19939 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019940 switch (base)
19941 {
19942 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19943 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19944 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19945 default: what = 0;
19946 }
19947 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019948 rettv->vval.v_number = n;
19949}
19950
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951#ifdef HAVE_STRFTIME
19952/*
19953 * "strftime({format}[, {time}])" function
19954 */
19955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019956f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957{
19958 char_u result_buf[256];
19959 struct tm *curtime;
19960 time_t seconds;
19961 char_u *p;
19962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019963 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019965 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019966 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 seconds = time(NULL);
19968 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 curtime = localtime(&seconds);
19971 /* MSVC returns NULL for an invalid value of seconds. */
19972 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 else
19975 {
19976# ifdef FEAT_MBYTE
19977 vimconv_T conv;
19978 char_u *enc;
19979
19980 conv.vc_type = CONV_NONE;
19981 enc = enc_locale();
19982 convert_setup(&conv, p_enc, enc);
19983 if (conv.vc_type != CONV_NONE)
19984 p = string_convert(&conv, p, NULL);
19985# endif
19986 if (p != NULL)
19987 (void)strftime((char *)result_buf, sizeof(result_buf),
19988 (char *)p, curtime);
19989 else
19990 result_buf[0] = NUL;
19991
19992# ifdef FEAT_MBYTE
19993 if (conv.vc_type != CONV_NONE)
19994 vim_free(p);
19995 convert_setup(&conv, enc, p_enc);
19996 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019997 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998 else
19999# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020000 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001
20002# ifdef FEAT_MBYTE
20003 /* Release conversion descriptors */
20004 convert_setup(&conv, NULL, NULL);
20005 vim_free(enc);
20006# endif
20007 }
20008}
20009#endif
20010
20011/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020012 * "strgetchar()" function
20013 */
20014 static void
20015f_strgetchar(typval_T *argvars, typval_T *rettv)
20016{
20017 char_u *str;
20018 int len;
20019 int error = FALSE;
20020 int charidx;
20021
20022 rettv->vval.v_number = -1;
20023 str = get_tv_string_chk(&argvars[0]);
20024 if (str == NULL)
20025 return;
20026 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020027 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020028 if (error)
20029 return;
20030#ifdef FEAT_MBYTE
20031 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020032 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020033
20034 while (charidx >= 0 && byteidx < len)
20035 {
20036 if (charidx == 0)
20037 {
20038 rettv->vval.v_number = mb_ptr2char(str + byteidx);
20039 break;
20040 }
20041 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020020042 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020043 }
20044 }
20045#else
20046 if (charidx < len)
20047 rettv->vval.v_number = str[charidx];
20048#endif
20049}
20050
20051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052 * "stridx()" function
20053 */
20054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020055f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056{
20057 char_u buf[NUMBUFLEN];
20058 char_u *needle;
20059 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000020060 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020061 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020064 needle = get_tv_string_chk(&argvars[1]);
20065 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020067 if (needle == NULL || haystack == NULL)
20068 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069
Bram Moolenaar33570922005-01-25 22:26:29 +000020070 if (argvars[2].v_type != VAR_UNKNOWN)
20071 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020072 int error = FALSE;
20073
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020074 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020075 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000020076 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020077 if (start_idx >= 0)
20078 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000020079 }
20080
20081 pos = (char_u *)strstr((char *)haystack, (char *)needle);
20082 if (pos != NULL)
20083 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084}
20085
20086/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020087 * "string()" function
20088 */
20089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020090f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020091{
20092 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020093 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010020096 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
20097 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020098 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000020099 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101}
20102
20103/*
20104 * "strlen()" function
20105 */
20106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020107f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020109 rettv->vval.v_number = (varnumber_T)(STRLEN(
20110 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111}
20112
20113/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020114 * "strchars()" function
20115 */
20116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020117f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020118{
20119 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020120 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020020121#ifdef FEAT_MBYTE
20122 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020123 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020020124#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020125
20126 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020127 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020020128 if (skipcc < 0 || skipcc > 1)
20129 EMSG(_(e_invarg));
20130 else
20131 {
20132#ifdef FEAT_MBYTE
20133 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20134 while (*s != NUL)
20135 {
20136 func_mb_ptr2char_adv(&s);
20137 ++len;
20138 }
20139 rettv->vval.v_number = len;
20140#else
20141 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20142#endif
20143 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020144}
20145
20146/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020147 * "strdisplaywidth()" function
20148 */
20149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020150f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020151{
20152 char_u *s = get_tv_string(&argvars[0]);
20153 int col = 0;
20154
20155 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020156 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020157
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020158 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020159}
20160
20161/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020162 * "strwidth()" function
20163 */
20164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020165f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020166{
20167 char_u *s = get_tv_string(&argvars[0]);
20168
20169 rettv->vval.v_number = (varnumber_T)(
20170#ifdef FEAT_MBYTE
20171 mb_string2cells(s, -1)
20172#else
20173 STRLEN(s)
20174#endif
20175 );
20176}
20177
20178/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020179 * "strcharpart()" function
20180 */
20181 static void
20182f_strcharpart(typval_T *argvars, typval_T *rettv)
20183{
20184#ifdef FEAT_MBYTE
20185 char_u *p;
20186 int nchar;
20187 int nbyte = 0;
20188 int charlen;
20189 int len = 0;
20190 int slen;
20191 int error = FALSE;
20192
20193 p = get_tv_string(&argvars[0]);
20194 slen = (int)STRLEN(p);
20195
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020196 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020197 if (!error)
20198 {
20199 if (nchar > 0)
20200 while (nchar > 0 && nbyte < slen)
20201 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020202 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020203 --nchar;
20204 }
20205 else
20206 nbyte = nchar;
20207 if (argvars[2].v_type != VAR_UNKNOWN)
20208 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020209 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020210 while (charlen > 0 && nbyte + len < slen)
20211 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020212 int off = nbyte + len;
20213
20214 if (off < 0)
20215 len += 1;
20216 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020217 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020218 --charlen;
20219 }
20220 }
20221 else
20222 len = slen - nbyte; /* default: all bytes that are available. */
20223 }
20224
20225 /*
20226 * Only return the overlap between the specified part and the actual
20227 * string.
20228 */
20229 if (nbyte < 0)
20230 {
20231 len += nbyte;
20232 nbyte = 0;
20233 }
20234 else if (nbyte > slen)
20235 nbyte = slen;
20236 if (len < 0)
20237 len = 0;
20238 else if (nbyte + len > slen)
20239 len = slen - nbyte;
20240
20241 rettv->v_type = VAR_STRING;
20242 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20243#else
20244 f_strpart(argvars, rettv);
20245#endif
20246}
20247
20248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 * "strpart()" function
20250 */
20251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020252f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253{
20254 char_u *p;
20255 int n;
20256 int len;
20257 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020258 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020260 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 slen = (int)STRLEN(p);
20262
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020263 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020264 if (error)
20265 len = 0;
20266 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020267 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 else
20269 len = slen - n; /* default len: all bytes that are available. */
20270
20271 /*
20272 * Only return the overlap between the specified part and the actual
20273 * string.
20274 */
20275 if (n < 0)
20276 {
20277 len += n;
20278 n = 0;
20279 }
20280 else if (n > slen)
20281 n = slen;
20282 if (len < 0)
20283 len = 0;
20284 else if (n + len > slen)
20285 len = slen - n;
20286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 rettv->v_type = VAR_STRING;
20288 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289}
20290
20291/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020292 * "strridx()" function
20293 */
20294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020295f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020296{
20297 char_u buf[NUMBUFLEN];
20298 char_u *needle;
20299 char_u *haystack;
20300 char_u *rest;
20301 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020302 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020304 needle = get_tv_string_chk(&argvars[1]);
20305 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020306
20307 rettv->vval.v_number = -1;
20308 if (needle == NULL || haystack == NULL)
20309 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020310
20311 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020312 if (argvars[2].v_type != VAR_UNKNOWN)
20313 {
20314 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020315 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020316 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020317 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020318 }
20319 else
20320 end_idx = haystack_len;
20321
Bram Moolenaar0d660222005-01-07 21:51:51 +000020322 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020323 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020324 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020325 lastmatch = haystack + end_idx;
20326 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020327 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020328 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020329 for (rest = haystack; *rest != '\0'; ++rest)
20330 {
20331 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020332 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020333 break;
20334 lastmatch = rest;
20335 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020336 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020337
20338 if (lastmatch == NULL)
20339 rettv->vval.v_number = -1;
20340 else
20341 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20342}
20343
20344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 * "strtrans()" function
20346 */
20347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020348f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020350 rettv->v_type = VAR_STRING;
20351 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352}
20353
20354/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020355 * "submatch()" function
20356 */
20357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020358f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020359{
Bram Moolenaar41571762014-04-02 19:00:58 +020020360 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020361 int no;
20362 int retList = 0;
20363
20364 no = (int)get_tv_number_chk(&argvars[0], &error);
20365 if (error)
20366 return;
20367 error = FALSE;
20368 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020369 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020370 if (error)
20371 return;
20372
20373 if (retList == 0)
20374 {
20375 rettv->v_type = VAR_STRING;
20376 rettv->vval.v_string = reg_submatch(no);
20377 }
20378 else
20379 {
20380 rettv->v_type = VAR_LIST;
20381 rettv->vval.v_list = reg_submatch_list(no);
20382 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020383}
20384
20385/*
20386 * "substitute()" function
20387 */
20388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020389f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020390{
20391 char_u patbuf[NUMBUFLEN];
20392 char_u subbuf[NUMBUFLEN];
20393 char_u flagsbuf[NUMBUFLEN];
20394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020395 char_u *str = get_tv_string_chk(&argvars[0]);
20396 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20397 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20398 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20399
Bram Moolenaar0d660222005-01-07 21:51:51 +000020400 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020401 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20402 rettv->vval.v_string = NULL;
20403 else
20404 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020405}
20406
20407/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020408 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020411f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412{
20413 int id = 0;
20414#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020415 linenr_T lnum;
20416 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020418 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020420 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020421 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20422 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020424 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020425 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020426 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427#endif
20428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020429 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430}
20431
20432/*
20433 * "synIDattr(id, what [, mode])" function
20434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020436f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437{
20438 char_u *p = NULL;
20439#ifdef FEAT_SYN_HL
20440 int id;
20441 char_u *what;
20442 char_u *mode;
20443 char_u modebuf[NUMBUFLEN];
20444 int modec;
20445
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020446 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020450 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020452 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 modec = 0; /* replace invalid with current */
20454 }
20455 else
20456 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020457#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020458 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459 modec = 'g';
20460 else
20461#endif
20462 if (t_colors > 1)
20463 modec = 'c';
20464 else
20465 modec = 't';
20466 }
20467
20468
20469 switch (TOLOWER_ASC(what[0]))
20470 {
20471 case 'b':
20472 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20473 p = highlight_color(id, what, modec);
20474 else /* bold */
20475 p = highlight_has_attr(id, HL_BOLD, modec);
20476 break;
20477
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020478 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 p = highlight_color(id, what, modec);
20480 break;
20481
20482 case 'i':
20483 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20484 p = highlight_has_attr(id, HL_INVERSE, modec);
20485 else /* italic */
20486 p = highlight_has_attr(id, HL_ITALIC, modec);
20487 break;
20488
20489 case 'n': /* name */
20490 p = get_highlight_name(NULL, id - 1);
20491 break;
20492
20493 case 'r': /* reverse */
20494 p = highlight_has_attr(id, HL_INVERSE, modec);
20495 break;
20496
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020497 case 's':
20498 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20499 p = highlight_color(id, what, modec);
20500 else /* standout */
20501 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 break;
20503
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020504 case 'u':
20505 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20506 /* underline */
20507 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20508 else
20509 /* undercurl */
20510 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 break;
20512 }
20513
20514 if (p != NULL)
20515 p = vim_strsave(p);
20516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020517 rettv->v_type = VAR_STRING;
20518 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519}
20520
20521/*
20522 * "synIDtrans(id)" function
20523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020525f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526{
20527 int id;
20528
20529#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020530 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531
20532 if (id > 0)
20533 id = syn_get_final_id(id);
20534 else
20535#endif
20536 id = 0;
20537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020538 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539}
20540
20541/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020542 * "synconcealed(lnum, col)" function
20543 */
20544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020545f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020546{
20547#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020548 linenr_T lnum;
20549 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020550 int syntax_flags = 0;
20551 int cchar;
20552 int matchid = 0;
20553 char_u str[NUMBUFLEN];
20554#endif
20555
20556 rettv->v_type = VAR_LIST;
20557 rettv->vval.v_list = NULL;
20558
20559#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20560 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020561 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020562
20563 vim_memset(str, NUL, sizeof(str));
20564
20565 if (rettv_list_alloc(rettv) != FAIL)
20566 {
20567 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20568 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20569 && curwin->w_p_cole > 0)
20570 {
20571 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20572 syntax_flags = get_syntax_info(&matchid);
20573
20574 /* get the conceal character */
20575 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20576 {
20577 cchar = syn_get_sub_char();
20578 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20579 cchar = lcs_conceal;
20580 if (cchar != NUL)
20581 {
20582# ifdef FEAT_MBYTE
20583 if (has_mbyte)
20584 (*mb_char2bytes)(cchar, str);
20585 else
20586# endif
20587 str[0] = cchar;
20588 }
20589 }
20590 }
20591
20592 list_append_number(rettv->vval.v_list,
20593 (syntax_flags & HL_CONCEAL) != 0);
20594 /* -1 to auto-determine strlen */
20595 list_append_string(rettv->vval.v_list, str, -1);
20596 list_append_number(rettv->vval.v_list, matchid);
20597 }
20598#endif
20599}
20600
20601/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020602 * "synstack(lnum, col)" function
20603 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020605f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020606{
20607#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020608 linenr_T lnum;
20609 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020610 int i;
20611 int id;
20612#endif
20613
20614 rettv->v_type = VAR_LIST;
20615 rettv->vval.v_list = NULL;
20616
20617#ifdef FEAT_SYN_HL
20618 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020619 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020620
20621 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020622 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020623 && rettv_list_alloc(rettv) != FAIL)
20624 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020625 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020626 for (i = 0; ; ++i)
20627 {
20628 id = syn_get_stack_item(i);
20629 if (id < 0)
20630 break;
20631 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20632 break;
20633 }
20634 }
20635#endif
20636}
20637
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020639get_cmd_output_as_rettv(
20640 typval_T *argvars,
20641 typval_T *rettv,
20642 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020644 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020646 char_u *infile = NULL;
20647 char_u buf[NUMBUFLEN];
20648 int err = FALSE;
20649 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020650 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020651 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020653 rettv->v_type = VAR_STRING;
20654 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020655 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020656 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020658 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020659 {
20660 /*
20661 * Write the string to a temp file, to be used for input of the shell
20662 * command.
20663 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020664 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020665 {
20666 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020667 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020668 }
20669
20670 fd = mch_fopen((char *)infile, WRITEBIN);
20671 if (fd == NULL)
20672 {
20673 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020674 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020675 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020676 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020677 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020678 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20679 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020680 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020681 else
20682 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020683 size_t len;
20684
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020685 p = get_tv_string_buf_chk(&argvars[1], buf);
20686 if (p == NULL)
20687 {
20688 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020689 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020690 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020691 len = STRLEN(p);
20692 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020693 err = TRUE;
20694 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020695 if (fclose(fd) != 0)
20696 err = TRUE;
20697 if (err)
20698 {
20699 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020700 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020701 }
20702 }
20703
Bram Moolenaar52a72462014-08-29 15:53:52 +020020704 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20705 * echoes typeahead, that messes up the display. */
20706 if (!msg_silent)
20707 flags += SHELL_COOKED;
20708
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020709 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020711 int len;
20712 listitem_T *li;
20713 char_u *s = NULL;
20714 char_u *start;
20715 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020716 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717
Bram Moolenaar52a72462014-08-29 15:53:52 +020020718 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020719 if (res == NULL)
20720 goto errret;
20721
20722 list = list_alloc();
20723 if (list == NULL)
20724 goto errret;
20725
20726 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020728 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020729 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020730 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020731 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020732
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020733 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020734 if (s == NULL)
20735 goto errret;
20736
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020737 for (p = s; start < end; ++p, ++start)
20738 *p = *start == NUL ? NL : *start;
20739 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020740
20741 li = listitem_alloc();
20742 if (li == NULL)
20743 {
20744 vim_free(s);
20745 goto errret;
20746 }
20747 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020748 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020749 li->li_tv.vval.v_string = s;
20750 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020752
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020753 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020754 rettv->v_type = VAR_LIST;
20755 rettv->vval.v_list = list;
20756 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020758 else
20759 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020760 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020761#ifdef USE_CR
20762 /* translate <CR> into <NL> */
20763 if (res != NULL)
20764 {
20765 char_u *s;
20766
20767 for (s = res; *s; ++s)
20768 {
20769 if (*s == CAR)
20770 *s = NL;
20771 }
20772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773#else
20774# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020775 /* translate <CR><NL> into <NL> */
20776 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020778 char_u *s, *d;
20779
20780 d = res;
20781 for (s = res; *s; ++s)
20782 {
20783 if (s[0] == CAR && s[1] == NL)
20784 ++s;
20785 *d++ = *s;
20786 }
20787 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789# endif
20790#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020791 rettv->vval.v_string = res;
20792 res = NULL;
20793 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020794
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020795errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020796 if (infile != NULL)
20797 {
20798 mch_remove(infile);
20799 vim_free(infile);
20800 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020801 if (res != NULL)
20802 vim_free(res);
20803 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020804 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020805}
20806
20807/*
20808 * "system()" function
20809 */
20810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020811f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020812{
20813 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20814}
20815
20816/*
20817 * "systemlist()" function
20818 */
20819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020820f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020821{
20822 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823}
20824
20825/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020826 * "tabpagebuflist()" function
20827 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020829f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020830{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020831#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020832 tabpage_T *tp;
20833 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020834
20835 if (argvars[0].v_type == VAR_UNKNOWN)
20836 wp = firstwin;
20837 else
20838 {
20839 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20840 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020841 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020842 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020843 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020844 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020845 for (; wp != NULL; wp = wp->w_next)
20846 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020847 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020848 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020849 }
20850#endif
20851}
20852
20853
20854/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020855 * "tabpagenr()" function
20856 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020858f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020859{
20860 int nr = 1;
20861#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020862 char_u *arg;
20863
20864 if (argvars[0].v_type != VAR_UNKNOWN)
20865 {
20866 arg = get_tv_string_chk(&argvars[0]);
20867 nr = 0;
20868 if (arg != NULL)
20869 {
20870 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020871 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020872 else
20873 EMSG2(_(e_invexpr2), arg);
20874 }
20875 }
20876 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020877 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020878#endif
20879 rettv->vval.v_number = nr;
20880}
20881
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020882
20883#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020884static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020885
20886/*
20887 * Common code for tabpagewinnr() and winnr().
20888 */
20889 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020891{
20892 win_T *twin;
20893 int nr = 1;
20894 win_T *wp;
20895 char_u *arg;
20896
20897 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20898 if (argvar->v_type != VAR_UNKNOWN)
20899 {
20900 arg = get_tv_string_chk(argvar);
20901 if (arg == NULL)
20902 nr = 0; /* type error; errmsg already given */
20903 else if (STRCMP(arg, "$") == 0)
20904 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20905 else if (STRCMP(arg, "#") == 0)
20906 {
20907 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20908 if (twin == NULL)
20909 nr = 0;
20910 }
20911 else
20912 {
20913 EMSG2(_(e_invexpr2), arg);
20914 nr = 0;
20915 }
20916 }
20917
20918 if (nr > 0)
20919 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20920 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020921 {
20922 if (wp == NULL)
20923 {
20924 /* didn't find it in this tabpage */
20925 nr = 0;
20926 break;
20927 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020928 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020929 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020930 return nr;
20931}
20932#endif
20933
20934/*
20935 * "tabpagewinnr()" function
20936 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020938f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020939{
20940 int nr = 1;
20941#ifdef FEAT_WINDOWS
20942 tabpage_T *tp;
20943
20944 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20945 if (tp == NULL)
20946 nr = 0;
20947 else
20948 nr = get_winnr(tp, &argvars[1]);
20949#endif
20950 rettv->vval.v_number = nr;
20951}
20952
20953
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020954/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020955 * "tagfiles()" function
20956 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020958f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020959{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020960 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020961 tagname_T tn;
20962 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020963
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020964 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020965 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020966 fname = alloc(MAXPATHL);
20967 if (fname == NULL)
20968 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020969
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020970 for (first = TRUE; ; first = FALSE)
20971 if (get_tagfname(&tn, first, fname) == FAIL
20972 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020973 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020974 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020975 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020976}
20977
20978/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020979 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020980 */
20981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020982f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020983{
20984 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020985
20986 tag_pattern = get_tv_string(&argvars[0]);
20987
20988 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020989 if (*tag_pattern == NUL)
20990 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020991
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020992 if (rettv_list_alloc(rettv) == OK)
20993 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020994}
20995
20996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 * "tempname()" function
20998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021000f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001{
21002 static int x = 'A';
21003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020021005 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006
21007 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
21008 * names. Skip 'I' and 'O', they are used for shell redirection. */
21009 do
21010 {
21011 if (x == 'Z')
21012 x = '0';
21013 else if (x == '9')
21014 x = 'A';
21015 else
21016 {
21017#ifdef EBCDIC
21018 if (x == 'I')
21019 x = 'J';
21020 else if (x == 'R')
21021 x = 'S';
21022 else
21023#endif
21024 ++x;
21025 }
21026 } while (x == 'I' || x == 'O');
21027}
21028
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021029#ifdef FEAT_FLOAT
21030/*
21031 * "tan()" function
21032 */
21033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021034f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021035{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021036 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021037
21038 rettv->v_type = VAR_FLOAT;
21039 if (get_float_arg(argvars, &f) == OK)
21040 rettv->vval.v_float = tan(f);
21041 else
21042 rettv->vval.v_float = 0.0;
21043}
21044
21045/*
21046 * "tanh()" function
21047 */
21048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021049f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021050{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021051 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020021052
21053 rettv->v_type = VAR_FLOAT;
21054 if (get_float_arg(argvars, &f) == OK)
21055 rettv->vval.v_float = tanh(f);
21056 else
21057 rettv->vval.v_float = 0.0;
21058}
21059#endif
21060
Bram Moolenaar574860b2016-05-24 17:33:34 +020021061/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021062 * "test_alloc_fail(id, countdown, repeat)" function
21063 */
21064 static void
21065f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
21066{
21067 if (argvars[0].v_type != VAR_NUMBER
21068 || argvars[0].vval.v_number <= 0
21069 || argvars[1].v_type != VAR_NUMBER
21070 || argvars[1].vval.v_number < 0
21071 || argvars[2].v_type != VAR_NUMBER)
21072 EMSG(_(e_invarg));
21073 else
21074 {
21075 alloc_fail_id = argvars[0].vval.v_number;
21076 if (alloc_fail_id >= aid_last)
21077 EMSG(_(e_invarg));
21078 alloc_fail_countdown = argvars[1].vval.v_number;
21079 alloc_fail_repeat = argvars[2].vval.v_number;
21080 did_outofmem_msg = FALSE;
21081 }
21082}
21083
21084/*
Bram Moolenaar5c719942016-07-09 23:40:45 +020021085 * "test_autochdir()"
21086 */
21087 static void
21088f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21089{
21090#if defined(FEAT_AUTOCHDIR)
21091 test_autochdir = TRUE;
21092#endif
21093}
21094
21095/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021096 * "test_disable_char_avail({expr})" function
21097 */
21098 static void
21099f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
21100{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021101 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020021102}
21103
21104/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020021105 * "test_garbagecollect_now()" function
21106 */
21107 static void
21108f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
21109{
21110 /* This is dangerous, any Lists and Dicts used internally may be freed
21111 * while still in use. */
21112 garbage_collect(TRUE);
21113}
21114
21115#ifdef FEAT_JOB_CHANNEL
21116 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021117f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021118{
21119 rettv->v_type = VAR_CHANNEL;
21120 rettv->vval.v_channel = NULL;
21121}
21122#endif
21123
21124 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021125f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021126{
21127 rettv->v_type = VAR_DICT;
21128 rettv->vval.v_dict = NULL;
21129}
21130
21131#ifdef FEAT_JOB_CHANNEL
21132 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021133f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021134{
21135 rettv->v_type = VAR_JOB;
21136 rettv->vval.v_job = NULL;
21137}
21138#endif
21139
21140 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021141f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021142{
21143 rettv->v_type = VAR_LIST;
21144 rettv->vval.v_list = NULL;
21145}
21146
21147 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021148f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021149{
21150 rettv->v_type = VAR_PARTIAL;
21151 rettv->vval.v_partial = NULL;
21152}
21153
21154 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021155f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021156{
21157 rettv->v_type = VAR_STRING;
21158 rettv->vval.v_string = NULL;
21159}
21160
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021161 static void
21162f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21163{
21164 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21165}
21166
Bram Moolenaar975b5272016-03-15 23:10:59 +010021167#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21168/*
21169 * Get a callback from "arg". It can be a Funcref or a function name.
21170 * When "arg" is zero return an empty string.
21171 * Return NULL for an invalid argument.
21172 */
21173 char_u *
21174get_callback(typval_T *arg, partial_T **pp)
21175{
21176 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21177 {
21178 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021179 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021180 return (*pp)->pt_name;
21181 }
21182 *pp = NULL;
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020021183 if (arg->v_type == VAR_FUNC)
21184 {
21185 func_ref(arg->vval.v_string);
21186 return arg->vval.v_string;
21187 }
21188 if (arg->v_type == VAR_STRING)
Bram Moolenaar975b5272016-03-15 23:10:59 +010021189 return arg->vval.v_string;
21190 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21191 return (char_u *)"";
21192 EMSG(_("E921: Invalid callback argument"));
21193 return NULL;
21194}
Bram Moolenaar1436d8d2016-07-11 22:41:15 +020021195
21196/*
21197 * Unref/free "callback" and "partial" retured by get_callback().
21198 */
21199 void
21200free_callback(char_u *callback, partial_T *partial)
21201{
21202 if (partial != NULL)
21203 partial_unref(partial);
21204 else if (callback != NULL)
21205 {
21206 func_unref(callback);
21207 vim_free(callback);
21208 }
21209}
Bram Moolenaar975b5272016-03-15 23:10:59 +010021210#endif
21211
21212#ifdef FEAT_TIMERS
21213/*
21214 * "timer_start(time, callback [, options])" function
21215 */
21216 static void
21217f_timer_start(typval_T *argvars, typval_T *rettv)
21218{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021219 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021220 timer_T *timer;
21221 int repeat = 0;
21222 char_u *callback;
21223 dict_T *dict;
21224
Bram Moolenaar38499922016-04-22 20:46:52 +020021225 if (check_secure())
21226 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021227 if (argvars[2].v_type != VAR_UNKNOWN)
21228 {
21229 if (argvars[2].v_type != VAR_DICT
21230 || (dict = argvars[2].vval.v_dict) == NULL)
21231 {
21232 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21233 return;
21234 }
21235 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21236 repeat = get_dict_number(dict, (char_u *)"repeat");
21237 }
21238
21239 timer = create_timer(msec, repeat);
21240 callback = get_callback(&argvars[1], &timer->tr_partial);
21241 if (callback == NULL)
21242 {
21243 stop_timer(timer);
21244 rettv->vval.v_number = -1;
21245 }
21246 else
21247 {
21248 timer->tr_callback = vim_strsave(callback);
21249 rettv->vval.v_number = timer->tr_id;
21250 }
21251}
21252
21253/*
21254 * "timer_stop(timer)" function
21255 */
21256 static void
21257f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21258{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021259 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021260
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021261 if (argvars[0].v_type != VAR_NUMBER)
21262 {
Bram Moolenaared59aa62016-07-09 17:41:12 +020021263 EMSG(_(e_number_exp));
21264 return;
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021265 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021266 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021267 if (timer != NULL)
21268 stop_timer(timer);
21269}
21270#endif
21271
Bram Moolenaard52d9742005-08-21 22:20:28 +000021272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 * "tolower(string)" function
21274 */
21275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021276f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277{
21278 char_u *p;
21279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021280 p = vim_strsave(get_tv_string(&argvars[0]));
21281 rettv->v_type = VAR_STRING;
21282 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283
21284 if (p != NULL)
21285 while (*p != NUL)
21286 {
21287#ifdef FEAT_MBYTE
21288 int l;
21289
21290 if (enc_utf8)
21291 {
21292 int c, lc;
21293
21294 c = utf_ptr2char(p);
21295 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021296 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 /* TODO: reallocate string when byte count changes. */
21298 if (utf_char2len(lc) == l)
21299 utf_char2bytes(lc, p);
21300 p += l;
21301 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021302 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 p += l; /* skip multi-byte character */
21304 else
21305#endif
21306 {
21307 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21308 ++p;
21309 }
21310 }
21311}
21312
21313/*
21314 * "toupper(string)" function
21315 */
21316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021317f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021319 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021320 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321}
21322
21323/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021324 * "tr(string, fromstr, tostr)" function
21325 */
21326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021327f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021328{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021329 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021330 char_u *fromstr;
21331 char_u *tostr;
21332 char_u *p;
21333#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021334 int inlen;
21335 int fromlen;
21336 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021337 int idx;
21338 char_u *cpstr;
21339 int cplen;
21340 int first = TRUE;
21341#endif
21342 char_u buf[NUMBUFLEN];
21343 char_u buf2[NUMBUFLEN];
21344 garray_T ga;
21345
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021346 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021347 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21348 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021349
21350 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 rettv->v_type = VAR_STRING;
21352 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021353 if (fromstr == NULL || tostr == NULL)
21354 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021355 ga_init2(&ga, (int)sizeof(char), 80);
21356
21357#ifdef FEAT_MBYTE
21358 if (!has_mbyte)
21359#endif
21360 /* not multi-byte: fromstr and tostr must be the same length */
21361 if (STRLEN(fromstr) != STRLEN(tostr))
21362 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021363#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021364error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021365#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021366 EMSG2(_(e_invarg2), fromstr);
21367 ga_clear(&ga);
21368 return;
21369 }
21370
21371 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021372 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021373 {
21374#ifdef FEAT_MBYTE
21375 if (has_mbyte)
21376 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021377 inlen = (*mb_ptr2len)(in_str);
21378 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021379 cplen = inlen;
21380 idx = 0;
21381 for (p = fromstr; *p != NUL; p += fromlen)
21382 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021383 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021384 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021385 {
21386 for (p = tostr; *p != NUL; p += tolen)
21387 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021388 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021389 if (idx-- == 0)
21390 {
21391 cplen = tolen;
21392 cpstr = p;
21393 break;
21394 }
21395 }
21396 if (*p == NUL) /* tostr is shorter than fromstr */
21397 goto error;
21398 break;
21399 }
21400 ++idx;
21401 }
21402
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021403 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021404 {
21405 /* Check that fromstr and tostr have the same number of
21406 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021407 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021408 first = FALSE;
21409 for (p = tostr; *p != NUL; p += tolen)
21410 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021411 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021412 --idx;
21413 }
21414 if (idx != 0)
21415 goto error;
21416 }
21417
Bram Moolenaarcde88542015-08-11 19:14:00 +020021418 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021419 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021420 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021421
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021422 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021423 }
21424 else
21425#endif
21426 {
21427 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021428 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021429 if (p != NULL)
21430 ga_append(&ga, tostr[p - fromstr]);
21431 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021432 ga_append(&ga, *in_str);
21433 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021434 }
21435 }
21436
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021437 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021438 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021439 ga_append(&ga, NUL);
21440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021441 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021442}
21443
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021444#ifdef FEAT_FLOAT
21445/*
21446 * "trunc({float})" function
21447 */
21448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021449f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021450{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021451 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021452
21453 rettv->v_type = VAR_FLOAT;
21454 if (get_float_arg(argvars, &f) == OK)
21455 /* trunc() is not in C90, use floor() or ceil() instead. */
21456 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21457 else
21458 rettv->vval.v_float = 0.0;
21459}
21460#endif
21461
Bram Moolenaar8299df92004-07-10 09:47:34 +000021462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 * "type(expr)" function
21464 */
21465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021466f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021467{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021468 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021469
21470 switch (argvars[0].v_type)
21471 {
21472 case VAR_NUMBER: n = 0; break;
21473 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021474 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021475 case VAR_FUNC: n = 2; break;
21476 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021477 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021478 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021479 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021480 if (argvars[0].vval.v_number == VVAL_FALSE
21481 || argvars[0].vval.v_number == VVAL_TRUE)
21482 n = 6;
21483 else
21484 n = 7;
21485 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021486 case VAR_JOB: n = 8; break;
21487 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021488 case VAR_UNKNOWN:
21489 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21490 n = -1;
21491 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021492 }
21493 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494}
21495
21496/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021497 * "undofile(name)" function
21498 */
21499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021500f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021501{
21502 rettv->v_type = VAR_STRING;
21503#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021504 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021505 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021506
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021507 if (*fname == NUL)
21508 {
21509 /* If there is no file name there will be no undo file. */
21510 rettv->vval.v_string = NULL;
21511 }
21512 else
21513 {
21514 char_u *ffname = FullName_save(fname, FALSE);
21515
21516 if (ffname != NULL)
21517 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21518 vim_free(ffname);
21519 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021520 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021521#else
21522 rettv->vval.v_string = NULL;
21523#endif
21524}
21525
21526/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021527 * "undotree()" function
21528 */
21529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021530f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021531{
21532 if (rettv_dict_alloc(rettv) == OK)
21533 {
21534 dict_T *dict = rettv->vval.v_dict;
21535 list_T *list;
21536
Bram Moolenaar730cde92010-06-27 05:18:54 +020021537 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021538 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021539 dict_add_nr_str(dict, "save_last",
21540 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021541 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21542 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021543 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021544
21545 list = list_alloc();
21546 if (list != NULL)
21547 {
21548 u_eval_tree(curbuf->b_u_oldhead, list);
21549 dict_add_list(dict, "entries", list);
21550 }
21551 }
21552}
21553
21554/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021555 * "values(dict)" function
21556 */
21557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021558f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021559{
21560 dict_list(argvars, rettv, 1);
21561}
21562
21563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 * "virtcol(string)" function
21565 */
21566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021567f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568{
21569 colnr_T vcol = 0;
21570 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021571 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021573 fp = var2fpos(&argvars[0], FALSE, &fnum);
21574 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21575 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 {
21577 getvvcol(curwin, fp, NULL, NULL, &vcol);
21578 ++vcol;
21579 }
21580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582}
21583
21584/*
21585 * "visualmode()" function
21586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021588f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021590 char_u str[2];
21591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021592 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 str[0] = curbuf->b_visual_mode_eval;
21594 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021595 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596
21597 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021598 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600}
21601
21602/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021603 * "wildmenumode()" function
21604 */
21605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021606f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021607{
21608#ifdef FEAT_WILDMENU
21609 if (wild_menu_showing)
21610 rettv->vval.v_number = 1;
21611#endif
21612}
21613
21614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 * "winbufnr(nr)" function
21616 */
21617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021618f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619{
21620 win_T *wp;
21621
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021622 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021626 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627}
21628
21629/*
21630 * "wincol()" function
21631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021633f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634{
21635 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021636 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637}
21638
21639/*
21640 * "winheight(nr)" function
21641 */
21642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021643f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644{
21645 win_T *wp;
21646
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021647 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021651 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652}
21653
21654/*
21655 * "winline()" function
21656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021658f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659{
21660 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021661 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662}
21663
21664/*
21665 * "winnr()" function
21666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021668f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669{
21670 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021671
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021673 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021675 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676}
21677
21678/*
21679 * "winrestcmd()" function
21680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021682f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683{
21684#ifdef FEAT_WINDOWS
21685 win_T *wp;
21686 int winnr = 1;
21687 garray_T ga;
21688 char_u buf[50];
21689
21690 ga_init2(&ga, (int)sizeof(char), 70);
21691 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21692 {
21693 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21694 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21696 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 ++winnr;
21698 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021699 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021701 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021705 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706}
21707
21708/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021709 * "winrestview()" function
21710 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021712f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021713{
21714 dict_T *dict;
21715
21716 if (argvars[0].v_type != VAR_DICT
21717 || (dict = argvars[0].vval.v_dict) == NULL)
21718 EMSG(_(e_invarg));
21719 else
21720 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021721 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021722 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021723 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021724 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021725#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021726 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021727 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021728#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021729 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21730 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021731 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021732 curwin->w_set_curswant = FALSE;
21733 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021734
Bram Moolenaar82c25852014-05-28 16:47:16 +020021735 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021736 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021737#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021738 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021739 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021740#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021741 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021742 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021743 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021744 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021745
21746 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021747 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021748# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021749 win_new_width(curwin, W_WIDTH(curwin));
21750# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021751 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021752
Bram Moolenaarb851a962014-10-31 15:45:52 +010021753 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021754 curwin->w_topline = 1;
21755 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21756 curwin->w_topline = curbuf->b_ml.ml_line_count;
21757#ifdef FEAT_DIFF
21758 check_topfill(curwin, TRUE);
21759#endif
21760 }
21761}
21762
21763/*
21764 * "winsaveview()" function
21765 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021767f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021768{
21769 dict_T *dict;
21770
Bram Moolenaara800b422010-06-27 01:15:55 +020021771 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021772 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021773 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021774
21775 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21776 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21777#ifdef FEAT_VIRTUALEDIT
21778 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21779#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021780 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021781 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21782
21783 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21784#ifdef FEAT_DIFF
21785 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21786#endif
21787 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21788 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21789}
21790
21791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 * "winwidth(nr)" function
21793 */
21794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021795f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796{
21797 win_T *wp;
21798
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021799 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021803#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021806 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807#endif
21808}
21809
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021811 * "wordcount()" function
21812 */
21813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021814f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021815{
21816 if (rettv_dict_alloc(rettv) == FAIL)
21817 return;
21818 cursor_pos_info(rettv->vval.v_dict);
21819}
21820
21821/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021822 * Write list of strings to file
21823 */
21824 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021825write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021826{
21827 listitem_T *li;
21828 int c;
21829 int ret = OK;
21830 char_u *s;
21831
21832 for (li = list->lv_first; li != NULL; li = li->li_next)
21833 {
21834 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21835 {
21836 if (*s == '\n')
21837 c = putc(NUL, fd);
21838 else
21839 c = putc(*s, fd);
21840 if (c == EOF)
21841 {
21842 ret = FAIL;
21843 break;
21844 }
21845 }
21846 if (!binary || li->li_next != NULL)
21847 if (putc('\n', fd) == EOF)
21848 {
21849 ret = FAIL;
21850 break;
21851 }
21852 if (ret == FAIL)
21853 {
21854 EMSG(_(e_write));
21855 break;
21856 }
21857 }
21858 return ret;
21859}
21860
21861/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021862 * "writefile()" function
21863 */
21864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021865f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021866{
21867 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021868 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021869 char_u *fname;
21870 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021871 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021872
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021873 if (check_restricted() || check_secure())
21874 return;
21875
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021876 if (argvars[0].v_type != VAR_LIST)
21877 {
21878 EMSG2(_(e_listarg), "writefile()");
21879 return;
21880 }
21881 if (argvars[0].vval.v_list == NULL)
21882 return;
21883
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021884 if (argvars[2].v_type != VAR_UNKNOWN)
21885 {
21886 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21887 binary = TRUE;
21888 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21889 append = TRUE;
21890 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021891
21892 /* Always open the file in binary mode, library functions have a mind of
21893 * their own about CR-LF conversion. */
21894 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021895 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21896 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021897 {
21898 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21899 ret = -1;
21900 }
21901 else
21902 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021903 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21904 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021905 fclose(fd);
21906 }
21907
21908 rettv->vval.v_number = ret;
21909}
21910
21911/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021912 * "xor(expr, expr)" function
21913 */
21914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021915f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021916{
21917 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21918 ^ get_tv_number_chk(&argvars[1], NULL);
21919}
21920
21921
21922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021924 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 */
21926 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021927var2fpos(
21928 typval_T *varp,
21929 int dollar_lnum, /* TRUE when $ is last line */
21930 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021932 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021934 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935
Bram Moolenaara5525202006-03-02 22:52:09 +000021936 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021937 if (varp->v_type == VAR_LIST)
21938 {
21939 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021940 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021941 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021942 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021943
21944 l = varp->vval.v_list;
21945 if (l == NULL)
21946 return NULL;
21947
21948 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021949 pos.lnum = list_find_nr(l, 0L, &error);
21950 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021951 return NULL; /* invalid line number */
21952
21953 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021954 pos.col = list_find_nr(l, 1L, &error);
21955 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021956 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021957 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021958
21959 /* We accept "$" for the column number: last column. */
21960 li = list_find(l, 1L);
21961 if (li != NULL && li->li_tv.v_type == VAR_STRING
21962 && li->li_tv.vval.v_string != NULL
21963 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21964 pos.col = len + 1;
21965
Bram Moolenaara5525202006-03-02 22:52:09 +000021966 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021967 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021968 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021969 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021970
Bram Moolenaara5525202006-03-02 22:52:09 +000021971#ifdef FEAT_VIRTUALEDIT
21972 /* Get the virtual offset. Defaults to zero. */
21973 pos.coladd = list_find_nr(l, 2L, &error);
21974 if (error)
21975 pos.coladd = 0;
21976#endif
21977
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021978 return &pos;
21979 }
21980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021981 name = get_tv_string_chk(varp);
21982 if (name == NULL)
21983 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021984 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021986 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21987 {
21988 if (VIsual_active)
21989 return &VIsual;
21990 return &curwin->w_cursor;
21991 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021992 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021994 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21996 return NULL;
21997 return pp;
21998 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021999
22000#ifdef FEAT_VIRTUALEDIT
22001 pos.coladd = 0;
22002#endif
22003
Bram Moolenaar477933c2007-07-17 14:32:23 +000022004 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000022005 {
22006 pos.col = 0;
22007 if (name[1] == '0') /* "w0": first visible line */
22008 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000022009 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000022010 pos.lnum = curwin->w_topline;
22011 return &pos;
22012 }
22013 else if (name[1] == '$') /* "w$": last visible line */
22014 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000022015 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000022016 pos.lnum = curwin->w_botline - 1;
22017 return &pos;
22018 }
22019 }
22020 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000022022 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 {
22024 pos.lnum = curbuf->b_ml.ml_line_count;
22025 pos.col = 0;
22026 }
22027 else
22028 {
22029 pos.lnum = curwin->w_cursor.lnum;
22030 pos.col = (colnr_T)STRLEN(ml_get_curline());
22031 }
22032 return &pos;
22033 }
22034 return NULL;
22035}
22036
22037/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022038 * Convert list in "arg" into a position and optional file number.
22039 * When "fnump" is NULL there is no file number, only 3 items.
22040 * Note that the column is passed on as-is, the caller may want to decrement
22041 * it to use 1 for the first column.
22042 * Return FAIL when conversion is not possible, doesn't check the position for
22043 * validity.
22044 */
22045 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022046list2fpos(
22047 typval_T *arg,
22048 pos_T *posp,
22049 int *fnump,
22050 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022051{
22052 list_T *l = arg->vval.v_list;
22053 long i = 0;
22054 long n;
22055
Bram Moolenaar493c1782014-05-28 14:34:46 +020022056 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
22057 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000022058 if (arg->v_type != VAR_LIST
22059 || l == NULL
22060 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020022061 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022062 return FAIL;
22063
22064 if (fnump != NULL)
22065 {
22066 n = list_find_nr(l, i++, NULL); /* fnum */
22067 if (n < 0)
22068 return FAIL;
22069 if (n == 0)
22070 n = curbuf->b_fnum; /* current buffer */
22071 *fnump = n;
22072 }
22073
22074 n = list_find_nr(l, i++, NULL); /* lnum */
22075 if (n < 0)
22076 return FAIL;
22077 posp->lnum = n;
22078
22079 n = list_find_nr(l, i++, NULL); /* col */
22080 if (n < 0)
22081 return FAIL;
22082 posp->col = n;
22083
22084#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020022085 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022086 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000022087 posp->coladd = 0;
22088 else
22089 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022090#endif
22091
Bram Moolenaar493c1782014-05-28 14:34:46 +020022092 if (curswantp != NULL)
22093 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
22094
Bram Moolenaar0e34f622006-03-03 23:00:03 +000022095 return OK;
22096}
22097
22098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * Get the length of an environment variable name.
22100 * Advance "arg" to the first character after the name.
22101 * Return 0 for error.
22102 */
22103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105{
22106 char_u *p;
22107 int len;
22108
22109 for (p = *arg; vim_isIDc(*p); ++p)
22110 ;
22111 if (p == *arg) /* no name found */
22112 return 0;
22113
22114 len = (int)(p - *arg);
22115 *arg = p;
22116 return len;
22117}
22118
22119/*
22120 * Get the length of the name of a function or internal variable.
22121 * "arg" is advanced to the first non-white character after the name.
22122 * Return 0 if something is wrong.
22123 */
22124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022125get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126{
22127 char_u *p;
22128 int len;
22129
22130 /* Find the end of the name. */
22131 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022132 {
22133 if (*p == ':')
22134 {
22135 /* "s:" is start of "s:var", but "n:" is not and can be used in
22136 * slice "[n:]". Also "xx:" is not a namespace. */
22137 len = (int)(p - *arg);
22138 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
22139 || len > 1)
22140 break;
22141 }
22142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 if (p == *arg) /* no name found */
22144 return 0;
22145
22146 len = (int)(p - *arg);
22147 *arg = skipwhite(p);
22148
22149 return len;
22150}
22151
22152/*
Bram Moolenaara7043832005-01-21 11:56:39 +000022153 * Get the length of the name of a variable or function.
22154 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022156 * Return -1 if curly braces expansion failed.
22157 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 * If the name contains 'magic' {}'s, expand them and return the
22159 * expanded name in an allocated string via 'alias' - caller must free.
22160 */
22161 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022162get_name_len(
22163 char_u **arg,
22164 char_u **alias,
22165 int evaluate,
22166 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167{
22168 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 char_u *p;
22170 char_u *expr_start;
22171 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172
22173 *alias = NULL; /* default to no alias */
22174
22175 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22176 && (*arg)[2] == (int)KE_SNR)
22177 {
22178 /* hard coded <SNR>, already translated */
22179 *arg += 3;
22180 return get_id_len(arg) + 3;
22181 }
22182 len = eval_fname_script(*arg);
22183 if (len > 0)
22184 {
22185 /* literal "<SID>", "s:" or "<SNR>" */
22186 *arg += len;
22187 }
22188
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022190 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022192 p = find_name_end(*arg, &expr_start, &expr_end,
22193 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 if (expr_start != NULL)
22195 {
22196 char_u *temp_string;
22197
22198 if (!evaluate)
22199 {
22200 len += (int)(p - *arg);
22201 *arg = skipwhite(p);
22202 return len;
22203 }
22204
22205 /*
22206 * Include any <SID> etc in the expanded string:
22207 * Thus the -len here.
22208 */
22209 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22210 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022211 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 *alias = temp_string;
22213 *arg = skipwhite(p);
22214 return (int)STRLEN(temp_string);
22215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216
22217 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022218 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 EMSG2(_(e_invexpr2), *arg);
22220
22221 return len;
22222}
22223
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022224/*
22225 * Find the end of a variable or function name, taking care of magic braces.
22226 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22227 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022228 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022229 * Return a pointer to just after the name. Equal to "arg" if there is no
22230 * valid name.
22231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022233find_name_end(
22234 char_u *arg,
22235 char_u **expr_start,
22236 char_u **expr_end,
22237 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 int mb_nest = 0;
22240 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022242 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022244 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022246 *expr_start = NULL;
22247 *expr_end = NULL;
22248 }
22249
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022250 /* Quick check for valid starting character. */
22251 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22252 return arg;
22253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022254 for (p = arg; *p != NUL
22255 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022256 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022257 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022258 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022259 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022260 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022261 if (*p == '\'')
22262 {
22263 /* skip over 'string' to avoid counting [ and ] inside it. */
22264 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22265 ;
22266 if (*p == NUL)
22267 break;
22268 }
22269 else if (*p == '"')
22270 {
22271 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22272 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22273 if (*p == '\\' && p[1] != NUL)
22274 ++p;
22275 if (*p == NUL)
22276 break;
22277 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022278 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22279 {
22280 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022281 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022282 len = (int)(p - arg);
22283 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022284 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022285 break;
22286 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022288 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022290 if (*p == '[')
22291 ++br_nest;
22292 else if (*p == ']')
22293 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022296 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022298 if (*p == '{')
22299 {
22300 mb_nest++;
22301 if (expr_start != NULL && *expr_start == NULL)
22302 *expr_start = p;
22303 }
22304 else if (*p == '}')
22305 {
22306 mb_nest--;
22307 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22308 *expr_end = p;
22309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311 }
22312
22313 return p;
22314}
22315
22316/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022317 * Expands out the 'magic' {}'s in a variable/function name.
22318 * Note that this can call itself recursively, to deal with
22319 * constructs like foo{bar}{baz}{bam}
22320 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22321 * "in_start" ^
22322 * "expr_start" ^
22323 * "expr_end" ^
22324 * "in_end" ^
22325 *
22326 * Returns a new allocated string, which the caller must free.
22327 * Returns NULL for failure.
22328 */
22329 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022330make_expanded_name(
22331 char_u *in_start,
22332 char_u *expr_start,
22333 char_u *expr_end,
22334 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022335{
22336 char_u c1;
22337 char_u *retval = NULL;
22338 char_u *temp_result;
22339 char_u *nextcmd = NULL;
22340
22341 if (expr_end == NULL || in_end == NULL)
22342 return NULL;
22343 *expr_start = NUL;
22344 *expr_end = NUL;
22345 c1 = *in_end;
22346 *in_end = NUL;
22347
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022348 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022349 if (temp_result != NULL && nextcmd == NULL)
22350 {
22351 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22352 + (in_end - expr_end) + 1));
22353 if (retval != NULL)
22354 {
22355 STRCPY(retval, in_start);
22356 STRCAT(retval, temp_result);
22357 STRCAT(retval, expr_end + 1);
22358 }
22359 }
22360 vim_free(temp_result);
22361
22362 *in_end = c1; /* put char back for error messages */
22363 *expr_start = '{';
22364 *expr_end = '}';
22365
22366 if (retval != NULL)
22367 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022368 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022369 if (expr_start != NULL)
22370 {
22371 /* Further expansion! */
22372 temp_result = make_expanded_name(retval, expr_start,
22373 expr_end, temp_result);
22374 vim_free(retval);
22375 retval = temp_result;
22376 }
22377 }
22378
22379 return retval;
22380}
22381
22382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022384 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 */
22386 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022387eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022389 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22390}
22391
22392/*
22393 * Return TRUE if character "c" can be used as the first character in a
22394 * variable or function name (excluding '{' and '}').
22395 */
22396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022397eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022398{
22399 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400}
22401
22402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 * Set number v: variable to "val".
22404 */
22405 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022406set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022408 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409}
22410
22411/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022412 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022414 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022415get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022417 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418}
22419
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022420/*
22421 * Get string v: variable value. Uses a static buffer, can only be used once.
22422 */
22423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022424get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022425{
22426 return get_tv_string(&vimvars[idx].vv_tv);
22427}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022428
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022430 * Get List v: variable value. Caller must take care of reference count when
22431 * needed.
22432 */
22433 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022434get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022435{
22436 return vimvars[idx].vv_list;
22437}
22438
22439/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022440 * Set v:char to character "c".
22441 */
22442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022443set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022444{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022445 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022446
22447#ifdef FEAT_MBYTE
22448 if (has_mbyte)
22449 buf[(*mb_char2bytes)(c, buf)] = NUL;
22450 else
22451#endif
22452 {
22453 buf[0] = c;
22454 buf[1] = NUL;
22455 }
22456 set_vim_var_string(VV_CHAR, buf, -1);
22457}
22458
22459/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022460 * Set v:count to "count" and v:count1 to "count1".
22461 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 */
22463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022464set_vcount(
22465 long count,
22466 long count1,
22467 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022469 if (set_prevcount)
22470 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022471 vimvars[VV_COUNT].vv_nr = count;
22472 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473}
22474
22475/*
22476 * Set string v: variable to a copy of "val".
22477 */
22478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022479set_vim_var_string(
22480 int idx,
22481 char_u *val,
22482 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483{
Bram Moolenaara542c682016-01-31 16:28:04 +010022484 clear_tv(&vimvars[idx].vv_di.di_tv);
22485 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022487 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022489 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022491 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492}
22493
22494/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022495 * Set List v: variable to "val".
22496 */
22497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022498set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022499{
Bram Moolenaara542c682016-01-31 16:28:04 +010022500 clear_tv(&vimvars[idx].vv_di.di_tv);
22501 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022502 vimvars[idx].vv_list = val;
22503 if (val != NULL)
22504 ++val->lv_refcount;
22505}
22506
22507/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022508 * Set Dictionary v: variable to "val".
22509 */
22510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022511set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022512{
22513 int todo;
22514 hashitem_T *hi;
22515
Bram Moolenaara542c682016-01-31 16:28:04 +010022516 clear_tv(&vimvars[idx].vv_di.di_tv);
22517 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022518 vimvars[idx].vv_dict = val;
22519 if (val != NULL)
22520 {
22521 ++val->dv_refcount;
22522
22523 /* Set readonly */
22524 todo = (int)val->dv_hashtab.ht_used;
22525 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22526 {
22527 if (HASHITEM_EMPTY(hi))
22528 continue;
22529 --todo;
22530 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22531 }
22532 }
22533}
22534
22535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 * Set v:register if needed.
22537 */
22538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022539set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540{
22541 char_u regname;
22542
22543 if (c == 0 || c == ' ')
22544 regname = '"';
22545 else
22546 regname = c;
22547 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022548 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 set_vim_var_string(VV_REG, &regname, 1);
22550}
22551
22552/*
22553 * Get or set v:exception. If "oldval" == NULL, return the current value.
22554 * Otherwise, restore the value to "oldval" and return NULL.
22555 * Must always be called in pairs to save and restore v:exception! Does not
22556 * take care of memory allocations.
22557 */
22558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022559v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560{
22561 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022562 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563
Bram Moolenaare9a41262005-01-15 22:18:47 +000022564 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 return NULL;
22566}
22567
22568/*
22569 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22570 * Otherwise, restore the value to "oldval" and return NULL.
22571 * Must always be called in pairs to save and restore v:throwpoint! Does not
22572 * take care of memory allocations.
22573 */
22574 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022575v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576{
22577 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022578 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579
Bram Moolenaare9a41262005-01-15 22:18:47 +000022580 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 return NULL;
22582}
22583
22584#if defined(FEAT_AUTOCMD) || defined(PROTO)
22585/*
22586 * Set v:cmdarg.
22587 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22588 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22589 * Must always be called in pairs!
22590 */
22591 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022592set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593{
22594 char_u *oldval;
22595 char_u *newval;
22596 unsigned len;
22597
Bram Moolenaare9a41262005-01-15 22:18:47 +000022598 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022599 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022601 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022602 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022603 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 }
22605
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022606 if (eap->force_bin == FORCE_BIN)
22607 len = 6;
22608 else if (eap->force_bin == FORCE_NOBIN)
22609 len = 8;
22610 else
22611 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022612
22613 if (eap->read_edit)
22614 len += 7;
22615
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022616 if (eap->force_ff != 0)
22617 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22618# ifdef FEAT_MBYTE
22619 if (eap->force_enc != 0)
22620 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022621 if (eap->bad_char != 0)
22622 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022623# endif
22624
22625 newval = alloc(len + 1);
22626 if (newval == NULL)
22627 return NULL;
22628
22629 if (eap->force_bin == FORCE_BIN)
22630 sprintf((char *)newval, " ++bin");
22631 else if (eap->force_bin == FORCE_NOBIN)
22632 sprintf((char *)newval, " ++nobin");
22633 else
22634 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022635
22636 if (eap->read_edit)
22637 STRCAT(newval, " ++edit");
22638
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022639 if (eap->force_ff != 0)
22640 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22641 eap->cmd + eap->force_ff);
22642# ifdef FEAT_MBYTE
22643 if (eap->force_enc != 0)
22644 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22645 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022646 if (eap->bad_char == BAD_KEEP)
22647 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22648 else if (eap->bad_char == BAD_DROP)
22649 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22650 else if (eap->bad_char != 0)
22651 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022652# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022653 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022654 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655}
22656#endif
22657
22658/*
22659 * Get the value of internal variable "name".
22660 * Return OK or FAIL.
22661 */
22662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022663get_var_tv(
22664 char_u *name,
22665 int len, /* length of "name" */
22666 typval_T *rettv, /* NULL when only checking existence */
22667 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22668 int verbose, /* may give error message */
22669 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670{
22671 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022672 typval_T *tv = NULL;
22673 typval_T atv;
22674 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676
22677 /* truncate the name, so that we can use strcmp() */
22678 cc = name[len];
22679 name[len] = NUL;
22680
22681 /*
22682 * Check for "b:changedtick".
22683 */
22684 if (STRCMP(name, "b:changedtick") == 0)
22685 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022686 atv.v_type = VAR_NUMBER;
22687 atv.vval.v_number = curbuf->b_changedtick;
22688 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 }
22690
22691 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692 * Check for user-defined variables.
22693 */
22694 else
22695 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022696 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022698 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022699 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022700 if (dip != NULL)
22701 *dip = v;
22702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 }
22704
Bram Moolenaare9a41262005-01-15 22:18:47 +000022705 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022707 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022708 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 ret = FAIL;
22710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022711 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022712 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022713
22714 name[len] = cc;
22715
22716 return ret;
22717}
22718
22719/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022720 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22721 * Also handle function call with Funcref variable: func(expr)
22722 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22723 */
22724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022725handle_subscript(
22726 char_u **arg,
22727 typval_T *rettv,
22728 int evaluate, /* do more than finding the end */
22729 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022730{
22731 int ret = OK;
22732 dict_T *selfdict = NULL;
22733 char_u *s;
22734 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022735 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022736
22737 while (ret == OK
22738 && (**arg == '['
22739 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022740 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22741 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022742 && !vim_iswhite(*(*arg - 1)))
22743 {
22744 if (**arg == '(')
22745 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022746 partial_T *pt = NULL;
22747
Bram Moolenaard9fba312005-06-26 22:34:35 +000022748 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022749 if (evaluate)
22750 {
22751 functv = *rettv;
22752 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022753
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022754 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022755 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022756 {
22757 pt = functv.vval.v_partial;
22758 s = pt->pt_name;
22759 }
22760 else
22761 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022762 }
22763 else
22764 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022765 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022766 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022767 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022768
22769 /* Clear the funcref afterwards, so that deleting it while
22770 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022771 if (evaluate)
22772 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022773
22774 /* Stop the expression evaluation when immediately aborting on
22775 * error, or when an interrupt occurred or an exception was thrown
22776 * but not caught. */
22777 if (aborting())
22778 {
22779 if (ret == OK)
22780 clear_tv(rettv);
22781 ret = FAIL;
22782 }
22783 dict_unref(selfdict);
22784 selfdict = NULL;
22785 }
22786 else /* **arg == '[' || **arg == '.' */
22787 {
22788 dict_unref(selfdict);
22789 if (rettv->v_type == VAR_DICT)
22790 {
22791 selfdict = rettv->vval.v_dict;
22792 if (selfdict != NULL)
22793 ++selfdict->dv_refcount;
22794 }
22795 else
22796 selfdict = NULL;
22797 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22798 {
22799 clear_tv(rettv);
22800 ret = FAIL;
22801 }
22802 }
22803 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022804
Bram Moolenaar1d429612016-05-24 15:44:17 +020022805 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22806 * Don't do this when "Func" is already a partial that was bound
22807 * explicitly (pt_auto is FALSE). */
22808 if (selfdict != NULL
22809 && (rettv->v_type == VAR_FUNC
22810 || (rettv->v_type == VAR_PARTIAL
22811 && (rettv->vval.v_partial->pt_auto
22812 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022813 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022814 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22815 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022816 char_u *tofree = NULL;
22817 ufunc_T *fp;
22818 char_u fname_buf[FLEN_FIXED + 1];
22819 int error;
22820
22821 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022822 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022823 fp = find_func(fname);
22824 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022825
Bram Moolenaar65639032016-03-16 21:40:30 +010022826 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022827 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022828 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22829
22830 if (pt != NULL)
22831 {
22832 pt->pt_refcount = 1;
22833 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022834 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022835 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022836 if (rettv->v_type == VAR_FUNC)
22837 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022838 /* Just a function: Take over the function name and use
22839 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022840 pt->pt_name = rettv->vval.v_string;
22841 }
22842 else
22843 {
22844 partial_T *ret_pt = rettv->vval.v_partial;
22845 int i;
22846
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022847 /* Partial: copy the function name, use selfdict and copy
22848 * args. Can't take over name or args, the partial might
22849 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022850 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022851 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022852 if (ret_pt->pt_argc > 0)
22853 {
22854 pt->pt_argv = (typval_T *)alloc(
22855 sizeof(typval_T) * ret_pt->pt_argc);
22856 if (pt->pt_argv == NULL)
22857 /* out of memory: drop the arguments */
22858 pt->pt_argc = 0;
22859 else
22860 {
22861 pt->pt_argc = ret_pt->pt_argc;
22862 for (i = 0; i < pt->pt_argc; i++)
22863 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22864 }
22865 }
22866 partial_unref(ret_pt);
22867 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022868 rettv->v_type = VAR_PARTIAL;
22869 rettv->vval.v_partial = pt;
22870 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022871 }
22872 }
22873
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022874 dict_unref(selfdict);
22875 return ret;
22876}
22877
22878/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022879 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022880 * value).
22881 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022882 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022883alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884{
Bram Moolenaar33570922005-01-25 22:26:29 +000022885 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022886}
22887
22888/*
22889 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 * The string "s" must have been allocated, it is consumed.
22891 * Return NULL for out of memory, the variable otherwise.
22892 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022893 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022894alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895{
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022898 rettv = alloc_tv();
22899 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022901 rettv->v_type = VAR_STRING;
22902 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 }
22904 else
22905 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022906 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907}
22908
22909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022910 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022913free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914{
22915 if (varp != NULL)
22916 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022917 switch (varp->v_type)
22918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022919 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022920 func_unref(varp->vval.v_string);
22921 /*FALLTHROUGH*/
22922 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022923 vim_free(varp->vval.v_string);
22924 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022925 case VAR_PARTIAL:
22926 partial_unref(varp->vval.v_partial);
22927 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022928 case VAR_LIST:
22929 list_unref(varp->vval.v_list);
22930 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022931 case VAR_DICT:
22932 dict_unref(varp->vval.v_dict);
22933 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022934 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022935#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022936 job_unref(varp->vval.v_job);
22937 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022938#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022939 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022940#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022941 channel_unref(varp->vval.v_channel);
22942 break;
22943#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022944 case VAR_NUMBER:
22945 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022946 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022947 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022948 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 vim_free(varp);
22951 }
22952}
22953
22954/*
22955 * Free the memory for a variable value and set the value to NULL or 0.
22956 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022958clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959{
22960 if (varp != NULL)
22961 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022962 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022964 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022965 func_unref(varp->vval.v_string);
22966 /*FALLTHROUGH*/
22967 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022968 vim_free(varp->vval.v_string);
22969 varp->vval.v_string = NULL;
22970 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022971 case VAR_PARTIAL:
22972 partial_unref(varp->vval.v_partial);
22973 varp->vval.v_partial = NULL;
22974 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022975 case VAR_LIST:
22976 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022977 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022978 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022979 case VAR_DICT:
22980 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022981 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022982 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022983 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022984 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022985 varp->vval.v_number = 0;
22986 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022987 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022988#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022989 varp->vval.v_float = 0.0;
22990 break;
22991#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022992 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022993#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022994 job_unref(varp->vval.v_job);
22995 varp->vval.v_job = NULL;
22996#endif
22997 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022998 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022999#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023000 channel_unref(varp->vval.v_channel);
23001 varp->vval.v_channel = NULL;
23002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023003 case VAR_UNKNOWN:
23004 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023006 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 }
23008}
23009
23010/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023011 * Set the value of a variable to NULL without freeing items.
23012 */
23013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023014init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023015{
23016 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023017 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023018}
23019
23020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 * Get the number value of a variable.
23022 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023023 * For incompatible types, return 0.
23024 * get_tv_number_chk() is similar to get_tv_number(), but informs the
23025 * caller of incompatible types: it sets *denote to TRUE if "denote"
23026 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023028 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023029get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023031 int error = FALSE;
23032
23033 return get_tv_number_chk(varp, &error); /* return 0L on error */
23034}
23035
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023036 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023037get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023038{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023039 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023043 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023044 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023045 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023046#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000023047 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023048 break;
23049#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023050 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023051 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023052 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023053 break;
23054 case VAR_STRING:
23055 if (varp->vval.v_string != NULL)
23056 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010023057 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023058 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023059 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023060 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023061 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023062 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000023063 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023064 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010023065 case VAR_SPECIAL:
23066 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
23067 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023068 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023069#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023070 EMSG(_("E910: Using a Job as a Number"));
23071 break;
23072#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023073 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023075 EMSG(_("E913: Using a Channel as a Number"));
23076 break;
23077#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023078 case VAR_UNKNOWN:
23079 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023082 if (denote == NULL) /* useful for values that must be unsigned */
23083 n = -1;
23084 else
23085 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023086 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087}
23088
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023089#ifdef FEAT_FLOAT
23090 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023091get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023092{
23093 switch (varp->v_type)
23094 {
23095 case VAR_NUMBER:
23096 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023097 case VAR_FLOAT:
23098 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023099 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023100 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023101 EMSG(_("E891: Using a Funcref as a Float"));
23102 break;
23103 case VAR_STRING:
23104 EMSG(_("E892: Using a String as a Float"));
23105 break;
23106 case VAR_LIST:
23107 EMSG(_("E893: Using a List as a Float"));
23108 break;
23109 case VAR_DICT:
23110 EMSG(_("E894: Using a Dictionary as a Float"));
23111 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023112 case VAR_SPECIAL:
23113 EMSG(_("E907: Using a special value as a Float"));
23114 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023115 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023116# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023117 EMSG(_("E911: Using a Job as a Float"));
23118 break;
23119# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023120 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023121# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023122 EMSG(_("E914: Using a Channel as a Float"));
23123 break;
23124# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010023125 case VAR_UNKNOWN:
23126 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010023127 break;
23128 }
23129 return 0;
23130}
23131#endif
23132
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023134 * Get the lnum from the first argument.
23135 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023136 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 */
23138 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023139get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140{
Bram Moolenaar33570922005-01-25 22:26:29 +000023141 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 linenr_T lnum;
23143
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023144 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 if (lnum == 0) /* no valid number, try using line() */
23146 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023147 rettv.v_type = VAR_NUMBER;
23148 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023149 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 }
23152 return lnum;
23153}
23154
23155/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000023156 * Get the lnum from the first argument.
23157 * Also accepts "$", then "buf" is used.
23158 * Returns 0 on error.
23159 */
23160 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010023161get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023162{
23163 if (argvars[0].v_type == VAR_STRING
23164 && argvars[0].vval.v_string != NULL
23165 && argvars[0].vval.v_string[0] == '$'
23166 && buf != NULL)
23167 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023168 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023169}
23170
23171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 * Get the string value of a variable.
23173 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023174 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23175 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 * If the String variable has never been set, return an empty string.
23177 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023178 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23179 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023181 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023182get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183{
23184 static char_u mybuf[NUMBUFLEN];
23185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023186 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187}
23188
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023189 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023190get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023192 char_u *res = get_tv_string_buf_chk(varp, buf);
23193
23194 return res != NULL ? res : (char_u *)"";
23195}
23196
Bram Moolenaar7d647822014-04-05 21:28:56 +020023197/*
23198 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23199 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023200 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023201get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023202{
23203 static char_u mybuf[NUMBUFLEN];
23204
23205 return get_tv_string_buf_chk(varp, mybuf);
23206}
23207
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023208 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023209get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023210{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023211 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023213 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023214 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23215 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023216 return buf;
23217 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023218 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023219 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023220 break;
23221 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023222 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023223 break;
23224 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023225 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023226 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023227 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023228#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023229 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023230 break;
23231#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023232 case VAR_STRING:
23233 if (varp->vval.v_string != NULL)
23234 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023235 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023236 case VAR_SPECIAL:
23237 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23238 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023239 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023240#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023241 {
23242 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023243 char *status;
23244
23245 if (job == NULL)
23246 return (char_u *)"no process";
23247 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023248 : job->jv_status == JOB_ENDED ? "dead"
23249 : "run";
23250# ifdef UNIX
23251 vim_snprintf((char *)buf, NUMBUFLEN,
23252 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023253# elif defined(WIN32)
23254 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023255 "process %ld %s",
23256 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023257 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023258# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023259 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023260 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23261# endif
23262 return buf;
23263 }
23264#endif
23265 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023266 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023267#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023268 {
23269 channel_T *channel = varp->vval.v_channel;
23270 char *status = channel_status(channel);
23271
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023272 if (channel == NULL)
23273 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23274 else
23275 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023276 "channel %d %s", channel->ch_id, status);
23277 return buf;
23278 }
23279#endif
23280 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023281 case VAR_UNKNOWN:
23282 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023285 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286}
23287
23288/*
23289 * Find variable "name" in the list of variables.
23290 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023291 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023292 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023295 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023296find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023299 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300
Bram Moolenaara7043832005-01-21 11:56:39 +000023301 ht = find_var_ht(name, &varname);
23302 if (htp != NULL)
23303 *htp = ht;
23304 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023306 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307}
23308
23309/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023310 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023311 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023313 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023314find_var_in_ht(
23315 hashtab_T *ht,
23316 int htname,
23317 char_u *varname,
23318 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023319{
Bram Moolenaar33570922005-01-25 22:26:29 +000023320 hashitem_T *hi;
23321
23322 if (*varname == NUL)
23323 {
23324 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023325 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023326 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023327 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023328 case 'g': return &globvars_var;
23329 case 'v': return &vimvars_var;
23330 case 'b': return &curbuf->b_bufvar;
23331 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023332#ifdef FEAT_WINDOWS
23333 case 't': return &curtab->tp_winvar;
23334#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023335 case 'l': return current_funccal == NULL
23336 ? NULL : &current_funccal->l_vars_var;
23337 case 'a': return current_funccal == NULL
23338 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023339 }
23340 return NULL;
23341 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023342
23343 hi = hash_find(ht, varname);
23344 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023345 {
23346 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023347 * worked find the variable again. Don't auto-load a script if it was
23348 * loaded already, otherwise it would be loaded every time when
23349 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023350 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023351 {
23352 /* Note: script_autoload() may make "hi" invalid. It must either
23353 * be obtained again or not used. */
23354 if (!script_autoload(varname, FALSE) || aborting())
23355 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023356 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023357 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023358 if (HASHITEM_EMPTY(hi))
23359 return NULL;
23360 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023361 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023362}
23363
23364/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023365 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023366 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023367 * Set "varname" to the start of name without ':'.
23368 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023369 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023370find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023372 hashitem_T *hi;
23373
Bram Moolenaar73627d02015-08-11 15:46:09 +020023374 if (name[0] == NUL)
23375 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 if (name[1] != ':')
23377 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023378 /* The name must not start with a colon or #. */
23379 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 return NULL;
23381 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023382
23383 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023384 hi = hash_find(&compat_hashtab, name);
23385 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023386 return &compat_hashtab;
23387
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023389 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023390 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 }
23392 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023393 if (*name == 'g') /* global variable */
23394 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023395 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23396 */
23397 if (vim_strchr(name + 2, ':') != NULL
23398 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023399 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023401 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023403 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023404#ifdef FEAT_WINDOWS
23405 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023406 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023407#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023408 if (*name == 'v') /* v: variable */
23409 return &vimvarht;
23410 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023411 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023412 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023413 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 if (*name == 's' /* script variable */
23415 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23416 return &SCRIPT_VARS(current_SID);
23417 return NULL;
23418}
23419
23420/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023421 * Get function call environment based on bactrace debug level
23422 */
23423 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023424get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023425{
23426 int i;
23427 funccall_T *funccal;
23428 funccall_T *temp_funccal;
23429
23430 funccal = current_funccal;
23431 if (debug_backtrace_level > 0)
23432 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023433 for (i = 0; i < debug_backtrace_level; i++)
23434 {
23435 temp_funccal = funccal->caller;
23436 if (temp_funccal)
23437 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023438 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023439 /* backtrace level overflow. reset to max */
23440 debug_backtrace_level = i;
23441 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023442 }
23443 return funccal;
23444}
23445
23446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023448 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 * Returns NULL when it doesn't exist.
23450 */
23451 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023452get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453{
Bram Moolenaar33570922005-01-25 22:26:29 +000023454 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023456 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 if (v == NULL)
23458 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023459 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460}
23461
23462/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023463 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 * sourcing this script and when executing functions defined in the script.
23465 */
23466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023467new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468{
Bram Moolenaara7043832005-01-21 11:56:39 +000023469 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023470 hashtab_T *ht;
23471 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023472
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23474 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023475 /* Re-allocating ga_data means that an ht_array pointing to
23476 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023477 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023478 for (i = 1; i <= ga_scripts.ga_len; ++i)
23479 {
23480 ht = &SCRIPT_VARS(i);
23481 if (ht->ht_mask == HT_INIT_SIZE - 1)
23482 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023483 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023484 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023485 }
23486
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 while (ga_scripts.ga_len < id)
23488 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023489 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023490 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023491 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 }
23494 }
23495}
23496
23497/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023498 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23499 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 */
23501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023502init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503{
Bram Moolenaar33570922005-01-25 22:26:29 +000023504 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023505 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023506 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023507 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023508 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023509 dict_var->di_tv.vval.v_dict = dict;
23510 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023511 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023512 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23513 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514}
23515
23516/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023517 * Unreference a dictionary initialized by init_var_dict().
23518 */
23519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023520unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023521{
23522 /* Now the dict needs to be freed if no one else is using it, go back to
23523 * normal reference counting. */
23524 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23525 dict_unref(dict);
23526}
23527
23528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023530 * Frees all allocated variables and the value they contain.
23531 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 */
23533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023534vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023535{
23536 vars_clear_ext(ht, TRUE);
23537}
23538
23539/*
23540 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23541 */
23542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023543vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544{
Bram Moolenaara7043832005-01-21 11:56:39 +000023545 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023546 hashitem_T *hi;
23547 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548
Bram Moolenaar33570922005-01-25 22:26:29 +000023549 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023550 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023551 for (hi = ht->ht_array; todo > 0; ++hi)
23552 {
23553 if (!HASHITEM_EMPTY(hi))
23554 {
23555 --todo;
23556
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023558 * ht_array might change then. hash_clear() takes care of it
23559 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023560 v = HI2DI(hi);
23561 if (free_val)
23562 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023563 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023564 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023565 }
23566 }
23567 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023568 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569}
23570
Bram Moolenaara7043832005-01-21 11:56:39 +000023571/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023572 * Delete a variable from hashtab "ht" at item "hi".
23573 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023576delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577{
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023579
23580 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023581 clear_tv(&di->di_tv);
23582 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583}
23584
23585/*
23586 * List the value of one internal variable.
23587 */
23588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023589list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023591 char_u *tofree;
23592 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023593 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023594
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023595 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023596 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023597 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023598 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599}
23600
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023602list_one_var_a(
23603 char_u *prefix,
23604 char_u *name,
23605 int type,
23606 char_u *string,
23607 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608{
Bram Moolenaar31859182007-08-14 20:41:13 +000023609 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23610 msg_start();
23611 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 if (name != NULL) /* "a:" vars don't have a name stored */
23613 msg_puts(name);
23614 msg_putchar(' ');
23615 msg_advance(22);
23616 if (type == VAR_NUMBER)
23617 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023618 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023619 msg_putchar('*');
23620 else if (type == VAR_LIST)
23621 {
23622 msg_putchar('[');
23623 if (*string == '[')
23624 ++string;
23625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023626 else if (type == VAR_DICT)
23627 {
23628 msg_putchar('{');
23629 if (*string == '{')
23630 ++string;
23631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 else
23633 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023634
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023636
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023637 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023638 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023639 if (*first)
23640 {
23641 msg_clr_eos();
23642 *first = FALSE;
23643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644}
23645
23646/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023647 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 * If the variable already exists, the value is updated.
23649 * Otherwise the variable is created.
23650 */
23651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023652set_var(
23653 char_u *name,
23654 typval_T *tv,
23655 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656{
Bram Moolenaar33570922005-01-25 22:26:29 +000023657 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023659 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023661 ht = find_var_ht(name, &varname);
23662 if (ht == NULL || *varname == NUL)
23663 {
23664 EMSG2(_(e_illvar), name);
23665 return;
23666 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023667 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023668
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023669 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23670 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023671 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023672
Bram Moolenaar33570922005-01-25 22:26:29 +000023673 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023675 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023676 if (var_check_ro(v->di_flags, name, FALSE)
23677 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023678 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023679
23680 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023681 * Handle setting internal v: variables separately where needed to
23682 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023683 */
23684 if (ht == &vimvarht)
23685 {
23686 if (v->di_tv.v_type == VAR_STRING)
23687 {
23688 vim_free(v->di_tv.vval.v_string);
23689 if (copy || tv->v_type != VAR_STRING)
23690 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23691 else
23692 {
23693 /* Take over the string to avoid an extra alloc/free. */
23694 v->di_tv.vval.v_string = tv->vval.v_string;
23695 tv->vval.v_string = NULL;
23696 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023697 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023698 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023699 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023700 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023701 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023702 if (STRCMP(varname, "searchforward") == 0)
23703 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023704#ifdef FEAT_SEARCH_EXTRA
23705 else if (STRCMP(varname, "hlsearch") == 0)
23706 {
23707 no_hlsearch = !v->di_tv.vval.v_number;
23708 redraw_all_later(SOME_VALID);
23709 }
23710#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023711 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023712 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023713 else if (v->di_tv.v_type != tv->v_type)
23714 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023715 }
23716
23717 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 }
23719 else /* add a new variable */
23720 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023721 /* Can't add "v:" variable. */
23722 if (ht == &vimvarht)
23723 {
23724 EMSG2(_(e_illvar), name);
23725 return;
23726 }
23727
Bram Moolenaar92124a32005-06-17 22:03:40 +000023728 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023729 if (!valid_varname(varname))
23730 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023731
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023732 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23733 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023734 if (v == NULL)
23735 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023736 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023737 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023739 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 return;
23741 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023742 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023744
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023745 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023746 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023747 else
23748 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023749 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023750 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023751 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753}
23754
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023755/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023756 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023757 * Also give an error message.
23758 */
23759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023760var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023761{
23762 if (flags & DI_FLAGS_RO)
23763 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023764 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023765 return TRUE;
23766 }
23767 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23768 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023769 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023770 return TRUE;
23771 }
23772 return FALSE;
23773}
23774
23775/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023776 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23777 * Also give an error message.
23778 */
23779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023780var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023781{
23782 if (flags & DI_FLAGS_FIX)
23783 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023784 EMSG2(_("E795: Cannot delete variable %s"),
23785 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023786 return TRUE;
23787 }
23788 return FALSE;
23789}
23790
23791/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023792 * Check if a funcref is assigned to a valid variable name.
23793 * Return TRUE and give an error if not.
23794 */
23795 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023796var_check_func_name(
23797 char_u *name, /* points to start of variable name */
23798 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023799{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023800 /* Allow for w: b: s: and t:. */
23801 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023802 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23803 ? name[2] : name[0]))
23804 {
23805 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23806 name);
23807 return TRUE;
23808 }
23809 /* Don't allow hiding a function. When "v" is not NULL we might be
23810 * assigning another function to the same var, the type is checked
23811 * below. */
23812 if (new_var && function_exists(name))
23813 {
23814 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23815 name);
23816 return TRUE;
23817 }
23818 return FALSE;
23819}
23820
23821/*
23822 * Check if a variable name is valid.
23823 * Return FALSE and give an error if not.
23824 */
23825 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023826valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023827{
23828 char_u *p;
23829
23830 for (p = varname; *p != NUL; ++p)
23831 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23832 && *p != AUTOLOAD_CHAR)
23833 {
23834 EMSG2(_(e_illvar), varname);
23835 return FALSE;
23836 }
23837 return TRUE;
23838}
23839
23840/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023841 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023842 * Also give an error message, using "name" or _("name") when use_gettext is
23843 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023844 */
23845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023846tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023847{
23848 if (lock & VAR_LOCKED)
23849 {
23850 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023851 name == NULL ? (char_u *)_("Unknown")
23852 : use_gettext ? (char_u *)_(name)
23853 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023854 return TRUE;
23855 }
23856 if (lock & VAR_FIXED)
23857 {
23858 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023859 name == NULL ? (char_u *)_("Unknown")
23860 : use_gettext ? (char_u *)_(name)
23861 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023862 return TRUE;
23863 }
23864 return FALSE;
23865}
23866
23867/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023868 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023869 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023870 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023871 * It is OK for "from" and "to" to point to the same item. This is used to
23872 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023873 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023875copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023877 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023878 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023879 switch (from->v_type)
23880 {
23881 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023882 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023883 to->vval.v_number = from->vval.v_number;
23884 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023885 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023886#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023887 to->vval.v_float = from->vval.v_float;
23888 break;
23889#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023890 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023891#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023892 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023893 if (to->vval.v_job != NULL)
23894 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023895 break;
23896#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023897 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023898#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023899 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023900 if (to->vval.v_channel != NULL)
23901 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023902 break;
23903#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023904 case VAR_STRING:
23905 case VAR_FUNC:
23906 if (from->vval.v_string == NULL)
23907 to->vval.v_string = NULL;
23908 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023909 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023910 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023911 if (from->v_type == VAR_FUNC)
23912 func_ref(to->vval.v_string);
23913 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023914 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023915 case VAR_PARTIAL:
23916 if (from->vval.v_partial == NULL)
23917 to->vval.v_partial = NULL;
23918 else
23919 {
23920 to->vval.v_partial = from->vval.v_partial;
23921 ++to->vval.v_partial->pt_refcount;
23922 }
23923 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023924 case VAR_LIST:
23925 if (from->vval.v_list == NULL)
23926 to->vval.v_list = NULL;
23927 else
23928 {
23929 to->vval.v_list = from->vval.v_list;
23930 ++to->vval.v_list->lv_refcount;
23931 }
23932 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023933 case VAR_DICT:
23934 if (from->vval.v_dict == NULL)
23935 to->vval.v_dict = NULL;
23936 else
23937 {
23938 to->vval.v_dict = from->vval.v_dict;
23939 ++to->vval.v_dict->dv_refcount;
23940 }
23941 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023942 case VAR_UNKNOWN:
23943 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023944 break;
23945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946}
23947
23948/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023949 * Make a copy of an item.
23950 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023951 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23952 * reference to an already copied list/dict can be used.
23953 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023954 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023956item_copy(
23957 typval_T *from,
23958 typval_T *to,
23959 int deep,
23960 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023961{
23962 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023963 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023964
Bram Moolenaar33570922005-01-25 22:26:29 +000023965 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023966 {
23967 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023968 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023969 }
23970 ++recurse;
23971
23972 switch (from->v_type)
23973 {
23974 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023975 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023976 case VAR_STRING:
23977 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023978 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023979 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023980 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023981 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023982 copy_tv(from, to);
23983 break;
23984 case VAR_LIST:
23985 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023986 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023987 if (from->vval.v_list == NULL)
23988 to->vval.v_list = NULL;
23989 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23990 {
23991 /* use the copy made earlier */
23992 to->vval.v_list = from->vval.v_list->lv_copylist;
23993 ++to->vval.v_list->lv_refcount;
23994 }
23995 else
23996 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23997 if (to->vval.v_list == NULL)
23998 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023999 break;
24000 case VAR_DICT:
24001 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024002 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024003 if (from->vval.v_dict == NULL)
24004 to->vval.v_dict = NULL;
24005 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
24006 {
24007 /* use the copy made earlier */
24008 to->vval.v_dict = from->vval.v_dict->dv_copydict;
24009 ++to->vval.v_dict->dv_refcount;
24010 }
24011 else
24012 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
24013 if (to->vval.v_dict == NULL)
24014 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024015 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010024016 case VAR_UNKNOWN:
24017 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024018 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024019 }
24020 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024021 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024022}
24023
24024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025 * ":echo expr1 ..." print each argument separated with a space, add a
24026 * newline at the end.
24027 * ":echon expr1 ..." print each argument plain.
24028 */
24029 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024030ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031{
24032 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024033 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024034 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 char_u *p;
24036 int needclr = TRUE;
24037 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024038 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039
24040 if (eap->skip)
24041 ++emsg_skip;
24042 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
24043 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024044 /* If eval1() causes an error message the text from the command may
24045 * still need to be cleared. E.g., "echo 22,44". */
24046 need_clr_eos = needclr;
24047
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024049 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 {
24051 /*
24052 * Report the invalid expression unless the expression evaluation
24053 * has been cancelled due to an aborting error, an interrupt, or an
24054 * exception.
24055 */
24056 if (!aborting())
24057 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024058 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 break;
24060 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024061 need_clr_eos = FALSE;
24062
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 if (!eap->skip)
24064 {
24065 if (atstart)
24066 {
24067 atstart = FALSE;
24068 /* Call msg_start() after eval1(), evaluating the expression
24069 * may cause a message to appear. */
24070 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010024071 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020024072 /* Mark the saved text as finishing the line, so that what
24073 * follows is displayed on a new line when scrolling back
24074 * at the more prompt. */
24075 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010024077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 }
24079 else if (eap->cmdidx == CMD_echo)
24080 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010024081 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024082 if (p != NULL)
24083 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024085 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024087 if (*p != TAB && needclr)
24088 {
24089 /* remove any text still there from the command */
24090 msg_clr_eos();
24091 needclr = FALSE;
24092 }
24093 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 }
24095 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024096 {
24097#ifdef FEAT_MBYTE
24098 if (has_mbyte)
24099 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000024100 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024101
24102 (void)msg_outtrans_len_attr(p, i, echo_attr);
24103 p += i - 1;
24104 }
24105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024107 (void)msg_outtrans_len_attr(p, 1, echo_attr);
24108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024110 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024112 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 arg = skipwhite(arg);
24114 }
24115 eap->nextcmd = check_nextcmd(arg);
24116
24117 if (eap->skip)
24118 --emsg_skip;
24119 else
24120 {
24121 /* remove text that may still be there from the command */
24122 if (needclr)
24123 msg_clr_eos();
24124 if (eap->cmdidx == CMD_echo)
24125 msg_end();
24126 }
24127}
24128
24129/*
24130 * ":echohl {name}".
24131 */
24132 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024133ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134{
24135 int id;
24136
24137 id = syn_name2id(eap->arg);
24138 if (id == 0)
24139 echo_attr = 0;
24140 else
24141 echo_attr = syn_id2attr(id);
24142}
24143
24144/*
24145 * ":execute expr1 ..." execute the result of an expression.
24146 * ":echomsg expr1 ..." Print a message
24147 * ":echoerr expr1 ..." Print an error
24148 * Each gets spaces around each argument and a newline at the end for
24149 * echo commands
24150 */
24151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024152ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153{
24154 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024155 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 int ret = OK;
24157 char_u *p;
24158 garray_T ga;
24159 int len;
24160 int save_did_emsg;
24161
24162 ga_init2(&ga, 1, 80);
24163
24164 if (eap->skip)
24165 ++emsg_skip;
24166 while (*arg != NUL && *arg != '|' && *arg != '\n')
24167 {
24168 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024169 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 {
24171 /*
24172 * Report the invalid expression unless the expression evaluation
24173 * has been cancelled due to an aborting error, an interrupt, or an
24174 * exception.
24175 */
24176 if (!aborting())
24177 EMSG2(_(e_invexpr2), p);
24178 ret = FAIL;
24179 break;
24180 }
24181
24182 if (!eap->skip)
24183 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024184 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 len = (int)STRLEN(p);
24186 if (ga_grow(&ga, len + 2) == FAIL)
24187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024188 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 ret = FAIL;
24190 break;
24191 }
24192 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 ga.ga_len += len;
24196 }
24197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024198 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 arg = skipwhite(arg);
24200 }
24201
24202 if (ret != FAIL && ga.ga_data != NULL)
24203 {
24204 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024207 out_flush();
24208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 else if (eap->cmdidx == CMD_echoerr)
24210 {
24211 /* We don't want to abort following commands, restore did_emsg. */
24212 save_did_emsg = did_emsg;
24213 EMSG((char_u *)ga.ga_data);
24214 if (!force_abort)
24215 did_emsg = save_did_emsg;
24216 }
24217 else if (eap->cmdidx == CMD_execute)
24218 do_cmdline((char_u *)ga.ga_data,
24219 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24220 }
24221
24222 ga_clear(&ga);
24223
24224 if (eap->skip)
24225 --emsg_skip;
24226
24227 eap->nextcmd = check_nextcmd(arg);
24228}
24229
24230/*
24231 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24232 * "arg" points to the "&" or '+' when called, to "option" when returning.
24233 * Returns NULL when no option name found. Otherwise pointer to the char
24234 * after the option name.
24235 */
24236 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024237find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238{
24239 char_u *p = *arg;
24240
24241 ++p;
24242 if (*p == 'g' && p[1] == ':')
24243 {
24244 *opt_flags = OPT_GLOBAL;
24245 p += 2;
24246 }
24247 else if (*p == 'l' && p[1] == ':')
24248 {
24249 *opt_flags = OPT_LOCAL;
24250 p += 2;
24251 }
24252 else
24253 *opt_flags = 0;
24254
24255 if (!ASCII_ISALPHA(*p))
24256 return NULL;
24257 *arg = p;
24258
24259 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24260 p += 4; /* termcap option */
24261 else
24262 while (ASCII_ISALPHA(*p))
24263 ++p;
24264 return p;
24265}
24266
24267/*
24268 * ":function"
24269 */
24270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024271ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272{
24273 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024274 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275 int j;
24276 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024278 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 char_u *name = NULL;
24280 char_u *p;
24281 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024282 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 garray_T newargs;
24284 garray_T newlines;
24285 int varargs = FALSE;
24286 int mustend = FALSE;
24287 int flags = 0;
24288 ufunc_T *fp;
24289 int indent;
24290 int nesting;
24291 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024292 dictitem_T *v;
24293 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024294 static int func_nr = 0; /* number for nameless function */
24295 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 hashtab_T *ht;
24297 int todo;
24298 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024299 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300
24301 /*
24302 * ":function" without argument: list functions.
24303 */
24304 if (ends_excmd(*eap->arg))
24305 {
24306 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024307 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024308 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024309 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024310 {
24311 if (!HASHITEM_EMPTY(hi))
24312 {
24313 --todo;
24314 fp = HI2UF(hi);
24315 if (!isdigit(*fp->uf_name))
24316 list_func_head(fp, FALSE);
24317 }
24318 }
24319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320 eap->nextcmd = check_nextcmd(eap->arg);
24321 return;
24322 }
24323
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024324 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024325 * ":function /pat": list functions matching pattern.
24326 */
24327 if (*eap->arg == '/')
24328 {
24329 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24330 if (!eap->skip)
24331 {
24332 regmatch_T regmatch;
24333
24334 c = *p;
24335 *p = NUL;
24336 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24337 *p = c;
24338 if (regmatch.regprog != NULL)
24339 {
24340 regmatch.rm_ic = p_ic;
24341
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024342 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024343 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24344 {
24345 if (!HASHITEM_EMPTY(hi))
24346 {
24347 --todo;
24348 fp = HI2UF(hi);
24349 if (!isdigit(*fp->uf_name)
24350 && vim_regexec(&regmatch, fp->uf_name, 0))
24351 list_func_head(fp, FALSE);
24352 }
24353 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024354 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024355 }
24356 }
24357 if (*p == '/')
24358 ++p;
24359 eap->nextcmd = check_nextcmd(p);
24360 return;
24361 }
24362
24363 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024364 * Get the function name. There are these situations:
24365 * func normal function name
24366 * "name" == func, "fudi.fd_dict" == NULL
24367 * dict.func new dictionary entry
24368 * "name" == NULL, "fudi.fd_dict" set,
24369 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24370 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024371 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24373 * dict.func existing dict entry that's not a Funcref
24374 * "name" == NULL, "fudi.fd_dict" set,
24375 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024376 * s:func script-local function name
24377 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024380 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024381 paren = (vim_strchr(p, '(') != NULL);
24382 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 {
24384 /*
24385 * Return on an invalid expression in braces, unless the expression
24386 * evaluation has been cancelled due to an aborting error, an
24387 * interrupt, or an exception.
24388 */
24389 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024390 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024391 if (!eap->skip && fudi.fd_newkey != NULL)
24392 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024393 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396 else
24397 eap->skip = TRUE;
24398 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024399
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 /* An error in a function call during evaluation of an expression in magic
24401 * braces should not cause the function not to be defined. */
24402 saved_did_emsg = did_emsg;
24403 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404
24405 /*
24406 * ":function func" with only function name: list function.
24407 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024408 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024409 {
24410 if (!ends_excmd(*skipwhite(p)))
24411 {
24412 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024413 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 }
24415 eap->nextcmd = check_nextcmd(p);
24416 if (eap->nextcmd != NULL)
24417 *p = NUL;
24418 if (!eap->skip && !got_int)
24419 {
24420 fp = find_func(name);
24421 if (fp != NULL)
24422 {
24423 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024424 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024426 if (FUNCLINE(fp, j) == NULL)
24427 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 msg_putchar('\n');
24429 msg_outnum((long)(j + 1));
24430 if (j < 9)
24431 msg_putchar(' ');
24432 if (j < 99)
24433 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024434 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435 out_flush(); /* show a line at a time */
24436 ui_breakcheck();
24437 }
24438 if (!got_int)
24439 {
24440 msg_putchar('\n');
24441 msg_puts((char_u *)" endfunction");
24442 }
24443 }
24444 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024445 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024447 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 }
24449
24450 /*
24451 * ":function name(arg1, arg2)" Define function.
24452 */
24453 p = skipwhite(p);
24454 if (*p != '(')
24455 {
24456 if (!eap->skip)
24457 {
24458 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024459 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 }
24461 /* attempt to continue by skipping some text */
24462 if (vim_strchr(p, '(') != NULL)
24463 p = vim_strchr(p, '(');
24464 }
24465 p = skipwhite(p + 1);
24466
24467 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24468 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24469
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024470 if (!eap->skip)
24471 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024472 /* Check the name of the function. Unless it's a dictionary function
24473 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024474 if (name != NULL)
24475 arg = name;
24476 else
24477 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024478 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024479 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24480 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024481 {
24482 if (*arg == K_SPECIAL)
24483 j = 3;
24484 else
24485 j = 0;
24486 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24487 : eval_isnamec(arg[j])))
24488 ++j;
24489 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024490 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024491 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024492 /* Disallow using the g: dict. */
24493 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24494 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024495 }
24496
Bram Moolenaar071d4272004-06-13 20:20:40 +000024497 /*
24498 * Isolate the arguments: "arg1, arg2, ...)"
24499 */
24500 while (*p != ')')
24501 {
24502 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24503 {
24504 varargs = TRUE;
24505 p += 3;
24506 mustend = TRUE;
24507 }
24508 else
24509 {
24510 arg = p;
24511 while (ASCII_ISALNUM(*p) || *p == '_')
24512 ++p;
24513 if (arg == p || isdigit(*arg)
24514 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24515 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24516 {
24517 if (!eap->skip)
24518 EMSG2(_("E125: Illegal argument: %s"), arg);
24519 break;
24520 }
24521 if (ga_grow(&newargs, 1) == FAIL)
24522 goto erret;
24523 c = *p;
24524 *p = NUL;
24525 arg = vim_strsave(arg);
24526 if (arg == NULL)
24527 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024528
24529 /* Check for duplicate argument name. */
24530 for (i = 0; i < newargs.ga_len; ++i)
24531 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24532 {
24533 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024534 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024535 goto erret;
24536 }
24537
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24539 *p = c;
24540 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 if (*p == ',')
24542 ++p;
24543 else
24544 mustend = TRUE;
24545 }
24546 p = skipwhite(p);
24547 if (mustend && *p != ')')
24548 {
24549 if (!eap->skip)
24550 EMSG2(_(e_invarg2), eap->arg);
24551 break;
24552 }
24553 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024554 if (*p != ')')
24555 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024556 ++p; /* skip the ')' */
24557
Bram Moolenaare9a41262005-01-15 22:18:47 +000024558 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 for (;;)
24560 {
24561 p = skipwhite(p);
24562 if (STRNCMP(p, "range", 5) == 0)
24563 {
24564 flags |= FC_RANGE;
24565 p += 5;
24566 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024567 else if (STRNCMP(p, "dict", 4) == 0)
24568 {
24569 flags |= FC_DICT;
24570 p += 4;
24571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024572 else if (STRNCMP(p, "abort", 5) == 0)
24573 {
24574 flags |= FC_ABORT;
24575 p += 5;
24576 }
24577 else
24578 break;
24579 }
24580
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024581 /* When there is a line break use what follows for the function body.
24582 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24583 if (*p == '\n')
24584 line_arg = p + 1;
24585 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 EMSG(_(e_trailing));
24587
24588 /*
24589 * Read the body of the function, until ":endfunction" is found.
24590 */
24591 if (KeyTyped)
24592 {
24593 /* Check if the function already exists, don't let the user type the
24594 * whole function before telling him it doesn't work! For a script we
24595 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024596 if (!eap->skip && !eap->forceit)
24597 {
24598 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24599 EMSG(_(e_funcdict));
24600 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024601 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024604 if (!eap->skip && did_emsg)
24605 goto erret;
24606
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 msg_putchar('\n'); /* don't overwrite the function name */
24608 cmdline_row = msg_row;
24609 }
24610
24611 indent = 2;
24612 nesting = 0;
24613 for (;;)
24614 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024615 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024616 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024617 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024618 saved_wait_return = FALSE;
24619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024621 sourcing_lnum_off = sourcing_lnum;
24622
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024623 if (line_arg != NULL)
24624 {
24625 /* Use eap->arg, split up in parts by line breaks. */
24626 theline = line_arg;
24627 p = vim_strchr(theline, '\n');
24628 if (p == NULL)
24629 line_arg += STRLEN(line_arg);
24630 else
24631 {
24632 *p = NUL;
24633 line_arg = p + 1;
24634 }
24635 }
24636 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 theline = getcmdline(':', 0L, indent);
24638 else
24639 theline = eap->getline(':', eap->cookie, indent);
24640 if (KeyTyped)
24641 lines_left = Rows - 1;
24642 if (theline == NULL)
24643 {
24644 EMSG(_("E126: Missing :endfunction"));
24645 goto erret;
24646 }
24647
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024648 /* Detect line continuation: sourcing_lnum increased more than one. */
24649 if (sourcing_lnum > sourcing_lnum_off + 1)
24650 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24651 else
24652 sourcing_lnum_off = 0;
24653
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654 if (skip_until != NULL)
24655 {
24656 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24657 * don't check for ":endfunc". */
24658 if (STRCMP(theline, skip_until) == 0)
24659 {
24660 vim_free(skip_until);
24661 skip_until = NULL;
24662 }
24663 }
24664 else
24665 {
24666 /* skip ':' and blanks*/
24667 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24668 ;
24669
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024670 /* Check for "endfunction". */
24671 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024673 if (line_arg == NULL)
24674 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 break;
24676 }
24677
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024678 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 * at "end". */
24680 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24681 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024682 else if (STRNCMP(p, "if", 2) == 0
24683 || STRNCMP(p, "wh", 2) == 0
24684 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 || STRNCMP(p, "try", 3) == 0)
24686 indent += 2;
24687
24688 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024689 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024690 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024691 if (*p == '!')
24692 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024694 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024695 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024696 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024697 ++nesting;
24698 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699 }
24700 }
24701
24702 /* Check for ":append" or ":insert". */
24703 p = skip_range(p, NULL);
24704 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24705 || (p[0] == 'i'
24706 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24707 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24708 skip_until = vim_strsave((char_u *)".");
24709
24710 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24711 arg = skipwhite(skiptowhite(p));
24712 if (arg[0] == '<' && arg[1] =='<'
24713 && ((p[0] == 'p' && p[1] == 'y'
24714 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24715 || (p[0] == 'p' && p[1] == 'e'
24716 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24717 || (p[0] == 't' && p[1] == 'c'
24718 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024719 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24720 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24722 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024723 || (p[0] == 'm' && p[1] == 'z'
24724 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 ))
24726 {
24727 /* ":python <<" continues until a dot, like ":append" */
24728 p = skipwhite(arg + 2);
24729 if (*p == NUL)
24730 skip_until = vim_strsave((char_u *)".");
24731 else
24732 skip_until = vim_strsave(p);
24733 }
24734 }
24735
24736 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024737 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024738 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024739 if (line_arg == NULL)
24740 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024742 }
24743
24744 /* Copy the line to newly allocated memory. get_one_sourceline()
24745 * allocates 250 bytes per line, this saves 80% on average. The cost
24746 * is an extra alloc/free. */
24747 p = vim_strsave(theline);
24748 if (p != NULL)
24749 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024750 if (line_arg == NULL)
24751 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024752 theline = p;
24753 }
24754
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024755 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24756
24757 /* Add NULL lines for continuation lines, so that the line count is
24758 * equal to the index in the growarray. */
24759 while (sourcing_lnum_off-- > 0)
24760 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024761
24762 /* Check for end of eap->arg. */
24763 if (line_arg != NULL && *line_arg == NUL)
24764 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 }
24766
24767 /* Don't define the function when skipping commands or when an error was
24768 * detected. */
24769 if (eap->skip || did_emsg)
24770 goto erret;
24771
24772 /*
24773 * If there are no errors, add the function
24774 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024775 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024776 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024777 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024778 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024779 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024780 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024781 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024782 goto erret;
24783 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024784
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024785 fp = find_func(name);
24786 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024788 if (!eap->forceit)
24789 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024790 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024791 goto erret;
24792 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024793 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024794 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024795 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024796 name);
24797 goto erret;
24798 }
24799 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024800 ga_clear_strings(&(fp->uf_args));
24801 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024802 vim_free(name);
24803 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805 }
24806 else
24807 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024808 char numbuf[20];
24809
24810 fp = NULL;
24811 if (fudi.fd_newkey == NULL && !eap->forceit)
24812 {
24813 EMSG(_(e_funcdict));
24814 goto erret;
24815 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024816 if (fudi.fd_di == NULL)
24817 {
24818 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024819 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024820 goto erret;
24821 }
24822 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024823 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024824 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024825
24826 /* Give the function a sequential number. Can only be used with a
24827 * Funcref! */
24828 vim_free(name);
24829 sprintf(numbuf, "%d", ++func_nr);
24830 name = vim_strsave((char_u *)numbuf);
24831 if (name == NULL)
24832 goto erret;
24833 }
24834
24835 if (fp == NULL)
24836 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024837 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024838 {
24839 int slen, plen;
24840 char_u *scriptname;
24841
24842 /* Check that the autoload name matches the script name. */
24843 j = FAIL;
24844 if (sourcing_name != NULL)
24845 {
24846 scriptname = autoload_name(name);
24847 if (scriptname != NULL)
24848 {
24849 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024850 plen = (int)STRLEN(p);
24851 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024852 if (slen > plen && fnamecmp(p,
24853 sourcing_name + slen - plen) == 0)
24854 j = OK;
24855 vim_free(scriptname);
24856 }
24857 }
24858 if (j == FAIL)
24859 {
24860 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24861 goto erret;
24862 }
24863 }
24864
24865 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866 if (fp == NULL)
24867 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024868
24869 if (fudi.fd_dict != NULL)
24870 {
24871 if (fudi.fd_di == NULL)
24872 {
24873 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024874 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024875 if (fudi.fd_di == NULL)
24876 {
24877 vim_free(fp);
24878 goto erret;
24879 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024880 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24881 {
24882 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024883 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024884 goto erret;
24885 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024886 }
24887 else
24888 /* overwrite existing dict entry */
24889 clear_tv(&fudi.fd_di->di_tv);
24890 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024891 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024892 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024893 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024894
24895 /* behave like "dict" was used */
24896 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024897 }
24898
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024900 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024901 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24902 {
24903 vim_free(fp);
24904 goto erret;
24905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024907 fp->uf_args = newargs;
24908 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024909#ifdef FEAT_PROFILE
24910 fp->uf_tml_count = NULL;
24911 fp->uf_tml_total = NULL;
24912 fp->uf_tml_self = NULL;
24913 fp->uf_profiling = FALSE;
24914 if (prof_def_func())
24915 func_do_profile(fp);
24916#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024917 fp->uf_varargs = varargs;
24918 fp->uf_flags = flags;
24919 fp->uf_calls = 0;
24920 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024921 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024922
24923erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 ga_clear_strings(&newargs);
24925 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024926ret_free:
24927 vim_free(skip_until);
24928 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024931 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932}
24933
24934/*
24935 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024936 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024938 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024939 * TFN_INT: internal function name OK
24940 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024941 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 * Advances "pp" to just after the function name (if no error).
24943 */
24944 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024945trans_function_name(
24946 char_u **pp,
24947 int skip, /* only find the end, don't evaluate */
24948 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024949 funcdict_T *fdp, /* return: info about dictionary used */
24950 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024952 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953 char_u *start;
24954 char_u *end;
24955 int lead;
24956 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024958 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024959
24960 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024961 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024962 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024963
24964 /* Check for hard coded <SNR>: already translated function ID (from a user
24965 * command). */
24966 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24967 && (*pp)[2] == (int)KE_SNR)
24968 {
24969 *pp += 3;
24970 len = get_id_len(pp) + 3;
24971 return vim_strnsave(start, len);
24972 }
24973
24974 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24975 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024977 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024978 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024979
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024980 /* Note that TFN_ flags use the same values as GLV_ flags. */
24981 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024982 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024983 if (end == start)
24984 {
24985 if (!skip)
24986 EMSG(_("E129: Function name required"));
24987 goto theend;
24988 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024989 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024990 {
24991 /*
24992 * Report an invalid expression in braces, unless the expression
24993 * evaluation has been cancelled due to an aborting error, an
24994 * interrupt, or an exception.
24995 */
24996 if (!aborting())
24997 {
24998 if (end != NULL)
24999 EMSG2(_(e_invarg2), start);
25000 }
25001 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025002 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025003 goto theend;
25004 }
25005
25006 if (lv.ll_tv != NULL)
25007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025008 if (fdp != NULL)
25009 {
25010 fdp->fd_dict = lv.ll_dict;
25011 fdp->fd_newkey = lv.ll_newkey;
25012 lv.ll_newkey = NULL;
25013 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025015 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
25016 {
25017 name = vim_strsave(lv.ll_tv->vval.v_string);
25018 *pp = end;
25019 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010025020 else if (lv.ll_tv->v_type == VAR_PARTIAL
25021 && lv.ll_tv->vval.v_partial != NULL)
25022 {
25023 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
25024 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010025025 if (partial != NULL)
25026 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010025027 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025028 else
25029 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025030 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
25031 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025032 EMSG(_(e_funcref));
25033 else
25034 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025035 name = NULL;
25036 }
25037 goto theend;
25038 }
25039
25040 if (lv.ll_name == NULL)
25041 {
25042 /* Error found, but continue after the function name. */
25043 *pp = end;
25044 goto theend;
25045 }
25046
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025047 /* Check if the name is a Funcref. If so, use the value. */
25048 if (lv.ll_exp_name != NULL)
25049 {
25050 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010025051 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025052 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025053 if (name == lv.ll_exp_name)
25054 name = NULL;
25055 }
25056 else
25057 {
25058 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010025059 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025060 if (name == *pp)
25061 name = NULL;
25062 }
25063 if (name != NULL)
25064 {
25065 name = vim_strsave(name);
25066 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020025067 if (STRNCMP(name, "<SNR>", 5) == 0)
25068 {
25069 /* Change "<SNR>" to the byte sequence. */
25070 name[0] = K_SPECIAL;
25071 name[1] = KS_EXTRA;
25072 name[2] = (int)KE_SNR;
25073 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
25074 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000025075 goto theend;
25076 }
25077
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025078 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025079 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025080 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000025081 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
25082 && STRNCMP(lv.ll_name, "s:", 2) == 0)
25083 {
25084 /* When there was "s:" already or the name expanded to get a
25085 * leading "s:" then remove it. */
25086 lv.ll_name += 2;
25087 len -= 2;
25088 lead = 2;
25089 }
25090 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025091 else
Bram Moolenaara7043832005-01-21 11:56:39 +000025092 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025093 /* skip over "s:" and "g:" */
25094 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000025095 lv.ll_name += 2;
25096 len = (int)(end - lv.ll_name);
25097 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025098
25099 /*
25100 * Copy the function name to allocated memory.
25101 * Accept <SID>name() inside a script, translate into <SNR>123_name().
25102 * Accept <SNR>123_name() outside a script.
25103 */
25104 if (skip)
25105 lead = 0; /* do nothing */
25106 else if (lead > 0)
25107 {
25108 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000025109 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
25110 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025111 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000025112 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025113 if (current_SID <= 0)
25114 {
25115 EMSG(_(e_usingsid));
25116 goto theend;
25117 }
25118 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
25119 lead += (int)STRLEN(sid_buf);
25120 }
25121 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025122 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025123 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025124 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025125 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025126 goto theend;
25127 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025128 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025129 {
25130 char_u *cp = vim_strchr(lv.ll_name, ':');
25131
25132 if (cp != NULL && cp < end)
25133 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020025134 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025135 goto theend;
25136 }
25137 }
25138
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025139 name = alloc((unsigned)(len + lead + 1));
25140 if (name != NULL)
25141 {
25142 if (lead > 0)
25143 {
25144 name[0] = K_SPECIAL;
25145 name[1] = KS_EXTRA;
25146 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000025147 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025148 STRCPY(name + 3, sid_buf);
25149 }
25150 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025151 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000025152 }
25153 *pp = end;
25154
25155theend:
25156 clear_lval(&lv);
25157 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025158}
25159
25160/*
25161 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25162 * Return 2 if "p" starts with "s:".
25163 * Return 0 otherwise.
25164 */
25165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025166eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025168 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25169 * the standard library function. */
25170 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25171 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 return 5;
25173 if (p[0] == 's' && p[1] == ':')
25174 return 2;
25175 return 0;
25176}
25177
25178/*
25179 * Return TRUE if "p" starts with "<SID>" or "s:".
25180 * Only works if eval_fname_script() returned non-zero for "p"!
25181 */
25182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025183eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184{
25185 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25186}
25187
25188/*
25189 * List the head of the function: "name(arg1, arg2)".
25190 */
25191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025192list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193{
25194 int j;
25195
25196 msg_start();
25197 if (indent)
25198 MSG_PUTS(" ");
25199 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025200 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 {
25202 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025203 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 }
25205 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025206 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025208 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 {
25210 if (j)
25211 MSG_PUTS(", ");
25212 msg_puts(FUNCARG(fp, j));
25213 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025214 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 {
25216 if (j)
25217 MSG_PUTS(", ");
25218 MSG_PUTS("...");
25219 }
25220 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025221 if (fp->uf_flags & FC_ABORT)
25222 MSG_PUTS(" abort");
25223 if (fp->uf_flags & FC_RANGE)
25224 MSG_PUTS(" range");
25225 if (fp->uf_flags & FC_DICT)
25226 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025227 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025228 if (p_verbose > 0)
25229 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230}
25231
25232/*
25233 * Find a function by name, return pointer to it in ufuncs.
25234 * Return NULL for unknown function.
25235 */
25236 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025237find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025239 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025241 hi = hash_find(&func_hashtab, name);
25242 if (!HASHITEM_EMPTY(hi))
25243 return HI2UF(hi);
25244 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245}
25246
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025247#if defined(EXITFREE) || defined(PROTO)
25248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025249free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025250{
25251 hashitem_T *hi;
25252
25253 /* Need to start all over every time, because func_free() may change the
25254 * hash table. */
25255 while (func_hashtab.ht_used > 0)
25256 for (hi = func_hashtab.ht_array; ; ++hi)
25257 if (!HASHITEM_EMPTY(hi))
25258 {
25259 func_free(HI2UF(hi));
25260 break;
25261 }
25262}
25263#endif
25264
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025266translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025267{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025268 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025269 return find_internal_func(name) >= 0;
25270 return find_func(name) != NULL;
25271}
25272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025273/*
25274 * Return TRUE if a function "name" exists.
25275 */
25276 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025277function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025278{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025279 char_u *nm = name;
25280 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025281 int n = FALSE;
25282
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025283 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025284 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025285 nm = skipwhite(nm);
25286
25287 /* Only accept "funcname", "funcname ", "funcname (..." and
25288 * "funcname(...", not "funcname!...". */
25289 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025290 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025291 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025292 return n;
25293}
25294
Bram Moolenaara1544c02013-05-30 12:35:52 +020025295 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025296get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025297{
25298 char_u *nm = name;
25299 char_u *p;
25300
Bram Moolenaar65639032016-03-16 21:40:30 +010025301 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025302
25303 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025304 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025305 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025306
Bram Moolenaara1544c02013-05-30 12:35:52 +020025307 vim_free(p);
25308 return NULL;
25309}
25310
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025311/*
25312 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025313 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25314 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025315 */
25316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025317builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025318{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025319 char_u *p;
25320
25321 if (!ASCII_ISLOWER(name[0]))
25322 return FALSE;
25323 p = vim_strchr(name, AUTOLOAD_CHAR);
25324 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025325}
25326
Bram Moolenaar05159a02005-02-26 23:04:13 +000025327#if defined(FEAT_PROFILE) || defined(PROTO)
25328/*
25329 * Start profiling function "fp".
25330 */
25331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025332func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025333{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025334 int len = fp->uf_lines.ga_len;
25335
25336 if (len == 0)
25337 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025338 fp->uf_tm_count = 0;
25339 profile_zero(&fp->uf_tm_self);
25340 profile_zero(&fp->uf_tm_total);
25341 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025342 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025343 if (fp->uf_tml_total == NULL)
25344 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025345 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025346 if (fp->uf_tml_self == NULL)
25347 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025348 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025349 fp->uf_tml_idx = -1;
25350 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25351 || fp->uf_tml_self == NULL)
25352 return; /* out of memory */
25353
25354 fp->uf_profiling = TRUE;
25355}
25356
25357/*
25358 * Dump the profiling results for all functions in file "fd".
25359 */
25360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025361func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025362{
25363 hashitem_T *hi;
25364 int todo;
25365 ufunc_T *fp;
25366 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025367 ufunc_T **sorttab;
25368 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025370 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025371 if (todo == 0)
25372 return; /* nothing to dump */
25373
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025374 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025375
Bram Moolenaar05159a02005-02-26 23:04:13 +000025376 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25377 {
25378 if (!HASHITEM_EMPTY(hi))
25379 {
25380 --todo;
25381 fp = HI2UF(hi);
25382 if (fp->uf_profiling)
25383 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025384 if (sorttab != NULL)
25385 sorttab[st_len++] = fp;
25386
Bram Moolenaar05159a02005-02-26 23:04:13 +000025387 if (fp->uf_name[0] == K_SPECIAL)
25388 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25389 else
25390 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25391 if (fp->uf_tm_count == 1)
25392 fprintf(fd, "Called 1 time\n");
25393 else
25394 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25395 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25396 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25397 fprintf(fd, "\n");
25398 fprintf(fd, "count total (s) self (s)\n");
25399
25400 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25401 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025402 if (FUNCLINE(fp, i) == NULL)
25403 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025404 prof_func_line(fd, fp->uf_tml_count[i],
25405 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025406 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25407 }
25408 fprintf(fd, "\n");
25409 }
25410 }
25411 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025412
25413 if (sorttab != NULL && st_len > 0)
25414 {
25415 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25416 prof_total_cmp);
25417 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25418 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25419 prof_self_cmp);
25420 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25421 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025422
25423 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025424}
Bram Moolenaar73830342005-02-28 22:48:19 +000025425
25426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025427prof_sort_list(
25428 FILE *fd,
25429 ufunc_T **sorttab,
25430 int st_len,
25431 char *title,
25432 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025433{
25434 int i;
25435 ufunc_T *fp;
25436
25437 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25438 fprintf(fd, "count total (s) self (s) function\n");
25439 for (i = 0; i < 20 && i < st_len; ++i)
25440 {
25441 fp = sorttab[i];
25442 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25443 prefer_self);
25444 if (fp->uf_name[0] == K_SPECIAL)
25445 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25446 else
25447 fprintf(fd, " %s()\n", fp->uf_name);
25448 }
25449 fprintf(fd, "\n");
25450}
25451
25452/*
25453 * Print the count and times for one function or function line.
25454 */
25455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025456prof_func_line(
25457 FILE *fd,
25458 int count,
25459 proftime_T *total,
25460 proftime_T *self,
25461 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025462{
25463 if (count > 0)
25464 {
25465 fprintf(fd, "%5d ", count);
25466 if (prefer_self && profile_equal(total, self))
25467 fprintf(fd, " ");
25468 else
25469 fprintf(fd, "%s ", profile_msg(total));
25470 if (!prefer_self && profile_equal(total, self))
25471 fprintf(fd, " ");
25472 else
25473 fprintf(fd, "%s ", profile_msg(self));
25474 }
25475 else
25476 fprintf(fd, " ");
25477}
25478
25479/*
25480 * Compare function for total time sorting.
25481 */
25482 static int
25483#ifdef __BORLANDC__
25484_RTLENTRYF
25485#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025486prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025487{
25488 ufunc_T *p1, *p2;
25489
25490 p1 = *(ufunc_T **)s1;
25491 p2 = *(ufunc_T **)s2;
25492 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25493}
25494
25495/*
25496 * Compare function for self time sorting.
25497 */
25498 static int
25499#ifdef __BORLANDC__
25500_RTLENTRYF
25501#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025502prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025503{
25504 ufunc_T *p1, *p2;
25505
25506 p1 = *(ufunc_T **)s1;
25507 p2 = *(ufunc_T **)s2;
25508 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25509}
25510
Bram Moolenaar05159a02005-02-26 23:04:13 +000025511#endif
25512
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025513/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025514 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025515 * Return TRUE if a package was loaded.
25516 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025518script_autoload(
25519 char_u *name,
25520 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025521{
25522 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025523 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025524 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025525 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025526
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025527 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025528 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025529 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025530 return FALSE;
25531
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025532 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025533
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025534 /* Find the name in the list of previously loaded package names. Skip
25535 * "autoload/", it's always the same. */
25536 for (i = 0; i < ga_loaded.ga_len; ++i)
25537 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25538 break;
25539 if (!reload && i < ga_loaded.ga_len)
25540 ret = FALSE; /* was loaded already */
25541 else
25542 {
25543 /* Remember the name if it wasn't loaded already. */
25544 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25545 {
25546 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25547 tofree = NULL;
25548 }
25549
25550 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025551 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025552 ret = TRUE;
25553 }
25554
25555 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025556 return ret;
25557}
25558
25559/*
25560 * Return the autoload script name for a function or variable name.
25561 * Returns NULL when out of memory.
25562 */
25563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025564autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025565{
25566 char_u *p;
25567 char_u *scriptname;
25568
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025569 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025570 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25571 if (scriptname == NULL)
25572 return FALSE;
25573 STRCPY(scriptname, "autoload/");
25574 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025575 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025576 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025577 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025578 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025579 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025580}
25581
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25583
25584/*
25585 * Function given to ExpandGeneric() to obtain the list of user defined
25586 * function names.
25587 */
25588 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025589get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025591 static long_u done;
25592 static hashitem_T *hi;
25593 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025594
25595 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025597 done = 0;
25598 hi = func_hashtab.ht_array;
25599 }
25600 if (done < func_hashtab.ht_used)
25601 {
25602 if (done++ > 0)
25603 ++hi;
25604 while (HASHITEM_EMPTY(hi))
25605 ++hi;
25606 fp = HI2UF(hi);
25607
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025608 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025609 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025610
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025611 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25612 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025613
25614 cat_func_name(IObuff, fp);
25615 if (xp->xp_context != EXPAND_USER_FUNC)
25616 {
25617 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025618 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 STRCAT(IObuff, ")");
25620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621 return IObuff;
25622 }
25623 return NULL;
25624}
25625
25626#endif /* FEAT_CMDL_COMPL */
25627
25628/*
25629 * Copy the function name of "fp" to buffer "buf".
25630 * "buf" must be able to hold the function name plus three bytes.
25631 * Takes care of script-local function names.
25632 */
25633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025634cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025636 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637 {
25638 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025639 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 }
25641 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025642 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643}
25644
25645/*
25646 * ":delfunction {name}"
25647 */
25648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025649ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025651 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 char_u *p;
25653 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025654 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655
25656 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025657 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025658 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025659 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025660 {
25661 if (fudi.fd_dict != NULL && !eap->skip)
25662 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 if (!ends_excmd(*skipwhite(p)))
25666 {
25667 vim_free(name);
25668 EMSG(_(e_trailing));
25669 return;
25670 }
25671 eap->nextcmd = check_nextcmd(p);
25672 if (eap->nextcmd != NULL)
25673 *p = NUL;
25674
25675 if (!eap->skip)
25676 fp = find_func(name);
25677 vim_free(name);
25678
25679 if (!eap->skip)
25680 {
25681 if (fp == NULL)
25682 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025683 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 return;
25685 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025686 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025687 {
25688 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25689 return;
25690 }
25691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025692 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025694 /* Delete the dict item that refers to the function, it will
25695 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025696 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025698 else
25699 func_free(fp);
25700 }
25701}
25702
25703/*
25704 * Free a function and remove it from the list of functions.
25705 */
25706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025707func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025708{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025709 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025710
25711 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025712 ga_clear_strings(&(fp->uf_args));
25713 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025714#ifdef FEAT_PROFILE
25715 vim_free(fp->uf_tml_count);
25716 vim_free(fp->uf_tml_total);
25717 vim_free(fp->uf_tml_self);
25718#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025719
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025720 /* remove the function from the function hashtable */
25721 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25722 if (HASHITEM_EMPTY(hi))
25723 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025724 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025725 hash_remove(&func_hashtab, hi);
25726
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025727 vim_free(fp);
25728}
25729
25730/*
25731 * Unreference a Function: decrement the reference count and free it when it
25732 * becomes zero. Only for numbered functions.
25733 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025735func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025736{
25737 ufunc_T *fp;
25738
25739 if (name != NULL && isdigit(*name))
25740 {
25741 fp = find_func(name);
25742 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025743 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025744#ifdef EXITFREE
25745 if (!entered_free_all_mem)
25746#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025747 EMSG2(_(e_intern2), "func_unref()");
25748 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025749 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025750 {
25751 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025752 * when "uf_calls" becomes zero. */
25753 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025754 func_free(fp);
25755 }
25756 }
25757}
25758
25759/*
25760 * Count a reference to a Function.
25761 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025763func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025764{
25765 ufunc_T *fp;
25766
25767 if (name != NULL && isdigit(*name))
25768 {
25769 fp = find_func(name);
25770 if (fp == NULL)
25771 EMSG2(_(e_intern2), "func_ref()");
25772 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025773 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 }
25775}
25776
25777/*
25778 * Call a user function.
25779 */
25780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025781call_user_func(
25782 ufunc_T *fp, /* pointer to function */
25783 int argcount, /* nr of args */
25784 typval_T *argvars, /* arguments */
25785 typval_T *rettv, /* return value */
25786 linenr_T firstline, /* first line of range */
25787 linenr_T lastline, /* last line of range */
25788 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789{
Bram Moolenaar33570922005-01-25 22:26:29 +000025790 char_u *save_sourcing_name;
25791 linenr_T save_sourcing_lnum;
25792 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025793 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025794 int save_did_emsg;
25795 static int depth = 0;
25796 dictitem_T *v;
25797 int fixvar_idx = 0; /* index in fixvar[] */
25798 int i;
25799 int ai;
25800 char_u numbuf[NUMBUFLEN];
25801 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025802 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025803#ifdef FEAT_PROFILE
25804 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025805 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807
25808 /* If depth of calling is getting too high, don't execute the function */
25809 if (depth >= p_mfd)
25810 {
25811 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025812 rettv->v_type = VAR_NUMBER;
25813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814 return;
25815 }
25816 ++depth;
25817
25818 line_breakcheck(); /* check for CTRL-C hit */
25819
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025820 fc = (funccall_T *)alloc(sizeof(funccall_T));
25821 fc->caller = current_funccal;
25822 current_funccal = fc;
25823 fc->func = fp;
25824 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025825 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025826 fc->linenr = 0;
25827 fc->returned = FALSE;
25828 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025830 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25831 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832
Bram Moolenaar33570922005-01-25 22:26:29 +000025833 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025834 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025835 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25836 * each argument variable and saves a lot of time.
25837 */
25838 /*
25839 * Init l: variables.
25840 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025841 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025842 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025843 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025844 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25845 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025846 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025847 name = v->di_key;
25848 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025849 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025850 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025851 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025852 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025853 v->di_tv.vval.v_dict = selfdict;
25854 ++selfdict->dv_refcount;
25855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025856
Bram Moolenaar33570922005-01-25 22:26:29 +000025857 /*
25858 * Init a: variables.
25859 * Set a:0 to "argcount".
25860 * Set a:000 to a list with room for the "..." arguments.
25861 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025862 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025863 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025864 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025865 /* Use "name" to avoid a warning from some compiler that checks the
25866 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025867 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025868 name = v->di_key;
25869 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025870 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025871 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025872 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025873 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025874 v->di_tv.vval.v_list = &fc->l_varlist;
25875 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25876 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25877 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025878
25879 /*
25880 * Set a:firstline to "firstline" and a:lastline to "lastline".
25881 * Set a:name to named arguments.
25882 * Set a:N to the "..." arguments.
25883 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025884 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025885 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025886 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025887 (varnumber_T)lastline);
25888 for (i = 0; i < argcount; ++i)
25889 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025890 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025891 if (ai < 0)
25892 /* named argument a:name */
25893 name = FUNCARG(fp, i);
25894 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025895 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025896 /* "..." argument a:1, a:2, etc. */
25897 sprintf((char *)numbuf, "%d", ai + 1);
25898 name = numbuf;
25899 }
25900 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25901 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025902 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025903 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25904 }
25905 else
25906 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025907 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25908 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025909 if (v == NULL)
25910 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025911 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025912 }
25913 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025914 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025915
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025916 /* Note: the values are copied directly to avoid alloc/free.
25917 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025918 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025919 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025920
25921 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25922 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025923 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25924 fc->l_listitems[ai].li_tv = argvars[i];
25925 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025926 }
25927 }
25928
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929 /* Don't redraw while executing the function. */
25930 ++RedrawingDisabled;
25931 save_sourcing_name = sourcing_name;
25932 save_sourcing_lnum = sourcing_lnum;
25933 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025934 /* need space for function name + ("function " + 3) or "[number]" */
25935 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25936 + STRLEN(fp->uf_name) + 20;
25937 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938 if (sourcing_name != NULL)
25939 {
25940 if (save_sourcing_name != NULL
25941 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025942 sprintf((char *)sourcing_name, "%s[%d]..",
25943 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944 else
25945 STRCPY(sourcing_name, "function ");
25946 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25947
25948 if (p_verbose >= 12)
25949 {
25950 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025951 verbose_enter_scroll();
25952
Bram Moolenaar555b2802005-05-19 21:08:39 +000025953 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 if (p_verbose >= 14)
25955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025957 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025958 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025959 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960
25961 msg_puts((char_u *)"(");
25962 for (i = 0; i < argcount; ++i)
25963 {
25964 if (i > 0)
25965 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025966 if (argvars[i].v_type == VAR_NUMBER)
25967 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 else
25969 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025970 /* Do not want errors such as E724 here. */
25971 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025972 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025973 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025974 if (s != NULL)
25975 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025976 if (vim_strsize(s) > MSG_BUF_CLEN)
25977 {
25978 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25979 s = buf;
25980 }
25981 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025982 vim_free(tofree);
25983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984 }
25985 }
25986 msg_puts((char_u *)")");
25987 }
25988 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025989
25990 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991 --no_wait_return;
25992 }
25993 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025994#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025995 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025996 {
25997 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25998 func_do_profile(fp);
25999 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026000 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000026001 {
26002 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000026003 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026004 profile_zero(&fp->uf_tm_children);
26005 }
26006 script_prof_save(&wait_start);
26007 }
26008#endif
26009
Bram Moolenaar071d4272004-06-13 20:20:40 +000026010 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026011 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012 save_did_emsg = did_emsg;
26013 did_emsg = FALSE;
26014
26015 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026016 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
26018
26019 --RedrawingDisabled;
26020
26021 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026022 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026024 clear_tv(rettv);
26025 rettv->v_type = VAR_NUMBER;
26026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026027 }
26028
Bram Moolenaar05159a02005-02-26 23:04:13 +000026029#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026030 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026031 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000026032 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000026033 profile_end(&call_start);
26034 profile_sub_wait(&wait_start, &call_start);
26035 profile_add(&fp->uf_tm_total, &call_start);
26036 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026037 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026038 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026039 profile_add(&fc->caller->func->uf_tm_children, &call_start);
26040 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026041 }
26042 }
26043#endif
26044
Bram Moolenaar071d4272004-06-13 20:20:40 +000026045 /* when being verbose, mention the return value */
26046 if (p_verbose >= 12)
26047 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026049 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026050
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000026052 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026053 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000026054 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026055 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000026056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000026058 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000026059 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000026060 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026061 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000026062
Bram Moolenaar555b2802005-05-19 21:08:39 +000026063 /* The value may be very long. Skip the middle part, so that we
26064 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020026065 * truncate it at the end. Don't want errors such as E724 here. */
26066 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026067 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020026068 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026069 if (s != NULL)
26070 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010026071 if (vim_strsize(s) > MSG_BUF_CLEN)
26072 {
26073 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
26074 s = buf;
26075 }
26076 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000026077 vim_free(tofree);
26078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079 }
26080 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026081
26082 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083 --no_wait_return;
26084 }
26085
26086 vim_free(sourcing_name);
26087 sourcing_name = save_sourcing_name;
26088 sourcing_lnum = save_sourcing_lnum;
26089 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026090#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026091 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026092 script_prof_restore(&wait_start);
26093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094
26095 if (p_verbose >= 12 && sourcing_name != NULL)
26096 {
26097 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026098 verbose_enter_scroll();
26099
Bram Moolenaar555b2802005-05-19 21:08:39 +000026100 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000026102
26103 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000026104 --no_wait_return;
26105 }
26106
26107 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026108 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026110
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026111 /* If the a:000 list and the l: and a: dicts are not referenced we can
26112 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026113 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
26114 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
26115 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
26116 {
26117 free_funccal(fc, FALSE);
26118 }
26119 else
26120 {
26121 hashitem_T *hi;
26122 listitem_T *li;
26123 int todo;
26124
26125 /* "fc" is still in use. This can happen when returning "a:000" or
26126 * assigning "l:" to a global variable.
26127 * Link "fc" in the list for garbage collection later. */
26128 fc->caller = previous_funccal;
26129 previous_funccal = fc;
26130
26131 /* Make a copy of the a: variables, since we didn't do that above. */
26132 todo = (int)fc->l_avars.dv_hashtab.ht_used;
26133 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
26134 {
26135 if (!HASHITEM_EMPTY(hi))
26136 {
26137 --todo;
26138 v = HI2DI(hi);
26139 copy_tv(&v->di_tv, &v->di_tv);
26140 }
26141 }
26142
26143 /* Make a copy of the a:000 items, since we didn't do that above. */
26144 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26145 copy_tv(&li->li_tv, &li->li_tv);
26146 }
26147}
26148
26149/*
26150 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000026151 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026152 */
26153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026154can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026155{
26156 return (fc->l_varlist.lv_copyID != copyID
26157 && fc->l_vars.dv_copyID != copyID
26158 && fc->l_avars.dv_copyID != copyID);
26159}
26160
26161/*
26162 * Free "fc" and what it contains.
26163 */
26164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026165free_funccal(
26166 funccall_T *fc,
26167 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026168{
26169 listitem_T *li;
26170
26171 /* The a: variables typevals may not have been allocated, only free the
26172 * allocated variables. */
26173 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26174
26175 /* free all l: variables */
26176 vars_clear(&fc->l_vars.dv_hashtab);
26177
26178 /* Free the a:000 variables if they were allocated. */
26179 if (free_val)
26180 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26181 clear_tv(&li->li_tv);
26182
26183 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184}
26185
26186/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026187 * Add a number variable "name" to dict "dp" with value "nr".
26188 */
26189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026190add_nr_var(
26191 dict_T *dp,
26192 dictitem_T *v,
26193 char *name,
26194 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026195{
26196 STRCPY(v->di_key, name);
26197 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26198 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26199 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026200 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026201 v->di_tv.vval.v_number = nr;
26202}
26203
26204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026205 * ":return [expr]"
26206 */
26207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026208ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026209{
26210 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026211 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212 int returning = FALSE;
26213
26214 if (current_funccal == NULL)
26215 {
26216 EMSG(_("E133: :return not inside a function"));
26217 return;
26218 }
26219
26220 if (eap->skip)
26221 ++emsg_skip;
26222
26223 eap->nextcmd = NULL;
26224 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026225 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226 {
26227 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026228 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231 }
26232 /* It's safer to return also on error. */
26233 else if (!eap->skip)
26234 {
26235 /*
26236 * Return unless the expression evaluation has been cancelled due to an
26237 * aborting error, an interrupt, or an exception.
26238 */
26239 if (!aborting())
26240 returning = do_return(eap, FALSE, TRUE, NULL);
26241 }
26242
26243 /* When skipping or the return gets pending, advance to the next command
26244 * in this line (!returning). Otherwise, ignore the rest of the line.
26245 * Following lines will be ignored by get_func_line(). */
26246 if (returning)
26247 eap->nextcmd = NULL;
26248 else if (eap->nextcmd == NULL) /* no argument */
26249 eap->nextcmd = check_nextcmd(arg);
26250
26251 if (eap->skip)
26252 --emsg_skip;
26253}
26254
26255/*
26256 * Return from a function. Possibly makes the return pending. Also called
26257 * for a pending return at the ":endtry" or after returning from an extra
26258 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026259 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026260 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 * FALSE when the return gets pending.
26262 */
26263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026264do_return(
26265 exarg_T *eap,
26266 int reanimate,
26267 int is_cmd,
26268 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269{
26270 int idx;
26271 struct condstack *cstack = eap->cstack;
26272
26273 if (reanimate)
26274 /* Undo the return. */
26275 current_funccal->returned = FALSE;
26276
26277 /*
26278 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26279 * not in its finally clause (which then is to be executed next) is found.
26280 * In this case, make the ":return" pending for execution at the ":endtry".
26281 * Otherwise, return normally.
26282 */
26283 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26284 if (idx >= 0)
26285 {
26286 cstack->cs_pending[idx] = CSTP_RETURN;
26287
26288 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026289 /* A pending return again gets pending. "rettv" points to an
26290 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026292 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 else
26294 {
26295 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026296 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026298 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026300 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026301 {
26302 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026303 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026304 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 else
26306 EMSG(_(e_outofmem));
26307 }
26308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026309 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310
26311 if (reanimate)
26312 {
26313 /* The pending return value could be overwritten by a ":return"
26314 * without argument in a finally clause; reset the default
26315 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026316 current_funccal->rettv->v_type = VAR_NUMBER;
26317 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318 }
26319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026320 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026321 }
26322 else
26323 {
26324 current_funccal->returned = TRUE;
26325
26326 /* If the return is carried out now, store the return value. For
26327 * a return immediately after reanimation, the value is already
26328 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026329 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026331 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026332 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026334 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335 }
26336 }
26337
26338 return idx < 0;
26339}
26340
26341/*
26342 * Free the variable with a pending return value.
26343 */
26344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026345discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026346{
Bram Moolenaar33570922005-01-25 22:26:29 +000026347 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026348}
26349
26350/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026351 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352 * is an allocated string. Used by report_pending() for verbose messages.
26353 */
26354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026355get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026357 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026358 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026359 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026361 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026362 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026363 if (s == NULL)
26364 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026365
26366 STRCPY(IObuff, ":return ");
26367 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26368 if (STRLEN(s) + 8 >= IOSIZE)
26369 STRCPY(IObuff + IOSIZE - 4, "...");
26370 vim_free(tofree);
26371 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372}
26373
26374/*
26375 * Get next function line.
26376 * Called by do_cmdline() to get the next line.
26377 * Returns allocated string, or NULL for end of function.
26378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026380get_func_line(
26381 int c UNUSED,
26382 void *cookie,
26383 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026384{
Bram Moolenaar33570922005-01-25 22:26:29 +000026385 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026386 ufunc_T *fp = fcp->func;
26387 char_u *retval;
26388 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389
26390 /* If breakpoints have been added/deleted need to check for it. */
26391 if (fcp->dbg_tick != debug_tick)
26392 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026393 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394 sourcing_lnum);
26395 fcp->dbg_tick = debug_tick;
26396 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026397#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026398 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026399 func_line_end(cookie);
26400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026401
Bram Moolenaar05159a02005-02-26 23:04:13 +000026402 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026403 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26404 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405 retval = NULL;
26406 else
26407 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026408 /* Skip NULL lines (continuation lines). */
26409 while (fcp->linenr < gap->ga_len
26410 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26411 ++fcp->linenr;
26412 if (fcp->linenr >= gap->ga_len)
26413 retval = NULL;
26414 else
26415 {
26416 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26417 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026418#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026419 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026420 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026421#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 }
26424
26425 /* Did we encounter a breakpoint? */
26426 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26427 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026428 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026430 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026431 sourcing_lnum);
26432 fcp->dbg_tick = debug_tick;
26433 }
26434
26435 return retval;
26436}
26437
Bram Moolenaar05159a02005-02-26 23:04:13 +000026438#if defined(FEAT_PROFILE) || defined(PROTO)
26439/*
26440 * Called when starting to read a function line.
26441 * "sourcing_lnum" must be correct!
26442 * When skipping lines it may not actually be executed, but we won't find out
26443 * until later and we need to store the time now.
26444 */
26445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026446func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026447{
26448 funccall_T *fcp = (funccall_T *)cookie;
26449 ufunc_T *fp = fcp->func;
26450
26451 if (fp->uf_profiling && sourcing_lnum >= 1
26452 && sourcing_lnum <= fp->uf_lines.ga_len)
26453 {
26454 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026455 /* Skip continuation lines. */
26456 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26457 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026458 fp->uf_tml_execed = FALSE;
26459 profile_start(&fp->uf_tml_start);
26460 profile_zero(&fp->uf_tml_children);
26461 profile_get_wait(&fp->uf_tml_wait);
26462 }
26463}
26464
26465/*
26466 * Called when actually executing a function line.
26467 */
26468 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026469func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026470{
26471 funccall_T *fcp = (funccall_T *)cookie;
26472 ufunc_T *fp = fcp->func;
26473
26474 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26475 fp->uf_tml_execed = TRUE;
26476}
26477
26478/*
26479 * Called when done with a function line.
26480 */
26481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026482func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026483{
26484 funccall_T *fcp = (funccall_T *)cookie;
26485 ufunc_T *fp = fcp->func;
26486
26487 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26488 {
26489 if (fp->uf_tml_execed)
26490 {
26491 ++fp->uf_tml_count[fp->uf_tml_idx];
26492 profile_end(&fp->uf_tml_start);
26493 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026494 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026495 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26496 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026497 }
26498 fp->uf_tml_idx = -1;
26499 }
26500}
26501#endif
26502
Bram Moolenaar071d4272004-06-13 20:20:40 +000026503/*
26504 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026505 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026506 */
26507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026508func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509{
Bram Moolenaar33570922005-01-25 22:26:29 +000026510 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026511
26512 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26513 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026514 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026515 || fcp->returned);
26516}
26517
26518/*
26519 * return TRUE if cookie indicates a function which "abort"s on errors.
26520 */
26521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026522func_has_abort(
26523 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026525 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526}
26527
26528#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26529typedef enum
26530{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026531 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26532 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26533 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534} var_flavour_T;
26535
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026536static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026537
26538 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026539var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540{
26541 char_u *p = varname;
26542
26543 if (ASCII_ISUPPER(*p))
26544 {
26545 while (*(++p))
26546 if (ASCII_ISLOWER(*p))
26547 return VAR_FLAVOUR_SESSION;
26548 return VAR_FLAVOUR_VIMINFO;
26549 }
26550 else
26551 return VAR_FLAVOUR_DEFAULT;
26552}
26553#endif
26554
26555#if defined(FEAT_VIMINFO) || defined(PROTO)
26556/*
26557 * Restore global vars that start with a capital from the viminfo file
26558 */
26559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026560read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561{
26562 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026563 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026564 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026565 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566
26567 if (!writing && (find_viminfo_parameter('!') != NULL))
26568 {
26569 tab = vim_strchr(virp->vir_line + 1, '\t');
26570 if (tab != NULL)
26571 {
26572 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026573 switch (*tab)
26574 {
26575 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026576#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026577 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026578#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026579 case 'D': type = VAR_DICT; break;
26580 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026581 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583
26584 tab = vim_strchr(tab, '\t');
26585 if (tab != NULL)
26586 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026587 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026588 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026589 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026590 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026591#ifdef FEAT_FLOAT
26592 else if (type == VAR_FLOAT)
26593 (void)string2float(tab + 1, &tv.vval.v_float);
26594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026596 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026597 if (type == VAR_DICT || type == VAR_LIST)
26598 {
26599 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26600
26601 if (etv == NULL)
26602 /* Failed to parse back the dict or list, use it as a
26603 * string. */
26604 tv.v_type = VAR_STRING;
26605 else
26606 {
26607 vim_free(tv.vval.v_string);
26608 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026609 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026610 }
26611 }
26612
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026613 /* when in a function use global variables */
26614 save_funccal = current_funccal;
26615 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026616 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026617 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026618
26619 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026620 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026621 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26622 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026623 }
26624 }
26625 }
26626
26627 return viminfo_readline(virp);
26628}
26629
26630/*
26631 * Write global vars that start with a capital to the viminfo file
26632 */
26633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026634write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026635{
Bram Moolenaar33570922005-01-25 22:26:29 +000026636 hashitem_T *hi;
26637 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026638 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026639 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026640 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026641 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026642 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026643
26644 if (find_viminfo_parameter('!') == NULL)
26645 return;
26646
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026647 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026648
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026649 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026650 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026651 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026652 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026653 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026654 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026655 this_var = HI2DI(hi);
26656 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026657 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026658 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026659 {
26660 case VAR_STRING: s = "STR"; break;
26661 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026662 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026663 case VAR_DICT: s = "DIC"; break;
26664 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026665 case VAR_SPECIAL: s = "XPL"; break;
26666
26667 case VAR_UNKNOWN:
26668 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026669 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026670 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026671 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026672 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026673 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026674 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026675 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026676 if (p != NULL)
26677 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026678 vim_free(tofree);
26679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026680 }
26681 }
26682}
26683#endif
26684
26685#if defined(FEAT_SESSION) || defined(PROTO)
26686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026687store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026688{
Bram Moolenaar33570922005-01-25 22:26:29 +000026689 hashitem_T *hi;
26690 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026691 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692 char_u *p, *t;
26693
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026694 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026695 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026696 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026697 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026698 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026699 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026700 this_var = HI2DI(hi);
26701 if ((this_var->di_tv.v_type == VAR_NUMBER
26702 || this_var->di_tv.v_type == VAR_STRING)
26703 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026704 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026705 /* Escape special characters with a backslash. Turn a LF and
26706 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026707 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026708 (char_u *)"\\\"\n\r");
26709 if (p == NULL) /* out of memory */
26710 break;
26711 for (t = p; *t != NUL; ++t)
26712 if (*t == '\n')
26713 *t = 'n';
26714 else if (*t == '\r')
26715 *t = 'r';
26716 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026717 this_var->di_key,
26718 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26719 : ' ',
26720 p,
26721 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26722 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026723 || put_eol(fd) == FAIL)
26724 {
26725 vim_free(p);
26726 return FAIL;
26727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026728 vim_free(p);
26729 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026730#ifdef FEAT_FLOAT
26731 else if (this_var->di_tv.v_type == VAR_FLOAT
26732 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26733 {
26734 float_T f = this_var->di_tv.vval.v_float;
26735 int sign = ' ';
26736
26737 if (f < 0)
26738 {
26739 f = -f;
26740 sign = '-';
26741 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026742 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026743 this_var->di_key, sign, f) < 0)
26744 || put_eol(fd) == FAIL)
26745 return FAIL;
26746 }
26747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026748 }
26749 }
26750 return OK;
26751}
26752#endif
26753
Bram Moolenaar661b1822005-07-28 22:36:45 +000026754/*
26755 * Display script name where an item was last set.
26756 * Should only be invoked when 'verbose' is non-zero.
26757 */
26758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026759last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026760{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026761 char_u *p;
26762
Bram Moolenaar661b1822005-07-28 22:36:45 +000026763 if (scriptID != 0)
26764 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026765 p = home_replace_save(NULL, get_scriptname(scriptID));
26766 if (p != NULL)
26767 {
26768 verbose_enter();
26769 MSG_PUTS(_("\n\tLast set from "));
26770 MSG_PUTS(p);
26771 vim_free(p);
26772 verbose_leave();
26773 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026774 }
26775}
26776
Bram Moolenaard812df62008-11-09 12:46:09 +000026777/*
26778 * List v:oldfiles in a nice way.
26779 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026781ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026782{
26783 list_T *l = vimvars[VV_OLDFILES].vv_list;
26784 listitem_T *li;
26785 int nr = 0;
26786
26787 if (l == NULL)
26788 msg((char_u *)_("No old files"));
26789 else
26790 {
26791 msg_start();
26792 msg_scroll = TRUE;
26793 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26794 {
26795 msg_outnum((long)++nr);
26796 MSG_PUTS(": ");
26797 msg_outtrans(get_tv_string(&li->li_tv));
26798 msg_putchar('\n');
26799 out_flush(); /* output one line at a time */
26800 ui_breakcheck();
26801 }
26802 /* Assume "got_int" was set to truncate the listing. */
26803 got_int = FALSE;
26804
26805#ifdef FEAT_BROWSE_CMD
26806 if (cmdmod.browse)
26807 {
26808 quit_more = FALSE;
26809 nr = prompt_for_number(FALSE);
26810 msg_starthere();
26811 if (nr > 0)
26812 {
26813 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26814 (long)nr);
26815
26816 if (p != NULL)
26817 {
26818 p = expand_env_save(p);
26819 eap->arg = p;
26820 eap->cmdidx = CMD_edit;
26821 cmdmod.browse = FALSE;
26822 do_exedit(eap, NULL);
26823 vim_free(p);
26824 }
26825 }
26826 }
26827#endif
26828 }
26829}
26830
Bram Moolenaar53744302015-07-17 17:38:22 +020026831/* reset v:option_new, v:option_old and v:option_type */
26832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026833reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026834{
26835 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26836 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26837 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26838}
26839
26840
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841#endif /* FEAT_EVAL */
26842
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026844#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026845
26846#ifdef WIN3264
26847/*
26848 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26849 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026850static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26851static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26852static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026853
26854/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026855 * Get the short path (8.3) for the filename in "fnamep".
26856 * Only works for a valid file name.
26857 * When the path gets longer "fnamep" is changed and the allocated buffer
26858 * is put in "bufp".
26859 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26860 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026861 */
26862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026863get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026865 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026866 char_u *newbuf;
26867
26868 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026869 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026870 if (l > len - 1)
26871 {
26872 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026873 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026874 newbuf = vim_strnsave(*fnamep, l);
26875 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026876 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026877
26878 vim_free(*bufp);
26879 *fnamep = *bufp = newbuf;
26880
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026881 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026882 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026883 }
26884
26885 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026886 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026887}
26888
26889/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026890 * Get the short path (8.3) for the filename in "fname". The converted
26891 * path is returned in "bufp".
26892 *
26893 * Some of the directories specified in "fname" may not exist. This function
26894 * will shorten the existing directories at the beginning of the path and then
26895 * append the remaining non-existing path.
26896 *
26897 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026898 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026899 * bufp - Pointer to an allocated buffer for the filename.
26900 * fnamelen - Length of the filename pointed to by fname
26901 *
26902 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026903 */
26904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026905shortpath_for_invalid_fname(
26906 char_u **fname,
26907 char_u **bufp,
26908 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026909{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026910 char_u *short_fname, *save_fname, *pbuf_unused;
26911 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026912 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026913 int old_len, len;
26914 int new_len, sfx_len;
26915 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026916
26917 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026918 old_len = *fnamelen;
26919 save_fname = vim_strnsave(*fname, old_len);
26920 pbuf_unused = NULL;
26921 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026922
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026923 endp = save_fname + old_len - 1; /* Find the end of the copy */
26924 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026925
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026926 /*
26927 * Try shortening the supplied path till it succeeds by removing one
26928 * directory at a time from the tail of the path.
26929 */
26930 len = 0;
26931 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026932 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026933 /* go back one path-separator */
26934 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26935 --endp;
26936 if (endp <= save_fname)
26937 break; /* processed the complete path */
26938
26939 /*
26940 * Replace the path separator with a NUL and try to shorten the
26941 * resulting path.
26942 */
26943 ch = *endp;
26944 *endp = 0;
26945 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026946 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026947 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26948 {
26949 retval = FAIL;
26950 goto theend;
26951 }
26952 *endp = ch; /* preserve the string */
26953
26954 if (len > 0)
26955 break; /* successfully shortened the path */
26956
26957 /* failed to shorten the path. Skip the path separator */
26958 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026959 }
26960
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026961 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026962 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026963 /*
26964 * Succeeded in shortening the path. Now concatenate the shortened
26965 * path with the remaining path at the tail.
26966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026967
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026968 /* Compute the length of the new path. */
26969 sfx_len = (int)(save_endp - endp) + 1;
26970 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026971
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026972 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026973 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026974 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026975 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026976 /* There is not enough space in the currently allocated string,
26977 * copy it to a buffer big enough. */
26978 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026979 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026980 {
26981 retval = FAIL;
26982 goto theend;
26983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026984 }
26985 else
26986 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026987 /* Transfer short_fname to the main buffer (it's big enough),
26988 * unless get_short_pathname() did its work in-place. */
26989 *fname = *bufp = save_fname;
26990 if (short_fname != save_fname)
26991 vim_strncpy(save_fname, short_fname, len);
26992 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026993 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026994
26995 /* concat the not-shortened part of the path */
26996 vim_strncpy(*fname + len, endp, sfx_len);
26997 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026998 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026999
27000theend:
27001 vim_free(pbuf_unused);
27002 vim_free(save_fname);
27003
27004 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027005}
27006
27007/*
27008 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027009 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027010 */
27011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027012shortpath_for_partial(
27013 char_u **fnamep,
27014 char_u **bufp,
27015 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027016{
27017 int sepcount, len, tflen;
27018 char_u *p;
27019 char_u *pbuf, *tfname;
27020 int hasTilde;
27021
Bram Moolenaar8c8de832008-06-24 22:58:06 +000027022 /* Count up the path separators from the RHS.. so we know which part
27023 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027024 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027025 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027026 if (vim_ispathsep(*p))
27027 ++sepcount;
27028
27029 /* Need full path first (use expand_env() to remove a "~/") */
27030 hasTilde = (**fnamep == '~');
27031 if (hasTilde)
27032 pbuf = tfname = expand_env_save(*fnamep);
27033 else
27034 pbuf = tfname = FullName_save(*fnamep, FALSE);
27035
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000027036 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027037
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027038 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
27039 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027040
27041 if (len == 0)
27042 {
27043 /* Don't have a valid filename, so shorten the rest of the
27044 * path if we can. This CAN give us invalid 8.3 filenames, but
27045 * there's not a lot of point in guessing what it might be.
27046 */
27047 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027048 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
27049 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027050 }
27051
27052 /* Count the paths backward to find the beginning of the desired string. */
27053 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027054 {
27055#ifdef FEAT_MBYTE
27056 if (has_mbyte)
27057 p -= mb_head_off(tfname, p);
27058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027059 if (vim_ispathsep(*p))
27060 {
27061 if (sepcount == 0 || (hasTilde && sepcount == 1))
27062 break;
27063 else
27064 sepcount --;
27065 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027067 if (hasTilde)
27068 {
27069 --p;
27070 if (p >= tfname)
27071 *p = '~';
27072 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027073 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027074 }
27075 else
27076 ++p;
27077
27078 /* Copy in the string - p indexes into tfname - allocated at pbuf */
27079 vim_free(*bufp);
27080 *fnamelen = (int)STRLEN(p);
27081 *bufp = pbuf;
27082 *fnamep = p;
27083
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027084 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027085}
27086#endif /* WIN3264 */
27087
27088/*
27089 * Adjust a filename, according to a string of modifiers.
27090 * *fnamep must be NUL terminated when called. When returning, the length is
27091 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027092 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027093 * When there is an error, *fnamep is set to NULL.
27094 */
27095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010027096modify_fname(
27097 char_u *src, /* string with modifiers */
27098 int *usedlen, /* characters after src that are used */
27099 char_u **fnamep, /* file name so far */
27100 char_u **bufp, /* buffer for allocated file name or NULL */
27101 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027102{
27103 int valid = 0;
27104 char_u *tail;
27105 char_u *s, *p, *pbuf;
27106 char_u dirname[MAXPATHL];
27107 int c;
27108 int has_fullname = 0;
27109#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027110 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027111 int has_shortname = 0;
27112#endif
27113
27114repeat:
27115 /* ":p" - full path/file_name */
27116 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
27117 {
27118 has_fullname = 1;
27119
27120 valid |= VALID_PATH;
27121 *usedlen += 2;
27122
27123 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
27124 if ((*fnamep)[0] == '~'
27125#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
27126 && ((*fnamep)[1] == '/'
27127# ifdef BACKSLASH_IN_FILENAME
27128 || (*fnamep)[1] == '\\'
27129# endif
27130 || (*fnamep)[1] == NUL)
27131
27132#endif
27133 )
27134 {
27135 *fnamep = expand_env_save(*fnamep);
27136 vim_free(*bufp); /* free any allocated file name */
27137 *bufp = *fnamep;
27138 if (*fnamep == NULL)
27139 return -1;
27140 }
27141
27142 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027143 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000027144 {
27145 if (vim_ispathsep(*p)
27146 && p[1] == '.'
27147 && (p[2] == NUL
27148 || vim_ispathsep(p[2])
27149 || (p[2] == '.'
27150 && (p[3] == NUL || vim_ispathsep(p[3])))))
27151 break;
27152 }
27153
27154 /* FullName_save() is slow, don't use it when not needed. */
27155 if (*p != NUL || !vim_isAbsName(*fnamep))
27156 {
27157 *fnamep = FullName_save(*fnamep, *p != NUL);
27158 vim_free(*bufp); /* free any allocated file name */
27159 *bufp = *fnamep;
27160 if (*fnamep == NULL)
27161 return -1;
27162 }
27163
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027164#ifdef WIN3264
27165# if _WIN32_WINNT >= 0x0500
27166 if (vim_strchr(*fnamep, '~') != NULL)
27167 {
27168 /* Expand 8.3 filename to full path. Needed to make sure the same
27169 * file does not have two different names.
27170 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27171 p = alloc(_MAX_PATH + 1);
27172 if (p != NULL)
27173 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027174 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027175 {
27176 vim_free(*bufp);
27177 *bufp = *fnamep = p;
27178 }
27179 else
27180 vim_free(p);
27181 }
27182 }
27183# endif
27184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027185 /* Append a path separator to a directory. */
27186 if (mch_isdir(*fnamep))
27187 {
27188 /* Make room for one or two extra characters. */
27189 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27190 vim_free(*bufp); /* free any allocated file name */
27191 *bufp = *fnamep;
27192 if (*fnamep == NULL)
27193 return -1;
27194 add_pathsep(*fnamep);
27195 }
27196 }
27197
27198 /* ":." - path relative to the current directory */
27199 /* ":~" - path relative to the home directory */
27200 /* ":8" - shortname path - postponed till after */
27201 while (src[*usedlen] == ':'
27202 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27203 {
27204 *usedlen += 2;
27205 if (c == '8')
27206 {
27207#ifdef WIN3264
27208 has_shortname = 1; /* Postpone this. */
27209#endif
27210 continue;
27211 }
27212 pbuf = NULL;
27213 /* Need full path first (use expand_env() to remove a "~/") */
27214 if (!has_fullname)
27215 {
27216 if (c == '.' && **fnamep == '~')
27217 p = pbuf = expand_env_save(*fnamep);
27218 else
27219 p = pbuf = FullName_save(*fnamep, FALSE);
27220 }
27221 else
27222 p = *fnamep;
27223
27224 has_fullname = 0;
27225
27226 if (p != NULL)
27227 {
27228 if (c == '.')
27229 {
27230 mch_dirname(dirname, MAXPATHL);
27231 s = shorten_fname(p, dirname);
27232 if (s != NULL)
27233 {
27234 *fnamep = s;
27235 if (pbuf != NULL)
27236 {
27237 vim_free(*bufp); /* free any allocated file name */
27238 *bufp = pbuf;
27239 pbuf = NULL;
27240 }
27241 }
27242 }
27243 else
27244 {
27245 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27246 /* Only replace it when it starts with '~' */
27247 if (*dirname == '~')
27248 {
27249 s = vim_strsave(dirname);
27250 if (s != NULL)
27251 {
27252 *fnamep = s;
27253 vim_free(*bufp);
27254 *bufp = s;
27255 }
27256 }
27257 }
27258 vim_free(pbuf);
27259 }
27260 }
27261
27262 tail = gettail(*fnamep);
27263 *fnamelen = (int)STRLEN(*fnamep);
27264
27265 /* ":h" - head, remove "/file_name", can be repeated */
27266 /* Don't remove the first "/" or "c:\" */
27267 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27268 {
27269 valid |= VALID_HEAD;
27270 *usedlen += 2;
27271 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027272 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027273 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027274 *fnamelen = (int)(tail - *fnamep);
27275#ifdef VMS
27276 if (*fnamelen > 0)
27277 *fnamelen += 1; /* the path separator is part of the path */
27278#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027279 if (*fnamelen == 0)
27280 {
27281 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27282 p = vim_strsave((char_u *)".");
27283 if (p == NULL)
27284 return -1;
27285 vim_free(*bufp);
27286 *bufp = *fnamep = tail = p;
27287 *fnamelen = 1;
27288 }
27289 else
27290 {
27291 while (tail > s && !after_pathsep(s, tail))
27292 mb_ptr_back(*fnamep, tail);
27293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027294 }
27295
27296 /* ":8" - shortname */
27297 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27298 {
27299 *usedlen += 2;
27300#ifdef WIN3264
27301 has_shortname = 1;
27302#endif
27303 }
27304
27305#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027306 /*
27307 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027308 */
27309 if (has_shortname)
27310 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027311 /* Copy the string if it is shortened by :h and when it wasn't copied
27312 * yet, because we are going to change it in place. Avoids changing
27313 * the buffer name for "%:8". */
27314 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027315 {
27316 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027317 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027318 return -1;
27319 vim_free(*bufp);
27320 *bufp = *fnamep = p;
27321 }
27322
27323 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027324 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027325 if (!has_fullname && !vim_isAbsName(*fnamep))
27326 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027327 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027328 return -1;
27329 }
27330 else
27331 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027332 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027333
Bram Moolenaardc935552011-08-17 15:23:23 +020027334 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027335 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027336 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027337 return -1;
27338
27339 if (l == 0)
27340 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027341 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027342 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027343 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027344 return -1;
27345 }
27346 *fnamelen = l;
27347 }
27348 }
27349#endif /* WIN3264 */
27350
27351 /* ":t" - tail, just the basename */
27352 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27353 {
27354 *usedlen += 2;
27355 *fnamelen -= (int)(tail - *fnamep);
27356 *fnamep = tail;
27357 }
27358
27359 /* ":e" - extension, can be repeated */
27360 /* ":r" - root, without extension, can be repeated */
27361 while (src[*usedlen] == ':'
27362 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27363 {
27364 /* find a '.' in the tail:
27365 * - for second :e: before the current fname
27366 * - otherwise: The last '.'
27367 */
27368 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27369 s = *fnamep - 2;
27370 else
27371 s = *fnamep + *fnamelen - 1;
27372 for ( ; s > tail; --s)
27373 if (s[0] == '.')
27374 break;
27375 if (src[*usedlen + 1] == 'e') /* :e */
27376 {
27377 if (s > tail)
27378 {
27379 *fnamelen += (int)(*fnamep - (s + 1));
27380 *fnamep = s + 1;
27381#ifdef VMS
27382 /* cut version from the extension */
27383 s = *fnamep + *fnamelen - 1;
27384 for ( ; s > *fnamep; --s)
27385 if (s[0] == ';')
27386 break;
27387 if (s > *fnamep)
27388 *fnamelen = s - *fnamep;
27389#endif
27390 }
27391 else if (*fnamep <= tail)
27392 *fnamelen = 0;
27393 }
27394 else /* :r */
27395 {
27396 if (s > tail) /* remove one extension */
27397 *fnamelen = (int)(s - *fnamep);
27398 }
27399 *usedlen += 2;
27400 }
27401
27402 /* ":s?pat?foo?" - substitute */
27403 /* ":gs?pat?foo?" - global substitute */
27404 if (src[*usedlen] == ':'
27405 && (src[*usedlen + 1] == 's'
27406 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27407 {
27408 char_u *str;
27409 char_u *pat;
27410 char_u *sub;
27411 int sep;
27412 char_u *flags;
27413 int didit = FALSE;
27414
27415 flags = (char_u *)"";
27416 s = src + *usedlen + 2;
27417 if (src[*usedlen + 1] == 'g')
27418 {
27419 flags = (char_u *)"g";
27420 ++s;
27421 }
27422
27423 sep = *s++;
27424 if (sep)
27425 {
27426 /* find end of pattern */
27427 p = vim_strchr(s, sep);
27428 if (p != NULL)
27429 {
27430 pat = vim_strnsave(s, (int)(p - s));
27431 if (pat != NULL)
27432 {
27433 s = p + 1;
27434 /* find end of substitution */
27435 p = vim_strchr(s, sep);
27436 if (p != NULL)
27437 {
27438 sub = vim_strnsave(s, (int)(p - s));
27439 str = vim_strnsave(*fnamep, *fnamelen);
27440 if (sub != NULL && str != NULL)
27441 {
27442 *usedlen = (int)(p + 1 - src);
27443 s = do_string_sub(str, pat, sub, flags);
27444 if (s != NULL)
27445 {
27446 *fnamep = s;
27447 *fnamelen = (int)STRLEN(s);
27448 vim_free(*bufp);
27449 *bufp = s;
27450 didit = TRUE;
27451 }
27452 }
27453 vim_free(sub);
27454 vim_free(str);
27455 }
27456 vim_free(pat);
27457 }
27458 }
27459 /* after using ":s", repeat all the modifiers */
27460 if (didit)
27461 goto repeat;
27462 }
27463 }
27464
Bram Moolenaar26df0922014-02-23 23:39:13 +010027465 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27466 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027467 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027468 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027469 if (c != NUL)
27470 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027471 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027472 if (c != NUL)
27473 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027474 if (p == NULL)
27475 return -1;
27476 vim_free(*bufp);
27477 *bufp = *fnamep = p;
27478 *fnamelen = (int)STRLEN(p);
27479 *usedlen += 2;
27480 }
27481
Bram Moolenaar071d4272004-06-13 20:20:40 +000027482 return valid;
27483}
27484
27485/*
27486 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27487 * "flags" can be "g" to do a global substitute.
27488 * Returns an allocated string, NULL for error.
27489 */
27490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027491do_string_sub(
27492 char_u *str,
27493 char_u *pat,
27494 char_u *sub,
27495 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027496{
27497 int sublen;
27498 regmatch_T regmatch;
27499 int i;
27500 int do_all;
27501 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027502 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027503 garray_T ga;
27504 char_u *ret;
27505 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027506 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027507
27508 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27509 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027510 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027511
27512 ga_init2(&ga, 1, 200);
27513
27514 do_all = (flags[0] == 'g');
27515
27516 regmatch.rm_ic = p_ic;
27517 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27518 if (regmatch.regprog != NULL)
27519 {
27520 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027521 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027522 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27523 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027524 /* Skip empty match except for first match. */
27525 if (regmatch.startp[0] == regmatch.endp[0])
27526 {
27527 if (zero_width == regmatch.startp[0])
27528 {
27529 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027530 i = MB_PTR2LEN(tail);
27531 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27532 (size_t)i);
27533 ga.ga_len += i;
27534 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027535 continue;
27536 }
27537 zero_width = regmatch.startp[0];
27538 }
27539
Bram Moolenaar071d4272004-06-13 20:20:40 +000027540 /*
27541 * Get some space for a temporary buffer to do the substitution
27542 * into. It will contain:
27543 * - The text up to where the match is.
27544 * - The substituted text.
27545 * - The text after the match.
27546 */
27547 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027548 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027549 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27550 {
27551 ga_clear(&ga);
27552 break;
27553 }
27554
27555 /* copy the text up to where the match is */
27556 i = (int)(regmatch.startp[0] - tail);
27557 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27558 /* add the substituted text */
27559 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27560 + ga.ga_len + i, TRUE, TRUE, FALSE);
27561 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027562 tail = regmatch.endp[0];
27563 if (*tail == NUL)
27564 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027565 if (!do_all)
27566 break;
27567 }
27568
27569 if (ga.ga_data != NULL)
27570 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27571
Bram Moolenaar473de612013-06-08 18:19:48 +020027572 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027573 }
27574
27575 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27576 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027577 if (p_cpo == empty_option)
27578 p_cpo = save_cpo;
27579 else
27580 /* Darn, evaluating {sub} expression changed the value. */
27581 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027582
27583 return ret;
27584}
27585
27586#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */